Question
Checks for invalid moves (anything outside the range 0-2) Gives the option to quit (Can become a challenge.... what is the sentinel value? If you
- Checks for invalid moves (anything outside the range 0-2)
- Gives the option to quit (Can become a challenge.... what is the sentinel value? If you use -1, you may want to double-check that option).
- Checks for stalemate game
- Give the option to play again
- Let 1st player choose either X or O
- MUST ADD COMMENTS/ANNOTATIONS
--------------------------------------------------------------------------------------------------------------------------------
First File(Main.java)
class Main { public static void main(String[] args) { TicTacToe game = new TicTacToe(); game.displayBoard(); while(!game.gameOver() ){ game.playerMove(); game.displayBoard(); }
System.out.println(" Game has ended....:(");
} }
--------------------------------------------------------------------------------------------------------------------------------
Second File(TicTacToe.java)
import java.util.Scanner; public class TicTacToe{
private char [] [] board; private char player; private Scanner input; private boolean gameEnd;
public TicTacToe(){ board = new char [3][3];
for(int rows=0; rows < board.length; rows++){ for(int cols=0; cols < board[0].length; cols++){ board[rows][cols] = ' '; }//end inner }//end outer player = 'X'; input = new Scanner(System.in); gameEnd = false; }
public void displayBoard(){ for(int rows=0; rows < board.length; rows++){ for(int cols=0; cols < board[0].length; cols++){ System.out.print(board[rows][cols]); if(cols < 2) System.out.print("|"); }//end inner System.out.println(); }//end outer System.out.println(); System.out.println(); }
public void playerMove(){ int r = 0, c = 0; boolean canMove = false;
while( !canMove){ System.out.println("Player " + player + " Where to move (row)?"); r = input.nextInt(); System.out.println("Player " + player + " Where to move (col)?"); c = input.nextInt(); canMove = checkMove(r, c); }//continue to prompt to get valid move
if(player == 'X'){ board[r][c] = 'X'; if(isWinner() ){ System.out.println("Game Over! Player" + player + "Wins!! "); gameEnd = true; } player = 'O'; }
else{ board[r][c] = 'O'; if(isWinner() ){ System.out.println("Game Over! Player" + player + "Wins!! "); gameEnd = true; } player = 'X'; } }//end playerMove
public boolean checkMove(int r, int c){ if( board[r][c] == ' '){ return true; }//empty char - ok to move
else if(board[r][c] == player){ System.out.println("You already moved there."); return false; }
else{ System.out.println("Other player already moved there."); return false; }
}//end checkMove
//isWinner public boolean isWinner(){ if( checkRows() ) return true; else if(checkCols() ) return true; else if(checkDiags() ) return true; else return false; }
public boolean checkRows(){ for(int row = 0; row < board.length; row++){ if( board[row][0] == player && board[row][1] == player && board[row][2] == player) return true; }//end traversing rows return false; }//end checkRows
public boolean checkCols(){ for(int cols=0; cols < board.length; cols++){ if(board[0][cols] == player && board[1][cols] == player && board[2][cols] == player){ return true; } }//end traversing columns return false; }//end checkCols
/*************************************/ //checkDiags public boolean checkDiags(){ if( board[0][0] == player && board[1][1] == player && board[2][2] == player) return true; else if( board[2][0] == player && board[1][1] == player && board[0][2] == player) return true; else return false; }
/********************************************************************/ //GameOver public boolean gameOver(){ return gameEnd; } }
Step by Step Solution
3.37 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
Answer Heres the annotated code with the requested features added Mainjava class Main public static void mainString args TicTacToe game new TicTacToe ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started