gpt4 book ai didi

java - 用 JAVA 编写井字棋游戏。使用许多 if 语句我如何让游戏读取平局游戏?

转载 作者:行者123 更新时间:2023-11-30 03:38:10 25 4
gpt4 key购买 nike

提示

欢迎来玩猜谁赢的游戏!请输入您的游戏1板(*退出)> XXOOOOXXOO
您的游戏1如下:
XXO
OOX
XOO
您的游戏 1:平局

请进入您的游戏2板(*表示退出)> XXXOOXXOO
您的游戏2如下:
XXX
OOX
XOO
您的游戏 2:X 在第 1 行赢得了游戏。
等等

<小时/>

我怎样才能让它读取平局游戏?

package PA6;
import java.util.Scanner;

public class PA6 {
private static char [ ] [ ] ttt = new char [4] [4] ;
private static int gameNum = 1;


public static void main(String[] args)
{//MAIN OPEN
System.out.println("Welcome to play the game of guessing who is winning!");
Scanner scan = new Scanner (System.in);
String gameString;
System.out.println("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();
int n;

while (!gameString.equals("*"))
{//WHILE LOOP OPEN
System.out.println("Your game " + gameNum + " is as follows: ");
n = 0;
ttt = new char [4][4];
for(int i = 1 ; i < 4; i++)
{//FOR LOOP OPEN
for(int j = 1; j < 4; j++)
{//NESTED FOR LOOP OPEN
ttt [i][j]= gameString.charAt(n);
n++;
}//NESTED FOR LOOP CLOSED
}//FOR LOOP CLOSED
System.out.println(ttt[1][1] + "" + ttt[1][2] + "" + ttt[1][3]);
System.out.println(ttt[2][1] + "" + ttt[2][2] + "" + ttt[2][3]);
System.out.println(ttt[3][1] + "" + ttt[3][2] + "" + ttt[3][3]);

if(winRow1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 1");
if(winRow2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 2");
if(winRow3('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 3");
if(winRow1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 1");
if(winRow2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 2");
if(winRow3('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 3");
if(winColumn1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 1");
if(winColumn2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 2");
if(winColumn3('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 3");
if(winColumn1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 1");
if(winColumn2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 2");
if(winColumn3('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 3");
if(winDiagonal1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by diagonal 1");
if(winDiagonal1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by diagonal 1");
if(winDiagonal2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by diagonal 2");
if(winDiagonal2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by diagonal 2");

gameNum++;

System.out.print("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();




}//WHILE LOOP CLOSED
}//MAIN CLOSED

public static boolean winDiagonal1( char player, char a [][])
{
if ( ttt[1][1] == player && ttt[2][2] == player && ttt[3][3] == player )
return true;
return false;
}

public static boolean winDiagonal2 (char player, char a [][])
{
if( ttt[1][3] == player && ttt[2][2] == player && ttt[3][1] == player )
return true;
return false;
}
public static boolean winRow1 (char player, char a [][])
{
if (ttt[1][1] == player && ttt[1][2] == player && ttt[1][3] == player)
return true;
return false;
}

public static boolean winRow2 (char player, char a [][])
{
if (ttt[2][1] == player && ttt[2][2] == player && ttt[2][3] == player)
return true;
return false;
}

public static boolean winRow3 (char player, char a [][])
{
if (ttt[3][1] == player && ttt[3][2] == player && ttt[3][3] == player)
return true;
return false;
}
public static boolean winColumn1 (char player, char a [][])
{
if (ttt[1][1] == player && ttt[2][1] == player && ttt [3][1] == player)
return true;
return false;
}

public static boolean winColumn2 (char player, char a [][])
{
if (ttt[1][2] == player && ttt[2][2] == player && ttt [3][2] == player)
return true;
return false;
}

public static boolean winColumn3 (char player, char a [][])
{
if (ttt[1][3] == player && ttt[2][3] == player && ttt [3][3] == player)
return true;
return false;
}
}

最佳答案

如果您将 if 语句转换为组合的 if else if 语句,那么 else 情况下剩下的内容将是平局。

if(winRow1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 1");
else if(winRow2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 2");
.
.
.
else
System.out.println("Your game " + gameNum + ": It is a tie");

顺便说一句,这样您就可以避免出现多条消息,例如同时选中一行和一列时;)。

关于java - 用 JAVA 编写井字棋游戏。使用许多 if 语句我如何让游戏读取平局游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com