在二维阵列游戏板上放置棋子
Placing pieces on 2D array gameboard
我正在尝试制作一个程序,其中有一个 8x8 星号游戏板。用户必须在棋盘上放置八个棋子,以便每行有一个棋子,但用户可以输入棋子在每一行中位于哪一列。最后,我想在他们决定每行中放置每块棋子的位置后打印棋盘的视觉效果。例如,如果他们输入:4, 1, 7, 0, 6, 4, 3, 2 棋盘的第一行看起来像 ***P****(P = 棋子),棋子在其他七行的正确列。现在在我的代码中,我创建了 8x8 游戏板,并用 0 填充了板(不知道如何使它成为星号)。我现在正处于需要用户输入以将每一行的片段放入正确列中的地步,但我正在努力这样做。我感谢所有决定帮助我的人!
public void problem3(){
Scanner in = new Scanner(System.in);
char[][] board = new char[8][8];//Initializes an 8x8 board
//This creates the 8x8 board and fills the board with asterisks
for (char row = 0; row < board.length; row++) {
for (char column = 0; column < board[0].length; column++) {
board[row][column] = '*';
}
for (row = 0; row < 8; row++) { //creates right amount of cells for 8 rows
for (char column=0; column<8; column++) { //creates right amount of cells for the 8 columns
System.out.print(board[row][column] + " "); //prints # of cells
}
System.out.println(); //prints each row on new line
}
}
for (int row = 0; row < 8; row++) {
int col = 0;
System.out.println("In which column would you like to place the queen for column ");
col = in.nextInt();
board[row][col] = 'P'; //mark board with "P"
}
for (int i=0; i<8; i++) { //loops prints board
System.out.println(Arrays.toString(board[i]));
}
}
- 使用合适的数据类型。现在因为你需要“*”和 "P",字符而不是 int 似乎更好。
- 用“*”而不是 0 初始化它们
- 循环每个 "row" 以获取用户输入 8 (0-7) 次
当用户输入int数据时,你知道它是上面步骤中提到的一列行,所以取数据如下:
col = in.nextInt();
board[row][col] = 'P';//mark it with p. You may need to validate input column added by user is in range of 0-7?
像这样打印电路板:
for (int i=0; i<8; i++) {
System.out.println(Arrays.toString(board[i]));
}
我正在尝试制作一个程序,其中有一个 8x8 星号游戏板。用户必须在棋盘上放置八个棋子,以便每行有一个棋子,但用户可以输入棋子在每一行中位于哪一列。最后,我想在他们决定每行中放置每块棋子的位置后打印棋盘的视觉效果。例如,如果他们输入:4, 1, 7, 0, 6, 4, 3, 2 棋盘的第一行看起来像 ***P****(P = 棋子),棋子在其他七行的正确列。现在在我的代码中,我创建了 8x8 游戏板,并用 0 填充了板(不知道如何使它成为星号)。我现在正处于需要用户输入以将每一行的片段放入正确列中的地步,但我正在努力这样做。我感谢所有决定帮助我的人!
public void problem3(){
Scanner in = new Scanner(System.in);
char[][] board = new char[8][8];//Initializes an 8x8 board
//This creates the 8x8 board and fills the board with asterisks
for (char row = 0; row < board.length; row++) {
for (char column = 0; column < board[0].length; column++) {
board[row][column] = '*';
}
for (row = 0; row < 8; row++) { //creates right amount of cells for 8 rows
for (char column=0; column<8; column++) { //creates right amount of cells for the 8 columns
System.out.print(board[row][column] + " "); //prints # of cells
}
System.out.println(); //prints each row on new line
}
}
for (int row = 0; row < 8; row++) {
int col = 0;
System.out.println("In which column would you like to place the queen for column ");
col = in.nextInt();
board[row][col] = 'P'; //mark board with "P"
}
for (int i=0; i<8; i++) { //loops prints board
System.out.println(Arrays.toString(board[i]));
}
}
- 使用合适的数据类型。现在因为你需要“*”和 "P",字符而不是 int 似乎更好。
- 用“*”而不是 0 初始化它们
- 循环每个 "row" 以获取用户输入 8 (0-7) 次
当用户输入int数据时,你知道它是上面步骤中提到的一列行,所以取数据如下:
col = in.nextInt(); board[row][col] = 'P';//mark it with p. You may need to validate input column added by user is in range of 0-7?
像这样打印电路板:
for (int i=0; i<8; i++) { System.out.println(Arrays.toString(board[i])); }