-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
275 lines (257 loc) · 6.43 KB
/
Copy pathboard.cpp
File metadata and controls
275 lines (257 loc) · 6.43 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/***************************************************************************************************
** Author: Kurt Kaiser
** Website: kurtkaiser.us
** Info: Designed a one-player, text-based game, the player can move through
** spaces to get items and accomplish goals.
****************************************************************************************************/
#include <iostream>
#include <string>
#include "board.hpp"
#include "space.hpp"
#include "house.hpp"
#include "cave.hpp"
#include "empty.hpp"
#include "player.hpp"
#include <time.h>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::cin;
// Constructor for the Board class
Board::Board() {
// Defing private variables
head = NULL;
playerTile = NULL;
}
Board::~Board() {
// Deconstructor
while (head != NULL)
removeFront();
}
// Defining public member functions
bool Board::isEmpty() {
bool empty = false;
if (head == NULL)
empty = true;
return empty;
}
// Add integer to back of list
void Board::addBack(Space* val) {
// Check if list is empty
if (isEmpty()) {
// If empty, define value for head
head = new BoardNode;
head->val = val;
head->right = NULL;
head->left = NULL;
head->up = NULL;
head->down = NULL;
}
else {
// Adding newData to list, reassigning needed pointers
BoardNode* newData = new BoardNode;
newData->val = val;
newData->left = head->left;
newData->right = head;
head->left->right = newData;
if (head->right == head)
head->right = newData;
head->left = newData;
}
}
// Define all the pointer values for the board
void Board::connectBoard() {
int count = 0;
struct BoardNode* above = head;
struct BoardNode* below = head;
struct BoardNode* tempBelow = head;
while (tempBelow->down != NULL) {
below = above->down;
tempBelow = below->down;
while (above->right != NULL) {
above = above->right;
below = below->right;
// Define pointers
above->down = below;
below->up = above;
}
count++;
tempBelow = head;
for (int i = 0; i < count; i++) {
tempBelow = tempBelow->down;
}
above = tempBelow;
}
}
// Create the entire board game
void Board::createBoard(int rows, int columns) {
srand((unsigned)time(NULL));
struct BoardNode* traverse;
// Check if list is empty
BoardNode* newData;
// Get random location for cave
int caveColumn1 = columns - 1;
int caveColumn2 = columns - 2;
int caveRow1 = (rand() % (rows-1)) + 1;
int caveRow2 = (rand() % (rows - 1)) + 1;
// Get random location for cave
int houseColumn1;
int houseColumn2;
int houseColumn3;
int playerRow = (rand() % (rows - 1)) + 0;
// If empty, define value for head
head = new BoardNode;
head->val = new Empty;
head->right = NULL;
head->left = NULL;
head->up = NULL;
head->down = NULL;
traverse = head;
// Create rows and columns of board
while (-3 < caveRow1 - caveRow2 && caveRow1 - caveRow2 < 3) {
caveRow2 = (rand() % (rows - 1)) + 1;
}
for (int r = 0; r < rows; r++) {
houseColumn1 = (rand() % columns) + 1;
houseColumn2 = (rand() % columns) + 1;
houseColumn3 = (rand() % columns) + 1;
while (houseColumn1 == houseColumn2) {
houseColumn2 = (rand() % columns) + 1;
}
while (houseColumn1 == houseColumn3 || houseColumn1 == houseColumn3) {
houseColumn3 = (rand() % columns) + 1;
}
// Place player, store location of ship
if (playerRow == r) {
traverse->val->setSquare('P');
traverse->val->setDefaultSquare('S');
playerTile = traverse;
}
// Place houses
for (int c = 1; c < columns; c++) {
newData = new BoardNode;
newData->val = new Empty;
if (c == houseColumn1)
{
newData->val = new House;
}
else if (c == houseColumn2)
{
newData->val = new House;
}
else if (c == houseColumn3)
{
newData->val = new House;
}
// Place caves
if (c == caveColumn1 && r == caveRow1) {
newData->val = new Cave;
} else if (c == caveColumn2 && r == caveRow2) {
newData->val = new Cave;
}
traverse->right = newData;
newData->left = traverse;
newData->right = NULL;
newData->down = NULL;
traverse = traverse->right;
}
traverse = head;
while (traverse->down != NULL) {
traverse = traverse->down;
}
/// if last set to null
if (r + 1 != rows) {
newData = new BoardNode;
newData->val = new Empty;
traverse->down = newData;
newData->left = NULL;
newData->up = traverse;
newData->right = NULL;
newData->down = NULL;
traverse = traverse->down;
}
}
// connect the board, set up the pointers
connectBoard();
}
// Print out front of list to the console
Space* Board::getFront() {
return head->val;
}
// Move based on user input
BoardNode* Board::moveRight(BoardNode* tile) {
tile->val->setToDefault();
if (tile->right != NULL) {
tile = tile->right;
}
return tile;
}
// Move based on user input
BoardNode* Board::moveLeft(BoardNode* tile) {
tile->val->setToDefault();
if (tile->left != NULL) {
tile = tile->left;
}
return tile;
}
// Move based on user input
BoardNode* Board::moveUp(BoardNode* tile) {
tile->val->setToDefault();
if (tile->up != NULL) {
tile = tile->up;
}
return tile;
}
// Move based on user input
BoardNode* Board::moveDown(BoardNode* tile) {
tile->val->setToDefault();
if (tile->down != NULL) {
tile = tile->down;
}
return tile;
}
// Remove front value of the list
void Board::removeFront() {
BoardNode* tranverse = head;
BoardNode *oldHeadNode;
// If list is empty define head as null
// If not empty, reassign pointers
while (head != NULL) {
tranverse = head;
head = head->down;
cout << endl;
while (tranverse->right != NULL) {
cout << "D" << endl;
cout << tranverse->val->getSquare();
oldHeadNode = tranverse;
tranverse->right->left = NULL;
tranverse = tranverse->right;
tranverse->left->right = tranverse;
delete oldHeadNode;
}
}
free(head);
}
// Output the board
void Board::printBoard() {
struct BoardNode* goDown;
struct BoardNode* goRight;
goDown = head;
if (!isEmpty()) {
while (goDown != NULL) {
goRight = goDown;
while (goRight != NULL)
{
cout << goRight->val->getSquare();
goRight = goRight->right;
}
cout << endl;
goDown = goDown->down;
}
}
}
// Return player tile
BoardNode* Board::getPlayerTile() {
return playerTile;
}