简体   繁体   English

如何跟踪和更新Java 2D数组中的值?

[英]How to track and update a value in a Java 2D array?

I need to develop a program that randomly creates an array (10x10) of int values (either 1 or 0; 1 = wall; 0 = no wall) and have a loop that requires the user to navigate the array in a maze-like fashion. 我需要开发一个程序,该程序随机创建一个int值数组(10x10)(1或0; 1 = wall; 0 = no wall),并有一个循环,要求用户以类似迷宫的方式导航数组。 This loop must also test if the user has reached the bottom of the array. 此循环还必须测试用户是否已到达数组的底部。 At the end, the program must print the array with the user's path, basically any value the user touches is a different number than 1 or 0. 最后,程序必须打印带有用户路径的数组,基本上,用户触摸的任何值都是不同于1或0的数字。

I can create and print the array no problem, and set it to only values of 1 or 0 my confusion is: 我可以创建并打印数组没有问题,并将其设置为仅1或0的值,我的困惑是:

  1. How to lower the odds of getting a 1. 如何降低获得1。

  2. How to create a loop to navigate/track the maze and the user's path. 如何创建一个循环来导航/跟踪迷宫和用户的路径。

  3. How to change the array's values that the user touches, and print the newly changed array correctly. 如何更改用户触摸的数组的值,以及如何正确打印新更改的数组。 (preferably using nodes). (最好使用节点)。


Let's say this is a randomly generated 5x5 array: 假设这是一个随机生成的5x5数组:

0 1 0 0 1 0 1 0 0 1

0 1 1 1 0 0 1 1 1 0

1 0 1 1 0 1 0 1 1 0

0 1 0 0 1 0 1 0 0 1

1 1 0 1 1 1 1 0 1 1


This is what the output window would look like using this array: 使用此数组的输出窗口如下所示:

Where do you want to go (up, down, left, right): 您想去哪里(上,下,左,右):

down

Where do you want to go (up, down, left, right): 您想去哪里(上,下,左,右):

right

You hit a wall! 你撞墙了! Game over! 游戏结束!

5 1 0 0 1 5 1 0 0 1

5 7 1 1 0 5 7 1 1 0

1 0 1 1 0 1 0 1 1 0

0 1 0 0 1 0 1 0 0 1

1 1 0 1 1 1 1 0 1 1


The 0's are open paths that allow the user to pass through them, the 1's are walls that end the game if the user runs into them. 0是允许用户通过的开放路径,而1是如果用户碰到游戏便会结束游戏的墙。 5 represents the path of the user, 7 is the spot the user hit a wall at. 5代表用户的路径,7代表用户撞墙的位置。

In my situation, the array would be 10x10 and only be printed once, at the end of the program. 在我的情况下,该数组将为10x10,并且在程序结束时只能打印一次。 The user would not see the original array. 用户将看不到原始数组。

I'm not asking anyone to program it for me, just a little push in the right direction. 我并没有要求任何人为我编程,只是向正确的方向稍加推动。 I can't seem to figure it out in my head, logically. 从逻辑上讲,我似乎无法弄清楚。 Again I'm very new to Java programming and would appreciate any help anyone is willing to offer. 同样,我对Java编程非常陌生,非常感谢任何人愿意提供的任何帮助。

I apologize for the very long question, I just want as much clarity as possible, given that my situation requires context to understand fully. 对于很长的问题,我深表歉意,鉴于我的情况需要充分理解背景,我只想尽可能地清楚。 If more info is required, I will respond within a day. 如果需要更多信息,我将在一天内回复。 I have also added photos of my code, if my explanation makes no sense. 如果我的解释没有道理,我还添加了我的代码的照片。

I greatly appreciate any and all help given, Thank you very much! 我非常感谢您提供的所有帮助,非常感谢! :D :D

Pic 1 of code 图片Pic 1

Pic 2 of code 图片Pic 2

For lower odds of getting 1 see this link . 要获得较低的赔率,请参阅此链接

Keep the current location of the played in an object/variable/class ex: 将播放的当前位置保留在对象/变量/类中,例如:

public static class Location {
    public static int x = 0;
    public static int y = 0;
}

Every time user moves for example down : 例如down每次用户down

  • Increase Location.x++ . 增加Location.x++
  • Check if the new location is a wall or not ex: if(Grid[Location.x][Location.y] == 1) /* do stuff */ . 检查新位置是否为墙,例如: if(Grid[Location.x][Location.y] == 1) /* do stuff */
  • Change the value of the grid cell: Grid[Location.x][Location.y] = '{your value}' . 更改网格单元Grid[Location.x][Location.y] = '{your value}'的值: Grid[Location.x][Location.y] = '{your value}'
  1. Use Random instead of secure random, you can get the next random with 使用随机而不是安全随机,您可以使用

Random r = new Random(); iWallChance = r.nextInt(100);

2 and 3. Ali Sharabiani has a good answer for that. 2和3。AliSharabiani为此提供了一个很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM