-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
548 lines (461 loc) · 20.6 KB
/
game.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/************************************************************************************************************************
* [FILE NAME] : <game.c> *
* [AUTHOR] : <Eslam EL-Naggar> *
* [DATE CREATED]: <JAN 25, 2020> *
* [Description} : <Source file for the GAME> *
************************************************************************************************************************/
/*-------------------------------------------------INCLUDES-------------------------------------------------------------*/
#include "game.h"
/*--------------------------------------------FUNCTIONS_DECLARATIONS-----------------------------------------------------*/
static void Setting_Interrupts(void);
static void GameInit(void);
static void GameIntiate(uint8 gamePlay, uint8 *shape_Ptr);
static void MoveRightCallBackFunction(void);
static void MoveLeftCallBackFunction(void);
static void RotateShapeCallBackFunction(void);
/*-----------------------------------------------GLOBAL VARIABLES---------------------------------------------------------*/
/* initialize the global variables */
uint8 moveLeftFlag = ZERO; /* this variable is set to one when user wants to move the shape to the left */
uint8 moveRightFlag = ZERO; /* this variable is set to one when user wants to move the shape to the right */
uint8 loseFlag = ZERO; /* this variable is set to one when user loses the game */
uint8 shape = STARTED_SHAPE; /* this variable holds the current shape number */
uint8 rotateShapeFlag = ZERO; /* this variable is set to one when user wants to rotate the shape */
uint8 nextShape = ZERO; /* this variable holds the value of the next shape */
/*----------------------------------------------------------------------------------------------------
[Function Name]: main
[Description] : This function is the main function of the program
[Returns] : This function returns Zero
----------------------------------------------------------------------------------------------------*/
int main() {
/* Initialize the main local varibles */
uint8 rowIndicator = STARTED_ROW; /* started row */
uint8 columnIndicator = STARTED_COLUMN; /* started column */
uint8 upperPageIndicator = STARTED_UPPER_PAGE; /* started upper page */
uint8 lowerPageIndicator = STARTED_LOWER_PAGE; /* started lower page */
uint8 nextShapeFlag = OFF; /* this variable is set to one when the shape collides with another shape */
uint8 prevRow = rowIndicator; /* stores previous row number */
uint8 prevColumn = columnIndicator; /* stores previous column number */
uint8 prevUpperPage = upperPageIndicator; /* stores previous upper page number */
uint8 prevLowerPage = lowerPageIndicator; /* stores previous lower page number */
uint8 upperState[SHAPE_WIDTH] = { ZERO }; /* array for storing the state of upper columns */
uint8 lowerState[SHAPE_WIDTH] = { ZERO }; /* array for storing the state of lower columns */
uint8 gamePlay = ON; /* sets Game mode " ON "*/
Setting_Interrupts(); /* intializes and sets the interrupts parameters */
GameInit(); /* intializes the game interface */
while (ONE) {
while (gamePlay) { /* looping while gamePlay mode is ON */
/* displaying the current shape in specific location (specific row, specific column, specific page ) */
DrawShape(&shape, &rowIndicator, &columnIndicator,
&lowerPageIndicator, &upperPageIndicator, &nextShapeFlag,
lowerState, upperState);
/* checking on the shape collides with another shape or not */
switch (nextShapeFlag) {
case ONE:
/* current shape collides with another shape */
/* set the nextShapeFlag variable again to Zero */
nextShapeFlag = ZERO;
/* displaying the last position of the shape before collision */
DrawShape(&shape, &prevRow, &prevColumn, &prevLowerPage,
&prevUpperPage, &nextShapeFlag, lowerState, upperState);
/* switch to the next shape */
completeCheck(&rowIndicator, &upperPageIndicator);
shape = nextShape;
/* displaying the next shape on the screen */
nextShape = NextShapeView(&shape);
/* check if it is the last shape or not */
if ((shape) == NUM_SHAPES) {
(shape) = ZERO; /* let the current shape is the first shape */
}
/* setting the location of the current shape to start from the top */
rowIndicator = STARTED_ROW;
upperPageIndicator = STARTED_UPPER_PAGE;
lowerPageIndicator = STARTED_LOWER_PAGE;
columnIndicator = STARTED_COLUMN;
break;
case ZERO:
/* current shape does not collide
* checks if the current location is the last location or not
* if true skip the erasing shape function
* if false erase the current shape
*/
if ((rowIndicator == 7)
&& (upperPageIndicator == LOCATION_LAST_UPPER_PAGE)
&& (lowerPageIndicator == LOCATION_LAST_LOWER_PAGE)) {
} else {
/* erasing the current shape */
EraseShape(&shape, &rowIndicator, &columnIndicator,
&lowerPageIndicator, &upperPageIndicator,
lowerState, upperState);
/* storing the location of the last shape erased */
prevRow = rowIndicator;
prevColumn = columnIndicator;
prevUpperPage = upperPageIndicator;
prevLowerPage = lowerPageIndicator;
_delay_ms(DELAY_AFTER_ERASING_TIME);
}
/* generates the new location of the next shape */
GenerateLocation(&shape, &rowIndicator, &upperPageIndicator,
&lowerPageIndicator, &columnIndicator);
break;
}
/* checking if the user loses or not */
switch (loseFlag) {
/* in case of losing the game */
case LOST:
/* set the loseFlag againg to zero
* and set the gamePlay mode to "OFF"
*/
gamePlay = OFF;
loseFlag = ZERO;
break;
/* in case of not losing the game */
case NOT_LOST:
/* checks if the user presses the moveLeft button or not */
if (moveLeftFlag == ONE) {
columnIndicator = columnIndicator - TWO;
checkAfterMoving(&columnIndicator);
moveLeftFlag = ZERO;
}
/* checks if the user presses the moveRight button or not */
if (moveRightFlag == ONE) {
columnIndicator = columnIndicator + TWO;
checkAfterMoving(&columnIndicator);
moveRightFlag = ZERO;
}
/* checks if the user presses the rotate button or not */
if (rotateShapeFlag == ONE) {
rotateShapeFlag = ZERO;
/* rotates the shape */
RotateShape(&columnIndicator);
}
break;
}
}
cli();
LosingFunction();
}
return ZERO;
}
/*----------------------------------------------------------FUNCTIONS_DEFINITIONS-------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------------------
[Function Name]: GameInit
[Description] : This function is responsible for intializing the game
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
static void GameInit(void) {
GLCD_Init(); /* intialize GLCD Driver */
GLCD_clearScreen(); /* clear the GLCD Screen */
GLCD_displayString(PAGE_3, " Welcome Tetris Game");
_delay_ms(400);
GLCD_clearScreen(); /* clear the GLCD Screen */
GameIntiate(0, &shape); /* initiate the game */
sei();
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: Setting_Interrupts
[Description] : This function is responsible for intializing the external interrupts
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
static void Setting_Interrupts(void) {
ExternalInterrupts_INT0_setCallBack(MoveLeftCallBackFunction); /* setting INT0 callback function */
ExternalInterrupts_INT1_setCallBack(MoveRightCallBackFunction);/* setting INT1 callback function */
ExternalInterrupts_INT2_setCallBack(RotateShapeCallBackFunction);
ExternalInterrupts_init(&interruptsConfig); /* intialize the External Interrupts Driver */
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GameIntiate
[Description] : This function is responsible for initiating the game and displaying the main borders
of the game.
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
static void GameIntiate(uint8 gamePlay, uint8 *shape_Ptr) {
uint8 page = ZERO;
uint8 loopCounter = ZERO;
cli();
/* Setting up records place */
GLCD_CTRL_PORT |= (ONE << CS2); /* Select Left half of display */
GLCD_CTRL_PORT &= ~(ONE << CS1);
GLCD_displayString(PAGE_1, " ->Shape:");
nextShape = NextShapeView(shape_Ptr); /* displaying the next shape */
/* generating border of the game */
GLCD_CTRL_PORT |= (ONE << CS1); /* Select Right half of display */
GLCD_CTRL_PORT &= ~(ONE << CS2);
GLCD_sendCommand(COLUMN_SETTING_ADDRESS + LEFT_BORDER); /* Set Y address (fifth column) */
GLCD_sendCommand(PAGE_SETTING_ADDRESS); /* Set x address (page=0) */
/* displaying the border of the game */
GLCD_sendData(ALL_ROWS);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE; loopCounter++) {
GLCD_sendData(FIRST_ROW);
}
GLCD_sendData(ALL_ROWS);
for (page = ONE; page < PAGE_7; page++) {
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page); /* Set page address */
GLCD_sendCommand(COLUMN_SETTING_ADDRESS + LEFT_BORDER); /* Set column address */
GLCD_sendData(ALL_ROWS);
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + BORDER_WIDTH); /* Set column address */
GLCD_sendData(ALL_ROWS);
}
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page);
GLCD_sendCommand(COLUMN_SETTING_ADDRESS + LEFT_BORDER); /* Set column address */
GLCD_sendData(ALL_ROWS);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE; loopCounter++) {
GLCD_sendData(LAST_ROW);
}
GLCD_sendData(ALL_ROWS);
GLCD_sendCommand(COLUMN_SETTING_ADDRESS); /* Set Y address (column=0) */
GLCD_sendCommand(PAGE_SETTING_ADDRESS); /* Set x address (page=0) */
sei();
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: MoveLeftCallBackFunction
[Description] : This function is call back function when moveLeft button is pressed
[Returns] : This function returns the masked part of the state
----------------------------------------------------------------------------------------------------*/
static void MoveLeftCallBackFunction(void) {
/* set the moveLeft flag to one */
moveLeftFlag = ONE;
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: MoveRightCallBackFunction
[Description] : This function is call back function when moveRight button is pressed
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
static void MoveRightCallBackFunction(void) {
/* set the moveRight flag to one */
moveRightFlag = ONE;
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: RotateShapeCallBackFunction
[Description] : This function is responsible is called when INT2 is triggred
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
static void RotateShapeCallBackFunction(void) {
/* set the roteteShape flag to one */
rotateShapeFlag = ONE;
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: checkUpper
[Description] : This function is responsible for returning the masking part of the state column
[Returns] : This function returns the masked part of the state
----------------------------------------------------------------------------------------------------*/
uint8 checkUpper(uint8 state, uint8 rowIndicator) {
return (uint8) (state & (HIGH_8BITS << rowIndicator));
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: checkLower
[Description] : This function is responsible for returning the masking part of the state column
[Returns] : This function returns the masked part of the state
----------------------------------------------------------------------------------------------------*/
uint8 checkLower(uint8 state, uint8 rowIndicator) {
return (uint8) (state & (HIGH_8BITS >> (NUM_BITS_IN_BYTE - rowIndicator)));
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: completeCheck
[Description] : This function is responsible for checking
the completion of the row or page according the configuration of ROW_TRANSFER
(ROW_TRANSFER) is defined as ON, function will check on Row Completition
(ROW_TRANSFER) is defined as OFF, function will check on Page completition
[Args] : uint8 * rowIndicator_Ptr
this argument shall contains the address of the rowIndicator variable
uint8 * upperPageIndicator_Ptr
this argument shall contains the address of the upperPageIndicator variable
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void completeCheck(uint8 *rowIndicator_Ptr, uint8 *upperPageIndicator_Ptr) {
#if ROW_TRANSFORM == OFF
uint8 loopCounter = ZERO;
uint8 state = ZERO;
uint8 counter = ZERO;
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *upperPageIndicator_Ptr);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE;
loopCounter++) {
state = GLCD_readData();
state = GLCD_readData();
if(state == HIGH_8BITS)
{
counter++;
}
}
if(counter == (BORDER_WIDTH - ONE))
{
RowCompleteTranform(*rowIndicator_Ptr, *upperPageIndicator_Ptr);
}
#endif
#if ROW_TRANSFORM== ON
uint8 loopCounter = ZERO;
uint8 state = ZERO;
uint8 rowCounter = ZERO;
uint8 counter[NUM_BITS_IN_BYTE] = { ZERO };
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *upperPageIndicator_Ptr);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE; loopCounter++) {
state = GLCD_readData();
state = GLCD_readData();
for (rowCounter = ZERO; rowCounter < NUM_BITS_IN_BYTE; rowCounter++) {
if (BIT_IS_SET(state, rowCounter)) {
counter[rowCounter]++;
}
}
}
for (rowCounter = NUM_BITS_IN_BYTE; rowCounter > ZERO; rowCounter--) {
if (counter[rowCounter - ONE] == (BORDER_WIDTH - ONE)) {
if (rowCounter - ONE == SEVEN && *upperPageIndicator_Ptr == SEVEN) {
} else {
RowCompleteTranform(rowCounter - ONE, *upperPageIndicator_Ptr);
}
}
}
#endif
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: RowCompleteTranform
[Description] : This function is responsible for checking
the completion of the row or page according the configuration of ROW_TRANSFER
(ROW_TRANSFER) is defined as ON, function will check on Row Completition
(ROW_TRANSFER) is defined as OFF, function will check on Page completitio
[Args] : uint8 rowIndicator
this argument shall contains the address of the rowIndicator variable
uint8 pageIndicator
this argument shall contains the address of the current page
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void RowCompleteTranform(uint8 rowIndicator, uint8 pageIndicator) {
#if ROW_TRANSFORM == ON
/* intialize local variables */
uint8 loopCounter = ZERO; /* this variable is responsible for looping counter */
/* this variable is responsible for storing the new state of the column */
uint8 newColumn = ZERO;
uint8 page = ZERO;
uint8 upperRow = ZERO;
uint8 upperPagePart[BORDER_WIDTH] = { ZERO };
uint8 lowerPagePart[BORDER_WIDTH] = { ZERO };
cli();
page = pageIndicator;
while (page >= ONE) {
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE;
loopCounter++) {
upperPagePart[loopCounter] = GLCD_readData();
upperPagePart[loopCounter] = GLCD_readData();
}
/* set the column address */
if (page + ONE == EIGHT) {
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page);
} else {
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page + ONE);
}
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE;
loopCounter++) {
lowerPagePart[loopCounter] = GLCD_readData();
lowerPagePart[loopCounter] = GLCD_readData();
}
if (page + ONE == EIGHT) {
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page);
} else {
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page + ONE);
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
/* displaying the new column state by ORing the new part with the remaining the old part*/
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE;
loopCounter++) {
upperRow = (upperPagePart[loopCounter] & 0x80)
>> LAST_ROW_IN_PAGE;
newColumn = ((lowerPagePart[loopCounter]
& (HIGH_8BITS >> (rowIndicator + ONE))) << ONE);
lowerPagePart[loopCounter] = (lowerPagePart[loopCounter]
& (HIGH_8BITS << (rowIndicator + ONE)));
GLCD_sendData(
(newColumn | lowerPagePart[loopCounter]) | upperRow);
}
}
page--;
rowIndicator = LAST_ROW_IN_PAGE;
}
#elif ROW_TRANSFORM == OFF
/* intialize local variables */
uint8 loopCounter = ZERO; /* this variable is responsible for looping counter */
/* this variable is responsible for storing the state of the column along the border width */
uint8 state[BORDER_WIDTH] = { ZERO };
uint8 page = pageIndicator;
cli();
while(page>=ZERO)
{
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE; loopCounter++) {
state[loopCounter] = GLCD_readData();
state[loopCounter] = GLCD_readData();
if(page == ZERO)
{
state[loopCounter] = state[loopCounter] & 0b11111110;
}
}
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + LEFT_BORDER + ONE);
if(page+ONE == EIGHT)
{
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page );
}
else
{
GLCD_sendCommand(PAGE_SETTING_ADDRESS + page + ONE);
}
/* displaying the new column state by ORing the new part with the remaining the old part*/
for (loopCounter = ONE; loopCounter <= BORDER_WIDTH - ONE; loopCounter++) {
if(page == SEVEN)
{
GLCD_sendData((((state[loopCounter])>>ONE) | 0x80));
}
else
{
GLCD_sendData(state[loopCounter]);
}
}
page--;
}
#endif
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: LosingFunction
[Description] : This function is responsible for generating the exit message
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void LosingFunction(void) {
GLCD_sendCommand(COLUMN_SETTING_ADDRESS); /* Set Y address (column=0) */
GLCD_sendCommand(PAGE_SETTING_ADDRESS); /* Set x address (page=0) */
GLCD_CTRL_PORT |= (ONE << CS1); /* Select Right half of display */
GLCD_CTRL_PORT &= ~(ONE << CS2);
/* clear the graphical LCD screen */
GLCD_clearScreen();
GLCD_CTRL_PORT |= (ONE << CS2); /* Select Left half of display */
GLCD_CTRL_PORT &= ~(ONE << CS1);
/* Display the " Sorry, you have lost the game " sentence */
GLCD_displayString(PAGE_2, "Sorry, you have lost the game :')");
_delay_ms(ONE_SECOND);
/* clear the graphical LCD screen */
GLCD_clearScreen();
/* Display the "Thank you" sentence */
GLCD_displayString(PAGE_2, " Thank You :')");
_delay_ms(ONE_SECOND);
/* reaches the end of the game */
while (ONE) {
}
}