Just starting to implement AI in Tic Tac Toe. So far, it’s just stupid and choosing random squares.
Don’t understand why variables defined at beginning of program wouldn’t be available to computer_move function. Getting “cannot read property [marked] of undefined” error when computer_move function enters draw function. Compendium_of_squares is my object defined at beginning of program with name, marked, and symbol properties for each square.
Note: the error only occurs after first game with computer has been played (with user as either X or O), after reset_game function has been run.
you might forget to attach your project to the post
initially you set
var available_squares = [“one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”];
but when you reset you do something like this
available_squares = [1, 2, 3, 4, 5, 6, 7, 8, 9];
You’re fantastic! Thank you very much.
glad that i can help happy coding
Hello again. You’ve been such a help to me, I thought I’d just reply to you first instead of making a post about new tic tac toe problems.
First of all, I was curious about trying strategies here instead of minimax. But I don’t know if it’s even possible to make it unbeatable this way.
Right now, my AI is pretty stupid. It seems in my computer_win function, chunks of code are being skipped over, or all of the else if statements are not being read in order. Not sure why. AI should be much smarter than this if it follows my trail of if/else if statements. Codepen
I actually tried min-max algorithm and not familiar with this approach/strategy you shared. though some of the people in fcc tried this kind of strategy so if you make a post about this you can get positive feedback I hope.
and current odd behavior can happen for 2 reason
- there is some coding mistake
- your algorithm is not correct one
so first make sure that the algorithm/strategy will make sure correct output then check out is there any coding problem or not
make a new post on this topic, in the meantime, I will take a look at your code if I found anything I will let you know
Thanks. I figured it out while the internet was briefly out, actually.
I have variables named game_mode and game_model (quite dumb, it turns out). In my computer_strategy function, there were a handful of times I accidentally substituted game_mode, which just keeps track of whether the current game’s between two humans or human and computer, for game_model, the array keeping track of whether each square is assigned X or O, or empty. Fixing these typos made my AI unbeatable, as far as I can tell right now.
@icartusacrimea I was able to beat the AI several times in a row with the following strategy:
Play any corner as X
AI plays center
Play the opposing corner
Play the corner marked 5
Whatever the AI does, complete one of your own rows.
I confirm that the issue has been resolved. I could not beat it anymore. Great work!
I also got one set of moves that guaranteed me like 8/10 wins.
- Place a margin-center one. Computer place in the middle.
- Place on a corner to form the L shape. Computer places in the corner.
- Place in the opposite corner from the place computer placed the last step, then you have 2 ways to win.
Very interesting. Similarly, someone in a separate post said the bottom of the board doesn’t seem to be as effective as the top. I will investigate.
Thanks much for testing.