From 70b8e60f4e4f2cf31c97bed9ee5d8eb8d0a8ab35 Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Sun, 4 Oct 2026 21:37:39 +0200 Subject: [PATCH] useless solution search --- src/game.odin | 460 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 356 insertions(+), 104 deletions(-) diff --git a/src/game.odin b/src/game.odin index 4756b52..4bcd274 100644 --- a/src/game.odin +++ b/src/game.odin @@ -11,9 +11,11 @@ import "core:fmt" import "core:log" import "core:math/rand" import "core:mem" +import "core:reflect" import "core:slice" import "core:strings" import ray "vendor:raylib" +import "vendor:raylib/rlgl" MAJOR_COUNT :: 22 MINOR_COUNT :: 14 @@ -27,7 +29,7 @@ ROUNDED_RECT_SEGMENTS :: 6 GAP :: 16 CARD_SEPARATION :: 24 CARD_DAMPING :: 70 -AUTO_MOVE_INTERVAL :: 0.2 +AUTO_MOVE_INTERVAL :: 0.1 game: Game Game :: struct { @@ -45,8 +47,10 @@ Game :: struct { suit_textures: [Suit]ray.Texture, dragged_card: Maybe(Card_Handle), dragged_card_position: Vec2, + dragged_card_rotation: f32, auto_moves: queue.Queue(Move), auto_move_cooldown: f32, + suits_overlay_opacity: f32, } Vec2 :: [2]f32 Card :: struct { @@ -62,11 +66,14 @@ Suit :: enum { } Card_Handle :: distinct i8 Card_Entity :: struct { - position: Vec2, - target_position: Vec2, - velocity: Vec2, - is_draggable: bool, - state: Card_State_Kind, + position: Vec2, + target_position: Vec2, + velocity: Vec2, + rotation: f32, + target_rotation: f32, + angular_velocity: f32, + is_draggable: bool, + state: Card_State_Kind, } Card_State_Kind :: enum { Idle, @@ -74,11 +81,10 @@ Card_State_Kind :: enum { Locked, } State :: struct { - columns: [COLUMN_COUNT][dynamic; 32]Card_Handle, - major_low: [dynamic; MAJOR_COUNT]Card_Handle, - major_high: [dynamic; MAJOR_COUNT]Card_Handle, - suits: [4][dynamic; MINOR_COUNT]Card_Handle, - aside: [dynamic; 1]Card_Handle, + columns: [COLUMN_COUNT][dynamic; 30]Card_Handle, + major: [Major_Pile_Kind][dynamic; MAJOR_COUNT]Card_Handle, + suits: [4][dynamic; MINOR_COUNT]Card_Handle, + aside: [dynamic; 1]Card_Handle, } Move :: struct { card: Card_Handle, @@ -89,10 +95,13 @@ Place :: struct { kind: Place_Kind, index: int, } +Major_Pile_Kind :: enum { + Low, + High, +} Place_Kind :: enum { Column, - Major_Low, - Major_High, + Major, Suit, Aside, } @@ -149,15 +158,32 @@ init :: proc() { codepointCount = 0, ) - state_init(&game.state) - for &obj in game_cards_zipped() { - obj.entity.position = card_position({.Column, 5}) - obj.entity.is_draggable = true + queue.init(&magic_moves) + auto_moves := make([dynamic]Move, context.temp_allocator) + for { + log.info("searching solution…") + state_init(&game.state) + state_make_stacks(&game.state, &auto_moves) + queue.append(&game.auto_moves, ..auto_moves[:]) + if solution, ok := state_find_solution(game.state, context.temp_allocator); ok { + queue.append(&magic_moves, ..solution[:]) + break + } + clear(&auto_moves) + free_all(context.temp_allocator) } + + queue.append(&game.auto_moves, ..auto_moves[:]) game.displayed_state = game.state - log.infof("%#v", game.state) + for &entity in game.entities { + entity.position = card_position({.Column, 5}) + } } +magic_moves: queue.Queue(Move) +magic_wait: f32 +MAGIC_INTERVAL :: 0.4 + update :: proc() { free_all(context.temp_allocator) delta := ray.GetFrameTime() @@ -175,11 +201,14 @@ update :: proc() { } mouse_position := ray.GetMousePosition() - game.dragged_card_position = mouse_position - CARD_SIZE * 0.5 + game.dragged_card_position = mouse_position if ray.IsMouseButtonPressed(.LEFT) { for &obj, index in game_cards_zipped() { handle := Card_Handle(index) - if rect_contains(rrect_from_pos_sz(obj.entity.position, CARD_SIZE), mouse_position) && + if rect_contains( + rrect_from_pos_sz(obj.entity.position - CARD_SIZE * 0.5, CARD_SIZE), + mouse_position, + ) && game.entities[int(handle)].is_draggable { game.dragged_card = handle obj.entity.state = .Dragged @@ -187,6 +216,18 @@ update :: proc() { } } } + + game.dragged_card_rotation = 0 + hovered_place := place_from_position(mouse_position) + draggin_card_over_aside := + game.dragged_card != nil && + hovered_place.kind == .Suit && + !state_can_stack_suit(&game.displayed_state, game.dragged_card.?) && + len(game.state.aside) == 0 + if draggin_card_over_aside { + game.dragged_card_rotation = -math.PI / 2 + } + if dragged_card, ok := game.dragged_card.?; ok { obj := &game_cards_zipped()[int(dragged_card)] if ray.IsMouseButtonReleased(.LEFT) { @@ -197,7 +238,11 @@ update :: proc() { to = new_place, card = dragged_card, } - effects, ok := state_perform_move(&game.state, move) + effects, ok := state_perform_move( + &game.state, + move, + allocator = context.temp_allocator, + ) obj.entity.state = .Idle game.dragged_card = nil for auto_move in effects { @@ -208,7 +253,24 @@ update :: proc() { } } - game.auto_move_cooldown -= ray.GetFrameTime() + magic_wait -= delta + if magic_wait <= 0 { + if magic_move, ok := queue.pop_front_safe(&magic_moves); ok { + effects, ok := state_perform_move( + &game.state, + magic_move, + allocator = context.temp_allocator, + ) + for auto_move in effects { + queue.push(&game.auto_moves, auto_move) + auto_obj := &game_cards_zipped()[int(auto_move.card)] + auto_obj.entity.state = .Locked + } + + } + } + + game.auto_move_cooldown -= delta if game.auto_move_cooldown <= 0 { if auto_move, ok := queue.pop_front_safe(&game.auto_moves); ok { game.auto_move_cooldown = AUTO_MOVE_INTERVAL @@ -224,13 +286,20 @@ update :: proc() { // NOTE(lilith): Draw slots and set card target positions - slot_draw(card_position({.Major_Low, 0})) - for handle in game.displayed_state.major_low { - game.entities[int(handle)].target_position = card_position({.Major_Low, 0}) + for &entity in game.entities { + entity.target_rotation = 0 } - slot_draw(card_position({.Major_High, 0})) - for handle in game.displayed_state.major_high { - game.entities[int(handle)].target_position = card_position({.Major_High, 0}) + for major_pile, major_pile_kind in game.displayed_state.major { + place := Place{.Major, int(major_pile_kind)} + position := card_position(place) + slot_draw(position) + for handle in major_pile { + game.entities[int(handle)].target_position = position + } + } + for handle in game.displayed_state.aside { + game.entities[int(handle)].target_position = card_position({.Aside, 0}) + game.entities[int(handle)].target_rotation = -math.PI / 2 } for suit in Suit.Chalice ..= Suit.Wand { @@ -242,7 +311,7 @@ update :: proc() { ray.DrawTexturePro( game.suit_textures[suit], source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE), - dest = rrect_from_pos_sz(suit_position + CARD_SIZE * 0.5 - icon_size * 0.5, icon_size), + dest = rrect_from_pos_sz(suit_position - icon_size * 0.5, icon_size), origin = {0, 0}, rotation = 0, tint = {255, 255, 255, 100}, @@ -271,14 +340,21 @@ update :: proc() { target_position, dt = delta, ) + target_rotation := + obj.entity.state != .Dragged ? entity.target_rotation : game.dragged_card_rotation + entity.rotation, entity.angular_velocity = critical_spring_scalar( + entity.rotation, + entity.angular_velocity, + target_rotation, + dt = delta, + ) } // NOTE(lilith): Draw cards - for handle in game.displayed_state.major_low { - card_draw(handle) - } - for handle in game.displayed_state.major_high { - card_draw(handle) + for major_pile in game.displayed_state.major { + for handle in major_pile { + card_draw(handle) + } } for suit in Suit.Chalice ..= Suit.Wand { suit_index := int(suit) - 1 @@ -287,6 +363,21 @@ update :: proc() { } } + if draggin_card_over_aside || len(game.displayed_state.aside) != 0 { + game.suits_overlay_opacity = min(160, game.suits_overlay_opacity + 500 * delta) + } else { + game.suits_overlay_opacity = max(0, game.suits_overlay_opacity - 500 * delta) + } + ray.DrawRectangleRounded( + rrect_from_pos_sz( + card_position({.Suit, 0}) - CARD_SIZE / 2 - GAP / 2, + {CARD_SIZE.x * 4 + GAP * 3, CARD_SIZE.y} + Vec2{GAP, GAP}, + ), + CARD_ROUNDNESS, + ROUNDED_RECT_SEGMENTS, + ray.Color{0, 0, 0, u8(game.suits_overlay_opacity)}, + ) + if len(game.displayed_state.aside) > 0 { card_draw(game.displayed_state.aside[0]) } @@ -304,7 +395,7 @@ update :: proc() { slot_draw :: proc(position: Vec2) { ray.DrawRectangleRounded( - rrect_from_pos_sz(position, CARD_SIZE), + rrect_from_pos_sz(position - CARD_SIZE * 0.5, CARD_SIZE), CARD_ROUNDNESS, ROUNDED_RECT_SEGMENTS, color = ray.Color{0, 0, 0, 90}, @@ -316,14 +407,19 @@ card_draw :: proc(handle: Card_Handle) { card := obj.card entity := obj.entity + rlgl.PushMatrix(); defer rlgl.PopMatrix() + rlgl.Translatef(entity.position.x, entity.position.y, 0) + rlgl.Rotatef(entity.rotation * math.DEG_PER_RAD, 0, 0, 1) + rlgl.Translatef(-entity.position.x, -entity.position.y, 0) + ray.DrawRectangleRounded( - rrect_from_pos_sz(entity.position, CARD_SIZE), + rrect_from_pos_sz(entity.position - CARD_SIZE * 0.5, CARD_SIZE), CARD_ROUNDNESS, ROUNDED_RECT_SEGMENTS, color = background_color(card), ) ray.DrawRectangleRoundedLinesEx( - rrect_from_pos_sz(entity.position, CARD_SIZE), + rrect_from_pos_sz(entity.position - CARD_SIZE * 0.5, CARD_SIZE), CARD_ROUNDNESS, ROUNDED_RECT_SEGMENTS, lineThick = 2, @@ -339,6 +435,7 @@ card_draw :: proc(handle: Card_Handle) { case: offset = {4, 0} } + offset -= CARD_SIZE * 0.5 ray.DrawTextEx( game.card_font, text, @@ -367,6 +464,7 @@ state_init :: proc(state: ^State) { for &handle, index in deck {handle = Card_Handle(index)} rand.shuffle(deck[:]) target_column: int + state^ = {} for handle, index in deck { append(&state.columns[target_column], handle) target_column += 1 @@ -382,9 +480,118 @@ state_init :: proc(state: ^State) { } } +state_find_solution :: proc( + state: State, + allocator := context.allocator, +) -> ( + solution: [dynamic]Move, + is_solution: bool, +) { + DEPTH_LIMIT :: 5000 + TRIAL_LIMIT :: 100000000 + solution = make([dynamic]Move, allocator) + + searcher := Searcher { + states = make([dynamic]State, allocator), + moves = make([dynamic]Move, allocator), + rejectables = make(map[[616]byte]struct{}, allocator), + } + append(&searcher.states, state) + return solution, inner(&searcher) + + Searcher :: struct { + states: [dynamic]State, + moves: [dynamic]Move, + rejectables: map[[616]byte]struct{}, + trials: int, + } + + inner :: proc(searcher: ^Searcher) -> (is_solution: bool) { + if len(searcher.moves) > DEPTH_LIMIT {return false} + searcher.trials += 1 + if searcher.trials > TRIAL_LIMIT {return false} + state := slice.last(searcher.states[:]) + if state_is_solved(&state) {return true} + // log.debugf("depth: %d, state: %v", len(searcher.moves), state) + + recurse :: proc(searcher: ^Searcher, move: Move) -> (is_solution: bool) { + state := slice.last(searcher.states[:]) + if _, ok := state_perform_move( + &state, + move, + auto_make_stacks = true, + allocator = context.temp_allocator, + ); ok { + for previous_state in searcher.states { + if reflect.eq( + previous_state, + state, + including_indirect_array_recursion = true, + recursion_level = 4, + ) {return false} + } + if transmute([616]byte)state in searcher.rejectables {return false} + append(&searcher.moves, move) + append(&searcher.states, state) + if inner(searcher) {return true} + searcher.rejectables[transmute([616]byte)state] = {} + pop(&searcher.states) + pop(&searcher.moves) + } + return false + } + + + if len(state.aside) > 0 { + for &to_column, to_column_index in state.columns { + move := Move { + card = state.aside[0], + from = {.Aside, 0}, + to = {.Column, to_column_index}, + } + if recurse(searcher, move) {return true} + } + } + + has_empty_columns := false + for column in state.columns {if len(column) == 0 {has_empty_columns = true; break}} + + for &from_column, from_column_index in state.columns { + if len(from_column) == 0 {continue} + for &to_column, to_column_index in state.columns { + if len(from_column) == 1 && len(to_column) == 0 {continue} + move := Move { + card = slice.last(from_column[:]), + from = {.Column, from_column_index}, + to = {.Column, to_column_index}, + } + if recurse(searcher, move) {return true} + } + if !has_empty_columns { + move := Move { + card = slice.last(from_column[:]), + from = {.Column, from_column_index}, + to = {.Suit, 0}, + } + if recurse(searcher, move) {return true} + } + } + return false + } +} + +state_is_solved :: proc(state: ^State) -> bool { + for &column in state.columns { + if len(column) > 0 {return false} + } + if len(state.aside) > 0 {return false} + return true +} + state_perform_move :: proc( state: ^State, move: Move, + auto_make_stacks := true, allocator := context.allocator, ) -> ( auto_moves: [dynamic]Move, @@ -407,14 +614,56 @@ state_perform_move :: proc( next_move.card = next_handle state_apply_single_move(state, next_move) append(&auto_moves, next_move) + last_moved_handle = next_handle + last_moved_card = next_card } else {break} } } - state_get_auto_moves(state, &auto_moves) + if auto_make_stacks { + state_make_stacks(state, &auto_moves) + } return auto_moves, true } -state_get_auto_moves :: proc(state: ^State, auto_moves: ^[dynamic]Move) { +state_make_stacks :: proc(state: ^State, auto_moves: ^[dynamic]Move) { + big_loop: for { + for &column, column_index in state.columns { + if len(column) == 0 {continue} + handle := slice.last(column[:]) + card := card_from_handle(handle) + if state_can_stack_suit(state, handle) { + move := Move { + card = handle, + from = {.Column, column_index}, + to = {.Suit, int(card.suit) - 1}, + } + state_apply_single_move(state, move) + append(auto_moves, move) + continue big_loop + } + if state_can_stack_major_low(state, handle) { + move := Move { + card = handle, + from = {.Column, column_index}, + to = {.Major, 0}, + } + state_apply_single_move(state, move) + append(auto_moves, move) + continue big_loop + } + if state_can_stack_major_high(state, handle) { + move := Move { + card = handle, + from = {.Column, column_index}, + to = {.Major, 1}, + } + state_apply_single_move(state, move) + append(auto_moves, move) + continue big_loop + } + } + break + } } state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) { @@ -427,15 +676,9 @@ state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) { } state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool) { - switch place.kind { + #partial switch place.kind { case .Column: return pop_safe(&state.columns[place.index]) - case .Major_Low: - return pop_safe(&state.major_low) - case .Major_High: - return pop_safe(&state.major_high) - case .Suit: - return pop_safe(&state.suits[place.index]) case .Aside: return pop_safe(&state.aside) } @@ -443,20 +686,51 @@ state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool } state_push :: proc(state: ^State, place: Place, handle: Card_Handle) { - switch place.kind { + #partial switch place.kind { case .Column: append(&state.columns[place.index], handle) - case .Major_Low: - append(&state.major_low, handle) - case .Major_High: - append(&state.major_high, handle) + case .Major: + card := card_from_handle(handle) + if state_can_stack_major_low(state, handle) { + append(&state.major[.Low], handle) + } else { + append(&state.major[.High], handle) + } case .Suit: - append(&state.suits[place.index], handle) - case .Aside: - append(&state.aside, handle) + if state_can_stack_suit(state, handle) { + suit_index := int(card_from_handle(handle).suit) - 1 + suit_pile := &state.suits[suit_index] + append(&state.suits[suit_index], handle) + } else { + append(&state.aside, handle) + } } } +state_can_stack_suit :: proc(state: ^State, handle: Card_Handle) -> bool { + if len(state.aside) > 0 {return false} + card := card_from_handle(handle) + if card.suit == .Major {return false} + suit_index := int(card.suit) - 1 + suit_pile := &state.suits[suit_index] + if len(suit_pile) == 0 {return card.number == 1} + return card.number - 1 == card_from_handle(slice.last(suit_pile[:])).number +} + +state_can_stack_major_low :: proc(state: ^State, handle: Card_Handle) -> bool { + card := card_from_handle(handle) + if card.suit != .Major {return false} + if len(state.major[.Low]) == 0 {return card.number == 0} + return card.number - 1 == card_from_handle(slice.last(state.major[.Low][:])).number +} + +state_can_stack_major_high :: proc(state: ^State, handle: Card_Handle) -> bool { + card := card_from_handle(handle) + if card.suit != .Major {return false} + if len(state.major[.High]) == 0 {return card.number == MAJOR_COUNT - 1} + return card.number + 1 == card_from_handle(slice.last(state.major[.High][:])).number +} + state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) { if move.from == move.to {return false} moved_card := card_from_handle(move.card) @@ -465,28 +739,20 @@ state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) { if slice.last(from_pile) != move.card {return false} to_pile := state_get_pile(state, move.to) - switch move.to.kind { + #partial switch move.to.kind { case .Column: if len(to_pile) == 0 {return true} top_card := card_from_handle(slice.last(to_pile)) return top_card.suit == moved_card.suit && abs(top_card.number - moved_card.number) == 1 - case .Major_Low: - if moved_card.suit != .Major {return false} - if len(to_pile) == 0 {return moved_card.number == 0} - return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1 - case .Major_High: - if moved_card.suit != .Major {return false} - if len(to_pile) == 0 {return moved_card.number == MAJOR_COUNT - 1} - return card_from_handle(slice.last(to_pile)).number == moved_card.number + 1 + case .Major: + return( + state_can_stack_major_low(state, move.card) || + state_can_stack_major_high(state, move.card) \ + ) case .Suit: - if len(state.aside) > 0 {return false} - if int(moved_card.suit) - 1 != move.to.index {return false} - if len(to_pile) == 0 {return moved_card.number == 1} - return card_from_handle(slice.last(to_pile)).number == moved_card.number - 1 - case .Aside: - return len(to_pile) == 0 + return len(state.aside) == 0 } - unreachable() + return false } state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) { @@ -494,10 +760,8 @@ state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) { case .Column: assert(0 <= place.index && place.index < COLUMN_COUNT) return state.columns[place.index][:] - case .Major_Low: - return state.major_low[:] - case .Major_High: - return state.major_high[:] + case .Major: + return state.major[Major_Pile_Kind(place.index)][:] case .Suit: assert(0 <= place.index && place.index < 4) return state.suits[place.index][:] @@ -511,8 +775,9 @@ state_card_place :: proc(state: ^State, handle: Card_Handle) -> Place { for column_index in 0 ..< COLUMN_COUNT { if slice.contains(state.columns[column_index][:], handle) {return {.Column, column_index}} } - if slice.contains(state.major_low[:], handle) {return {.Major_Low, 0}} - if slice.contains(state.major_high[:], handle) {return {.Major_High, 0}} + for &major_pile, major_pile_kind in state.major { + if slice.contains(major_pile[:], handle) {return {.Major, int(major_pile_kind)}} + } for suit in 0 ..< 4 { if slice.contains(state.suits[suit][:], handle) {return {.Suit, suit}} } @@ -530,29 +795,29 @@ card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) { switch place.kind { case .Column: column_offset := Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} + column_section_offset - return Vec2{0, f32(pile_index) * CARD_SEPARATION} + column_offset - case .Major_Low: - return top_section_offset - case .Major_High: - return top_section_offset + Vec2{CARD_SIZE.x + GAP, 0} + position = Vec2{0, f32(pile_index) * CARD_SEPARATION} + column_offset + case .Major: + position = top_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} case .Suit: - return suits_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} + position = suits_section_offset + Vec2{f32(place.index) * (CARD_SIZE.x + GAP), 0} case .Aside: - return suits_section_offset + Vec2{1.5 * (CARD_SIZE.x + GAP), 0} + suits_section_center := + suits_section_offset + 0.5 * Vec2{4 * CARD_SIZE.x + 3 * GAP, CARD_SIZE.y} + return suits_section_center } - unreachable() + return position + CARD_SIZE * 0.5 } place_from_position :: proc(p: Vec2) -> (place: Place) { for column_index in 0 ..< COLUMN_COUNT { place := Place{.Column, column_index} - pos := card_position(place) - rect := rrect_from_pos_sz(pos, {CARD_SIZE.x, 10000}) + pos := card_position(place) - CARD_SIZE * 0.5 - GAP * 0.5 + rect := rrect_from_pos_sz(pos, {CARD_SIZE.x + GAP, 10000}) if rect_contains(rect, p) {return place} } - for place in ([]Place{{.Major_Low, 0}, {.Major_High, 0}, {.Suit, 0}, {.Suit, 1}, {.Suit, 2}, {.Suit, 3}}) { - pos := card_position(place) - rect := rrect_from_pos_sz(pos, CARD_SIZE) + for place in ([]Place{{.Major, 0}, {.Major, 1}, {.Suit, 0}, {.Suit, 1}, {.Suit, 2}, {.Suit, 3}}) { + pos := card_position(place) - CARD_SIZE * 0.5 - GAP * 0.5 + rect := rrect_from_pos_sz(pos, CARD_SIZE + GAP) if rect_contains(rect, p) {return place} } @@ -633,19 +898,6 @@ outline_color :: proc(card: Card) -> ray.Color { } short_title :: proc(card: Card) -> string { - suit_name: string - switch card.suit { - case .Major: - return fmt.tprintf("%d", card.number) - case .Chalice: - suit_name = "Chalices" - case .Coin: - suit_name = "Coins" - case .Sword: - suit_name = "Swords" - case .Wand: - suit_name = "Wands" - } number_name: string switch card.number { case 1: @@ -699,8 +951,8 @@ critical_spring_scalar :: proc( position: f32, velocity: f32, goal_position: f32, - goal_velocity: f32, - damping: f32, + goal_velocity: f32 = 0, + damping: f32 = CARD_DAMPING, dt: f32, ) -> ( new_position: f32,