I\'m creating a TicTacToe Program for java. The code works and everything but I
ID: 3530519 • Letter: I
Question
I'm creating a TicTacToe Program for java. The code works and everything but I want the game to repeat if the user inputs Continue and Quit if the user wants end the game. All I needs is help in making the game do repitition like do while or something. Here is my code. import java.util.Scanner; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.io.BufferedReader; public class TicTacToe { private static char [][] board = new char[3][3]; private String player1; private String player2; private int currentPlayer; private char marker1; private char marker2; private char newGame = 'N'; private int plays; private static BufferedReader userInput = new BufferedReader (new InputStreamReader ( System.in)); private static boolean checkWin = false; public static void newGame() { for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { board[i][j]=' '; } } String newGame = board[0][0]+ " "+"|"+" " +board[0][1]+" "+"|"+" " +board[0][2] +" "+ "-----------" +" "+ board[1][0]+ " "+"|"+" " +board[1][1]+" "+ "|"+" " +board[1][2] +" "+ "-----------" +" "+ board[2][0]+ " "+"|"+" " +board[2][1]+" "+ "|"+" " +board[2][2] +" "; System.out.print(" " + newGame); } public static boolean checkdraw() { for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { if (board[i][j]==' ') return false; } } return true; } public static void writeBoard() { System.out.println("Tic Tac Toe: " + " "); String topRow = "NW | N | NE"; System.out.println(" " +topRow); System.out.println("----|----|----"); String MiddleRow = "W | C | E"; System.out.println(" " + MiddleRow); System.out.println("----|----|----"); String BottomRow = "SW | S | SE"; System.out.println(" " + BottomRow + " "); } public static void getMove() throws IOException { String in=userInput.readLine(); if(in.equals("Q")) { System.out.println("Thanks for Playing!"); System.exit(0); } else if (in.equals("N")){ System.out.println("New Game"); } if(in.equals("X-NW")) { board[0][0] = 'X'; } else if(in.equals("O-NW")) { board[0][0] = 'O'; } else if(in.equals("X-N")) { board[0][1] = 'X'; } else if(in.equals("O-N")) { board[0][1] = 'O'; } if(in.equals("X-NE")) { board[0][2] = 'X'; } else if(in.equals("O-NE")) { board[0][2] = 'O'; } if(in.equals("X-W")) { board[1][0] = 'X'; } else if(in.equals("O-W")) { board[1][0] = 'O'; } if(in.equals("X-C")) { board[1][1] = 'X'; } else if(in.equals("O-C")) { board[1][1] = 'O'; } if(in.equals("X-E")) { board[1][2] = 'X'; } else if(in.equals("O-E")) { board[1][2] = 'O'; } if(in.equals("X-SW")) { board[2][0] = 'X'; } else if(in.equals("O-SW")) { board[2][0] = 'O'; } if(in.equals("X-S")) { board[2][1] = 'X'; } else if(in.equals("O-S")) { board[2][1] = 'O'; } if(in.equals("X-SE")) { board[2][2] = 'X'; } else if(in.equals("O-SE")) { board[2][2] = 'O'; } } public static boolean winner() throws IOException { //getMove(); if (board[0][0]=='X' && board[0][1] =='X' && board[0][2]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[1][0] =='X' && board[1][1]=='X' && board[1][2]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[2][0]=='O' && board[2][1]=='O' && board[2][1]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O') { System.out.println("Congrats! Player O Wins! "/*+thumb*/); checkWin = true; } else if (board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='X') { System.out.println("Congrats! Player X Wins! "/*+thumb*/); checkWin = true; } else if (board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O') { System.out.println("Congrats! Player 0 Wins! "/*+thumb*/); checkWin = true; //System.exit(0); }/*else System.out.println("You tied the game.");*/ if (!checkWin && !checkdraw()) return false; else if (checkdraw()) { System.out.print("The game is drawn "); return true; } return true; } public static void writeboard() { String bord = board[0][0]+ " "+"|"+" " +board[0][1]+" "+"|"+" " +board[0][2] +" "+ "----------" +" "+ " " +board[1][0]+ " "+"|"+" " +board[1][1]+" "+ "|"+" " +board[1][2] +" "+ "----------" +" "+ " " +board[2][0]+ " "+"|"+" " +board[2][1]+" "+ "|"+" " +board[2][2] +" "; System.out.print(bord); } // private static void newGame() // { // getnewGame(); // } //public static void main(String[] args) throws IOException //{ // newGame(); // boolean flag=true; // while(!winner()) // { // if (!flag) // { // System.out.print("Player O turn: "); // flag=true; // } // else // { // System.out.print("Player X turn: "); // flag=false; // } // writeboard(); // getMove(); //} //} } // TicTacTeoDemo import java.util.Scanner; import java.io.IOException; public class TicTacToeDemo extends TicTacToe { //private static char[][]board = new char[3][3]; //public static void main(String[] args)throws IOException //{ //newGame(); //while(!winner()) //{ // writeBoard(); // getMove(); //} //} public static void main(String[] args) throws IOException { newGame(); boolean flag=true; while(!winner()) { if (!flag) { System.out.print("Player O turn: "); flag=true; } else { System.out.print("Player X turn: "); flag=false; } writeboard(); getMove(); } } }Explanation / Answer
The thing you need to do is add a loop inside the main funcion for repeatinf it whenever the user enters Continue.
The main method code is given below
public static void main(String[] args) throws IOException {
newGame();
Scanner inputScanner = new Scanner(System.in);
String response = "Continue";
boolean flag = true;
do {
while (!winner())
{
if (!flag)
{
System.out.print("Player O turn: ");
flag = true;
}
else
{
System.out.print("Player X turn: ");
flag = false;
}
writeboard();
getMove();
}
response= inputScanner.next();
}while(response.equalsIgnoreCase("Continue"));
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.