0

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. 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.

I can create and print the array no problem, and set it to only values of 1 or 0 my confusion is:

  1. How to lower the odds of getting a 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:

0 1 0 0 1

0 1 1 1 0

1 0 1 1 0

0 1 0 0 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 7 1 1 0

1 0 1 1 0

0 1 0 0 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. 5 represents the path of the user, 7 is the spot the user hit a wall at.

In my situation, the array would be 10x10 and only be printed once, at the end of the program. 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.

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

Pic 1 of code

Pic 2 of code

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Beau Henry
  • 13
  • 3
  • Possible duplicate of [Java: Programming a simple maze game](https://stackoverflow.com/questions/12648898/java-programming-a-simple-maze-game) – Jérôme Feb 21 '18 at 23:26
  • 1
    Don't use pictures to show your code. Please paste your code here, using [proper code formatting](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks). – Vince Feb 22 '18 at 00:53

2 Answers2

0

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:

  • Increase Location.x++.
  • Check if the new location is a wall or not ex: if(Grid[Location.x][Location.y] == 1) /* do stuff */.
  • Change the value of the grid cell: Grid[Location.x][Location.y] = '{your value}'.
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
0
  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.

Yamuk
  • 750
  • 8
  • 27
  • Thanks a lot! That's very helpful and gave me some more insight to that function :D – Beau Henry Feb 24 '18 at 02:08
  • I figured it out haha, the random num comparison worked fine, I just re-randomized them right after. Thanks for the help tho, I do understand how this stuff works better because of that page :D – Beau Henry Feb 24 '18 at 02:28