Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

Need to Rewrite this Java program into Object Oriented:

OpenStudy (anonymous):

package madness; import java.util.Scanner; public class TicTacToe { // create a user defined class private char t[][]; // create all instance variables private String p1,p2; private boolean boxCheck=true; private int ctr=1; public TicTacToe( int dim, String n1, String n2){ t=new char[dim][dim]; // allows for boards of dim rows and dim cols p1=n1; p2=n2; start(); } public void start(){ printBoard(); enterValue(ctr); } //precondition: ttt board instantiated as md array //postcondition: ttt board is reset and ready for new game public void resetBoard(){ for (int r = 1; r<=3; r++) { for (int c = 1; c<=3; c++){ t[r][c]=0; }} } //precondition: ttt board instantiated as md array //postcondition: ttt board printed in console public void printBoard(){ System.out.println("******Tic Tac Toe Board******"); System.out.println(" 1 2 3"); int f=1; for (int r = 1; r<=3; r++) { for (int c = 1; c<=3; c++) { if (c==1) {System.out.print(" " +f +" |"); f++;} System.out.print(t[r][c]); System.out.print("|"); if (c==3) {System.out.println(); System.out.println(" -------");} } }} //precondition: ttt board instantiated as md array, n defined as odd or even (1-9) //postcondition: user input added to array t[][]. public void enterValue( int n){ System.out.println("Enter row, column, value. Ex: 11 or 12 or 33"); do { if (n==1 || n==3 || n==5 || n==7 || n==9) { System.out.println("Player 1 Entry:"); Scanner keyboard = new Scanner(System.in); String tictac_input = keyboard.nextLine(); //for row String tt_inputR=tictac_input.substring(0, 1); int inputR = Integer.parseInt(tt_inputR); //for column String tt_inputC=tictac_input.substring(1, 2); int inputC = Integer.parseInt(tt_inputC); //checks the value of t[r][c] to ensure that it is not already filled. boxCheck(inputR,inputC); if (boxCheck==true) { t[inputR][inputC] = 'X';//to fill box: } } } while (boxCheck==false); //loop repeats if user enters a value equal to a previous entry. do { if (n==2 || n==4 || n==6 || n==8) { System.out.println("Player 2 Entry:"); Scanner keyboard = new Scanner(System.in); String tictac_input = keyboard.nextLine(); //for row String tt_inputR=tictac_input.substring(0, 1); int inputR = Integer.parseInt(tt_inputR); //for column String tt_inputC=tictac_input.substring(1, 2); int inputC = Integer.parseInt(tt_inputC); //checks the value of t[r][c] to ensure that it is not already filled. boxCheck( inputR,inputC); if (boxCheck==true) //only sets value of t[r][c] if boxCheck is valid { t[inputR][inputC] = 'O';//to fill box: } } } while (boxCheck==false);//loop repeats if user enters a value equal to a previous entry. } //precondition: ttt board instantiated as md array, r and c of user input defined //postcondition: will return boxCheck as t/f, allowing program to decide whether box already filled. public void boxCheck( int r, int c){ if (t[r][c]!=0) {System.out.println("This box is already filled. Try entering a value for another box."); boxCheck = false;} if (t[r][c]==0) {boxCheck = true;} } //precondition: ttt board instantiated as md array, 3x's or 3o's entered into md array and appear consecutively //postcondition: will decide if game is over and who is winner public static boolean checkWinner(char t[][]){ if (t[1][1]=='X' && t[1][2]=='X' && t[1][3]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][1]=='O' && t[1][2]=='O' && t[1][3]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } if (t[2][1]=='X' && t[2][2]=='X' && t[2][3]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[2][1]=='O' && t[2][2]=='O' && t[2][3]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } if (t[3][1]=='O' && t[3][2]=='O' && t[3][3]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } if (t[3][1]=='X' && t[3][2]=='X' && t[3][3]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][1]=='X' && t[2][2]=='X' && t[3][3]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][1]=='O' && t[2][2]=='O' && t[3][3]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } if (t[1][3]=='O' && t[2][2]=='O' && t[3][1]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } if (t[1][3]=='X' && t[2][2]=='X' && t[3][1]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][1]=='X' && t[2][1]=='X' && t[3][1]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][3]=='O' && t[2][3]=='O' && t[3][3]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } if (t[1][3]=='X' && t[2][3]=='X' && t[3][3]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][2]=='X' && t[2][2]=='X' && t[3][2]=='X') { System.out.println("Player 1 Wins!!!!."); return true; } if (t[1][2]=='O' && t[2][2]=='O' && t[3][2]=='O') { System.out.println("Player 2 Wins!!!!."); return true; } else return false; } public static void main(String[] args) { // TODO Auto-generated method stub TicTacToe tic=new TicTacToe(3,"John", "Joan"); int want2play = 1; //declares value which later checks whether players want to play again do { TicTacToe.resetBoard(tic); TicTacToe.printBoard(); //prints board for (int n=1; n<=10; n++) { if (n<=9) //only allows 9 turns { TicTacToe.enterValue(tic, n); TicTacToe.printBoard(tic); if (TicTacToe.checkWinner(tic)==true) {break;} } else //this is effectively the 10th turn. this was made in case nobody wins. System.out.println("Nobody Won! Wow, that's pathetic. LOL!!!"); } System.out.println("Would you like to play again? Press 1 for Yes; 2 for No."); Scanner keyboard = new Scanner(System.in); want2play = keyboard.nextInt(); if (want2play == 2) {break;} } while (want2play == 1);//if user wants to play again, this will be true; otherwise, it will be false. System.out.println("************End of Game************"); } }

OpenStudy (anonymous):

the object is almost completed but can't print out the output :(

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!