Completed Game design advice

HI,

I have a completed project of a checkers game. My module is a Design and Implementation module I am asking if someone can run through my code and see if there is any way of improving the design of it? This is a working game.

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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
class Checkers : public Board
{

private:
	
	string validMove(Position origin) {
		
		if(coord[lastMove.coordX][lastMove.coordY].getType() != "default")
			return "occupied";
		
		//Check if piece is moving north
		if(origin.playerID == 2 || coord[origin.coordX][origin.coordY].getType() == "King"){
			//Check Jump Move North West
			if(origin.coordX-2 == lastMove.coordX && origin.coordY-2 == lastMove.coordY && coord[origin.coordX-1][origin.coordY-1].getPlayer() == (origin.playerID%2)+1){
				return "validJumpNW";
			}
			//Check Jump Move North East
			if(origin.coordX+2 == lastMove.coordX && origin.coordY-2 == lastMove.coordY && coord[origin.coordX+1][origin.coordY-1].getPlayer() == (origin.playerID%2)+1){
				return "validJumpNE";
			}
			//Check Single Move North East
			if(origin.coordX+1 == lastMove.coordX && origin.coordY-1 == lastMove.coordY){
				return "valid";
			}
			//Check Single Move North West
			if(origin.coordX-1 == lastMove.coordX && origin.coordY-1 == lastMove.coordY)
				return "valid";
		}
		//Check if the piece is free to move South
		if(origin.playerID == 1 || coord[origin.coordX][origin.coordY].getType() == "King"){
			//Check Jump Move South East
			if(origin.coordX+2 == lastMove.coordX && origin.coordY+2 == lastMove.coordY && coord[origin.coordX+1][origin.coordY+1].getPlayer() == (origin.playerID%2)+1){
				return "validJumpSE";
			}
			//Check Jump Move South West
			if(origin.coordX-2 == lastMove.coordX && origin.coordY+2 == lastMove.coordY && coord[origin.coordX-1][origin.coordY+1].getPlayer() == (origin.playerID%2)+1){
				return "validJumpSW";
			}
			//Check Single Move South East
			if(origin.coordX+1 == lastMove.coordX && origin.coordY+1 == lastMove.coordY)
				return "valid";
			//Check Single Move South West
			if(origin.coordX-1 == lastMove.coordX && origin.coordY+1 == lastMove.coordY)
				return "valid";
		}
		
		return "invalid";
	}
    void statusMessage (string message) {
		move(10,0);
		clrtoeol();
		mvprintw(10,0, message.c_str());
		refresh();
	}
    
    bool makeMove(){ //take the destination, origin and owner
		getSelection();
		Position origin = lastMove;
		
		if(coord[origin.coordX][origin.coordY].getPlayer() != origin.playerID) {
			statusMessage("That's not your piece");
			return false;
		}
		
		getSelection();
		string move = validMove(origin);
		
		if(move.substr(0,5) != "valid" ) {
			if(move == "occupied") {
				statusMessage("There is already a piece in that square!");
				return false;
			}
			
			if(move == "invalid") {
				statusMessage("Invalid move");
				return false;
			}
			
			getSelection();
			move = validMove(origin);
		}
		
		if(move == "valid") {
			coord[lastMove.coordX][lastMove.coordY] = coord[origin.coordX][origin.coordY];	
			removePiece(origin);
			if((lastMove.coordY == 0 && lastMove.playerID == 2) || (lastMove.coordY == 7 && lastMove.playerID == 1))
				coord[lastMove.coordX][lastMove.coordY].changeToKing();
			
			return true;
		}
		if(move.substr(0,9) == "validJump") {
			string direction = move.substr(9,2);
			coord[lastMove.coordX][lastMove.coordY] = coord[origin.coordX][origin.coordY];
			removePiece(origin);
			if((lastMove.coordY == 0 && lastMove.playerID == 2) || (lastMove.coordY == 7 && lastMove.playerID == 1))
				coord[lastMove.coordX][lastMove.coordY].changeToKing();
			
			Position middle = Position();
			if(direction == "NW") {
				middle.coordX = lastMove.coordX+1;
				middle.coordY = lastMove.coordY+1;
			}
			else if(direction == "NE") {
				middle.coordX = lastMove.coordX-1;
				middle.coordY = lastMove.coordY+1;
			}
			else if(direction == "SW") {
				middle.coordX = lastMove.coordX+1;
				middle.coordY = lastMove.coordY-1;
			}
			else if(direction == "SE") {
				middle.coordX = lastMove.coordX-1;
				middle.coordY = lastMove.coordY-1;
			}
			else
				return false;
			removePiece(middle);
			return true;
		}
		
		return false;
	}
	
	
	
	
	
	/*    This method is of boolean type, it will take in the destination of the intended new Piece as a parameter and return true if the Piece is added and False if the Piece is not added
	 */
	
	
	
	int isGameOver() {
		int player1Count =0;
		int player2Count =0;
		
		for(int i=0; i<8;i++){
			for(int j=0; j<8; j++){
				if(coord[i][j].getPlayer() == 1)
					player1Count++;
				if(coord[i][j].getPlayer() == 2)
					player2Count++;
			}
		}
		if(player1Count < 1){
			mvprintw(17,15, "Player 1 has :  %i  many pieces left", player1Count);
			return 1;
		}
		if (player2Count < 1) {
			
			mvprintw(18,15, "Player 2 has :  %i  pieces left", player2Count);
			return 1;
		}
		
		for(int j=0; j<8;j++){
			for(int i=0; i<8; i++){
				//Find pieces owned by the other player and see if they are free to move
				if(coord[i][j].getPlayer() == (lastMove.playerID%2)+1){
					//Check if the piece is free to move South
					if(lastMove.playerID == 2 || coord[i][j].getType() == "King"){
						//Check South East
						if(i+1 < 8 && j+1 < 8 && coord[i+1][j+1].getType() == "default"){
							return 0;
						}
						//Check South West
						if(i-1 > 0 && j+1 < 8 && coord[i-1][j+1].getType() == "default"){
							return 0;
						}
					}
					
					//Check if the piece is free to move North
					if(lastMove.playerID == 1 || coord[i][j].getType() == "King"){
						//Check North East
						if(i+1 < 8 && j-1 > 0 && coord[i+1][j-1].getType() == "default"){
							return 0;
						}
						
						//Check North West
						if(i-1 > 0 && j-1 > 0 && coord[i-1][j-1].getType() == "default"){
							return 0;
						}
					}
					
				}
			}
		}
		
		return 1;
	}
	void getSelection()
	{
		keypad(stdscr, TRUE); //Enable extended keys for the standard screen
		int key;
		drawScreen();
		do{
			key = getch(); //Capture a key	
			switch(key) {
				case KEY_UP:
					if(lastMove.coordY == 0)
						lastMove.coordY = 7;
					else
						lastMove.coordY = lastMove.coordY-1;
					break;
				case KEY_DOWN:
					lastMove.coordY = (lastMove.coordY+1)%8;
					break;
				case KEY_LEFT:
					if(lastMove.coordX == 0)
						lastMove.coordX = 7;
					else
						lastMove.coordX = lastMove.coordX-1;
					break;
				case KEY_RIGHT:
					lastMove.coordX = (lastMove.coordX+1)%8;
					break;
			}
			drawScreen(); //Draw current contents of coord to the screen
		} while(key != '\n'); //Do the above until enter or return is pressed
		drawScreen();

	};
	
	
    /*
     This method will scan the coord for winning or draw conditions. If a winning condition is met it will set the winner integer to the id of the winning player or will set the winner integer to -1 in the case of a draw. This method will return the winning player as an int. This is a pure virtual method.
     */
    

	
public:
	Checkers() {
	
		//Prepare the coord by placing starting pieces
		//Put the black pieces on the board
		int j=0;
		for(int i=0;i<3;i++){
			for(j=(j+1)%2; j<8; j=j+2){
				coord[j][i] = Piece("Checker", 1);
			}
		}
		
		//Put the white pieces on the board
		for(int i=5;i<8;i++){
			for(j=(j+1)%2; j<8; j=j+2){
				coord[j][i] = Piece("Checker", 2);
			}
		}
	}

    
		

    
    void startGame() {

		while(true)	{
			mvprintw(4,0, "Player %i it is your turn", lastMove.playerID);
			refresh();
			if(makeMove())
				lastMove.playerID=(lastMove.playerID%2)+1;
			if(isGameOver() == 1){
				drawScreen();
				mvprintw(15,15, "The game is over 2 , congratulations Player %i", lastMove.playerID);
				refresh();
				napms(3000);

				break;
			}
				
		}
	}
    
    /*
     This will define which player is currently active, the purpose of this is for games like Snakes & Ladders in which the turn order can be extended. This is a pure virtual method.
     */
    
    
    void drawScreen() {
		int colour=2;
		move(5, 0);
		for(int i=0; i< 8; i++) {
			colour=(colour%2)+1;
			for(int j=0; j< 8; j++){
				colour=(colour%2)+1;
				attrset(COLOR_PAIR(colour));
				
				if(i == lastMove.coordY && j == lastMove.coordX)
					attrset(COLOR_PAIR(9));
				else if(coord[j][i].getPlayer() == 1)
					attrset(COLOR_PAIR(7));
				else if(coord[j][i].getPlayer() == 2)
					attrset(COLOR_PAIR(8));
					
				if(coord[j][i].getType() == "Checker") 
					addstr(" O ");
				else if(coord[j][i].getType() == "King") 
					addstr(" K ");	
				else
					addstr("   ");
			}
			addstr("\n");
		}
		attrset(COLOR_PAIR(1));
		refresh();


	}
};


Any advice big or small is greatly appreciated,

Cheers
Last edited on
Topic archived. No new replies allowed.