Question

For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays...

For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson.

We will add a couple of methods to the TicTacToe class.

To track whose turn it is, we will use a counter turn. This is already declared as a private instance variable.

Create a getTurn method that returns the value of turn.

Other methods to implement:

printBoard()- This method should print the TicTacToe array onto the console. The board should include numbers that can help the user figure out which row and which column they are viewing at any given time. Sample output for this would be:

0 1 2
0 - - -
1 - - -
2 - - -
pickLocation(int row, int col)- This method returns a boolean value that determines if the spot a user picks to put their piece is valid. A valid space is one where the row and column are within the size of the board, and there are no X or O values currently present.
takeTurn(int row, int col)- This method adds an X or O to the array at position row,col depending on whose turn it is. If it’s an even turn, X should be added to the array, if it’s odd, O should be added. It also adds one to the value of turn.
checkWin()- This method returns a boolean that determines if a user has won the game. This method uses three methods to make that check:

checkCol- This checks if a player has three X or O values in a single column, and returns true if that’s the case.
checkRow - This checks if a player has three X or O values in a single row.
checkDiag - This checks if a player has three X or O values diagonally.
checkWin() only returns true if one of these three checks is true.


public class TicTacToeTester
{
public static void main(String[] args)
{
//This is to help you test your methods. Feel free to add code at the end to check
//to see if your checkWin method works!
TicTacToe game = new TicTacToe();
System.out.println("Initial Game Board:");
game.printBoard();
  
//Prints the first row of turns taken
for(int row = 0; row < 3; row++)
{
if(game.pickLocation(0, row))
{
game.takeTurn(0, row);
}
}
System.out.println("\nAfter three turns:");
game.printBoard();
  
  
  
}
}

public class TicTacToe
{
//copy over your constructor from the Tic Tac Toe Board activity in the previous lesson!
private int turn;
//this method returns the current turn
public int getTurn()
{
}
/*This method prints out the board array on to the console
*/
public void printBoard()
{
}
//This method returns true if space row, col is a valid space
public boolean pickLocation(int row, int col)
{
}
//This method places an X or O at location row,col based on the int turn
public void takeTurn(int row, int col)
{
}
//This method returns a boolean that returns true if a row has three X or O's in a row
public boolean checkRow()
{
}
//This method returns a boolean that returns true if a col has three X or O's
public boolean checkCol()
{
}
//This method returns a boolean that returns true if either diagonal has three X or O's
public boolean checkDiag()
{
}
//This method returns a boolean that checks if someone has won the game
public boolean checkWin()
{
}

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//TicTacToe.java

public class TicTacToe {

   //copy over your constructor from the Tic Tac Toe Board activity in the previous lesson!
   private int turn; // determines the turn
   private String board[][]; // board
  
   // constructor that creates an empty board and set turn to 0
   public TicTacToe()
   {
       turn = 0;
       board = new String[3][];
       for(int i=0;i<board.length;i++)
       {
           board[i] = new String[3];
           for(int j=0;j<board[i].length;j++)
               board[i][j] = "";
       }
   }
  
   //this method returns the current turn
   public int getTurn()
   {
       return turn;
   }
   /*This method prints out the board array on to the console
   */
   public void printBoard()
   {
       System.out.printf("%-3s","");
       for(int i=0;i<board.length;i++)
       {
           System.out.printf("%-3d",i);
       }
      
       System.out.println();
      
       for(int i=0;i<board.length;i++)
       {
           System.out.printf("%-3d",i);
           for(int j=0;j<board[i].length;j++)
           {
               if(board[i][j].equalsIgnoreCase("X") || board[i][j].equalsIgnoreCase("O"))
                   System.out.printf("%-3s",board[i][j]);
               else
                   System.out.printf("%-3s","-");
           }
          
           System.out.println();
       }
       System.out.println();
   }
  
   //This method returns true if space row, col is a valid space
   public boolean pickLocation(int row, int col)
   {
       if(row >= 0 && row < board.length && col >=0 && col < board.length)
       {
           if(!board[row][col].equals("X") && !board[row][col].equals("O"))
               return true;
       }
      
       return false;
   }
   //This method places an X or O at location row,col based on the int turn
   public void takeTurn(int row, int col)
   {
       if(turn%2 == 0)
           board[row][col] = "X";
       else
           board[row][col] = "O";
       turn += 1;
   }
  
   //This method returns a boolean that returns true if a row has three X or O's in a row
   public boolean checkRow()
   {
       for(int i=0;i<board.length;i++)
       {
           if(board[i][0].equalsIgnoreCase(board[i][1]) && board[i][0].equalsIgnoreCase(board[i][2]))
           {
               if(board[i][0].equalsIgnoreCase("X") || board[i][0].equalsIgnoreCase("O"))
                   return true;
           }
       }
      
       return false;
   }
  
   //This method returns a boolean that returns true if a col has three X or O's
   public boolean checkCol()
   {
       for(int i=0;i<board.length;i++)
       {
           if(board[0][i].equalsIgnoreCase(board[1][i]) && board[0][i].equalsIgnoreCase(board[2][i]))
           {
               if(board[0][i].equalsIgnoreCase("X") || board[0][i].equalsIgnoreCase("O"))
                   return true;
           }
       }
      
       return false;
   }
  
   //This method returns a boolean that returns true if either diagonal has three X or O's
   public boolean checkDiag()
   {
       if(board[0][0].equalsIgnoreCase(board[1][1]) && board[0][0].equalsIgnoreCase(board[2][2]))
       {
           if(board[0][0].equalsIgnoreCase("X") || board[0][0].equalsIgnoreCase("O"))
               return true;
       }
      
       if(board[0][2].equalsIgnoreCase(board[1][1]) && board[0][2].equalsIgnoreCase(board[2][0]))
       {
           if(board[0][2].equalsIgnoreCase("X") || board[0][2].equalsIgnoreCase("O"))
               return true;
       }
      
       return false;
   }
  
   //This method returns a boolean that checks if someone has won the game
   public boolean checkWin()
   {
       return(checkRow() || checkCol() || checkDiag());
   }
  
}

//end of TicTacToe.java

//TicTacToeTester.java

public class TicTacToeTester{

   public static void main(String[] args)
   {
       //This is to help you test your methods. Feel free to add code at the end to check
       //to see if your checkWin method works!
       TicTacToe game = new TicTacToe();
       System.out.println("Initial Game Board:");
       game.printBoard();
      
       //Prints the first row of turns taken
       for(int row = 0; row < 3; row++)
       {
           if(game.pickLocation(0, row))
           {
               game.takeTurn(0, row);
           }
       }
       System.out.println("\nAfter three turns:");
       game.printBoard();
       //Prints the second row of turns taken
       for(int row = 0; row < 3; row++)
       {
           if(game.pickLocation(1, row))
           {
               game.takeTurn(1, row);
           }
       }
      
       if(game.pickLocation(2, 0))
       {
           game.takeTurn(2, 0);
       }
       System.out.println("\nAfter seven turns:");      
       game.printBoard();
       System.out.println("Check win: "+game.checkWin());
  
   }
}

//end of TicTacToeTester.java

Output:

Add a comment
Know the answer?
Add Answer to:
For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this...

    Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this class in future exercises to fully build out a Tic Tac Toe game! The TicTacToe class should have a 2D array as an instance variable and a constructor that initializes the 2D array with the "-" value. Add a getter method that returns the private 2D instance variable. public class TicTacToeTester { //You don't need to alter any of the code in this class!...

  • Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending)....

    Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT