By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 2,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

finding best move in a multi-dimensional array driven tic-tac-toe game

100+
P: 228
Hi,

I am trying tic-tac-toe game with multi-dimensional array. I saw many posts online but almost all use single dimensional and frankly, I didn't really understand the portions regarding detecting the best moves.

It is an API (with no interface) so interaction is using GET method. So first time server/computer makes the move, the response will be:

****s****
where * is empty square and s is moves by server. then user might reply with

u***s****

and so forth. I have implemented the logic of checking if the move is allowed and if allowed, to reserve the spot.

But I am stuck at deciding where the server should move after the player makes his move even though I have his latest x,y coordinates (or the square in the multi-dimensional array):

Expand|Select|Wrap|Line Numbers
  1. board =[
  2.         ['*','*','*'],
  3.         ['*','*','*'],
  4.         ['*','*','*'],
  5.        ]
  6.  
  7.  
This is the structure of the board/array. First state is going to be, example server makes the move:

Expand|Select|Wrap|Line Numbers
  1.  
  2. board =[
  3.         ['*','*','*'],
  4.         ['*','s','*'],
  5.         ['*','*','*'],
  6.        ]
  7.  
  8.  
then player mokes a move

Expand|Select|Wrap|Line Numbers
  1.  
  2. board =[
  3.         ['*','*','*'],
  4.         ['*','s','*'],
  5.         ['u','*','*'],
  6.        ]
  7.  
  8.  
when he made the move, I have 2,0 saved in variables but am really clueless on how to determine the next smart/best move.

Here is what I have attempted so far

1. get all empty squares and save them
2. get neighbors of the last move made (2,0) and for each square that is empty, decide to make server move.

While I have gotten empty squares and neighbors, I don't know how to use them or if they are needed even.


Any help please.
Nov 21 '18 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,333
well - while this is not a javascript question but more an algorithm question you can start from here:

https://medium.freecodecamp.org/buil...e-29d4d5adee07

for example. This explains a bit the solution that this implementation used - so you have to adapt it to your own implementation.
Nov 21 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.