Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: Snake/Snake.cpp
- Revision:
- 10:62d8cb7742c3
- Parent:
- 9:561e5681b7a6
- Child:
- 11:e260c17a0489
diff -r 561e5681b7a6 -r 62d8cb7742c3 Snake/Snake.cpp
--- a/Snake/Snake.cpp Tue Apr 03 12:24:18 2018 +0000
+++ b/Snake/Snake.cpp Wed Apr 04 12:25:51 2018 +0000
@@ -11,54 +11,271 @@
}
-void Snake::update(Direction d)
+void Snake::init()
+{
+
+ _x = 41;
+ _y = 23;
+
+ _next = W;
+
+}
+
+void Snake::update(Direction in, Direction cur)
{
- Vector2D values = get_snake_position();
+ set_snake_direction(in, cur);
+ Direction next = get_snake_direction();
+ set_snake_position(next);
+ Vector2D pos = get_snake_position();
- _x = values.x;
- _y = values.y;
-
+}
+
+void Snake::draw(N5110 &lcd)
+{
+
+ lcd.setPixel(_x, _y, true);
+
}
-void Snake::set_snake_direction(Direction input)
+void Snake::set_snake_direction(Direction input, Direction current)
{
- StateDirection sd;
-
- int state = 1;
-
- StateDirection fsm[4] = {
+
+
+
+ switch(current) {
+
+ case CENTRE:
+
+ switch(input) {
+
+ case CENTRE:
+
+ _next = current;
+
+ break;
+
+ case N:
+
+ _next = input;
+
+ break;
+
+ case E:
+
+ _next = input;
+
+ break;
+
+ case S:
+
+ _next = input;
+
+ break;
+
+ case W:
+
+ _next = input;
+
+ break;
+
+ default:
+
+ _next = current;
+ error("Invalid state");
+
+ break;
+
+ }
+
+ case N:
+
+ switch(input) {
+
+ case CENTRE:
+
+ _next = current;
+
+ break;
+
+ case N:
+
+ _next = input;
+
+ break;
+
+ case E:
+
+ _next = input;
+
+ break;
+
+ case S:
+
+ _next = current;
+
+ break;
+
+ case W:
+
+ _next = input;
+
+ break;
+
+ default:
+
+ _next = current;
+ error("Invalid state");
+
+ break;
+
+ }
+ case E:
- {N,{N,E,N,W}},
- {E,{N,E,S,E}},
- {S,{S,E,S,W}},
- {W,{N,W,S,W}}
-
-};
-
- sd.output = fsm[state].output; // set ouput depending on current state
- state = fsm[state].nextState[input]; // read input and update curent state
- _direction = sd.output;
- //_direction.nextState[state] = input;
-
+ switch(input) {
+
+ case CENTRE:
+
+ _next = current;
+
+ break;
+
+ case N:
+
+ _next = input;
+
+ break;
+
+ case E:
+
+ _next = current;
+
+ break;
+
+ case S:
+
+ _next = current;
+
+ break;
+
+ case W:
+
+ _next = input;
+
+ break;
+
+ default:
+
+ _next = current;
+ error("Invalid state");
+
+ break;
+
+ }
+ case S:
+ switch(input) {
+
+ case CENTRE:
+
+ _next = current;
+
+ break;
+
+ case N:
+
+ _next = current;
+
+ break;
+
+ case E:
+
+ _next = input;
+
+ break;
+
+ case S:
+
+ _next = current;
+
+ break;
+
+ case W:
+
+ _next = input;
+
+ break;
+
+ default:
+
+ _next = current;
+ error("Invalid state");
+
+ break;
+
+ }
+ case W:
+
+ switch(input) {
+
+ case CENTRE:
+
+ _next = current;
+
+ break;
+
+ case N:
+
+ _next = input;
+
+ break;
+
+ case E:
+
+ _next = current;
+
+ break;
+
+ case S:
+
+ _next = input;
+
+ break;
+
+ case W:
+
+ _next = current;
+
+ break;
+
+ default:
+
+ _next = current;
+ error("Invalid state");
+
+ break;
+
+ }
+ }
+
+
}
Direction Snake::get_snake_direction()
{
- Direction d = _direction;
+ Direction d = _next;
return d;
}
-void Snake::set_snake_position()
+void Snake::set_snake_position(Direction next)
{
- Direction v = get_snake_direction();
+ next = get_snake_direction();
Vector2D p;
- switch(v) {
+ switch(next) {
case(N):
@@ -108,226 +325,25 @@
return pos;
}
-/*
-void Snake::set_next_dir(Direction next)
-{
-
-}
-
-Vector2D Snake::move_snake()
-{
- Direction pre;
- Direction next;
- Vector2D i;
-
- switch(pre) {
-
- case(N):
-
- switch(next) {
-
- case(N):
-
- i.x = i.x;
- ++i.y;
-
- break;
-
- case(E):
-
- ++i.x;
- i.y = i.y;
-
- break;
-
- case(S):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- case(W):
-
- --i.x;
- i.y = i.y;
-
- break;
-
- case(CENTRE):
-
- i.x = i.x;
- ++i.y;
-
- break;
-
- default:
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- }
-
- break;
-
- case(E):
-
- switch(next) {
-
- case(N):
-
- i.x = i.x;
- ++i.y;
-
- break;
-
- case(E):
-
- ++i.x;
- i.y = i.y;
-
- break;
-
- case(S):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- case(W):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
+
+ // FSM Attempt
+
+ /*
+ int state = N;
+
+ StateDirection fsm[4] = {
- case(CENTRE):
-
- ++i.x;
- i.y = i.y;
-
- break;
-
- default:
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- }
-
- break;
-
- case(S):
-
- switch(next) {
-
- case(N):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- case(E):
-
- ++i.x;
- i.y = i.y;
-
- break;
-
- case(S):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- case(W):
-
- --i.x;
- i.y = i.y;
-
- break;
-
- case(CENTRE):
-
- i.x = i.x;
- --i.y;
-
- break;
-
- default:
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- }
-
- break;
-
- case(W):
-
- switch(next) {
-
- case(N):
-
- i.x = i.x;
- ++i.y;
-
- break;
-
- case(E):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- case(S):
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- case(W):
-
- --i.x;
- i.y = i.y;
-
- break;
-
- case(CENTRE):
-
- --i.x;
- i.y = i.y;
-
- break;
-
- default:
-
- i.x = i.x;
- i.y = i.y;
-
- break;
-
- }
-
- break;
-
- }
+ {N,{N,E,N,W}},
+ {E,{N,E,S,E}},
+ {S,{S,E,S,W}},
+ {W,{N,W,S,W}}
+
+};
- return i;
-
- }*/
+ _direction = fsm[state].output; // set ouput depending on current state
+ state = fsm[state].nextState[input]; // read input and update curent state
+ */
\ No newline at end of file