我必须在我的项目中工作,我必须找到TicTacToe游戏胜利者。
String [][]board=new String [10][10];10行10列在哪里
我可以在水平上找到赢家:
for (int I=0;I<10;I++){
String line1=new String();
for (int i=0;i<10;i++) {
line1+=index[I][i];
}
if (line1.equals("XXXXXXXXXX")) {
System.out.println("winner is X");///return here X
} else if (line1.equals("OOOOOOOOOO")) {
System.out.println("winner is O");//return here O
}
}
}但我无法找到一个垂直的赢家,我有一个选项,我可以硬编码,但我想对它进行逻辑编码,就像我的代码水平。
发布于 2021-04-19 15:35:21
我已经解决了这个问题,首先旋转数组,然后使用水平代码再次检查
//first find the transpose of the index.
for (int i = 0; i < 10; i++){
for (int j = i; j < 10; j++){
String temp = index[i][j];
index[i][j] = index[j][i];
index[j][i] = temp;
}
}
//reverse each row
for (int i = 0; i< 10; i++){
for(int j = 0; j< 10/2; j++){
String temp = index[i][j];
index[i][j] = index[i][10 - 1 - j];
index[i][10 - 1 - j] = temp;
}
}现在数组是旋转的,现在我可以使用我的水平检查代码。
for (int I=0;I<10;I++){
String line1=new String();
for (int i=0;i<10;i++) {
line1+=index[I][i];
}
if (line1.equals("XXXXXXXXXX")) {
System.out.println("winner is X");///return here X
}else if (line1.equals("OOOOOOOOOO")){
System.out.println("winner is O");//return here O
}
}发布于 2021-06-30 20:01:08
它只是首先旋转数组,然后再使用水平代码进行检查
//first find the transpose of the index.
for (int i = 0; i < 10; i++){
for (int j = i; j < 10; j++){
String temp = index[i][j];
index[i][j] = index[j][i];
index[j][i] = temp;
}
}
//reverse each row
for (int i = 0; i< 10; i++){
for(int j = 0; j< 10/2; j++){
String temp = index[i][j];
index[i][j] = index[i][10 - 1 - j];
index[i][10 - 1 - j] = temp;
}
}发布于 2021-06-30 20:08:08
旋转数组,然后再次检查使用水平代码,这很容易
//first find the transpose of the index.
for (int i = 0; i < 10; i++){
for (int j = i; j < 10; j++){
String temp = index[i][j];
index[i][j] = index[j][i];
index[j][i] = temp;
}
}
//reverse each row
for (int i = 0; i< 10; i++){
for(int j = 0; j< 10/2; j++){
String temp = index[i][j];
index[i][j] = index[i][10 - 1 - j];
index[i][10 - 1 - j] = temp;
}
}https://stackoverflow.com/questions/67162124
复制相似问题