Need a second pair of eyes on my Tic-Tac-Toe project algorithm!

Need a second pair of eyes on my Tic-Tac-Toe project algorithm!
0.0 0


Well I decided to bite the bullet and implement React in my Tic-Tac-Toe project (an initial sketch of the project had enough jQuery DOM manipulation to make me want to gouge my eyes out), and it turns out that React isn’t the hard part!

I’m working to implement minimax as per Ahmad Abdolsaheb, and I’m pretty sure I’ve implemented it correctly (and checked it against his example). When I endeavor to run it in codepen however, I get Firefox’s “too much recursion” error, which I could maybe understand given that it’s trying to implement every end state of the game except for the fact that Ahmad’s implementation on Codepen works! Can anyone see anything I’m missing that’s causing the recursion to go infinite? My project is here.


Okay, so I’ve noticed a couple of problems.

First, on line 45, toward the end of componentDidUpdate() in the App class, you call the minimax function with the board state, and this.state.symbol, which is undefined. You’ll need to find a way to get the CPU’s symbol to pass to the function to get it to work correctly.

Second, once I fixed that, too many moves are being made. I’m too tired to pinpoint exactly what’s causing it, but I think it’s due to the fact that componentDidUpdate() is being fired too many times, so you may want to move the cpu player logic out of that function and into something that you can make sure is only called once.

Good luck!


Bump, to alert you of my edit.


Good catch! I missed that one when I changed the variable name for clarity. Thanks!


It looks pretty good to me but it’s good to get more eyes on your code. You can also try a program to it for you. Heard of one called checkmarx, not sure about them but you can try.
Good luck.