我刚刚编写了一个通用的 Tic Tac Toe 程序,并希望通过在用户选择填充棋盘矩阵上的全神贯注位置时显示错误消息来对其进行修改。我面临的问题是我不能多次弹出错误消息。让我向您解释一下这个场景。
考虑到我是一个用户,我决定在 Tic Tac Toe 板上填补一个已经被占用的位置(被 X 或 O)。现在根据代码,会弹出一条错误消息,要求我选择另一个空网格。但是如果我仍然继续填充同一个位置,错误信息将不再弹出并且该位置现在将被新角色强行占用。我想要的是这样设计代码,即使用户输入无效位置 n 次,代码也应该返回错误消息 n 次。
请帮助我解决这个小错误。我在此消息中附上了我的代码。(代码有点长,请耐心等待)
import java.util.*;
class Main
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
String str[][]={{" "," | "," "," | "," "},{" - "," "," - "," "," - "},{" "," | "," "," | "," "},{" - "," "," - "," "," - "},{" "," | "," "," | "," "}};
int i,j;
System.out.println("Rules : \n 1.It is a two player game thus one chooses O while the other chooses X \n 2.Enter the coordinate of the postion which you want to fill \nExample 1,2 or 3,3(Commas not Required)");
Main ob=new Main();
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(str[i][j]);
System.out.println();
}
System.out.println("Player 1 will go first");
System.out.println("Player 1 enter your choice(O or X)");
String in=sc.nextLine();
if(in.equalsIgnoreCase("O"))
{
for(i=0;i<9;i++)
{
if(i%2==0)
{
ob.ChanceO(i,str);
if(ob.Win(str)=='O')
{
System.out.print("O wins ! \n Bye\n Created By Parth Singh");
break;
}
if(ob.Win(str)=='X')
{
System.out.print("X wins! \n Bye \n Created By Parth Singh");
break;
}
}
else
{
ob.ChanceX(i,str);
if(ob.Win(str)=='O')
{
System.out.print("O wins ! \n Bye \n Created By Parth Singh");
break;
}
if(ob.Win(str)=='X')
{
System.out.print("X wins!\n Created By Parth Singh");
break;
}
}
if(i==8)
System.out.print("Match Tied \n Well Played");
}
}
else if(in.equalsIgnoreCase("X"))
{
for(i=0;i<9;i++)
{
if(i%2!=0)
{
ob.ChanceO(i,str);
if(ob.Win(str)=='O')
{
System.out.print("O wins !");
break;
}
else if(ob.Win(str)=='X')
{
System.out.print("X wins!");
break;
}
}
else
{
ob.ChanceX(i,str);
if(ob.Win(str)=='O')
{
System.out.print("O wins !");
break;
}
if(ob.Win(str)=='X')
{
System.out.print("X wins!");
break;
}
}
if(i==8)
System.out.print("Match Tied \n Well Played");
}
}
else
{
System.out.println("Ivalid Input \n Try Agian \n Bye !");
}
}
void ChanceO(int i,String [][] str)
{
Scanner s=new Scanner(System.in);
System.out.println("O Enter Co-ordinate");
int O[]=new int[10];
O[i]=s.nextInt();
O[i+1]=s.nextInt();
{
if(O[i]>3||O[i]<1||O[i+1]>3||O[i+1]<1)
{
System.out.println("Invalid Co-ordinates \n Please Enter Agian");
O[i]=s.nextInt();
O[i+1]=s.nextInt();
}
}
if(O[i]%2==0&&O[i+1]%2==0&&str[O[i]][O[i+1]]==" ")
str[O[i]][O[i+1]]=" O ";
else if(O[i]%2!=0&&O[i+1]%2==0&&O[i]<2&& str[O[i]-1][O[i+1]]==" ")
str[O[i]-1][O[i+1]]=" O ";
else if(O[i]%2!=0&&O[i+1]%2==0&&O[i]>2&&str[O[i]+1][O[i+1]]==" ")
str[O[i]+1][O[i+1]]=" O ";
else if(O[i]%2==0&&O[i+1]%2!=0&&O[i+1]<2&&str[O[i]][O[i+1]-1]==" ")
str[O[i]][O[i+1]-1]=" O ";
else if(O[i]%2==0&&O[i+1]%2!=0&&O[i+1]>2&&str[O[i]][O[i+1]+1]==" ")
str[O[i]][O[i+1]+1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]<2&&O[i]<2&& str[O[i]-1][O[i+1]-1]==" ")
str[O[i]-1][O[i+1]-1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]>2&&O[i]<2&&str[O[i]-1][O[i+1]+1]==" ")
str[O[i]-1][O[i+1]+1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]<2&&O[i]>2&&str[O[i]+1][O[i+1]-1]==" ")
str[O[i]+1][O[i+1]-1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]>2&&O[i]>2&&str[O[i]+1][O[i+1]+1]==" ")
str[O[i]+1][O[i+1]+1]=" O ";
else
{
System.out.println("Position Occupied \n Please Enter the co-ordinates of a vacant position");
O[i]=s.nextInt();
O[i+1]=s.nextInt();
if(O[i]%2==0&&O[i+1]%2==0&&str[O[i]][O[i+1]]==" ")
str[O[i]][O[i+1]]=" O ";
else if(O[i]%2!=0&&O[i+1]%2==0&&O[i]<2&& str[O[i]-1][O[i+1]]==" ")
str[O[i]-1][O[i+1]]=" O ";
else if(O[i]%2!=0&&O[i+1]%2==0&&O[i]>2&&str[O[i]+1][O[i+1]]==" ")
str[O[i]+1][O[i+1]]=" O ";
else if(O[i]%2==0&&O[i+1]%2!=0&&O[i+1]<2&&str[O[i]][O[i+1]-1]==" ")
str[O[i]][O[i+1]-1]=" O ";
else if(O[i]%2==0&&O[i+1]%2!=0&&O[i+1]>2&&str[O[i]][O[i+1]+1]==" ")
str[O[i]][O[i+1]+1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]<2&&O[i]<2&& str[O[i]-1][O[i+1]-1]==" ")
str[O[i]-1][O[i+1]-1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]>2&&O[i]<2&&str[O[i]-1][O[i+1]+1]==" ")
str[O[i]-1][O[i+1]+1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]<2&&O[i]>2&&str[O[i]+1][O[i+1]-1]==" ")
str[O[i]+1][O[i+1]-1]=" O ";
else if(O[i]%2!=0&&O[i+1]%2!=0&&O[i+1]>2&&O[i]>2&&str[O[i]+1][O[i+1]+1]==" ")
str[O[i]+1][O[i+1]+1]=" O ";
}
for(int k=0;k<5;k++)
{
for(int j=0;j<5;j++)
System.out.print(str[k][j]);
System.out.println();
}
System.out.println("--------------------------------------------");
}
void ChanceX(int i,String [][] str)
{
Scanner sa=new Scanner(System.in);
System.out.println("X Enter Coordinate");
int X[]=new int[10];
X[i]=sa.nextInt();
X[i+1]=sa.nextInt();
{
if(X[i]>3||X[i]<1||X[i+1]>3||X[i+1]<1)
{
System.out.println("Invalid Co-ordinates \n Please Enter Correct Co-ordinates");
X[i]=sa.nextInt();
X[i+1]=sa.nextInt();
}
}
if(X[i]%2==0&&X[i+1]%2==0&&str[X[i]][X[i+1]]==" ")
str[X[i]][X[i+1]]=" X ";
else if(X[i]%2!=0&&X[i+1]%2==0&&X[i]<2&&str[X[i]-1][X[i+1]]==" ")
str[X[i]-1][X[i+1]]=" X ";
else if(X[i]%2!=0&&X[i+1]%2==0&&X[i]>2&&str[X[i]+1][X[i+1]]==" ")
str[X[i]+1][X[i+1]]=" X ";
else if(X[i]%2==0&&X[i+1]%2!=0&&X[i+1]<2&&str[X[i]][X[i+1]-1]==" ")
str[X[i]][X[i+1]-1]=" X ";
else if(X[i]%2==0&&X[i+1]%2!=0&&X[i+1]>2&&str[X[i]][X[i+1]+1]==" ")
str[X[i]][X[i+1]+1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]<2&&X[i]<2&&str[X[i]-1][X[i+1]-1]==" ")
str[X[i]-1][X[i+1]-1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]>2&&X[i]<2&&str[X[i]-1][X[i+1]+1]==" ")
str[X[i]-1][X[i+1]+1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]<2&&X[i]>2&&str[X[i]+1][X[i+1]-1]==" ")
str[X[i]+1][X[i+1]-1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]>2&&X[i]>2&&str[X[i]+1][X[i+1]+1]==" ")
str[X[i]+1][X[i+1]+1]=" X ";
else
{
System.out.println("Position Occupied \n Please Enter the co-ordinates of a vacant position");
X[i]=sa.nextInt();
X[i+1]=sa.nextInt();
if(X[i]%2==0&&X[i+1]%2==0&&str[X[i]][X[i+1]]==" ")
str[X[i]][X[i+1]]=" X ";
else if(X[i]%2!=0&&X[i+1]%2==0&&X[i]<2&&str[X[i]-1][X[i+1]]==" ")
str[X[i]-1][X[i+1]]=" X ";
else if(X[i]%2!=0&&X[i+1]%2==0&&X[i]>2&&str[X[i]+1][X[i+1]]==" ")
str[X[i]+1][X[i+1]]=" X ";
else if(X[i]%2==0&&X[i+1]%2!=0&&X[i+1]<2&&str[X[i]][X[i+1]-1]==" ")
str[X[i]][X[i+1]-1]=" X ";
else if(X[i]%2==0&&X[i+1]%2!=0&&X[i+1]>2&&str[X[i]][X[i+1]+1]==" ")
str[X[i]][X[i+1]+1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]<2&&X[i]<2&&str[X[i]-1][X[i+1]-1]==" ")
str[X[i]-1][X[i+1]-1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]>2&&X[i]<2&&str[X[i]-1][X[i+1]+1]==" ")
str[X[i]-1][X[i+1]+1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]<2&&X[i]>2&&str[X[i]+1][X[i+1]-1]==" ")
str[X[i]+1][X[i+1]-1]=" X ";
else if(X[i]%2!=0&&X[i+1]%2!=0&&X[i+1]>2&&X[i]>2&&str[X[i]+1][X[i+1]+1]==" ")
str[X[i]+1][X[i+1]+1]=" X ";
}
for(int k=0;k<5;k++)
{
for(int j=0;j<5;j++)
System.out.print(str[k][j]);
System.out.println();
}
System.out.println("--------------------------------------------");
}
char Win(String [][]str)
{
if((str[0][0].equals(" O ")&&str[0][2].equals(" O ")&&str[0][4].equals(" O "))||
(str[2][0].equals(" O ")&&str[2][2].equals(" O ")&&str[2][4].equals(" O "))||
(str[4][0].equals(" O ")&&str[4][2].equals(" O ")&&str[4][4].equals(" O "))||
(str[0][0].equals(" O ")&&str[2][0].equals(" O ")&&str[4][0].equals(" O "))||
(str[0][2].equals(" O ")&&str[2][2].equals(" O ")&&str[4][2].equals(" O "))||
(str[0][4].equals(" O ")&&str[2][4].equals(" O ")&&str[4][4].equals(" O "))||
(str[0][0].equals(" O ")&&str[2][2].equals(" O ")&&str[4][4].equals(" O "))||
(str[0][4].equals(" O ")&&str[2][2].equals(" O ")&&str[4][0].equals(" O ")))
{
return 'O';
}
else if((str[0][0].equals(" X ")&&str[0][2].equals(" X ")&&str[0][4].equals(" X "))||
(str[2][0].equals(" X ")&&str[2][2].equals(" X ")&&str[2][4].equals(" X "))||
(str[4][0].equals(" X ")&&str[4][2].equals(" X ")&&str[4][4].equals(" X "))||
(str[0][0].equals(" X ")&&str[2][0].equals(" X ")&&str[4][0].equals(" X "))||
(str[0][2].equals(" X ")&&str[2][2].equals(" X ")&&str[4][2].equals(" X "))||
(str[0][4].equals(" X ")&&str[2][4].equals(" X ")&&str[4][4].equals(" X "))||
(str[0][0].equals(" X ")&&str[2][2].equals(" X ")&&str[4][4].equals(" X "))||
(str[0][4].equals(" X ")&&str[2][2].equals(" X ")&&str[4][0].equals(" X ")))
{
return 'X';
}
else
return 'A';
}
}
`