Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need some Help with my java TicTacToe assignment code is below. Having trouble with the following Requirements below Requirements: 1. All exceptions must be caught,
Need some Help with my java TicTacToe assignment code is below. Having trouble with the following Requirements below
Requirements:
1. All exceptions must be caught, invalid input should prompt for new input
2. Forfeit A player must be prompted at the beginning of every turn to forfeit if they so choose to. This would lead to starting a new game.
3.Game Reset The game must be able to be played over and over. The game should not terminate at any point unless the user dictates that action to the game. ======================================================================================================================== import java.io.*; import java.util.*; public class TicTacToe { private char[][] board; private int whoseturn; private String[] players; int movesmade; final static private char[] pieces = {'X','O'}; // Initializes a Tic Tac Toe object. public TicTacToe(String player1, String player2) { board = new char[3][3]; for (int i=0;i < 3; i++) for (int j=0; j < 3; j++) board[i][j] = '_'; whoseturn = 0; movesmade = 0; players = new String[2]; players[0] = player1; players[1] = player2; } // Tries to make a move at row, column. If it's valid the move is made // and true is returned. Else nothing is done and false is returned. public boolean Move(int row, int column) { if ( (board[row][column] == '_') && inbounds(row,column) ) { board[row][column] = pieces[whoseturn]; movesmade++; return true; } else return false; } // Returns true if indexes passed to the method are inbounds. public boolean inbounds(int row, int column) { if ((row < 0) || (column < 0)) return false; if ((row > 2) || (column > 2)) return false; return true; } // Changes whose turn it is. public void changeturn() { whoseturn = (whoseturn + 1)%2; } // Returns the current player's name. public String getCurrentPlayer() { return players[whoseturn]; } // Prints out the playing board. public void printboard() { System.out.println("\t0 1 2"); for (int i=0; i<3; i++) { System.out.print(i+"\t"); for (int j=0; j<3; j++) System.out.print(board[i][j]+" "); System.out.println(); } } // Returns a character signifying the winner. public char winner() { // Check for three X's or O's in a row. for (int i=0; i<3; i++) if (SameArray(board[i]) && board[i][0] != '_') return board[i][0]; // Check for three X's or O's in a column. for (int i=0; i<3; i++) if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && board[0][i] != '_') return board[0][i]; // Checks forward diagonal. Here, we know if there are three blanks, no one has one. if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) return board[0][0]; // Checks backward diagonal. Here, we know if there are three blanks, no one has one. if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2])) return board[2][0]; if (movesmade == 9) return 'T'; return '_'; } // Checks to see if all the characters in a single character array are // the same. private static boolean SameArray(char[] word) { char check = word[0]; for (int i=1; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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