我现在在这个tictactoe项目中工作。现在,我的问题是如何在不使用方法的情况下检查tictactoe 中的绘制游戏和 Spot已经采取,只检查数组。我被困了,我解决了3天。
这是2名球员Tictactoe。
这是我的代码:
public static void main(String[] args)
{
Scanner wow = new Scanner(System.in);
int players;
int j=0, i=0;
char[][] board;
board=new char[3][3];
board[0][0]='1';
board[0][1]='2';
board[0][2]='3';
board[1][0]='4';
board[1][1]='5';
board[1][2]='6';
board[2][0]='7';
board[2][1]='8';
board[2][2]='9';
System.out.println("Tic-tac-toe game");
System.out.println("_____________");
System.out.println("|-"+board[0][0]+"-|-"+board[0][1]+"-|-"+board[0][2]+"-|");
System.out.println("|-"+board[1][0]+"-|-"+board[1][1]+"-|-"+board[1][2]+"-|");
System.out.println("|-"+board[2][0]+"-|-"+board[2][1]+"-|-"+board[2][2]+"-|");
System.out.println("|---|---|---|");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
players=wow.nextInt();
if(board[i][j]%2==1)
{
if (players==1)
{
board[0][0]='X';
}
else if (players==2)
{
board[0][1]='X';
}
else if (players==3)
{
board[0][2]='X';
}
else if (players==4)
{
board[1][0]='X';
}
else if (players==5)
{
board[1][1]='X';
}
else if (players==6)
{
board[1][2]='X';
}
else if (players==7)
{
board[2][0]='X';
}
else if (players==8)
{
board[2][1]='X';
}
else if (players==9)
{
board[2][2]='X';
}
else if(players>=10||players<=0)
{
System.out.println("Invalid position");
}
System.out.println("_____________");
System.out.println("|-"+board[0][0]+"-|-"+board[0][1]+"-|-"+board[0][2]+"-|");
System.out.println("|-"+board[1][0]+"-|-"+board[1][1]+"-|-"+board[1][2]+"-|");
System.out.println("|-"+board[2][0]+"-|-"+board[2][1]+"-|-"+board[2][2]+"-|");
System.out.println("|---|---|---|");
if(board[0][0]=='X'&&board[0][1]=='X'&&board[0][2]=='X')
{
System.out.println("X wins");
break;
}
if(board[1][0]=='X'&&board[1][1]=='X'&&board[1][2]=='X')
{
System.out.println("X wins");
break;
}
if(board[2][0]=='X'&&board[2][1]=='X'&&board[2][2]=='X')
{
System.out.println("X wins");
break;
}
if(board[0][0]=='X'&&board[1][0]=='X'&&board[2][0]=='X')
{
System.out.println("X wins");
break;
}
if(board[0][1]=='X'&&board[1][1]=='X'&&board[2][1]=='X')
{
System.out.println("X wins");
break;
}
if(board[0][2]=='X'&&board[1][2]=='X'&&board[2][2]=='X')
{
System.out.println("X wins");
break;
}
if(board[0][0]=='X'&&board[1][1]=='X'&&board[2][2]=='X')
{
System.out.println("X wins");
break;
}
if(board[0][2]=='X'&&board[1][1]=='X'&&board[2][0]=='X')
{
System.out.println("X wins");
break;
}
}
else if(board[i][j]%2==0)
{
if (players==1)
{
board[0][0]='O';
}
else if (players==2)
{
board[0][1]='O';
}
else if (players==3)
{
board[0][2]='O';
}
else if (players==4)
{
board[1][0]='O';
}
else if (players==5)
{
board[1][1]='O';
}
else if (players==6)
{
board[1][2]='O';
}
else if (players==7)
{
board[2][0]='O';
}
else if (players==8)
{
board[2][1]='O';
}
else if (players==9)
{
board[2][2]='O';
}
else if(players>=10||players<=0)
{
System.out.println("Invalid position");
}
System.out.println("_____________");
System.out.println("|-"+board[0][0]+"-|-"+board[0][1]+"-|-"+board[0][2]+"-|");
System.out.println("|-"+board[1][0]+"-|-"+board[1][1]+"-|-"+board[1][2]+"-|");
System.out.println("|-"+board[2][0]+"-|-"+board[2][1]+"-|-"+board[2][2]+"-|");
System.out.println("|---|---|---|");
if(board[0][0]=='O'&&board[0][1]=='O'&&board[0][2]=='O')
{
System.out.println("O wins");
break;
}
else if(board[1][0]=='O'&&board[1][1]=='O'&&board[1][2]=='O')
{
System.out.println("X wins");
break;
}
else if(board[2][0]=='O'&&board[2][1]=='O'&&board[2][2]=='O')
{
System.out.println("O wins");
break;
}
else if(board[0][0]=='O'&&board[1][0]=='O'&&board[2][0]=='O')
{
System.out.println("O wins");
break;
}
else if(board[0][1]=='O'&&board[1][1]=='O'&&board[2][1]=='O')
{
System.out.println("O wins");
break;
}
else if(board[0][2]=='O'&&board[1][2]=='X'&&board[2][2]=='O')
{
System.out.println("O wins");
break;
}
else if(board[0][0]=='O'&&board[1][1]=='O'&&board[2][2]=='O')
{
System.out.println("O wins");
break;
}
else if(board[0][2]=='O'&&board[1][1]=='O'&&board[2][0]=='O')
{
System.out.println("O wins");
break;
}
}
}
}
}
另外,有时我无法打破循环。
答案 0 :(得分:0)
这是很多代码。我真的不知道你希望别人看什么。
参考您的问题,检查已经拍摄的地点:
if(board[x][y] == 'X' || board[x][y] == 'O') {
//choose another spot
}
答案 1 :(得分:0)
当您询问用户放置下一件的位置时,如果当前点已经包含X和O,则需要检查您的板变量。这可以通过简单的if语句来完成
if (board[0][0] == 'X' && board[0][0] == 'O') { // you need to check the whole board, not just [0][0], but you get the point.
如果是,请要求用户输入新的合法移动。这可以通过简单的Do-While Loop
来完成我还建议您查看Switch Statement,这将使您的代码更容易阅读。例如,代码的第一部分,第34-73行可以替换为以下内容:
switch (players) {
case 1: board[0][0]='X'; break;
case 2: board[0][3]='X'; break;
case 3: board[0][2]='X'; break;
case 4: board[1][0]='X'; break;
case 5: board[1][4]='X'; break;
case 6: board[1][2]='X'; break;
case 7: board[2][0]='X'; break;
case 8: board[2][5]='X'; break;
case 9: board[2][2]='X'; break;
default: System.out.println("Invalid position");
}
修改:与如何确定平局相关。
你了解游戏如何与Tic-Tac-Toe结合?一旦你知道确定平局不应该太困难。
提示:如果您的代码达到了董事会已满且未宣布获胜者的程度,那么游戏必须具有捆绑性。 (在for循环之后)