让玩家在 2D 数组游戏网格上移动
我正在使用 10x10 2D 数组创建游戏。玩家从左上角标为“P”的地方开始,目标是让玩家避开障碍物,到达位于右下角标为“T”的宝藏。
我将如何使用上/下/左/右命令让玩家在网格上移动?
我会使用 for 循环来计算数组中的元素以指定移动吗?
这是我到目前为止所拥有的:
import java.util.Scanner;
import java.util.Random;
public class Adventure {
public static void main(String[] args) {
char grid[][]= new char[10][10];
Scanner move = new Scanner(System.in);
System.out.println("Here is the current game board:");
System.out.println("-------------------------------");
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid.length; j++) {
double random = Math.random();
if(random <=.05) {
grid[i][j]='*';
}
else if(random > .06 && random <= .15) {
grid[i][j]='X';
}
else {
grid[i][j]='.';
}
grid[0][0]='P';
grid[9][9]='T';
System.out.print(grid[i][j]);
}
System.out.println("");
}
System.out.print("Enter your move (U/D/L/R)>");
}
}
I am creating a game using a 10x10 2D array. The player starts at the top left hand corner indicated as "P" and the objective is to get the player to avoid obstacles to get to the treasure indicated as "T" located in the lower right corner.
How would I go about making the player move about the grid using commands Up/Down/Left/Right?
Would I use a for loop to count through the elements in the array to designate the move?
Here is what I have so far:
import java.util.Scanner;
import java.util.Random;
public class Adventure {
public static void main(String[] args) {
char grid[][]= new char[10][10];
Scanner move = new Scanner(System.in);
System.out.println("Here is the current game board:");
System.out.println("-------------------------------");
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid.length; j++) {
double random = Math.random();
if(random <=.05) {
grid[i][j]='*';
}
else if(random > .06 && random <= .15) {
grid[i][j]='X';
}
else {
grid[i][j]='.';
}
grid[0][0]='P';
grid[9][9]='T';
System.out.print(grid[i][j]);
}
System.out.println("");
}
System.out.print("Enter your move (U/D/L/R)>");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该跟踪玩家的当前位置并更新这些变量。
正如你所说,初始值是 (0,0) 。
当进行移动时,相应地更新变量:
当然你不应该只是“盲目”地更新值,你应该插入一些验证逻辑来遵循游戏规则:
you should keep track of the current position of the player and just update those variables.
initial values would be (0,0) as you said.
when a move is made, update the variables accordingly:
of course you shouldn't just updated the values "blindly", you should insert some validation logic to follow the rules of the game:
从电路板打印的方式来看,您似乎正在使用行优先排序。基于此,您需要执行以下操作:
row -= 1
。右表示列+= 1
。等等。这就是它的基本要点。现在你正在
main()
中完成所有操作,这没关系,但如果是我,我会开始将事情分成单独的方法,例如InitializeBoard()
,GetNextMove()
、CheckIfMoveIsValid(int r, int c)
等等。这样,main()
就成为游戏循环的高级视图,并且不同操作的内部被划分并且更容易处理。这将需要将游戏板之类的东西存储到类变量而不是局部变量中,这实际上应该使障碍物检测之类的事情比现在更容易。Looks like you're using row-major ordering, judging from the way your board prints out. Based on that, here's what you'll need to do:
row -= 1
. Right meanscolumn += 1
. Etc.That's the basic gist of it. Right now you are doing everything in
main()
, and that's okay, but if it were me I would start to split things out into separate methods, likeInitializeBoard()
,GetNextMove()
,CheckIfMoveIsValid(int r, int c)
, and so on. That way,main()
becomes a high-level view of your game loop, and the guts of the different operations are compartmentalized and more easy to deal with. This will require storing off things like your game board into class variables rather than local variables, which should actually make things like obstacle detection easier than it would be currently.以上所有答案都很棒。以下是我提出的一些建议:
我将创建一个自定义对象,例如 Space,而不是 char 二维数组,并定义一个二维数组空间(例如,Space[][])。造成这种情况的原因有几个:
关于你的运动问题,我还会推荐一些东西。类似于 redneckjedi 对 CheckIfMoveIsValid() 方法的建议,我将传递网格和移动方向作为参数并返回一个布尔值。为了确保您不会遇到 ArrayIndexOutOfBounds 问题,我还建议在每一侧添加一排/一列墙。我会将网格扩大到 12x12,并在外侧放置一条障碍物类型的方块。这将确保您无法走出网格,因为在有效的移动中撞到墙壁将始终返回“false”。
All of the above answers are great. Here are a few suggestions I would make:
Instead of a char two-dimensional array, I would make a custom object, such as Space, and define a two-dimensional array of Spaces (eg, Space[][]). There are a few reasons for this:
More to your question of movement, I would also recommend a few things. Similar to redneckjedi's suggestion of a CheckIfMoveIsValid() method, I would pass the grid and move direction as parameters and return a boolean. To ensure that you do not end up with ArrayIndexOutOfBounds issues, I would also suggest adding a row/column of walls on each side. I would widen the grid out to 12x12 and put a strip of obstacle-type blocks around the outside. This will ensure that you cannot step outside of the grid as hitting a wall will always return 'false' on a valid move.