-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
167 lines (130 loc) · 3.02 KB
/
game.c
File metadata and controls
167 lines (130 loc) · 3.02 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
#include <MK64F12.h>
#include "utils.h"
#include "score.h"
#include "test.h"
#include "tools.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// globals
int ttime = 420;
int bttnPrssd;
int RP = 0; // stop double input
int LP = 0; // " "
int score = 0;
int random = 0;
// TODO: create a large random number
void random_num(void)
{
srand(ttime);
random = rand();
//random = 1111111111; // test
}
// LED sequence to follow, n length
void LED_sequence(int n)
{
int next;
for (int i=0; i<n; i++) {
half_delay();
// use squares to not have a pattern
next = ( random / ((i+1)^2)) % 2; // binary digit at pos n
if (next == 0) { // if 0, right
red();
}
else { // if 1, left
blue();
}
}
}
// Check if correct sequence entered
int user_sequence(int n)
{
int next;
// for the round number
for (int i=0; i<n; i++) {
// same as LED_sequence to get correct response
next = ( random / ((i+1)^2)) % 2; // binary digit at pos n
// wait for a button to be pressed
while ( bttnPrssd == 0 ){}
bttnPrssd -= 1; // make so button pressed equivelent to corresponding pattern
// wrong button choice, round over, game over, failed!!!
if ( next != bttnPrssd ) {
incorrect();
return 1;
}
// correct button choice
correct();
q_delay();
bttnPrssd = 0;
RP = 0;
LP = 0;
}
// completed round successfully
next_round();
RP = 0;
LP = 0;
return 0;
}
int main (void)
{
// init IO
LED_Initialize();
bttn_Initialize();
// set globals
random_num();
RP = 1; // not change bttnPrssd when 1
LP = 1; // " "
// keep track of level & if the user is has/hasn't lost
int play = 0;
int round = 0;
// indicate start of game
game_start();
half_delay();
// go until user fails a round
while (play == 0) {
round += 1; // next round
half_delay();
// pattern for user to follow
LED_sequence(round);
// indicate to user to start
round_start();
// let buttons work
RP = 0;
LP = 0;
// 0 if user succedded, 1 if failed
play = user_sequence(round);
// make buttons not work
RP = 1;
LP = 1;
bttnPrssd = 0;
}
// give user score
relay_score(round-1);
// tell user game over
game_start();
while (1) ;
return 0;
}
//right button
void PORTC_IRQHandler (void)
{
if ( RP == 0) { // help stop double bouncing
RP = 1; // " "
bttnPrssd = 1; // update that button pressed
}
//LEDRed_Toggle // Used for testing buttons
q_delay(); // help stop double bouncing
PORTC->PCR[6] = 0x10A0100; /* Connect Switch as interrupt */
}
// left button
void PORTA_IRQHandler (void)
{
if ( LP == 0) { //help stop double bouncing
LP = 1; // " "
bttnPrssd = 2; // update that button pressed
}
//LEDBlue_Toggle(); // Used for testing buttons
q_delay(); // help stop double bouncing
PORTA->PCR[4] = 0x10A0100; /* Connect Switch as interrupt */
}