-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
283 lines (275 loc) · 8.59 KB
/
main.cpp
File metadata and controls
283 lines (275 loc) · 8.59 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
276
277
278
279
280
281
282
283
*/
#include <iostream>
#include <cctype>
using namespace std;
const char* INSTRUCTIONS =
"Welcome to the peg jump puzzle. The board starts out with a \n"
"single blank position, represented by the '*'. To make a \n"
"move, select the letter of a peg. A peg must be able to \n"
"jump over an adjacent peg into a blank position for a move \n"
"to be valid. The jumped peg is then removed from the board. \n"
"The game is over when there are no valid moves left. At any \n"
"point enter '-' to exit, or '+' for instructions. \n";
const char* startingMessage =
"Welcome to the peg jump puzzle! \n"
"Enter '-' to exit, or '+' for instructions.";
const char EMPTY_PIECE = '*';
const int BOARD_SIZE = 121;
const int PRINT_BOARD_ROW_SIZE = 11;
const int PRINT_BOARD_COL_SIZE = 12;
const int PRINT_BOARD_START_DRAWING_INDEX = 27;
const int PRINT_BOARD_END_DRAWING_INDEX = PRINT_BOARD_START_DRAWING_INDEX + 49;
const int PRINT_BOARD_SIZE = PRINT_BOARD_ROW_SIZE * PRINT_BOARD_COL_SIZE;
char printBoard[] = {
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','\n',
};
char multipleMovesBoard[] = {
' ',' ','*','*','*',' ',' ',
' ',' ','*','*','*',' ',' ',
'*','*','*','*','*','*','*',
'*','*','*','*','*','*','*',
'*','*','*','*','*','*','*',
' ',' ','*','*','*',' ',' ',
' ',' ','*','*','*',' ',' '
};
char startingBoard[] = {
' ',' ','A','B','C',' ',' ',
' ',' ','D','E','F',' ',' ',
'G','H','I','J','K','L','M',
'N','O','P','*','Q','R','S',
'T','U','V','W','X','Y','Z',
' ',' ','1','2','3',' ',' ',
' ',' ','4','5','6',' ',' '
};
struct moveData {
int originalPieceLocation;
int firstHopLocation;
int secondHopLocation;
};
//board being displayed
void displayBoard(char board[], char output[]) {
int gameBoardIndex = 0;
for(int row = 2; row < 9; row++) {
int rowIndex = 12 * row;
for(int col = 2; col < 9; col++) {
output[rowIndex + col] = board[gameBoardIndex++];
}
}
}
//checking the input
bool checkInput(char *inputPtr)
{
char input = *inputPtr;
bool valid = false;
if(isalpha(input)) {
valid |= true;
if(islower(input)) {
*inputPtr = toupper(input);
}
}
valid |= isdigit(input);
return valid;
}
//checking if the piece is oin the board
int isPieceOnBoard(char board[], char piece)
{
for(int i = 0; i < BOARD_SIZE; i++)
{
if(board[i] == piece) {
return i;
}
}
return -1;
}
// *** Abhinav: add comments for function
int* squareSearch(char board[], int pieceIndex, bool doubleMove, int valueBuffer[4]) {
if(pieceIndex < 0 && pieceIndex > 49) {
// invalid piece index, should have been checked, something went wrong
return valueBuffer;
}
valueBuffer[0] = pieceIndex - (doubleMove ? 14 : 7);
valueBuffer[1] = pieceIndex - (doubleMove ? 2 : 1);
valueBuffer[2] = pieceIndex + (doubleMove ? 2 : 1);
valueBuffer[3] = pieceIndex + (doubleMove ? 14 : 7);
return valueBuffer;
}
// *** Abhinav: add comments for function
void updatePieces(char board[], int pieceLocation, int searchPieceLocation, int moveLocation) {
char piece = board[pieceLocation];
board[moveLocation] = piece;
board[searchPieceLocation] = EMPTY_PIECE;
board[pieceLocation] = EMPTY_PIECE;
}
int countPiecesRemaining(char board[]) { // *** Abhinav: add comments for function
int pieceCount = 0;
for(int i = 0; i < 49; i++) {
if(board[i] == EMPTY_PIECE || board[i] == ' ') {
continue;
}
pieceCount++;
}
return pieceCount;
}
//moving the piece
int movePiece(char board[], char piece, struct moveData moveInfo[4], bool patch) { // *** Abhinav: need to add comments within this function
bool validMove = checkInput(&piece);
int pieceLocation = isPieceOnBoard(board, piece);
validMove &= pieceLocation != -1;
if(!validMove) {
return -1;
}
int singleSquareSearch[4] = {};
int doubleSquareSearch[4] = {};
squareSearch(board, pieceLocation, false, singleSquareSearch);
squareSearch(board, pieceLocation, true, doubleSquareSearch);
int validMoveCount = 0;
for(int i = 0; i < 4; i++) {
int searchPieceLocation = singleSquareSearch[i];
char searchPiece = board[searchPieceLocation];
if((searchPieceLocation < 0 || searchPieceLocation > 49) ||
(searchPiece == ' ' || searchPiece == EMPTY_PIECE)) {
continue;
}
int moveLocation = doubleSquareSearch[i];
if(moveLocation > 0 && moveLocation < 49 && board[moveLocation] == EMPTY_PIECE) {
validMoveCount++;
moveInfo[i] = { pieceLocation,
searchPieceLocation,
moveLocation };
} else {
moveInfo[i] = {-1};
}
}
if(validMoveCount == 0) {
return -1;
}
int possibleMoves = 1;
for(int i = 0; i < 4; i++) {
struct moveData moveInfoAtIndex = moveInfo[i];
if(moveInfoAtIndex.originalPieceLocation == -1) {
continue;
}
if(validMoveCount == 1) {
if(patch) {
updatePieces(board, moveInfoAtIndex.originalPieceLocation, moveInfoAtIndex.firstHopLocation, moveInfoAtIndex.secondHopLocation);
}
} else {
multipleMovesBoard[moveInfoAtIndex.originalPieceLocation] = '+';
multipleMovesBoard[moveInfoAtIndex.secondHopLocation] = (char) 48 + possibleMoves++;
}
}
return validMoveCount;
}
bool existsValidMove(char board[]) { // *** Abhinav: add comments for function
struct moveData moveInfo[4] = {{-1},{-1},{-1},{-1}};
for(int i = 0; i < 49; i++) {
char piece = board[i];
if(board[i] == EMPTY_PIECE || board[i] == ' ') {
continue;
}
if(movePiece(board,piece, moveInfo, false) > 0) {
return true;
}
}
return false;
}
//-------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
int moveCount = 1;
/* logic:
*
* a valid move on the board is possible, if
* input is a number or letter, and (checkInput(char* ..))
* the piece is on the board, and (isPieceOnBoard(char .., char[] ..)
* a jump leads to an empty space, and
* the jump overtakes an existing piece
* and the board contains a possible move
*
*/
//Variable declarations
displayBoard(startingBoard,printBoard);
cout << startingMessage
<< printBoard
<< endl;
bool playing = true;
// Main loop to play the game
int mode = 0;
struct moveData moveInfo[4] = {{-1},{-1},{-1},{-1}};
while(playing) {
char input;
if(mode > 0) {
moveCount++;
}
else if(mode == -1) {
cout << "*** Invalid move, please retry. ***" << endl;
}
if(mode == 1) {
cout << printBoard;
}
if(mode > 1) {
displayBoard(multipleMovesBoard,printBoard);
cout << printBoard;
cout << "There is more than one possible move. Which move do you want? " ;
cin >> input;
int selectionLocation = isPieceOnBoard(multipleMovesBoard,input);
if(selectionLocation < 0) {
return 1;
} else {
for(int i = 0; i < 4; i++) {
struct moveData moveInfoAtIndex = moveInfo[i];
if(moveInfoAtIndex.secondHopLocation == selectionLocation) {
updatePieces(startingBoard, moveInfoAtIndex.originalPieceLocation, moveInfoAtIndex.firstHopLocation, moveInfoAtIndex.secondHopLocation);
}
}
}
displayBoard(startingBoard,printBoard);
cout << printBoard;
}
else {
cout << endl << moveCount << ". Enter the peg to move: ";
cin >> input;
}
switch(input) {
// Check for '-' to exit, and '+' for displaying instructions
case '+': cout << INSTRUCTIONS;
break;
case '-': playing = false;
break;
default: mode = movePiece(startingBoard,input,moveInfo, true);
break;
}
displayBoard(startingBoard,printBoard);
playing &= countPiecesRemaining(startingBoard) > 1
&& existsValidMove(startingBoard);
}// how many pieces reamingng
cout << endl << "Exiting..." << endl << endl;
int piecesRemaining = countPiecesRemaining(startingBoard);
cout << piecesRemaining << " pieces remaining. ";
if(piecesRemaining >= 5){
cout << "Ouch!" << endl;
}
else if(piecesRemaining == 4){
cout << "Needs Improvement." << endl;
}
else if(piecesRemaining == 3){
cout << "OK." << endl;
}
else if(piecesRemaining == 2){
cout << "Good!" << endl;
}
else if(piecesRemaining == 1){
cout << "Excellent!" << endl;
}
return 0;
}