Answered step by step
Verified Expert Solution
Question
1 Approved Answer
*please write step by step algorithm of this java tictactoe game code given below* *Also I have attached the algorithm example below, this is exactly
*please write step by step algorithm of this java tictactoe game code given below**Also I have attached the algorithm example below, this is exactly how you have to write the algorithm of the given code* _____ package main; import java.util.Scanner; public class Main { public static void main(String[] args) { char[][] board = new char[3][3]; for (int i = 0; i for (int j = 0; j board[i][j] = '-'; } } Scanner scan = new Scanner(System.in); System.out.println("Let's start the game TIC TAC TOE."); System.out.print("Enter the name of Player1: "); String play1 = scan.nextLine(); System.out.print("Enter the name of Player2: "); String play2 = scan.nextLine(); boolean player1 = true; boolean gameEnded = false; while (!gameEnded) { drawBoard(board); if (player1) { System.out.println(play1 + "'s Turn (X): "); } else { System.out.println(play2 + "'s Turn (O): "); } char c = '-'; if (player1) { c = 'X'; } else { c = 'O'; } int row = 0; int col = 0; while (true) { System.out.print("Enter a row number (0, 1, or 2): "); row = scan.nextInt(); System.out.print("Enter a column number (0, 1, or 2): "); col = scan.nextInt(); if (row 2 || col > 2) { System.out.println("This position is off the "+ "bounds of the board! Try again."); } else if (board[row][col] != '-') { System.out.println("Someone has already made a "+ "move at this position! Try again."); } else { break; } } board[row][col] = c; if (playerHasWon(board) == 'X') { System.out.println(play1 + " has won!"); gameEnded = true; } else if (playerHasWon(board) == 'O') { System.out.println(play2 + " has won!"); gameEnded = true; } else { if (boardIsFull(board)) { System.out.println("It's a tie!"); gameEnded = true; } else { player1 = !player1; } } } drawBoard(board); scan.close(); } public static void drawBoard(char[][] board) { System.out.println("Board:"); for (int i = 0; i for (int j = 0; j System.out.print(board[i][j]); } System.out.println(); } } public static char playerHasWon(char[][] board) { for (int i = 0; i if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-') { return board[i][0]; } } for (int j = 0; j if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != '-') { return board[0][j]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') { return board[0][0]; } if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != '-') { return board[2][0]; } return ' '; } public static boolean boardIsFull(char[][] board) { for (int i = 0; i for (int j = 0; j if (board[i][j] == '-') { return false; } } } return true; } } Algorithm: 1. Start 2. Initialize 2 arrays, one named prime and other named nonprime. 3. New function prime which checks prime number 4. If ( num %i==0) then Return False Else true 5. Then add prime strings to new arrays. 6. Repeat step 7 until all the arrays are added 7. For int i=0 8. NewArray = oldArrays; 9. Increment counter 10. Loop end 11. Same add Non Prime numbers into a new array 12. Repeat step 7 until all the arrays are added 13. Print primeArray. 14. Print NonPrime array. 15. End
*Also I have attached the algorithm example below, this is exactly how you have to write the algorithm of the given code*
_____
package main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[][] board = new char[3][3];
for (int i = 0; i
for (int j = 0; j
board[i][j] = '-';
}
}
Scanner scan = new Scanner(System.in);
System.out.println("Let's start the game TIC TAC TOE.");
System.out.print("Enter the name of Player1: ");
String play1 = scan.nextLine();
System.out.print("Enter the name of Player2: ");
String play2 = scan.nextLine();
boolean player1 = true;
boolean gameEnded = false;
while (!gameEnded) {
drawBoard(board);
if (player1) {
System.out.println(play1 + "'s Turn (X): ");
} else {
System.out.println(play2 + "'s Turn (O): ");
}
char c = '-';
if (player1) {
c = 'X';
} else {
c = 'O';
}
int row = 0;
int col = 0;
while (true) {
System.out.print("Enter a row number (0, 1, or 2): ");
row = scan.nextInt();
System.out.print("Enter a column number (0, 1, or 2): ");
col = scan.nextInt();
if (row 2 || col > 2) {
System.out.println("This position is off the "+
"bounds of the board! Try again.");
} else if (board[row][col] != '-') {
System.out.println("Someone has already made a "+
"move at this position! Try again.");
} else {
break;
}
}
board[row][col] = c;
if (playerHasWon(board) == 'X') {
System.out.println(play1 + " has won!");
gameEnded = true;
} else if (playerHasWon(board) == 'O') {
System.out.println(play2 + " has won!");
gameEnded = true;
} else {
if (boardIsFull(board)) {
System.out.println("It's a tie!");
gameEnded = true;
} else {
player1 = !player1;
}
}
}
drawBoard(board);
scan.close();
}
public static void drawBoard(char[][] board) {
System.out.println("Board:");
for (int i = 0; i
for (int j = 0; j
System.out.print(board[i][j]);
}
System.out.println();
}
}
public static char playerHasWon(char[][] board) {
for (int i = 0; i
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != '-') {
return board[i][0];
}
}
for (int j = 0; j
if (board[0][j] == board[1][j] &&
board[1][j] == board[2][j] &&
board[0][j] != '-') {
return board[0][j];
}
}
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != '-') {
return board[0][0];
}
if (board[2][0] == board[1][1] &&
board[1][1] == board[0][2] &&
board[2][0] != '-') {
return board[2][0];
}
return ' ';
}
public static boolean boardIsFull(char[][] board) {
for (int i = 0; i
Algorithm: 1. Start 2. Initialize 2 arrays, one named prime and other named nonprime. 3. New function prime which checks prime number 4. If ( num %i==0) then Return False Else true 5. Then add prime strings to new arrays. 6. Repeat step 7 until all the arrays are added 7. For int i=0 8. NewArray = oldArrays; 9. Increment counter 10. Loop end 11. Same add Non Prime numbers into a new array 12. Repeat step 7 until all the arrays are added 13. Print primeArray. 14. Print NonPrime array. 15. End for (int j = 0; j
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
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