我对其中一种方法有疑问 java tic tac toe
I have a question about one of the method java tic tac toe
public static void main(String[] args) {
// create scanner to read user input
// create char[] array to store board values
// set positions of board from 1 to 9
// 2 players: 'X' and 'O', start with 'X'
Scanner input = new Scanner(System.in);
char board[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
char player = 'X';
char player2 = 'O';
// display the playing board in 3x3 layout
display(board);
// play game until gameOver is true
boolean gameOver = false;
while (!gameOver) {
// get current player choice, and update board value
receiveUserChoice(player, input, board);
// display the updated board
display(board);
// switch player 'X' -> 'O' or 'O' -> 'X'
player = (player == 'X') ? 'O' : 'X';
// check for winner, draw or game is not over yet
gameOver = isGameOver(board);
// ** important ** remove the following statement
// I added it to avoid infinite loop
// gameOver value should return from isGameOver() method as above
gameOver = true; // TODO: remove this
}
}
// display board in 3x3 layout
// note that array indices from 0 to 8 = positions from 1 to 9
public static void display(char[] board) {
System.out.println("");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
System.out.println("");
}
// get current player choice, and update board value
public static void receiveUserChoice(char player, Scanner input, char[] board) {
// Step 1: get user input, a position from 1 to 9
// Step 2: make sure it is a valid position, and the position is not taken yet
// 2.1: if it is valid input, mark the board position with the current player
// 2.2: if it is not valid, print message repeat Step 1. until a valid input is obtained
boolean loop = true;
while(loop)
{
System.out.print("Which position do you want?");
int n = input.nextInt();
if(n<10&&n>0&&Character.isDigit(board[n-1]))
{
board[n-1]=player;
loop=false; //After update, loop is false.
//Whenever the board value is updated, everytime we will check if the game is over or if it's still continued.
}
else
{
System.out.println("Invalid value!Repeat Step1!");
}
}
// TODO: Add statements
}
// check for winner, draw or game is not over yet
public static boolean isGameOver(char[] board) {
// Step 1: if there is a winner, print winner message, return true
// Step 2: if it is a draw, print draw message, return true
// Step 3: else the game is not over yet, return false
if(board[0]==board[1]&&board[1]==board[2])//It's == cuz we are comparing these value.
{
System.out.println(board[0]+" wins."); //Board 0 can be O or X.
return true;
}
else if(board[3]==board[4]&&board[4]==board[5])
{
System.out.println(board[3]+" wins.");
return true;
}
else if(board[6]==board[7]&&board[7]==board[8])
{
System.out.println(board[6]+" wins.");
return true;
}
else if(board[0]==board[3]&&board[3]==board[6])
{
System.out.println(board[0]+" wins.");
return true;
}
else if(board[1]==board[4]&&board[4]==board[7])
{
System.out.println(board[1]+" wins.");
return true;
}
else if(board[2]==board[5]&&board[5]==board[8])
{
System.out.println(board[2]+" wins.");
return true;
}
else if(board[0]==board[4]&&board[4]==board[8])
{
System.out.println(board[0]+" wins.");
return true;
}
else if(board[2]==board[4]&&board[4]==board[6])
{
System.out.println(board[2]+" wins.");
return true;
}
else //It has step 2 and step 3.
{
int cnt=0;
for(int i=0;i<board.length;i++)
{
if(board[i]=='O'||board[i]=='X')
{
cnt+=1;
}
}
if(cnt==9)
{
System.out.println("Draw!");
return true;
}
}
}
这是我根据我教授所说的关于井字棋的代码。他说我可以把第2步和第3步都放在else语句中,但我不知道该怎么做。我试了一下,但 IntelliJ(我正在使用的程序)说它没有 return 语句。我不明白为什么它有错误。请帮我。谢谢
问题正如你的 IDE 告诉你的那样,你有一个函数,IDE 无法验证你有来自每个可能路径的 return 语句。
在您的情况下,将最后一个 if(the cnt<9) 更改为前一个 if(the cnt==9) 的 else,因为它必须是其中之一。
public static void main(String[] args) {
// create scanner to read user input
// create char[] array to store board values
// set positions of board from 1 to 9
// 2 players: 'X' and 'O', start with 'X'
Scanner input = new Scanner(System.in);
char board[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
char player = 'X';
char player2 = 'O';
// display the playing board in 3x3 layout
display(board);
// play game until gameOver is true
boolean gameOver = false;
while (!gameOver) {
// get current player choice, and update board value
receiveUserChoice(player, input, board);
// display the updated board
display(board);
// switch player 'X' -> 'O' or 'O' -> 'X'
player = (player == 'X') ? 'O' : 'X';
// check for winner, draw or game is not over yet
gameOver = isGameOver(board);
// ** important ** remove the following statement
// I added it to avoid infinite loop
// gameOver value should return from isGameOver() method as above
gameOver = true; // TODO: remove this
}
}
// display board in 3x3 layout
// note that array indices from 0 to 8 = positions from 1 to 9
public static void display(char[] board) {
System.out.println("");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
System.out.println("");
}
// get current player choice, and update board value
public static void receiveUserChoice(char player, Scanner input, char[] board) {
// Step 1: get user input, a position from 1 to 9
// Step 2: make sure it is a valid position, and the position is not taken yet
// 2.1: if it is valid input, mark the board position with the current player
// 2.2: if it is not valid, print message repeat Step 1. until a valid input is obtained
boolean loop = true;
while(loop)
{
System.out.print("Which position do you want?");
int n = input.nextInt();
if(n<10&&n>0&&Character.isDigit(board[n-1]))
{
board[n-1]=player;
loop=false; //After update, loop is false.
//Whenever the board value is updated, everytime we will check if the game is over or if it's still continued.
}
else
{
System.out.println("Invalid value!Repeat Step1!");
}
}
// TODO: Add statements
}
// check for winner, draw or game is not over yet
public static boolean isGameOver(char[] board) {
// Step 1: if there is a winner, print winner message, return true
// Step 2: if it is a draw, print draw message, return true
// Step 3: else the game is not over yet, return false
if(board[0]==board[1]&&board[1]==board[2])//It's == cuz we are comparing these value.
{
System.out.println(board[0]+" wins."); //Board 0 can be O or X.
return true;
}
else if(board[3]==board[4]&&board[4]==board[5])
{
System.out.println(board[3]+" wins.");
return true;
}
else if(board[6]==board[7]&&board[7]==board[8])
{
System.out.println(board[6]+" wins.");
return true;
}
else if(board[0]==board[3]&&board[3]==board[6])
{
System.out.println(board[0]+" wins.");
return true;
}
else if(board[1]==board[4]&&board[4]==board[7])
{
System.out.println(board[1]+" wins.");
return true;
}
else if(board[2]==board[5]&&board[5]==board[8])
{
System.out.println(board[2]+" wins.");
return true;
}
else if(board[0]==board[4]&&board[4]==board[8])
{
System.out.println(board[0]+" wins.");
return true;
}
else if(board[2]==board[4]&&board[4]==board[6])
{
System.out.println(board[2]+" wins.");
return true;
}
else //It has step 2 and step 3.
{
int cnt=0;
for(int i=0;i<board.length;i++)
{
if(board[i]=='O'||board[i]=='X')
{
cnt+=1;
}
}
if(cnt==9)
{
System.out.println("Draw!");
return true;
}
}
}
这是我根据我教授所说的关于井字棋的代码。他说我可以把第2步和第3步都放在else语句中,但我不知道该怎么做。我试了一下,但 IntelliJ(我正在使用的程序)说它没有 return 语句。我不明白为什么它有错误。请帮我。谢谢
问题正如你的 IDE 告诉你的那样,你有一个函数,IDE 无法验证你有来自每个可能路径的 return 语句。 在您的情况下,将最后一个 if(the cnt<9) 更改为前一个 if(the cnt==9) 的 else,因为它必须是其中之一。