Commit graph

28 commits

Author SHA1 Message Date
ItsDrike 88cdf28cfc
Use unique ptr for nicer memory freeing in win simulation func
This also switches from using `QQueue` here to `std::queue` as `QQueue`
doesn't seem to play well with holding unique pointers.
2024-12-07 21:26:28 +01:00
ItsDrike b209fbc94b
Use BFS approach for win simulation, not DFS
The original approach here was using depth-limited depth first search,
which was a hacky workaround to avoid exploring a single branch too
deeply. A much saner approach is to just explore in a breadth-first
manner.

This also completely negates the need for depth limitations and we can
instead let the algorithm run until it either finishes or hits the time
limit, as we always want to explore as much depth as we can, without it
slowing down the responsiveness of the UI.
2024-12-07 21:26:28 +01:00
ItsDrike 7cc52f272d
Add safe-guard against value underflow in column move check 2024-12-07 21:26:23 +01:00
ItsDrike bea2be5f63
bug: Fix state hash for empty piles
The current implementation of state hash doesn't represent empty columns
or foundations properly. This leads to a potential collision if there is
a full column next to an empty column, as it's indistinguishable which
column the data lies on. (In practice, this can't happen for
foundations, as they only hold cards of distinct types, so the collision
only occurs with columns.)

This commit fixes the issue and makes sure to represent empty piles
properly.
2024-12-07 20:48:41 +01:00
ItsDrike 985e99cf77
bug: fix moveColumnCardToColumn for multi-card moves 2024-12-07 20:44:10 +01:00
ItsDrike 36167edc78
Register parent QObjects properly 2024-12-07 13:30:02 +01:00
ItsDrike 9be91f0942
Remove left over unneccessary clean-up logic 2024-12-07 12:49:28 +01:00
ItsDrike 3ab8cfb7dc
Clean up GameState constructor 2024-12-07 12:34:11 +01:00
ItsDrike fdc3405366
Limit runtime for win scenario calculations
Depth limit alone often does a poor job at ensuring the simulation
doesn't take too long, as the amount of branches may differ depending on
the game and in some cases, the function can take way too long.

This solution introduces another stop condition, based on the runtime
of the evaluation, ensuring we don't block the game for too long.

Note that the original depth limiting, while fairly effective is a hacky
solution, instead, it may be a good idea to change the simulation logic
from DFS to BFS based search.
2024-12-07 12:26:47 +01:00
ItsDrike 1eb72163b5
Fix win simulation unnecessarily drawing same card 2024-12-07 12:02:55 +01:00
ItsDrike 0bee5ddc12
bug: ignore correct column for moving multiple cards 2024-12-06 18:38:25 +01:00
ItsDrike b2cad5ac14
bug: Don't reverse throwaway pile on flipping 2024-12-06 18:37:44 +01:00
ItsDrike 2b44a39ee8
slight code clean up 2024-12-06 17:09:19 +01:00
ItsDrike 18891308bc
bug: fix manual cross-column movement logic
The original implementation didn't use references for the QList
instances, which meant they were getting copied, so the changes made
didn't mutate the actual values held by the class.
2024-12-06 17:08:34 +01:00
ItsDrike 5240949353
Significantly optimize the winnability simulation
The original approach for calculating winnability first checked for
inter-column movements, which isn't ideal, instead, the new logic now
first attempts to make foundation pile movements. Additionally, this
converts the function to return an option value, which will be null if
the winnability check fails to determine the result within given maximum
depth. This is a necessary check, as the original logic took really long
to finish, especially if ran at the beginning of the game, where it
could keep going for hundreds of moves.
2024-12-06 17:04:15 +01:00
ItsDrike 2ec6206e26
Improve hash state computing
The new logic avoids possible collisions that could previously occur.
2024-12-06 17:01:24 +01:00
ItsDrike 5da97d7c0e
Add a destructor 2024-12-06 16:59:54 +01:00
ItsDrike 135d16daae
Add winnability check 2024-12-06 05:08:58 +01:00
ItsDrike eec98ba110
Return bool (success status) from drawNextCard 2024-12-06 04:54:34 +01:00
ItsDrike 206ff25a1a
Make parent the first arg in PlayingCard constructor 2024-12-06 04:33:29 +01:00
ItsDrike 03e74a6c67
Const correctness for valid move checks 2024-12-06 03:49:25 +01:00
ItsDrike 4692ce356b
Return QVariantList objects instead of QList
QML doesn't have a proper type-safe generic list type, returning QList
instances does technically work, however, qmlls (LSP) complains about
using this as it isn't a proper QML type. Instead, return QVarianList
objects, that are meant for QML.
2024-12-06 03:40:23 +01:00
ItsDrike 84a8aa57d6
Don't use deprecated SIGNAL & SLOT macros 2024-12-06 03:39:34 +01:00
ItsDrike 36a010d5aa
Add clang-format config & format files 2024-12-05 03:06:34 +01:00
ItsDrike ddb17f8bba
Move main.cpp to src/ dir 2024-12-05 02:43:42 +01:00
ItsDrike 3facc3333c
Don't shadow variable 2024-12-05 02:34:28 +01:00
ItsDrike 30e935b7ce
Remove left-over (fixed) bug comment 2024-12-05 02:24:55 +01:00
ItsDrike c710d0d1b3
Move source code to src/ dir 2024-12-05 01:59:15 +01:00