forked from MrRonne/lab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.ino
176 lines (160 loc) · 3.62 KB
/
lab2.ino
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
#include <LedControl.h>
#include "point.h"
const int displaysCount = 1;
const int dataPin = 12;
const int clkPin = 10;
const int csPin = 11;
LedControl lc = LedControl(dataPin, clkPin, csPin, displaysCount);
/*
void setup()
{
lc.shutdown(0, false);
lc.setIntensity(0, 16);
lc.clearDisplay(0);
}
void loop() {
lc.setLed(0, 3, 4, true);
delay(500);
lc.setLed(0, 3, 4, false);
delay(500);
}
*/
Point keyUp = Point(0, 1);
Point keyDown = Point(0, -1);
Point keyRight = Point(1, 0);
Point keyLeft = Point(-1, 0);
Point keyNone = Point(0, 0);
Point keyFixation = Point(100, 100);
Point keyGo = Point(-1, -1);
const byte rowAmount = 4;
const byte colAmount = 4;
bool lifeMatrix[8][8] ={
{false, false, false, false},
{false, false, false, false},
{false, false, false, false},
{false, false, false, false}
};
bool lifeMatrixTwo[8][8] ={
{false, false, false, false},
{false, false, false, false},
{false, false, false, false},
{false, false, false, false}
};
Point keyMatrix[rowAmount][colAmount] = {
{keyNone, keyUp, keyNone, keyNone},
{keyLeft, keyFixation, keyRight, keyNone},
{keyNone, keyDown, keyNone, keyNone},
{keyNone, keyNone, keyNone, keyGo}
};
static bool keyDownMatrix[rowAmount][colAmount];
byte rowPins[rowAmount] = { 5, 4, 3, 2 };
byte colPins[colAmount] = { 6, 7, 8, 9 };
void setup()
{
keyFixation.fixation = true;
lc.shutdown(0, false);
lc.setIntensity(0, 16);
lc.clearDisplay(0);
for (int i = 0; i < rowAmount; i++) {
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], HIGH);
}
for (int i = 0; i < colAmount; i++) {
pinMode(colPins[i], INPUT);
digitalWrite(colPins[i], HIGH);
}
Serial.begin(115200);
}
Point curent = Point(0,0);
void loop()
{
lc.setLed(0, curent.X, curent.Y, true);
Point key = getKey();
if(key == keyFixation)
{
lifeMatrix[curent.X][ curent.Y] = true;
return;
}
if(key == keyGo)
{
while(true)
{
PlayGame();
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
lc.setLed(0, i, j, lifeMatrix[i][j]);
}
}
delay(2000);
}
}
if (key != keyNone) {
if (!lifeMatrix[curent.X] [curent.Y])
lc.setLed(0, curent.X, curent.Y, false);
curent = curent.offset(key);
}
}
void PlayGame(){
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
int sum = 0;
for(int k = -1; k < 2; k++)
for(int g = -1; g < 2; g++)
{
if(k == 0 && g == 0)
continue;
int newX = (i + k) % 8;
int newY = (j+ g) % 8;
if (newX < 0)
{
newX += 8;
}
if (newY < 0)
{
newY += 8;
}
if (lifeMatrix[newX][newY])
sum += 1;
}
if (lifeMatrix[i][j])
lifeMatrixTwo[i][j] = sum == 2 || sum == 3;
else
lifeMatrixTwo[i][j] = sum == 3;
}
}
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
lifeMatrix[i][j] = lifeMatrixTwo[i][j];
}
}
}
Point getKey()
{
Point result = keyNone;
for (int i = 0; i < rowAmount; i++) {
for (int j = 0; j < colAmount; j++) {
bool isDown = isKeyDown(i, j);
if (!keyDownMatrix[i][j] && isDown) {
result = keyMatrix[i][j];
}
keyDownMatrix[i][j] = isDown;
}
}
return result;
}
bool isKeyDown(int i, int j)
{
bool result = false;
digitalWrite(rowPins[i], LOW);
if (digitalRead(colPins[j]) == LOW) {
result = true;
}
digitalWrite(rowPins[i], HIGH);
return result;
}