gpt4 book ai didi

java,游戏板创建不需要的新行

转载 作者:行者123 更新时间:2023-12-01 12:45:54 25 4
gpt4 key购买 nike

我真的需要帮助,我已经浪费了很多时间来弄清楚。所以我试图制作一个游戏,其中游戏板表示为数组。当我到达字段的右侧时,一切都完美运行,新行被创建。像这样:

 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){

}

}

最佳答案

您用空格分割了 field 字符串,因此“0\n”被视为一个标记。因此,当您替换字段中的最后一个元素时,您也会替换新行。

关于java,游戏板创建不需要的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24711683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com