Now I made a mistake and can´t figure it out.
Detailed questions at the end
I picked a project I thought about for a long time and that is similar to some videos to follow along.
It´s a variation on TicTacToe. The board is 3 dimensional, 4 positions per dimension (64 spots total), 4 in row wins
Project is backed up to github and can be seen here:
or here (P5js preview)
My problem is the evaluation function that is part of the minimax algorythm.
Minimax is implemented with a maximum depth to search and alpha-beta-pruning.
the evaluation function is (as of this moment) deprecated to only attach values to rows of 3 or for cells to make debugging easier.
The unclear behaviour I observe is the following:
sketch is running, I´m playing Human vs AI1
I play starting in the upper left corner an go horizontally to the right (cells 0,0,0; 0,1,0)
AI reacts pseudo-randomly (that´s ok, at this point in time the evaluation is expected to be 0 for all cells).
Now I set
debug = true via the console to activate some console.log()s
My third move is 0,2,0 thus filling a line with 3 “crosses”, threatening a line of 4
AI1 invokes minimax algorithm, moves and their scores are being console.log()ged
I expect a negative value for all moves but 0,3,0 (which would block my line of 4) and a value of 0 (or greater depending on previous moves).
I see ~25 moves with a value of 0, the other moves have the expected negative value (-20). The move I expect to be evaluated 0 is evaluated 0 - just not as the only one.
AI1 (pseudo-randomly) selects one of the moves that are evaluated as 0 (that is ok and expected of minimax).
If I invoke the evaluation function directly after AI1 made the wrong move the board evaluates to -20 as expected.
Sorry for the wall of text, I didn´t want to leave out possibly important information.
Here the questions:
What did I do wrong?
Is there a flaw in the order of operations?
Is there some asynchronicity happening?
Can anyone please point out my mistake, possibly including (hints to) a solution?
Thank you all in advance.
Greetings from Germany