Player transition to 2D array grid

I am creating a game using a 10x10 2D array. The player starts in the upper left corner labeled โ€œP,โ€ and the goal is to make the player avoid obstacles in order to get to the treasure indicated as โ€œTโ€ located in the lower right corner.

How can I make a player navigate the grid using the Up / Down / Left / Right commands?

Can I use a for loop to count elements in an array to indicate movement?

Here is what I still have:

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)>"); } } 
+4
source share
3 answers

you should keep track of the current position of the player and just update these variables. the initial values โ€‹โ€‹would be (0,0), as you said.

 int px = 0; int py = 0; 

when moving is done, update the variables accordingly:

 grid[px][py] = <empty cell>; switch (move) { case 'u': py += 1; break; case 'r': px += 1; break; ... } grid[px][py] = 'P'; 

Of course, you should not just update the values โ€‹โ€‹"blindly", you should insert some validation logic to follow the rules of the game:

 if (grid[px][py] != <obstacle> ) // update player coordinates... 
+2
source

It looks like you are using the line order, judging by how your board is printed. Based on this, here is what you need to do:

  • Firstly, you need to keep the playerโ€™s position somewhere. Now it is hard-coded to 0.0.
  • Secondly, you need to read the playerโ€™s move. This should happen in a loop where you get a move, check if moving is allowed, moving is done and the results are displayed.
  • Third, you must be able to calculate a new position based on movement. Up means row -= 1 . The rule means column += 1 . Etc.
  • Given the new coordinates, you need to make sure that this step is valid. At the very least, you should stop them from exiting the board, but you can also prevent them from entering a square with an obstacle, etc.
  • Once you know that this action is valid, you should update the variables in which the current coordinates are stored.
  • At the end of the loop, you will need to redraw the board.

This is the main point. Right now you are doing everything in main() , and thatโ€™s good, but if it were me, I would start to separate things into separate methods such as InitializeBoard() , GetNextMove() , CheckIfMoveIsValid(int r, int c) and t .d. Thus, main() becomes a high-level representation of your game cycle, and the guts of different operations are divided and more easily cope. This will require storing things like your game board in class variables rather than local variables, which should actually make it easier to find obstacles than you would at present.

+2
source

All of the above answers are great. Here are some tips I would do:

Instead of a two-dimensional char array, I would create a custom object, such as Space , and define a two-dimensional array of spaces (for example, Cosmos [] []). There are several reasons for this:

  • You can define a space in various ways (and not just with one character). For example, Space [i] [j] .hasTreasure () may return a boolean so that you know if you have found this treasure.
  • If you want to add functionality later, it's as simple as adding an attribute to your Space class. Again, you are not limited to one character here.

More about your question about traffic, I would also recommend a few things. Like the redneckjedi suggestion of the CheckIfMoveIsValid () method, I would pass the grid and direction of movement as parameters and return a boolean value. To make sure you have no problems with ArrayIndexOutOfBounds, I would also suggest adding a row / column of walls on each side. I would expand the grid to 12x12 and put a strip of blocks like obstacles outside. This ensures that you cannot go beyond the grid, since hitting the wall will always return โ€œfalseโ€ to the actual move.

0
source

Source: https://habr.com/ru/post/1395441/


All Articles