Why is function out of scope? Tic Tac Toe

Why is function out of scope? Tic Tac Toe
0

#1

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.

https://codepen.io/LaerTrech/pen/XNNxEE


#2

you might forget to attach your project to the post


#3

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];


#4

You’re fantastic! Thank you very much. :smile:


#5

glad that i can help :slight_smile: happy coding


#7

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


#8

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

  1. there is some coding mistake
  2. 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


#9

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.


#10

@icartusacrimea I was able to beat the AI several times in a row with the following strategy:

  1. Play any corner as X

  2. AI plays center

  3. Play the opposing corner

  4. Play the corner marked 5

  5. Whatever the AI does, complete one of your own rows.


#11

Got it. Thanks.
Try now.


#12

I confirm that the issue has been resolved. I could not beat it anymore. Great work! :smile:


#13

I also got one set of moves that guaranteed me like 8/10 wins.

  1. Place a margin-center one. Computer place in the middle.
  2. Place on a corner to form the L shape. Computer places in the corner.
  3. Place in the opposite corner from the place computer placed the last step, then you have 2 ways to win.

#14

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.