[CLEANUP] Refactored the code

This commit is contained in:
Diogo Cordeiro 2020-02-13 16:15:23 +00:00
parent 297095a166
commit 5e8402354e
2 changed files with 234 additions and 352 deletions

View File

@ -1,153 +0,0 @@
#define BOARD_LENGTH 8
struct pair {
int x;
int y;
};
class Snake {
private:
char state;
int snake_size;
pair pos[64];
pair get_next_move(pair p);
public:
Snake() {
this->state = 'U';
this->snake_size = 4;
for (int i = snake_size - 1; i >= 0; i--) {
pair p;
p.x = 5;
p.y = i;
this->pos[i] = p;
}
}
void change_state(char m);
void eat(pair p);
void snake_to_matrix(int (*pixels)[BOARD_LENGTH]){
for(int i = 0; i < BOARD_LENGTH; i++){
for(int j = 0; j < BOARD_LENGTH; j++){
pixels[i][j] = HIGH; // CLEAR ALL PIXELS;
}
}
for(int i = 0; i < snake_size; i++) {
pair p = pos[i];
pixels[p.x][p.y] = LOW; // TURN ON PIXELS OF SNAKE
}
}
void next_move();
pair snake_head() {
return pos[0];
}
};
pair Snake::get_next_move(pair p) {
pair r;
switch (state) {
case 'U':
r.x = (p.x + BOARD_LENGTH - 1) % BOARD_LENGTH;
r.y = p.y;
break;
case 'D':
r.x = (p.x + 1) % BOARD_LENGTH;
r.y = p.y;
break;
case 'L':
r.x = p.x;
r.y = (p.y + 1) % BOARD_LENGTH;
break;
case 'R':
r.x = p.x;
r.y = (p.y + BOARD_LENGTH - 1) % BOARD_LENGTH;
break;
}
return r;
}
void Snake::next_move() {
for (int i = snake_size; i > 0; i--) {
pos[i] = pos[i - 1];
}
pos[0] = get_next_move(pos[0]);
}
void Snake::eat(pair p) { pos[snake_size++] = p; }
const int row[8] = {2, 7, 19, 5, 13, 18, 12, 16};
// 2-dimensional array of column pin numbers:
const int col[8] = {6, 11, 10, 3, 17, 4, 8, 9};
// 2-dimensional array of pixels:
int pixels[8][8];
// cursor position:
int x = 5;
int y = 5;
Snake snake;
void setup() {
// initialize the I/O pins as outputs
// iterate over the pins:
for (int thisPin = 0; thisPin < 8; thisPin++) {
// initialize the output pins:
pinMode(col[thisPin], OUTPUT);
pinMode(row[thisPin], OUTPUT);
// take the col pins (i.e. the cathodes) high to ensure that
// the LEDS are off:
digitalWrite(col[thisPin], HIGH);
}
// initialize the pixel matrix:
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
pixels[x][y] = HIGH;
}
}
}
void loop() {
// read input:
// readSensors();
snake.snake_to_matrix(pixels);
snake.next_move();
// draw the screen:
for(int i = 0; i < 100; i++)
Display();
}
void readSensors() {
// turn off the last position:
pixels[x][y] = HIGH;
// read the sensors for X and Y values:
x = 7 - map(analogRead(A0), 0, 1022, 0, 7);
y = map(analogRead(A1), 0, 1022, 0, 7);
// set the new pixel position low so that the LED will turn on
// in the next screen refresh:
pixels[x][y] = LOW;
}
void Display()
{
for(int c = 0; c<8;c++)
{
digitalWrite(col[c],LOW);//use thr column
//loop
for(int r = 0;r<8;r++)
{
digitalWrite(row[r],(pixels[r][c]+1)%2);
}
delay(1);
Clear(); //Remove empty display light
}
}
void Clear() //清空显示
{
for(int i = 0;i<8;i++)
{
digitalWrite(row[i],LOW);
digitalWrite(col[i],HIGH);
}
}

215
snake.ino
View File

@ -12,43 +12,49 @@ private:
int snake_size; int snake_size;
pair pos[64]; pair pos[64];
pair get_next_move(pair p); pair get_next_move(pair p);
pair fruitPos; pair fruit_pos;
bool fruit; bool fruit;
public: public:
Snake() { Snake() {
this->state = 'U'; this->state = 'R';
this->snake_size = 4; this->snake_size = 4;
fruit = false; fruit = false;
for (int i = snake_size - 1; i >= 0; i--) { for (int i = 0; i < snake_size ; ++i) {
pair p; pair p;
p.x = 5; p.x = 7;
p.y = i; p.y = i;
this->pos[i] = p; this->pos[0] = p;
} }
} }
void change_state(char state) { void change_state(char state) {
if (state != '\0'){ if (state != 'I') {
if((state == 'L' || state == 'R') && (this->state == 'U' || this->state=='D')) // if currently moving up or down and state change is left or right update // if currently moving up or down and state change is
// left or right, then update
if ((state == 'R' || state == 'L') &&
(this->state == 'U' || this->state == 'D')) {
this->state = state; this->state = state;
else if((state == 'U' || state == 'D') && (this->state == 'L' || this->state=='R')) // the opposite }
else if ((state == 'U' || state == 'D') &&
(this->state == 'R' || this->state == 'L')) {
this->state = state; this->state = state;
} }
}
//else ignore //else ignore
} }
void grow();
void snake_to_matrix(int (*pixels)[BOARD_LENGTH]) { void snake_to_matrix(int (*pixels)[BOARD_LENGTH]) {
for(int i = 0; i < BOARD_LENGTH; i++){ for (int i = 0; i < BOARD_LENGTH; ++i) {
for(int j = 0; j < BOARD_LENGTH; j++){ for (int j = 0; j < BOARD_LENGTH; ++j) {
pixels[i][j] = HIGH; // CLEAR ALL PIXELS; pixels[i][j] = LOW; // CLEAR ALL PIXELS;
} }
} }
for(int i = 0; i < snake_size; i++) { for (int i = 0; i < snake_size; ++i) {
pair p = pos[i]; pair p = pos[i];
pixels[p.x][p.y] = LOW; // TURN ON PIXELS OF SNAKE pixels[p.x][p.y] = HIGH; // TURN ON PIXELS OF SNAKE
}
if (fruit) {
pixels[fruit_pos.x][fruit_pos.y] = HIGH;
} }
if(fruit)
pixels[fruitPos.x][fruitPos.y] = LOW;
} }
pair snake_head() { pair snake_head() {
return pos[0]; return pos[0];
@ -56,30 +62,36 @@ public:
void generate_fruit() { void generate_fruit() {
if (!fruit) { if (!fruit) {
fruit = true; fruit = true;
fruitPos.x = random(7); // random inclusive (starts at 0)
fruitPos.y = random(7); fruit_pos.x = random(7);
fruit_pos.y = random(7);
} }
} }
void grow();
void next_move() { void next_move() {
for (int i = snake_size; i > 0; i--) { for (int i = snake_size; i > 0; --i) {
pos[i] = pos[i - 1]; pos[i] = pos[i - 1];
} }
pos[0] = get_next_move(pos[0]); pos[0] = get_next_move(pos[0]);
if (fruit) { if (fruit) {
if (pos[0].x == fruitPos.x && pos[0].y == fruitPos.y) { if (pos[0].x == fruit_pos.x && pos[0].y == fruit_pos.y) {
grow(); grow();
fruit = false; fruit = false;
} }
} }
} }
bool eats_itself() { bool eats_itself() {
for(int i = 1; i < snake_size; i++) for (int i = 1; i < snake_size; ++i) {
if( pos[0].x == pos[i].x && pos[0].y == pos[i].y ) return true; if (pos[0].x == pos[i].x && pos[0].y == pos[i].y) {
return true;
}
}
return false; return false;
} }
}; };
pair Snake::get_next_move(pair p) { pair Snake::get_next_move(pair p)
{
pair r(p); pair r(p);
switch (state) { switch (state) {
case 'U': case 'U':
@ -90,11 +102,11 @@ pair Snake::get_next_move(pair p) {
r.x = (p.x + 1) % BOARD_LENGTH; r.x = (p.x + 1) % BOARD_LENGTH;
r.y = p.y; r.y = p.y;
break; break;
case 'L': case 'R':
r.x = p.x; r.x = p.x;
r.y = (p.y + 1) % BOARD_LENGTH; r.y = (p.y + 1) % BOARD_LENGTH;
break; break;
case 'R': case 'L':
r.x = p.x; r.x = p.x;
r.y = (p.y + BOARD_LENGTH - 1) % BOARD_LENGTH; r.y = (p.y + BOARD_LENGTH - 1) % BOARD_LENGTH;
break; break;
@ -102,7 +114,8 @@ pair Snake::get_next_move(pair p) {
return r; return r;
} }
void Snake::grow() { void Snake::grow()
{
if (snake_size < SNAKE_MAX_SIZE) { if (snake_size < SNAKE_MAX_SIZE) {
pos[snake_size] = pos[snake_size - 1]; pos[snake_size] = pos[snake_size - 1];
++snake_size; ++snake_size;
@ -110,8 +123,6 @@ void Snake::grow() {
} }
const int row[8] = { 2, 7, 19, 5, 13, 18, 12, 16 }; const int row[8] = { 2, 7, 19, 5, 13, 18, 12, 16 };
// 2-dimensional array of column pin numbers:
const int col[8] = { 6, 11, 10, 3, 17, 4, 8, 9 }; const int col[8] = { 6, 11, 10, 3, 17, 4, 8, 9 };
// 2-dimensional array of pixels: // 2-dimensional array of pixels:
@ -122,109 +133,133 @@ int x = 5;
int y = 5; int y = 5;
Snake snake; Snake snake;
void setup() {
// A BIG SAD CROSS
int dead[8][8] = {
{HIGH, LOW, LOW, LOW, LOW, LOW, LOW, HIGH},
{LOW, HIGH, LOW, LOW, LOW, LOW, HIGH, LOW},
{LOW, LOW, HIGH, LOW, LOW, HIGH, LOW, LOW},
{LOW, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW},
{LOW, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW},
{LOW, LOW, HIGH, LOW, LOW, HIGH, LOW, LOW},
{LOW, HIGH, LOW, LOW, LOW, LOW, HIGH, LOW},
{HIGH, LOW, LOW, LOW, LOW, LOW, LOW, HIGH},
};
/**
* The setup routine runs once when you press reset.
*/
void setup()
{
Serial.begin(9600); // 9600 bps Serial.begin(9600); // 9600 bps
// initialize the I/O pins as outputs // initialize the I/O pins as outputs
// iterate over the pins: // iterate over the pins:
for (int thisPin = 0; thisPin < 8; thisPin++) { for (int this_pin = 0; this_pin < 8; ++this_pin) {
// initialize the output pins: // initialize the output pins:
pinMode(col[thisPin], OUTPUT); pinMode(col[this_pin], OUTPUT);
pinMode(row[thisPin], OUTPUT); pinMode(row[this_pin], OUTPUT);
// take the col pins (i.e. the cathodes) high to ensure that // take the col pins (i.e. the cathodes) high to ensure that
// the LEDS are off: // the LEDS are off:
digitalWrite(col[thisPin], HIGH); digitalWrite(col[this_pin], HIGH);
} }
// initialize the pixel matrix: // initialize the pixel matrix:
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; ++y) {
pixels[x][y] = HIGH; pixels[x][y] = HIGH;
} }
} }
randomSeed(analogRead(0)); randomSeed(analogRead(0));
} }
void loop() { /**
if(random(20) == 0) snake.generate_fruit(); * The loop routine runs over and over again forever.
snake.snake_to_matrix(pixels); */
void loop()
{
snake.generate_fruit();
snake.next_move(); snake.next_move();
snake.snake_to_matrix(pixels);
for (int i = 0; i < 30; ++i) {
display_pixels();
snake.change_state(read_sensors());
}
// draw the screen: // draw the screen:
if (snake.eats_itself()) { if (snake.eats_itself()) {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
for(int j = 0; j < 100; j++) for (int j = 0; j < 100; ++j) {
Display(); display_pixels();
}
delay(500); delay(500);
} }
int dead[8][8] = { for (int i = 0; i < 8; ++i) {
{0,1,1,1,1,1,1,0}, for (int j = 0; j < 8; ++j) {
{1,0,1,1,1,1,0,1},
{1,1,0,1,1,0,1,1},
{1,1,1,0,0,1,1,1},
{1,1,1,0,0,1,1,1},
{1,1,0,1,1,0,1,1},
{1,0,1,1,1,1,0,1},
{0,1,1,1,1,1,1,0},
};
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
pixels[i][j] = dead[i][j]; pixels[i][j] = dead[i][j];
while(true) Display();
} }
for(int i = 0; i < 30; i++) { }
Display(); while (true) {
snake.change_state(read_sensors()); display_pixels();
}
}
} }
} /**
* Prints the pixels array in the matrix
void readSensors() { */
// turn off the last position: void display_pixels()
pixels[x][y] = HIGH;
// read the sensors for X and Y values:
x = 7 - map(analogRead(A0), 0, 1022, 0, 7);
y = map(analogRead(A1), 0, 1022, 0, 7);
// set the new pixel position low so that the LED will turn on
// in the next screen refresh:
pixels[x][y] = LOW;
}
void Display()
{ {
for(int c = 0; c<8;c++) for (int c = 0; c < 8; ++c) {
{ digitalWrite(col[c], LOW);
digitalWrite(col[c],LOW);//use thr column for (int r = 0; r < 8; ++r) {
//loop digitalWrite(row[r], pixels[r][c]);
for(int r = 0;r<8;r++)
{
digitalWrite(row[r],(pixels[r][c]+1)%2);
} }
delay(1); delay(1);
Clear(); //Remove empty display light display_clear();
} }
} }
void Clear() //清空显示 /**
{ * Remove empty display light
for(int i = 0;i<8;i++) *
*/
void display_clear()
{ {
for (int i = 0; i < 8; ++i) {
digitalWrite(row[i], LOW); digitalWrite(row[i], LOW);
digitalWrite(col[i], HIGH); digitalWrite(col[i], HIGH);
} }
} }
char read_sensors() { /**
* Returns the current joystick input with some leeway
* I means Iddle
* L means Left
* R means Right
* U means Up
* D means Down
*/
char read_sensors()
{
//int clicked = digitalRead(z); // if 0, then clicked //int clicked = digitalRead(z); // if 0, then clicked
//if(!clicked) return 'C'; //if(!clicked) return 'C';
x = map(analogRead(A0), 0, 1023, 0, 10) - 5; x = map(analogRead(A0), 0, 1023, 0, 10) - 5;
y = map(analogRead(A1), 0, 1023, 0, 10) - 5; y = map(analogRead(A1), 0, 1023, 0, 10) - 5;
if(abs(x) <= 1 && abs(y) <= 1) { // test if joystick is the middle with some leeway // test if joystick is the middle with some leeway
return '\0'; if (abs(x) <= 3 && abs(y) <= 3) {
}else if(abs(x) > abs(y) ) { // test if move in x axis is > than in y axis return 'I';
if(x > 1) return 'L'; // test if move in x axis is > than in y axis
else return 'R'; } else if (abs(x) > abs(y)) {
if (x > 1) {
return 'R';
} else { } else {
if(y > 1) return 'U'; return 'L';
else return 'D';
} }
return '\0'; } else {
if (y > 1) {
return 'U';
} else {
return 'D';
}
}
return 'I';
} }