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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samvb
    New Member
    • Oct 2006
    • 228

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

    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):

    Code:
    board =[
            ['*','*','*'],
            ['*','*','*'],
            ['*','*','*'],
           ]
    This is the structure of the board/array. First state is going to be, example server makes the move:

    Code:
    board =[
            ['*','*','*'],
            ['*','s','*'],
            ['*','*','*'],
           ]
    then player mokes a move

    Code:
    board =[
            ['*','*','*'],
            ['*','s','*'],
            ['u','*','*'],
           ]
    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.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    well - while this is not a javascript question but more an algorithm question you can start from here:

    By Ben Carp As part of the freeCodeCamp curriculum, I was challenged build a Tic-Tac-Toe web app. It was a real pleasure. The app includes an ultimate computer player. It can optimize any given situation on the Tic-Tac-Toe board. The outcome surprise...


    for example. This explains a bit the solution that this implementation used - so you have to adapt it to your own implementation.

    Comment

    Working...