Search code examples
javatic-tac-toe

Player vs. computer tic tac toe game code


I've been recently assigned a project from my AP Java class and ran into some difficulty. The whole objective of the project is to create a one player vs computer tic tac toe game. I have found success in creating a game board object and entering certain chars in the array. However i'm quite stuck and have no idea what to do with the following aspects of the game:

-Allowing the computer to make a move after the player has -Allowing the user to enter two numbers that will allow them to make a move instead of hardcoding it (i've been using random values just to test around) -Evaluating a win or tie condition.

Thanks so much! I've really been frustrated with this code lately and any advice will help!!

  • Board Class Code:

public class TicTacToeBoard{ private char[][] ttBoard;//initializes the type of array for the board private boolean gameActive = true;

public TicTacToeBoard(){ //Constructs our board as an object

ttBoard = new char[3][3]; //initialize the 2D array

for(int i = 0; i < ttBoard.length ; i++){
  Arrays.fill(ttBoard[i], ' '); 
}
}


public void printBoard(){ //we print our board object using this method
for(int i = 0; i<ttBoard.length; i++){
  for(int j = 0; j<ttBoard[i].length; j++){
    System.out.print(ttBoard[i][j]);
    if(j==0 || j==1)   //this loop helps separate indices, makes it look like a real board.
      System.out.print("|"); 
  }
  if(i==0||i==1)
  System.out.print("\n-----\n");
}
System.out.println();
}


  public boolean makeMove(char player , int i, int j)//here it prompts the player for a move 
//and also uses boolean to make the move
  {
   if(i >= 0 && i<= 2 && j >= 0 && j<= 2)
   {
      if(ttBoard[i][j] != ' ')
    return false;
   else
   {
    ttBoard[i][j] = player;
    return true;
   }
  } 
else
  return false;
}


public boolean gameOn()//this method will determine whether the game continues
{
 return gameActive
}

public void askPlayer(char player) //prompts users turn
{

}

public void askComp()  //prompts computers turn
{
}


}

Main Class code:

public class TicTacToe{

public static void main(String[] args){
  TicTacToeBoard game = new TicTacToeBoard();
  game.printBoard();
  int count = 1;

while(game.gameOn())
{
  if (count%2 == 0)
    game.askPlayer('X');
  else
    game.askComp('O');
  count++;
}
game.printBoard();
}
}

Solution

  • Is that something like this you want for your first function? (Not sure I truly get what you want)

    public void askPlayer(char player) {
        Scanner sc = new Scanner(System.in);
        bool ok=false,ok2=false;
        int one, two;
        while(!ok2){//run while the input wanted is already taken
            while(!ok){//run while the input for the first coordinate isn't correct
                System.out.print("Enter first coordiante : ");
                try{
                    one=sc.nextInt();
                    if(one<1 || one>3)//in the array?
                        System.out.println("Wrong input");
                    else
                        ok=true;
                }catch(InputMismatchException e){//a number is entered?
                    System.out.println("Wrong input");
                }
            }
            ok=false;
            while(!ok){//same for second coordiante
                System.out.print("Enter second coordiante : ");
                try{
                    two=sc.nextInt();
                    if(two<1 || two>3)
                        System.out.println("Wrong input");
                    else
                        ok=true;
                }catch(InputMismatchException e){
                    System.out.println("Wrong input");
                }
            }   
            if(ttBoard[one-1][two-1]!=' ')//taken?
                System.out.println("Case already taken");
            else
                ok2=true;
        }
        ttBoard[one-1][two-1]=player;//affect players character
    }