gpt4 book ai didi

Java数组: 2D array dysfunction in terminal ASCII terminal game during attempt to swap elements

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

出于某种奇怪的原因,当我尝试在游戏中移动我的角色时,交换“移动”的数组将不起作用,并且当我尝试打印游戏中前两个移动的角色的当前位置时,什么也没有显示。总的来说,它的表现很奇怪。有人可以向我解释一下我做错了什么或者至少我怎样才能做得更好吗?谢谢。

代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class Adventure {
private Scanner in_file;
private int x_size, y_size, px, py, dx, dy;
private char[][] level;
private boolean game;
public Adventure() {
x_size = y_size = 20;
level = new char[x_size][y_size];
px = py = 1;
dx = dy = 0;
game = true;
}
public static void main(String []args) {
Adventure adv = new Adventure();
adv.load_game();
adv.main_game();
System.out.println("ADVENTURE");
}
public void load_game() {
try {
in_file = new Scanner(new File("level.txt"));
} catch (FileNotFoundException e) {
System.err.println("ERROR: Level could not be loaded!");
System.exit(1);
}
for (int i = 0; i < level.length; i++ ) {
for (int j = 0; j < level[i].length; j++) {
if (in_file.hasNextInt()) {
int nextInt = in_file.nextInt();
if (nextInt == 0)
level[i][j] = '-';
else if (nextInt == 1)
level[i][j] = '#';
}
}
}
in_file.close();
}
public void new_game() {

}
public void main_game() {
Scanner key = new Scanner(System.in);
Adventure adv_move = new Adventure();
level[px][py] = 'Q';
while (game) {
for (int i = 0; i < level.length; i++) {
for (int j = 0; j < level[i].length; j++) {
System.out.print(level[i][j]);
}
System.out.println();
}
System.out.println();
System.out.print("Enter a letter choice to move ->");
String temp = key.nextLine();
if (temp.equals("w")) {
dx = -1;
dy = 0;
}
else if (temp.equals("a")) {
dy = -1;
dx = 0;
}
else if (temp.equals("s")) {
dx = 1;
dy = 0;
}
else if (temp.equals("d")) {
dy = 1;
dx = 0;
}
else
dx = dy = 0;
System.out.println();
adv_move.move_check();
}
}
public void move_check() { //DYSFUNCTIONAL ARRAY!!!!
System.out.println(level[px][py]);
// System.out.println(level[px+dx][py+dy]);
if (level[px+dx][py+dy] != '#') {
level[px][py] = '-';
px += dx;
py += dy;
level[px][py] = 'Q';
}
else
System.out.print("Move invalid.\n");
}
public void save_game() {

}
}

最佳答案

您遇到的问题是,在 main_game() 方法中,您正在创建一个新的 Adventure 对象并调用 move_check() 上面没有任何级别数据,您将其加载到 main() 中的 adv 变量中。因此它总是引用一个空的字符数组并给你意想不到的结果。

删除新对象声明并仅调用 move_check() 本身应该会让您回到正轨。

关于Java数组: 2D array dysfunction in terminal ASCII terminal game during attempt to swap elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11534522/

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