-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
197 lines (151 loc) · 5.24 KB
/
Copy pathGrid.cpp
File metadata and controls
197 lines (151 loc) · 5.24 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
//-----------------------------------------------------------
//Program Name: Grid Game
//Author: Nischaal Cooray
//Desciption: Simulates a game of 'pegs' on a square board. Similar to a game of checkers, pieces are eliminated if they are 'jumped' over.
//-----------------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "GridGame.h"
using namespace std;
//----------Prototypes------------
bool wantToQuit(int quitNum, int quitCol);
string test();
//--------------------------------
int main(void)
{
//-----Local Variables----------------
GridGame myGrid;
int startRow, startCol, endRow, endCol;
ofstream outFile("ouput.txt");
int useFile;
string fileName;
ifstream inFile;
//------------------------------------
//-----Introduction and Guide---------
cout << "Welcome to the Grid Game!" << endl;
cout << "The aim of the game is simple: Simply try and remove all the X's from the grid\nbelow." << endl;
cout << "Use the grid coordinates provided to indicate which X you want to move. Keep in mind that there must be an O in the position you want to move it to, and there\nmust also be an X inbetween these two points." << endl;
cout << "Format for entering coordinates is: startRow startColumn endRow endColumn. For\nexample: 2 0 2 2 moves an X from (2,0) to (2,2)." << endl;
cout << "Enter \"99 99\" at any time to quit." << endl << endl;
//------------------------------------
cout << myGrid << endl; //Generate the initial grid
outFile << myGrid << endl;
cout << "Use file or console input? (1 for file, 2 for console): ";
cin >> useFile;
if (useFile == 1)
{
cout << "Enter filename: ";
cin >> fileName;
inFile.open(fileName);
inFile >> startRow >> startCol;
bool quit = wantToQuit(startRow, startCol); //Check if the user wants to quit
if (quit == false) //Only proceed if the user does not want to quit
{
myGrid.startPosition(startRow - 1, startCol - 1); //Update the start position by calling the appropriate method
while (quit == false) //Only execute this loop if the user does not want to quit
{
bool legalMove = false;
cout << myGrid << endl; //Update the displayed grid
outFile << myGrid << endl;
while (legalMove == false) //Loop will continue to execute indefinitely
{
int complete = myGrid.movesLeft();
if (complete == 3)
{
cout << "You beat the game!" << endl;
return 0;
}
cout << "Enter move (four values): "; //Request a move from the user
inFile >> startRow >> startCol;
quit = wantToQuit(startRow, startCol); //Check if the user wants to quit
if (quit == false)
{
inFile >> endRow >> endCol;
cout << endl;
legalMove = myGrid.isLegalMove(startRow - 1, startCol - 1, endRow - 1, endCol - 1); //Check if this was a legal move
if (legalMove == false)
{
cout << "There is an error in your file!" << endl << endl;
return 0;
}
else
{
legalMove = true;
myGrid.applyMove(startRow - 1, startCol - 1, endRow - 1, endCol - 1); //Apply the move only if it was legal
}
}
else
{
cout << "Thanks for Playing!" << endl;
return 0;
}
}
}
}
}
else if (useFile == 2)
{
cout << "Enter starting position (only two values): "; //Request for position to remove first peg from
cin >> startRow >> startCol;
bool quit = wantToQuit(startRow, startCol); //Check if the user wants to quit
if (quit == false) //Only proceed if the user does not want to quit
{
myGrid.startPosition(startRow - 1, startCol - 1); //Update the start position by calling the appropriate method
while (quit == false) //Only execute this loop if the user does not want to quit
{
bool legalMove = false;
cout << myGrid << endl; //Update the displayed grid
outFile << myGrid << endl;
while (legalMove == false) //Loop will continue to execute indefinitely
{
int complete = myGrid.movesLeft();
if (complete == 3)
{
cout << "You beat the game!" << endl;
return 0;
}
cout << "Enter move (four values): "; //Request a move from the user
cin >> startRow >> startCol;
quit = wantToQuit(startRow, startCol); //Check if the user wants to quit
if (quit == false)
{
cin >> endRow >> endCol;
cout << endl;
legalMove = myGrid.isLegalMove(startRow - 1, startCol - 1, endRow - 1, endCol - 1); //Check if this was a legal move
if (legalMove == false)
{
cout << "Please enter a valid move!" << endl << endl;
}
else
{
legalMove = true;
myGrid.applyMove(startRow - 1, startCol - 1, endRow - 1, endCol - 1); //Apply the move only if it was legal
}
}
else
{
cout << "Thanks for Playing!" << endl;
return 0;
}
}
}
}
else
cout << "Thanks for Playing!" << endl;
}
outFile.close();
return 0;
}
/*
Checks parameter values to check whether the user has indicated that they want to quit
@param quitRow, quitCol: the values input by the user
*/
bool wantToQuit(int quitRow, int quitCol)
{
if ((quitRow == 99) && (quitCol == 99))
return true;
else
return false;
}