0
我真的需要幫助,我浪費了很多時間搞清楚。所以我試圖做一個遊戲,其中,遊戲板表示爲數組。當我到達現場的右側時,一切都可以完美運行,新的線路被創建。這樣的:java,gameboard創建一個不需要的新行
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){
}
}
[調試器](http://en.wikipedia.org/wiki/Debugger) – drewmoore
當您到達場的右側時,是否要將該x移到下一行?或者你想要什麼? –
當它到達右側的最後0時,程序會告訴無效的移動,並且用戶必須移動到其他地方,無效的移動消息可以工作 – m2rt