Objectives
- Use a multidimensional array to represent a game grid
- Implement user input handling with a
Scanner
to control a player’s movements on the grid
Scanner
to control a player’s movements on the gridScanner
in its environment, please use an alternative platform to complete this exercise! You may use replit.com, Visual Studio Code, or any other IDE of your choice that supports Java and allows the use of Scanner for user input.P O . . .
. . . . O
. . . . .
. . . . .
. O . . E
Scanner
. Each time the game is run, a new grid must be created. That is, the coins must be placed at a random spot each time.char[][] buildGrid(int width, int height)
char[][]
and populate it with ’.’ which represents empty spacesprintGrid(char[][] grid)
char[][]
and print each rowplaceCoins(char[][] grid, int coinCount)
coinCount
parameter. Coins are represented by ’O’Random
object to generate random x and y positions for the coins. Here is a code snippet that uses Random
: Random random = new Random();
int randomInt = random.nextInt(100);
playGame(int width, int height, int coinCount)
buildGrid()
method and place the player (P) and the exit (E) in their designated spots on the grid. Ensure that you initialize variables to keep track of the player’s positionplaceCoins()
methodwhile
loop to keep the game running until the player reaches the exit. Inside the loop, you will:Scanner
P . .
O . .
. . E
Enter move (Only WASD, case sensitive):
S
Got a coin!
. . .
P . .
. . E
Enter move (Only WASD, case sensitive):
D
. . .
. P .
. . E
Enter move (Only WASD, case sensitive):
D
. . .
. . P
. . E
Enter move (Only WASD, case sensitive):
S
You won! Points: 1