-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
125 lines (118 loc) · 2.97 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
#include "TimeDelay.h"
#include "IOs.h"
#include "UART2.h"
extern int CN0flag;
extern int CN1flag;
extern int CN2flag;
int game_over_flag = 0;
int end_program_flag = 0;
int seq[16] = {2, 1, 1, 0};
int max_level = 4; // change these two later
void flash_single(int led_no, uint16_t time_ms_on, uint16_t time_ms_off){
switch(led_no){
case 0:
LED0 = 1;
delay_ms(time_ms_on);
LED0 = 0;
break;
case 1:
LED1 = 1;
delay_ms(time_ms_on);
LED1 = 0;
break;
case 2:
LED2 = 1;
delay_ms(time_ms_on);
LED2 = 0;
default:
return; //display to string should be called?
}
delay_ms(time_ms_off);
return;
}
void demo(int level, uint16_t end_delay_ms){
int temp = 0;
Display2String(" Sequence Is: \r ", 1);
delay_ms(1000);
while(temp < level){
flash_single(seq[temp],500,500); //the flash interval
//changes with difficulty
temp++;
}
delay_ms(end_delay_ms);
return;
}
int get_button_pressed(){
if(CN0flag == 1){
return(0);
}
else if(CN1flag == 1){
return(1);
}
else if(CN2flag == 1){
return(2);
}
else{
return(-1);
}
}
void listen(int level, uint16_t sleep_delay_ms, int difficulty){
Display2String("Your Turn: \r ", 1);
int counter = 0;
while(counter < level){
int time = sleep_delay_ms / difficulty;
delay_ms(time);
if(TMR2_FLAG == 1){
Display2String("Time's Up! \r", 1);
game_over_flag = 1;
break;
}
int button_pressed = get_button_pressed();
if(button_pressed == -1){ // this catches button bounces and helps with menu return
continue;
}
if(button_pressed == seq[counter]){
counter++;
}
else{
Display2String("Incorrect Sequence - Game Over \r", 1);
game_over_flag = 1;
break;
}
CN0flag = 0;
CN1flag = 0;
CN2flag = 0;
}
if(game_over_flag == 0){
Display2String("Correct Sequence - Level Complete \r", 1);
delay_ms(2000);
}
}
void play(int difficulty){
int level = 1;
game_over_flag = 0;
while((level <= max_level) && (game_over_flag == 0)){
//seq[level] = rand() % 4;
//Display2Dec(seq[level]);
//delay_ms(1500);
//rand is the random number function
//%2 limits the value to be in range [0,2).
demo(level, 500);
listen(level, 10000, difficulty);
level++;
}
if(game_over_flag == 1){
delay_ms(2000);
return;
}
else{
Display2String("YOU WIN \r", 1);
delay_ms(2000);
}
game_over_flag = 0;
return;
}
int set_difficulty(){
//unimplemented;
return 1;
}