我真的需要帮助,我浪费了很多时间搞清楚。所以我试图制作一个游戏,游戏板表示为数组。当我到达文件的右侧时,一切都完美地运行,创建了新的行。像那样:
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 X 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
d
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 X 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
d
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 X 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
d
Invalid move
无效移动
import java.util.Scanner;
public class SimpleGame {
static String [] board;
int posy = 0;
int EnemyY = 65;
public static void main(String [] args){
String showField = " X 0 0 0 0 0 0 0 0 0 0 \n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 M ";
System.out.print(showField);
System.out.print("");
System.out.print("");
String field = "0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0 ";
SimpleGame sg = new SimpleGame();
while(true){
sg.gamePlay(field);
}
}
public void gamePlay(String field){
field.trim();
board = field.split(" ");
Scanner sc = new Scanner(System.in);
System.out.println("");
int len = 11;
String move = sc.nextLine();
if(move.equals("d")){
if(posy == 10 || posy == 21 || posy == 32 || posy == 43 || posy == 54 || posy == 65){
System.out.println("Invalid move");
return;
}else{
posy += 1;
board[posy] = "X";
}
}else if(move.equals("s")){
if(55 <= posy && posy <= 66){
System.out.println("Invalid move");
return;
}else{
posy += len;
board[posy] = "X";
}
}else if(move.equals("a")){
if(posy == 0 || posy == 11 || posy == 22 || posy == 33 || posy == 44 || posy == 55){
System.out.println("Invalid move");
return;
}else{
posy -= 1;
board[posy] = "X";
}
}else if(move.equals("w")){
if(posy < 12){
System.out.println("Invalid move");
return;
}else{
posy -= len;
board[posy] = "X";
}
}
/*if(EnemyY == posy + len + 1 || posy == EnemyY + len + 1 || EnemyY == posy + len || posy == EnemyY +1 ){
System.out.println("Game Over");
return;
}*/
for (String board1 : board){
System.out.print(board1);
System.out.print(" ");
}
}
public void addEnemy(int start, int x, int y, int [] board){
}
}
答案 0 :(得分:1)
您将field
字符串拆分为空格,因此“0 \ n”被视为一个标记。因此,当您替换字段中的最后一个元素时,也可以替换新行。