solution generating code
This commit is contained in:
parent
eb9ef541e7
commit
e048d138af
2 changed files with 65 additions and 14 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
|
||||||
@(rodata)
|
@(rodata)
|
||||||
CARDS := [Card_Handle(TAROT_COUNT)]Card {
|
CARDS := [Card_Handle(TAROT_COUNT)]Card {
|
||||||
{.Major, 0},
|
{.Major, 0},
|
||||||
|
|
@ -140,5 +143,20 @@ card_full_name :: proc(card: Card) -> (name: string) {
|
||||||
case .Wand:
|
case .Wand:
|
||||||
suit_name = "of Wands"
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -286,10 +286,17 @@ game_update_searching :: proc() {
|
||||||
for ray.GetTime() < start_time + f64(1.0 / 30.0) {
|
for ray.GetTime() < start_time + f64(1.0 / 30.0) {
|
||||||
game.random_game_count += 1
|
game.random_game_count += 1
|
||||||
log.debugf("generating random game %d", game.random_game_count)
|
log.debugf("generating random game %d", game.random_game_count)
|
||||||
|
|
||||||
|
seed := rand.uint64()
|
||||||
|
{
|
||||||
|
random_state := rand.create_u64(seed)
|
||||||
|
context.random_generator = rand.default_random_generator(&random_state)
|
||||||
state_init_fully_random(&game.state)
|
state_init_fully_random(&game.state)
|
||||||
|
}
|
||||||
|
|
||||||
log.debugf("searching solution")
|
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
|
game.displayed_state = game.state
|
||||||
auto_moves := make([dynamic]Move, context.temp_allocator)
|
auto_moves := make([dynamic]Move, context.temp_allocator)
|
||||||
state_make_stacks_with_effects(&game.state, &auto_moves)
|
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)
|
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()
|
tracy.Zone()
|
||||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||||
allocator := context.temp_allocator
|
|
||||||
context.allocator = allocator
|
context.allocator = allocator
|
||||||
TRIAL_LIMIT :: 10_000
|
TRIAL_LIMIT :: 10_000
|
||||||
NO_PROGRESS_TRIAL_BASE_LIMIT :: 800
|
NO_PROGRESS_TRIAL_BASE_LIMIT :: 800
|
||||||
|
|
@ -977,6 +990,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) {
|
||||||
state: ^State,
|
state: ^State,
|
||||||
cost: int,
|
cost: int,
|
||||||
distance: int,
|
distance: int,
|
||||||
|
moves: []Move,
|
||||||
}
|
}
|
||||||
|
|
||||||
searcher := Searcher {
|
searcher := Searcher {
|
||||||
|
|
@ -991,12 +1005,16 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) {
|
||||||
searcher.best_distance = state_solution_distance(state)
|
searcher.best_distance = state_solution_distance(state)
|
||||||
pq.push(
|
pq.push(
|
||||||
&searcher.queue,
|
&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) {
|
for search_item in pq.pop_safe(&searcher.queue) {
|
||||||
searcher.trial_count += 1
|
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
|
state := search_item.state
|
||||||
if search_item.distance < searcher.best_distance {
|
if search_item.distance < searcher.best_distance {
|
||||||
|
|
@ -1005,7 +1023,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) {
|
||||||
} else {
|
} else {
|
||||||
searcher.no_progress_count += 1
|
searcher.no_progress_count += 1
|
||||||
if searcher.no_progress_count > NO_PROGRESS_TRIAL_BASE_LIMIT {
|
if searcher.no_progress_count > NO_PROGRESS_TRIAL_BASE_LIMIT {
|
||||||
return false
|
return nil, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tracy.Plot("queue_len", f64(pq.len(searcher.queue)))
|
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("trial_count", f64(searcher.trial_count))
|
||||||
tracy.Plot("no_progress_count", f64(searcher.no_progress_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
|
// NOTE(lilith): Move things out of the aside
|
||||||
if len(state.aside) > 0 {
|
if len(state.aside) > 0 {
|
||||||
|
|
@ -1023,7 +1041,7 @@ state_is_solvable :: proc(state: State) -> (is_solution: bool) {
|
||||||
from = {.Aside, 0},
|
from = {.Aside, 0},
|
||||||
to = {.Column, i8(to_column_index)},
|
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)},
|
from = {.Column, i8(from_column_index)},
|
||||||
to = {.Column, i8(to_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
|
// 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)},
|
from = {.Column, i8(from_column_index)},
|
||||||
to = {.Suit, 0},
|
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^
|
state := search_item.state^
|
||||||
if !state_perform_move(&state, move, auto_make_stacks = true) {return}
|
if !state_perform_move(&state, move, auto_make_stacks = true) {return}
|
||||||
|
|
||||||
_, _, just_inserted, _ := map_entry(&searcher.visited, state_to_bytes(state))
|
_, _, just_inserted, _ := map_entry(&searcher.visited, state_to_bytes(state))
|
||||||
if !just_inserted {return}
|
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(
|
pq.push(
|
||||||
&searcher.queue,
|
&searcher.queue,
|
||||||
Search_Item {
|
Search_Item {
|
||||||
state = new_clone(state),
|
state = new_clone(state),
|
||||||
distance = state_solution_distance(state),
|
distance = state_solution_distance(state),
|
||||||
cost = search_item.cost + 1,
|
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 {
|
canonical_state_from_state :: proc(state: State) -> (canonical: Canonical_State) #no_bounds_check {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue