ZIYI CHEN ml17z4c 201214999
Dependencies: mbed
Diff: Game/Game.cpp
- Revision:
- 13:08bdb4cffacd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Game/Game.cpp Thu May 09 14:36:47 2019 +0000 @@ -0,0 +1,100 @@ +#include "Game.h" + + +Game::Game() +{ + +} + +Game::~Game() +{ + +} + +void Game::init(int x, int y) +{ + memset(_x, 0, sizeof(_x)); + memset(_y, 0, sizeof(_y)); + + _length = 5; + + Previous_direction = 'N'; + + for (int i = 0; i < _length; i++) { + _x[i] = x; + _y[i] = y + i; + } + +} + +void Game::update(Direction d) +{ + + for (int i = _length-1; i >= 1; i--) { + _x[i] = _x[i-1]; + _y[i] = _y[i-1]; + } + + + switch(d) { + case N: + if(Previous_direction !='S') { + _y[0] = _y[0] - 1; + Previous_direction = 'N'; + } + break; + + case S: + if(Previous_direction !='N') { + _y[0] = _y[0] + 1; + Previous_direction = 'S'; + } + break; + + case E: + if(Previous_direction != 'W') { + _x[0] = _x[0] + 1; + Previous_direction = 'E'; + } + break; + case W: + if(Previous_direction != 'E') { + _x[0] = _x[0]- 1; + Previous_direction = 'W'; + } + break; + } + if (Previous_direction == 'N') { + _y[0] = _y[0] - 1; + } else if (Previous_direction == 'S') { + _y[0] = _y[0] + 1; + } else if (Previous_direction == 'E') { + _x[0] = _x[0] + 1; + } else if (Previous_direction == 'W') { + _x[0] = _x[0] - 1; + } +} + +int Game::getLength() +{ + return _length; +} + +void Game::grow() +{ + _length =_length + 1; + printf("length + 1\n"); +} + +int Game::xcoordinate(int now) +{ + return _x[now]; +} + +int Game::ycoordinate(int now) +{ + return _y[now]; +} + + +