aboutsummaryrefslogtreecommitdiffstats
path: root/2048 Game/Game.cpp
blob: e440290dd94304fa5bd2b0da46103da4788a7236 (plain)
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
/* 
 * includes necessary libraries
 */
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>

/* 
 * defines names for the constants we want to use
 * this is to make the code more readable 
 */
#define GRIDSIZE 	 4
#define BASE 		 2
#define UP 			'w'
#define DOWN 		's'
#define RIGHT 		'd'
#define LEFT 		'a'

using namespace std;

// declares the functions 
void printGrid(int(&) [GRIDSIZE][GRIDSIZE]);
void moveGrid(int(&) [GRIDSIZE][GRIDSIZE], char&);
void mergeArray(int*&, int&);
void placeBase(int (&) [GRIDSIZE][GRIDSIZE]);

bool checkGridChange(int (&) [GRIDSIZE][GRIDSIZE], int (&) [GRIDSIZE][GRIDSIZE]);
bool checkGameOver(int(&) [GRIDSIZE][GRIDSIZE]);

/* 
 * main function that handles all the i/o except for printing the grid
 * as this is easier done by a seperate function
 */
int main(int argc, char* argv[]) {
	// defines the 2D and tmp array that is used to check change
	int grid[GRIDSIZE][GRIDSIZE];
	int before[GRIDSIZE][GRIDSIZE];
	// defines tmp string file name to check if file exists
	string fileName;
	char usrInput;
	// defines input filestream for configFile
	ifstream configFile;

	// set random seed to be the current time
	srand(time(NULL));

	// gets the input from the user for the input file
	cout << "Enter initial configuration file name:" << endl;
	cin >> fileName;

	// opens the user defined file
	configFile.open(fileName.c_str());

	// enters the integers from the file into the array if file is correct
	if(configFile.is_open()) {
		for(int i = 0; i < GRIDSIZE; ++i) {
			for(int j = 0; j < GRIDSIZE; ++j) {
				configFile >> grid[i][j];
			}
		}
		configFile.close();
	} else {
		// if the file is not valid it will enter the default values of 0 and 2 in the
		// bottom right 
		cout << "The file doesn't exist, using default start" << endl;
		// creates default grid
		for(int i = 0; i < GRIDSIZE; ++i) {
			for(int j = 0; j < GRIDSIZE; ++j) {
				grid[i][j] = 0;
			}
		}
		grid[GRIDSIZE-1][GRIDSIZE-1] = BASE;
	}
	printGrid(grid);
	/*
	 * executes this loop while the game isn't over, which means that the user can
	 * still have turns and there are zero's left, as well as adjacent equivalent 
	 * numbers that can be added 
	 */
	while(!checkGameOver(grid)) {
		// gets user input
		cin >> usrInput;
		// copies the grid into the before grid
		for(int i = 0; i < GRIDSIZE; ++i) {
			for(int j = 0; j < GRIDSIZE; ++j)  {
				before[i][j] = grid[i][j];
			}
		}
		// moves grid in correct direction
		moveGrid(grid, usrInput);
		// check if grid has changed which means that the movement was valid
		if(checkGridChange(grid, before)){
			cout << endl;
			// places the random number in the correct base defined above
			placeBase(grid);
			// prints the grid
			printGrid(grid);
		}
	}
	cout << "game over" << endl;
    return 0;
}

/* 
 * prints the contents a 2D array with a constant size
 * so that the user can see the result of their input
 */
void printGrid(int (&gridArray)[GRIDSIZE][GRIDSIZE]) {
	for(int i = 0; i < GRIDSIZE; ++i) {
		for(int j = 0; j < GRIDSIZE; ++j) {
			cout << gridArray[i][j] << '\t';
		}
		cout << endl;
	}
	cout << endl;
}

/* 
 * handles the inputs of UP, DOWN, LEFT and RIGHT by the user and 
 * sends them to the merge algorithm in the correct order
 */
void moveGrid(int (&gridArray)[GRIDSIZE][GRIDSIZE], char &movDir) {
	// pointer that will point towards first element in array
	int* elementPtr;
	// defines the separation to the next element which 
	// says if it moves in rows or columns
	int separation;

	// if statements to find what direction the user wants to move the grid
	if(movDir == UP) {
		// separation set so that it goes up in rows
		separation = GRIDSIZE;
		for(int i = 0; i < GRIDSIZE; ++i) {
			// set elementpointer to be the first element in the rows
			elementPtr = &gridArray[0][i];
			// calls merge function
			mergeArray(elementPtr, separation);
		}
	} else if(movDir == DOWN) {
		separation = -GRIDSIZE;
		for(int i = 0; i < GRIDSIZE; ++i) {
			elementPtr = &gridArray[GRIDSIZE-1][i];
			mergeArray(elementPtr, separation);
		}
	} else if(movDir == LEFT) {
		// set separation to increment in columns
		separation = 1;
		for(int i = 0; i < GRIDSIZE; ++i) {
			elementPtr = gridArray[i];
			mergeArray(elementPtr, separation);
		}
	} else if(movDir == RIGHT) {
		separation = -1;
		for(int i = 0; i < GRIDSIZE; ++i) {
			elementPtr = &gridArray[i][GRIDSIZE-1];
			mergeArray(elementPtr, separation);
		}
	}
}

/* 
 * merges arrays together depending on the input
 */
void mergeArray(int* &arrayPointer, int &addrSep) {
	// defines array of one size larger so that all elements are moved correctly
	int nonZeroRow[GRIDSIZE+1];
	// sets all elements to 0
	for(int i = 0; i < GRIDSIZE+1; ++i) {
		nonZeroRow[i] = 0;
	}
	// sets the counter to 0
	int nonZeroCounter = 0;
	// sets all the elements in the array to be the nonzero elements
	// pointed to by the pointer
	for(int i = 0; i < GRIDSIZE; ++i) {
		if(*(arrayPointer+addrSep*i) != 0) {
			nonZeroRow[nonZeroCounter++] = *(arrayPointer+addrSep*i);
		}
	}
	for(int i = 0; i < nonZeroCounter; ++i) {
		// if two adjacent cells are equal multiply by the base
		if(nonZeroRow[i] == nonZeroRow[i+1]) {
			nonZeroRow[i] *= BASE;
			// then move all the other elements up
			for(int j = i+1; j < GRIDSIZE; ++j) {
				nonZeroRow[j] = nonZeroRow[j+1];
			}
		}
	}
	// set all the pointer locations to be equal to the elements in the array
	for(int i = 0; i < GRIDSIZE; ++i) {
		*(arrayPointer+addrSep*i) = nonZeroRow[i];
	}
}

/*
 * adds a base num at a random location where a zero was
 */
void placeBase(int (&gridArray)[GRIDSIZE][GRIDSIZE]) {
	// 2D array that will hold locations of the zeros
	int zeroArray[GRIDSIZE*GRIDSIZE][2];
	// sets the 0 counter
	int zeroCount = 0;
	for(int i = 0; i < GRIDSIZE; ++i) {
		for(int j = 0; j < GRIDSIZE; ++j) {
			if(gridArray[i][j] == 0) {
				// sets the x and y values in the zero array
				zeroArray[zeroCount][0] = i;
				zeroArray[zeroCount++][1] = j;
			}
		}
	}
	// gets a random number
	int randomLoc = rand() % zeroCount;
	// sets the random location in the grid to equal that value
	gridArray[zeroArray[randomLoc][0]][zeroArray[randomLoc][1]] = BASE;
}

/* 
 * checks if the two grids are the same
 * if they are this means it will not add the random 2
 * and not print the grid
 */
bool checkGridChange(int (&gridArray)[GRIDSIZE][GRIDSIZE], int (&beforeGrid)[GRIDSIZE][GRIDSIZE]) {
	for(int i = 0; i < GRIDSIZE; ++i) {
		for(int j = 0; j < GRIDSIZE; ++j) {
			if(gridArray[i][j] != beforeGrid[i][j]) {
				// if it finds a value that doesn't match, grid must have changed hence return true
				return true;
			}
		}
	}
	return false;
}

/* 
 * checks if the game is over by checking if 0's are present
 * or if there are equal numbers side by side
 */
bool checkGameOver(int (&gridArray)[GRIDSIZE][GRIDSIZE]) {
	// uses two for loops to go through all boxes in the grid
	for(int i = 0; i < GRIDSIZE; ++i) {
		for(int j = 0; j < GRIDSIZE; ++j) {
			// checks if the current box is a 0
			if(gridArray[i][j] == 0) {
				// returns false as this means the game isn't over
				return false;
			} 
			 // if not it checks if there are any repeating numbers around 
			 // it as this means they can add 
			else if(i > 0 && gridArray[i][j] == gridArray[i-1][j]) {
				return false;
			} else if(i < GRIDSIZE-1 && gridArray[i][j] == gridArray[i+1][j]) {
				return false;
			} else if(j > 0 && gridArray[i][j] == gridArray[i][j-1]) {
				return false;
			} else if(j < GRIDSIZE-1 && gridArray[i][j] == gridArray[i][j+1]) {
				return false;
			}
		}
	}
	// if none of the if statements is true it will end the games
	return true;
}