Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PlayTicTacToe.java URL: http://pastebin.com/7jx892M4 Thanks. Write a class calle

ID: 3923891 • Letter: P

Question

PlayTicTacToe.java URL: http://pastebin.com/7jx892M4

Thanks.

Write a class called TicTacToe to handle the basics of a two-player game of Write a class calld TicTacToe to handle the basics of a two-player game of Tic-Tac-Toe. The required methods are below. You may add any additional methods you believe would aid the user of your class to play the game Instance Variables board a two-dimensional array of chars turns an integer keeping track of the number of turns played this game Constructors TicTacToeClass() the default constructor, which just creates the two-dimensional array and fills each slot with(a blank space) and initializes the other attributes

Explanation / Answer

public class TicTacToe{


   char[][] board = new char[3][3];
   int turns;

   public TicTacToe(){
       for (int i = 0; i < 3 ;i++ ) {
           for (int j = 0; j < 3 ;j++ ) {
               board[i][j] = ' ';
           }
       }

       turns = 0;
   }


   public int getTurns(){
       return turns;
   }

   public char getPlayerAt(int r, int c){
       return board[r][c];
   }

   public char playerAt(int r, int c){
       return board[r][c];
   }


   public String toString(){

       StringBuilder sb=new StringBuilder("");

       for (int i = 0; i < 3 ;i++ ) {
           for (int j = 0; j < 3 ;j++ ) {
               sb.append(board[i][j]);
           }

           sb.append(" ");
       }

       return sb.toString();


   }

   public boolean isWinner(char p ){
           if(board[0][0] == p && board[0][1] == p && board[0][2] == p) return true;
           if(board[1][0] == p && board[1][1] == p && board[1][2] == p) return true;
           if(board[2][0] == p && board[2][1] == p && board[2][2] == p) return true;

           if(board[0][0] == p && board[1][0] == p && board[2][0] == p) return true;
           if(board[0][1] == p && board[1][1] == p && board[2][1] == p) return true;
           if(board[0][2] == p && board[1][2] == p && board[2][2] == p) return true;

           if(board[0][0] == p && board[1][1] == p && board[2][2] == p) return true;
           if(board[0][2] == p && board[1][1] == p && board[2][0] == p) return true;


           return false;
   }

   public boolean isFull(){
       for (int i = 0; i < 3 ;i++ ) {
           for (int j = 0; j < 3 ;j++ ) {
               if(board[i][j] == ' ') return false;
           }
       }

       return true;

   }

   public boolean isValid(int r, int c){

       if(r >= 0 && r< 3 && c >= 0 && c < 3) return true;

       return false;

   }

   public boolean isTied(){

       if( isFull() && !isWinner('O') && ! isWinner('X') ) return true;

       return false;
   }

   public void playMove(char p, int r, int c){

       if(isValid(r,c)) board[r][c] = p;

       turns +=1;
   }


}