0

Hi everyone,

I have created an implementation of the game Tic Tac Toe using a board array that is 3 x 3. It reads in a turn from the user via the keyboard, and then the computer returns a move.

I have written the computer AI to basically do the following:

  1. If the computer can win (has two 'O's in a row either vertically, horizontally, or diagonally), then it will try to win.
  2. If it can't win, it will check to see if it can block the user from winning and do so.
  3. If neither of the above conditions are true, it will look for the next best move (a line where two positions are blank, and the other is an 'O')
  4. Else, it will simply return 'O'in a blank spot on the board.

For some combinations, the algorithm performs correctly.

For others (namely when the user is set up for a win), it does not perform correctly, and returns O in a random blank spot rather than where it should go (blocking the user).

I have used the modulo operator as follows:

3 booleans, one for win (tryforWin), one for next best move (nextBest) and one for block (tryforBlock), both initialized as false

Some of the code:

  {
    int[] compmove = new int[2]; 

    int AImove = 0;

    compmove[0] = 0;              
    compmove[1] = 0;

    for(int i= 0; i < board.length; i++)
    {
      for(int j = 0; j<board[i].length; j++)
      {
        if((board[i][j] == '_') && (AImove == 0))
        {
          compmove[0] = i;
          compmove[1] = j;
          AImove++;
        }
      }
    }

    if  (AImove > 0) { 
    // big if statement, containing the algorithm
    // performs only after computer has made its first move
    // player always goes first

    // computer always tries to win first
    for(int i = 0; i < board.length; i++) {
      for( int j = 0; j < board[i].length; j++){
        if ( (board[i][j] == 'O') && (board[i][(j+1)%3] == 'O') && (board[i][(j+2)%3] == '-'))
        {
          // horizontal check
          compmove[0] = i;
          compmove[1] = (j+2)%3;
          // set boolean tryWin to true - this is actually written in my code and initialized, not commented out
          return compmove;
        }
        else if( (board[j][i] == 'O') && (board[j][(i+1)%3] == 'O') && (board[j][(i+2)%3] == '-'))
        {
          // vertical check
          compmove[0] = j;
          compmove[1] = (i+2)%3;
          // set boolean tryWin to true
          return compmove;
        }
      }

      if( (board[i][i] == 'O') && (board[(i+1)%3][(i+1)%3] == 'O') && (board[(i+2)%3][(i+2)%3] == '-'))
      {
        // check first diagonal
        compmove[0] = (i+2)%3;
        compmove[1] = (i+2)%3;
        // set boolean tryWin to true
        return compmove;
      }
      else if((board[i][2-i] == 'O') && (board[(i+1)%3][2-((i+1)%3)] == 'O') && (board[(i+2)%3][2-((i+2)%3)] == '-'))
      {
        // check second diagonal
        compmove[0] = (i+2)%3;
        compmove[1] = 2 - ((i+2)%3);
        // set boolean tryWin to true
        return compmove;
      }
    }

    // if(tryWin == false) {
    // repeat the same code with for loops as above except switch out the O's for X's in the if statement
    // set boolean tryBlock to true in each if statement
    // returns the move
    // }

    // else if both tryWin and tryBlock are false, look for next best move. switch out second two positions in each if statement with '_' and put first as 'O'


    return compmove;
  }
} // end of myMethod

All of the other methods in my code are error free. It's a logic problem within this method that's causing the algorithm to not respond properly to the user's input. I feel like maybe it has something to do with my use of the modulo operator but I can't figure it out. Any help is hugely appreciated.

Edited by needhelp88

1
Contributor
1
Reply
26
Views
4 Years
Discussion Span
Last Post by needhelp88
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.