我正在写一个TicTacToe游戏,它反复提示用户移动.它要求选择超过9次,我不知道为什么.
我的设计有一个二维数组来存储有关电路板当前状态的信息,使用JOptionPane要求用户选择电路板(左上1个,中上2个,右上3个,左中4个,等等).每次移动后,将当前板输出到控制台.如果已使用某个位置,请再次提示用户.(没有必要进行胜利者检查,因为我们被告知要再次这样做.
这是我的整个代码:
import javax.swing.JOptionPane;
// Basic TicTacToe game.
public class TicTacToe1 {
public static void main(String[] args){
// Hello there!
Object[] options = { "I'm ready to play!",};
Object[] options2 = { "Select another number.",};
JOptionPane.showOptionDialog(null,"To play TicTacToe, use the numbers 1 to 9 to choose where you place a mark. Are you ready?","TicTacToe1",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
// Define the board.
char board[][] = new char[3][3];
// Zero out the board.
for(int a=0; a<3; a++) {
for(int b=0; b<3; b++) {
board[a][b] = ' ';
}
}
// Print an initial, clean board.
print(board);
// Use a for loop to ask for the selection and nest the selector into it.
for(int i=1; i<10 ; i++) {
if ((i%2) == 1) {
boolean goahead = true;
while (goahead) {
int selection = Integer.parseInt(JOptionPane.showInputDialog(null,"Where do you want to place an X?"));
if ((board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='X') || (board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='O')) {
JOptionPane.showOptionDialog(null,"I'm sorry, the number "+selection+" spot is already being used.","TicTacToe1",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options2, options2[0]);
} else {
goahead = false;
board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3] = 'X';
print(board);
}
}
}
if ((i%2) == 1) {
boolean goahead = true;
while (goahead) {
int selection = Integer.parseInt(JOptionPane.showInputDialog(null,"Where do you want to place an O?"));
if ((board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='X') || (board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3]=='O')) {
JOptionPane.showOptionDialog(null,"I'm sorry, the number "+selection+" spot is already being used.","TicTacToe1",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options2, options2[0]);
} else {
goahead = false;
board[(((selection-1)-((selection-1)%3))/3)][(selection-1)%3] = 'O';
print(board);
}
}
}
// didsomeonewinyet(board);
}
}
// Make a helper function named print to print the board
public static void print(char board[][]){
for(int a=0; a<3; a++) {
for(int b=0; b<3; b++) {
System.out.print("|"+board[a][b]+"|");
}
System.out.println();
}
System.out.println();
}
// Winner checker.
// public static void didsomeonewinyet(char board[][]){
// Manually checking will yield 16 cases (2 diagonal, 3 horizontal, 3 vertical, either X or O), which is not that bad.
// }
}
Run Code Online (Sandbox Code Playgroud)
为什么要求选择次数太多?
for循环中的if条件是相同的,因此它们都在循环的相同迭代期间发生:
if ((i%2) == 1) {
Run Code Online (Sandbox Code Playgroud)
其中一个if语句应该是:
if ((i%2) == 0) {
Run Code Online (Sandbox Code Playgroud)
取决于你想先去的人.
或者你可以使用else语句:
if ((i%2) == 1) {
//code for x to go
}else {
//code for y to go
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |