trying to make a player go around a map and give you certain phrases when you land on specific spots

The problem that im having is that when the player [P] goes over any of the map tiles it doesnt register properly.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <iomanip>
#include<time.h>

using namespace std;

//Globals
const int rows = 8;
const int columns = 8;
char gameBoard[rows][columns];
bool dismount = false;
bool gameover = false;

void drawBoard(int, int);
void changePosition(int&, int&, int);



int main()
{
	srand(time(NULL));
	//DONT CHANGE THESE
	char spacesToMove = 0;
	char spacesToUse = spacesToMove;
	int positionX = 0;
	int positionY = 0;



	int choice;
	cout << "make your choice" << endl;
	cout << "press 1 to start" << endl;
	cout << "Press 2 to load" << endl;
	cin >> choice;
	if (choice == 1) {

		cout << "Hello and welcome to the game" << endl;
		cout << "_____________________________" << endl;
		drawBoard(positionX, positionY);
		cout << "_____________________________" << endl;

		while (gameover == false) {

			cout << "If you would like to make a move press 1" << endl;
			cout << "press 2 if you would like to dismount" << endl;
			cin >> choice;

			if (choice == 1)
			{
				spacesToMove = rand() % 6;

				dismount = false;

				if (spacesToMove > 0) {
					changePosition(positionX, positionY, spacesToMove);
					cout << "_____________________________" << endl;
					drawBoard(positionX, positionY);
					cout << "_____________________________" << endl;
				}
			}
			if (choice == 2)
			{

				cout << "You dismount.";

				dismount = true;


				
				if (gameBoard[positionX][positionY] == gameBoard[0][7] || gameBoard[7][7]) {
					cout << "Cathedral";

				}
				else if (gameBoard[positionX][positionY] == gameBoard[3][7] || gameBoard[7][0]) {
					cout << "Graveyard";

				}
				else if (gameBoard[positionX][positionY] == gameBoard[0][4] || gameBoard[7][4]) {
					cout << "Well Of Reflection";

				}
				else if (gameBoard[positionX][positionY] == gameBoard[3][0]) {
					cout << "Dungeon Exit";

				}
				else  {

					int random1;
					random1 = rand() % 4;
					if (random1 == 1) {
						cout << " its 1" << endl;
					}
					else if (random1 == 2) {
						cout << " its 2" << endl;

					}
					else if (random1 == 3) {
						cout << " its 3" << endl;

					}
					else if (random1 == 0) {
						cout << " its 0" << endl;
					}


				}

			}

		}
		return 0;
	}
	if (choice == 2) {
		cout << "LOADING....." << endl;
	}
	return 0;
}

void changePosition(int& positionX, int& positionY, int spacesToMove)

{

	while (spacesToMove > 0)
	{

		//position is in the first row and moving right
		if (spacesToMove > 0 && positionX == 0 && positionY != 7) {
			if (positionY + spacesToMove >= columns) {
				spacesToMove = spacesToMove - (columns - (positionY + 1));
				positionY = 7;
			}
			else {
				positionY = positionY + spacesToMove;
				spacesToMove = 0;
			}
		}
		//position is last column and moving down 
		if (spacesToMove > 0 && positionY == 7 && positionX != 7) {
			if (positionX + spacesToMove >= rows) {
				spacesToMove = spacesToMove - (rows - (positionX + 1));
				positionX = 7;
			}
			else {
				positionX = positionX + spacesToMove;
				spacesToMove = 0;
			}
		}

		//position is on last row and moving left
		if (spacesToMove > 0 && positionX == 7 && positionY != 0) {
			if ((columns - (positionY + 1)) + spacesToMove >= columns) {
				spacesToMove = spacesToMove - (positionY);
				positionY = 0;
			}
			else {
				positionY = positionY - spacesToMove;
				spacesToMove = 0;
			}
		}

		//position is on first column and moving up
		if (spacesToMove > 0 && positionY == 0 && positionX != 0) {
			if ((rows - (positionX + 1)) + spacesToMove >= rows) {
				spacesToMove = spacesToMove - (positionX);
				positionX = 0;
			}
			else {
				positionX = positionX - spacesToMove;
				spacesToMove = 0;
			}
		}
	} //end of while
}

void drawBoard(int positionX, int positionY) {
	//Use two loops to draw the game board. If the passed in player position matches
	//a position, place a P on it.
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < columns; y++)
		{
			//The board representation will be drawn with * only if is the outer paths
			//of the 2D Array
			if (y == 0 || y == 7 || x == 0 || x == 7) {
				if (positionX == x && positionY == y)
				{
					gameBoard[x][y] = 'P';
				}
				else {
					gameBoard[x][y] = '*';


				}
				if (gameBoard[x][y] != 'P')
				{
					gameBoard[0][0] = 'S';
					gameBoard[0][4] = 'W';
					gameBoard[0][7] = 'C';
					gameBoard[3][7] = 'G';
					gameBoard[7][7] = 'C';
					gameBoard[7][4] = 'W';
					gameBoard[7][0] = 'G';
					gameBoard[3][0] = 'E';
				}
				cout << setw(2) << gameBoard[x][y];
			}
			else {
				cout << setw(2) << " ";
			}
		}
		cout << endl;
	}
}
Last edited on
> if (gameBoard[positionX][positionY] == gameBoard[0][7] || gameBoard[7][7])
This isn't how you compare one position with a set of possible positions.

> if (y == 0 || y == 7 || x == 0 || x == 7)
Compare with this statement which is expressed correctly.

> gameBoard[0][0] = 'S';
You should be doing this once.
Not for every single position on the board where the player isn't.
All this should be in an initialiseBoard function you call once.
Topic archived. No new replies allowed.