'How to set array value of object from inside the object class
I'm doing simple console dungeon crawler and i'm trying to meake function that pick up the item. It works only for a while but when i refresh board, the tile with the item type does not change to tile with player object, it means that i can't update my array of objects at specific idnex with another object type. It happens after i meake another move. Program flow: 1.I create object map and player (player is on hard coded start-position) 2.I fill in an array that stores various types of floor under indexes, 3.I print arrays of a size such as the number of indices in an array with objects and depending on the type of floor I print the sign 4. With getItem function i switch floor object from Item to Player, BUT WITH ANOTHER MOVE SWITCHED FLOOR OBJECT IS STILL THE SAME
Maby you see something that shoud't work? Maybe I'm referencing to the wrong object, while I'm updating the object arrays?
[Player is on item][1]
[Player get item][2]
[enter image description here][3]
[1]: https://i.stack.imgur.com/Eh0Hm.png
[2]: https://i.stack.imgur.com/JJMO5.png
[3]: https://i.stack.imgur.com/Fe0Ho.png
//Map.cs
namespace DungeonGame
{
public class Map
{
public int Rows { get; private set; }
private int Cols { get; set; }
public Object[,] BoardTiles { get; private set; }
private Floor Floor { get; set; }
private PlayerTile PlayerTile { get; set; }
private Item Item1 { get; set; }
private Item Item2 { get; set; }
private Item Item3 { get; set; }
public Map()
{
this.Rows = 10;
this.Cols = 40;
this.BoardTiles = new Object[Rows, Cols];
this.Floor = new Floor();
this.PlayerTile = new PlayerTile();
this.Item1 = new Item();
this.Item2 = new Item();
this.Item3 = new Item();
}
public void fillBoard(int playerPositionRowIndex, int playerPositionColumnIndex)
{
Floor floor = Floor;
PlayerTile playerTile = PlayerTile;
Item item1 = Item1;
Item item2 = Item2;
Item item3 = Item3;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Cols; c++)
{
placePlayerOnTheBoard(playerPositionRowIndex, playerPositionColumnIndex, playerTile);
placeFloorOnTheBoard(floor, r, c);
placeItemOnTheBoard(4,4, item1);
placeItemOnTheBoard(4, 7, item2);
placeItemOnTheBoard(4, 10, item3);
}
}
}
public void fillBoard1(int playerPositionRowIndex, int playerPositionColumnIndex)
{
PlayerTile playerTile = PlayerTile;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Cols; c++)
{
getItem(playerPositionRowIndex, playerPositionColumnIndex);
}
}
}
public void printBoard()
{
for (int col = 0; col < Rows; col++)
{
for (int row = 0; row < Cols; row++)
{
if (BoardTiles[col, row].GetType().ToString() == ("DungeonGame.PlayerTile"))
{
Console.Write("@");
}
else if (BoardTiles[col, row].GetType().ToString() == ("DungeonGame.Floor"))
{
Console.Write("*");
}
else if (BoardTiles[col, row].GetType().ToString() == ("DungeonGame.Item"))
{
Console.Write("O");
}
}
Console.WriteLine("");
}
}
private void placePlayerOnTheBoard(int playerPositionRowIndex, int playerPositionColumnIndex, PlayerTile playerTile)
{
BoardTiles[playerPositionRowIndex, playerPositionColumnIndex] = playerTile;
}
private void placeFloorOnTheBoard(Floor floor, int r, int c)
{
BoardTiles[r, c] = floor;
}
private void placeItemOnTheBoard(int itemPositionX, int itemPositionY, Item item)
{
BoardTiles[itemPositionX, itemPositionY] = item;
}
public void getItem(int playerPositionRowIndex, int playerPositionColumnIndex)
{
BoardTiles[playerPositionRowIndex, playerPositionColumnIndex] = PlayerTile;
}
}
}
//////////////////////Program.cs
using DungeonGame;
Map map = new Map();
ConsoleKeyInfo keyInfo;
Actor player = new Player(map.Rows);
int playerPositionX = player.ActorPosition["positionX"];
int playerPositionY = player.ActorPosition["positionY"];
Console.Clear();
PlayerTile playerTile = new PlayerTile();
map.fillBoard(playerPositionX, playerPositionY);
map.printBoard();
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
{
switch (keyInfo.Key)
{
case ConsoleKey.UpArrow:
Console.Clear();
playerPositionX = player.moveUp(playerPositionX);
map.fillBoard(playerPositionX, playerPositionY);
map.printBoard();
break;
case ConsoleKey.DownArrow:
Console.Clear();
playerPositionX = player.moveDown(playerPositionX);
map.fillBoard(playerPositionX, playerPositionY);
map.printBoard();
Console.Write(map.BoardTiles[playerPositionX, playerPositionY]);
Console.Write(map.BoardTiles[playerPositionX+1, playerPositionY]);
break;
case ConsoleKey.LeftArrow:
Console.Clear();
playerPositionY = player.moveLeft(playerPositionY);
map.fillBoard(playerPositionX, playerPositionY);
map.printBoard();
break;
case ConsoleKey.RightArrow:
Console.Clear();
playerPositionY = player.moveRight(playerPositionY);
map.fillBoard(playerPositionX, playerPositionY);
map.printBoard();
break;
case ConsoleKey.Spacebar:
Console.Clear();
map.getItem(playerPositionX, playerPositionY);
map.fillBoard1(playerPositionX, playerPositionY);
map.printBoard();
Console.Write(map.BoardTiles[playerPositionX, playerPositionY]);
break;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
