From e048d138af23ad989f8c239d16a6bb153c0b5a39 Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Thu, 8 Oct 2026 02:48:06 +0200 Subject: [PATCH] solution generating code --- src/cards.odin | 20 ++++++++++++++++- src/game.odin | 59 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/cards.odin b/src/cards.odin index bbcbe67..8800f87 100644 --- a/src/cards.odin +++ b/src/cards.odin @@ -1,4 +1,7 @@ package game + +import "core:fmt" + @(rodata) CARDS := [Card_Handle(TAROT_COUNT)]Card { {.Major, 0}, @@ -140,5 +143,20 @@ card_full_name :: proc(card: Card) -> (name: string) { case .Wand: suit_name = "of Wands" } - return name + number_name: string + switch card.number { + case 1: + number_name = "Ace" + case 11: + number_name = "Page" + case 12: + number_name = "Knight" + case 13: + number_name = "Queen" + case 14: + number_name = "King" + case: + number_name = fmt.tprintf("%d", card.number) + } + return fmt.tprintf("%s %s", number_name, suit_name) } diff --git a/src/game.odin b/src/game.odin index fecc4b8..5577778 100644 --- a/src/game.odin +++ b/src/game.odin @@ -286,10 +286,17 @@ game_update_searching :: proc() { for ray.GetTime() < start_time + f64(1.0 / 30.0) { game.random_game_count += 1 log.debugf("generating random game %d", game.random_game_count) - state_init_fully_random(&game.state) + + seed := rand.uint64() + { + random_state := rand.create_u64(seed) + context.random_generator = rand.default_random_generator(&random_state) + state_init_fully_random(&game.state) + } log.debugf("searching solution") - if ok := state_is_solvable(game.state); ok { + if _, ok := state_is_solvable(game.state, false, allocator = context.temp_allocator); ok { + log.debugf("Found solvable game with seed %d", seed) game.displayed_state = game.state auto_moves := make([dynamic]Move, context.temp_allocator) state_make_stacks_with_effects(&game.state, &auto_moves) @@ -958,10 +965,16 @@ state_to_bytes :: proc(state: State) -> State_Bytes { return transmute(State_Bytes)canonical_state_from_state(state) } -state_is_solvable :: proc(state: State) -> (is_solution: bool) { +state_is_solvable :: proc( + state: State, + $collect_moves: bool, + allocator := context.allocator, +) -> ( + moves: []Move, + is_solution: bool, +) { tracy.Zone() runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - allocator := context.temp_allocator context.allocator = allocator TRIAL_LIMIT :: 10_000 NO_PROGRESS_TRIAL_BASE_LIMIT :: 800 @@ -977,6 +990,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { state: ^State, cost: int, distance: int, + moves: []Move, } searcher := Searcher { @@ -991,12 +1005,16 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { searcher.best_distance = state_solution_distance(state) pq.push( &searcher.queue, - Search_Item{state = new_clone(state), distance = searcher.best_distance}, + Search_Item { + state = new_clone(state), + distance = searcher.best_distance, + moves = {} when collect_moves else nil, + }, ) for search_item in pq.pop_safe(&searcher.queue) { searcher.trial_count += 1 - if searcher.trial_count > TRIAL_LIMIT {return false} + if searcher.trial_count > TRIAL_LIMIT {return nil, false} state := search_item.state if search_item.distance < searcher.best_distance { @@ -1005,7 +1023,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { } else { searcher.no_progress_count += 1 if searcher.no_progress_count > NO_PROGRESS_TRIAL_BASE_LIMIT { - return false + return nil, false } } tracy.Plot("queue_len", f64(pq.len(searcher.queue))) @@ -1013,7 +1031,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { tracy.Plot("trial_count", f64(searcher.trial_count)) tracy.Plot("no_progress_count", f64(searcher.no_progress_count)) - if state_is_solved(search_item.state^) {return true} + if state_is_solved(search_item.state^) {return search_item.moves, true} // NOTE(lilith): Move things out of the aside if len(state.aside) > 0 { @@ -1023,7 +1041,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { from = {.Aside, 0}, to = {.Column, i8(to_column_index)}, } - add_move(&searcher, search_item, move) + add_move(&searcher, search_item, move, collect_moves) } } @@ -1037,7 +1055,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { from = {.Column, i8(from_column_index)}, to = {.Column, i8(to_column_index)}, } - add_move(&searcher, search_item, move) + add_move(&searcher, search_item, move, collect_moves) } // NOTE(lilith): Move cards into the aside { @@ -1046,29 +1064,44 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) { from = {.Column, i8(from_column_index)}, to = {.Suit, 0}, } - add_move(&searcher, search_item, move) + add_move(&searcher, search_item, move, collect_moves) } } } - add_move :: proc(searcher: ^Searcher, search_item: Search_Item, move: Move) { + add_move :: proc( + searcher: ^Searcher, + search_item: Search_Item, + move: Move, + $collect_moves: bool, + ) { state := search_item.state^ if !state_perform_move(&state, move, auto_make_stacks = true) {return} _, _, just_inserted, _ := map_entry(&searcher.visited, state_to_bytes(state)) if !just_inserted {return} + when collect_moves { + new_moves := make([]Move, len(search_item.moves) + 1) + + copy(new_moves[:len(search_item.moves)], search_item.moves) + new_moves[len(new_moves) - 1] = move + } else { + new_moves: []Move = nil + } + pq.push( &searcher.queue, Search_Item { state = new_clone(state), distance = state_solution_distance(state), cost = search_item.cost + 1, + moves = new_moves, }, ) } - return false + return nil, false } canonical_state_from_state :: proc(state: State) -> (canonical: Canonical_State) #no_bounds_check {