Loading a game map from a file and having the player move around

Hey guys! I have this issue I need solved. I have looked on every website I possibly could've and haven't found anything, so I came to this forum.

How can I have a player move around on an ASCII map that has been loaded into the game from a text file? I want the player to move with the WASD keys. The player I don't care about collision right now, I can probably code that on my own if I know how to do movement by itself. The player should be the '@'.

Here is what the map looks like from the text file. It is 69x21 tiles in case that helps.

##############################
#....................................................___............#
#.....................................................|$|.........M..#
#........@............................................|.............#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#...................................................................#
#############################
Last edited on
You need to set a x and y coordinate for the player. When the right key is pressed and x < the right border inc x. Similarly for all other movements.
After each movement you have to draw the map again.
Should I use vectors or a 2d array? If vectors, how would I call a specific element?
You can use a 2d array since the number of columns and rows is fixed - at least I assume so.
I am not sure if you know OOP already, but I would do it like this - just an incomplete idea -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Board
{
public:
  int GetRowCount ();
  int GetColCount ();
  char GetElem (const int row, const int col);
  void SetElem (const int row, const int col, const char elem);
  void Load (const char *filename);
  void Display ();
private:
  enum { ROWS = 21, COLS = 21 };
  char m_Fields[ROWS][COLS];
};


class Player
{ 
public:
  Player(Board board);
  void MoveRight ();
  void MoveUp ();
  void MoveLeft ();
  void MoveDown ();
  int GetCol ();
  int GetRow ();
private:
  Board m_Board;
  int m_Col, m_Row;
};


How would I load the map from a file and store it into the 2d array?
Board.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef BOARD_H
#define BOARD_H

class Board
{
public:
  Board (){};
  int GetRowCount () {return m_RowCount;}
  int GetColCount () {return m_ColCount;}
  char GetElem (const int row, const int col);
  void SetElem (const int row, const int col, const char elem);

  void CheckRow (const int row);
  void CheckCol (const int col);

  void Load (const char *filename);
  void Display ();
private:
  static const int m_RowCount = 21;
  static const int m_ColCount = 69;
  char m_Fields[m_RowCount][m_ColCount];

};
#endif 


Board.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Board.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <fstream>

using namespace std;

char Board::GetElem (const int row, const int col)
{
  CheckCol (col);
  CheckRow (row);

  return m_Fields[row][col];
}
void Board::SetElem (const int row, const int col, const char elem)
{
  CheckCol (col);
  CheckRow (row);

  m_Fields[row][col] = elem;
}
void Board::Load (const char *filename)
{
  ifstream src (filename);
  if (!src)
    throw runtime_error ("Board::Load  - Unable to load file");

  int line = 0;
  string input;

  while (src && line < m_RowCount - 1)
  {
    getline (src, input);
    if (input.size () > m_ColCount)
      throw runtime_error ("Board::Load - Line > 69 chars ");
    for (size_t i = 0; i < input.size (); i++)
    {
      SetElem (line, i, input[i]);
    }
    line++;
  }
}

void Board::Display ()
{
  for (int row = 0; row < m_RowCount - 1; row++)
  {
    for (int col = 0; col < m_ColCount - 1; col++)
    {
      cout << m_Fields[row][col];
    }
    cout << '\n';
  }
}

void Board::CheckRow (const int row)
{
  if (row < 0 || row > m_RowCount - 1)
    throw out_of_range ("Board::CheckRow - Row out of range"); 
}
void Board::CheckCol (const int col)
{
  if (col < 0 || col > m_ColCount - 1)
    throw out_of_range ("CheckCol - Row out of range");
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include "Board.h"

using namespace std;

int main ()
{
  Board board;
  try
  {
    board.Load ("Map.txt");
    board.Display ();
  }
  catch (exception& ex)
  {
    cerr << "\a" << ex.what () << "\n\n";
  }

  system ("pause");
  return 0;
}


Map.txt
####################################################################
#....................................................___...........#
#.....................................................|$|.........M#
#........@............................................|............#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
#..................................................................#
####################################################################
Last edited on
Topic archived. No new replies allowed.