我在java中创建这个控制台游戏,其中将提示玩家输入棋盘的尺寸,并且将被要求使用WASD键在棋盘内移动,这分别表示向上,向左,向下和向右。我已经用符号@
显示了棋盘和玩家的初始位置。每次我希望玩家在棋盘中移动时,我很难改变符号。这是我的代码
public class Bord {
public int n;
public int i, j, a, b, c;
public char move;
public char board[][] = new char[n][n];
Scanner in = new Scanner(System.in);
public Bord() {
System.out.println("\n\t************");
System.out.println("\n\tThe Ant Game\n");
System.out.println("\t************");
System.out.println("\nInstructions: \nWelcome to Ant Game. You are the ant (@). \nUse 'w', 'a', 's', or 'd' to move");
System.out.print("up, left, down or right consecutively. \nThere will be traps or rewards hidden \nin each tile (*) so be careful. Good luck!");
System.out.print("\n\nEnter board dimension: ");
n = in.nextInt();
System.out.println();
char board[][] = new char[n][n];
for(i=0; i<n; i++){
for(j=0; j<n; j++)
board[i][j] = '*';
}
i = 0;
j = 0;
board[i][j] = '@';
for(a=0; a<n; a++){
System.out.print("\t");
for(b=0; b<n; b++){
System.out.print(board[a][b]);
for(c=0; c<n; c++)
System.out.print("");
}
System.out.println();
}
System.out.println();
System.out.print("Press WASD for movement: ");
move = in.next().charAt(0);
switch (move)
{
//Move Up
case 'w':{
if(i==0&&j==0){
System.out.println("End of boundary.");
System.out.print("Move again: ");
move = in.next().charAt(0);
}
else{
for(i=0; i<n; i++){
for(j=0; j<n; j++)
board[i][j] = '*';
}
i = 0;
j = 0;
board[i-1][j] = '@';
display();
System.out.print("Move again: ");
move = in.next().charAt(0);
}
break;
}
//Move Left
case 'a':
System.out.println("End of boundary.");
System.out.print("Move again: ");
move = in.next().charAt(0);
break;
//Move Right
case 'd':{
board[i][j+1] = '@';
i = 0;
j = 0;
board[i][j] = '*';
System.out.println();
for(a=0; a<n; a++){
System.out.print("\t");
for(b=0; b<n; b++){
System.out.print(board[a][b]);
for(c=0; c<n; c++)
System.out.print("");
}
System.out.println();
}
System.out.println();
System.out.print("Move again: ");
move = in.next().charAt(0);
break;
}
//Move Down
case 's':{
board[i+1][j] = '@';
i = 0;
j = 0;
board[i][j] = '*';
System.out.println();
for(a=0; a<n; a++){
System.out.print("\t");
for(b=0; b<n; b++){
System.out.print(board[a][b]);
for(c=0; c<n; c++)
System.out.print("");
}
System.out.println();
}
System.out.println();
System.out.print("Move again: ");
move = in.next().charAt(0);
break;
}
default:{
System.out.print("Incorrect move. Press WASD only: ");
move = in.next().charAt(0);
}
}
System.out.print("Move again.");
move = in.next().charAt(0);
}
public void display(){
for(a=0; a<n; a++){
System.out.print("\t");
for(b=0; b<n; b++){
System.out.print(board[a][b]);
for(c=0; c<n; c++)
System.out.print("");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args){
Bord brd = new Bord();
}
}
这是我未完成的代码。由于这是一个动态2D数组,每次用户移动到他/她选择的方向时,如何将符号从*
更改为@
?
答案 0 :(得分:0)
访问数组中的n
元素是一种越界访问。
第一个元素位于索引0
,第二个元素位于1
,等等。最后一个元素位于索引n-1
。
答案 1 :(得分:0)
在java和大多数语言中,Arrays的索引从零开始。因此第一个元素位于索引0等
此外,一种可以改进代码的方法是用一个嵌套循环替换前两个循环。
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j] = '*';
}
}