1 Replies - 632 Views - Last Post: 16 February 2014 - 09:48 AM Rate Topic: -----

#1 JaAnTr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-November 13

Move player around a 2d array game.

Posted 16 February 2014 - 04:18 AM

Hi, I'm making a basic dungeon game where the player has to walk around, pick up gold, and when the gold is all collected, they can exit the dungeon. I've set this up by loading a map from a text file and putting it in to a 2d array. I've also made it so to move through the dungeon you use commands such as MOVE N and MOVE E. The problem is I can't figure out how to put the letter "P" wherever the player is in the map without overwriting what ever was in the array. Ideally, I want to just be able to print the "P" wherever the player is without actually changing the information of the array but I couldn't work out how to do that. Instead I tried to overwrite the original array but save the whatever was on the tile before the player got there but I haven't been able to get that to work.

Here's what the map looks like:
###################
#.................#
#......G........E.#
#.................#
#..E..............#
#..........G......#
#.................#
#.................#
###################



Here's some of my code:
	public void commands(String answer){
		if("PICKUP".equals(answer)){
			if(Map.matrix[playerX][playerY] == gold){
				System.out.println(Map.matrix[playerX][playerY]);
				playerGOLD += 1;
				Map.matrix[playerX][playerY] = floor;
				System.out.println("SUCCESS, GOLD COINS:" +  playerGOLD);
				System.out.println(Map.matrix[playerX][playerY]);
			}else{
				System.out.println("FAIL");}
		}else if("HELLO".equals(answer)){
			System.out.println("GOLD " + Map.theGOLD);
		}else if("MOVE N".equals(answer)){
			if(Map.matrix[playerX - 1][playerY] != wall){
				playerX -= 1;
				saveTile = Map.matrix[playerX][playerY];
				Map.matrix[playerX][playerY] = player;
				Map.printMap();
			}else{
				System.out.println("FAIL");
			}
		}else if("MOVE S".equals(answer)){
			if(Map.matrix[playerX + 1][playerY] != wall){
				playerX += 1;
				saveTile = Map.matrix[playerX][playerY];
				Map.matrix[playerX][playerY] = player;
				Map.printMap();
				System.out.println(saveTile);
			}else{
				System.out.println("FAIL");
				}	
		}else if("MOVE E".equals(answer)){
			if(Map.matrix[playerX][playerY + 1] != wall){
				playerY += 1;
				saveTile = Map.matrix[playerX][playerY];
				Map.matrix[playerX][playerY] = player;
				Map.printMap();
				System.out.println(saveTile);
			}else{
				System.out.println("FAIL");
			}	
		}else if("MOVE W".equals(answer)){
			if(Map.matrix[playerX][playerY - 1] != wall){
				playerY -= 1;
				System.out.println(Map.matrix[playerX][playerY]);
				saveTile = Map.matrix[playerX][playerY];
				Map.matrix[playerX][playerY] = player;
				Map.printMap();
				System.out.println(saveTile);
			}else{
				System.out.println("FAIL");
		}
	



For example, if I were to move around the map to a piece of gold, from a random start location the map would look like:
# # # # # # # # # # # # # # # # # # # 
# . . . . . . p p p p p p p p . . . # 
# . . . . . . p . . . . . . . . E . # 
# . . . . . . . . . . . . . . . . . # 
# . . E . . . . . . . . . . . . . . # 
# . . . . . . . . . . G . . . . . . # 
# . . . . . . . . . . . . . . . . . # 
# . . . . . . . . . . . . . . . . . # 
# # # # # # # # # # # # # # # # # # # 



and I wouldn't be able to use the "PICKUP" command to collect the gold because the "G" in the array has been overwritten by a "p".

Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Move player around a 2d array game.

#2 AfterBurner66  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 116
  • Joined: 02-August 08

Re: Move player around a 2d array game.

Posted 16 February 2014 - 09:48 AM

One thing I can comment about, is that you obviously need something not so simplistic as one array, to do all things in one. So, you have to redesign the whole thing conceptually and see what you can do to have the value of some variables intact and map the player's course handled separately. So don't go with the mindset of just adding simple structures on the go, because this won't be efficient. Redesign the whole thing
and then write code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1