ZIYI CHEN ml17z4c 201214999
Dependencies: mbed
Game/Game.cpp@9:a8b2086a46e5, 2019-05-08 (annotated)
- Committer:
- ziyi11
- Date:
- Wed May 08 20:48:33 2019 +0000
- Revision:
- 9:a8b2086a46e5
- Parent:
- 8:52e0506e98b8
try1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ziyi11 | 8:52e0506e98b8 | 1 | #include "Game.h" |
ziyi11 | 8:52e0506e98b8 | 2 | |
ziyi11 | 8:52e0506e98b8 | 3 | |
ziyi11 | 9:a8b2086a46e5 | 4 | Game::Game() |
ziyi11 | 8:52e0506e98b8 | 5 | { |
ziyi11 | 8:52e0506e98b8 | 6 | |
ziyi11 | 8:52e0506e98b8 | 7 | } |
ziyi11 | 8:52e0506e98b8 | 8 | |
ziyi11 | 9:a8b2086a46e5 | 9 | Game::~Game() |
ziyi11 | 8:52e0506e98b8 | 10 | { |
ziyi11 | 8:52e0506e98b8 | 11 | |
ziyi11 | 8:52e0506e98b8 | 12 | } |
ziyi11 | 8:52e0506e98b8 | 13 | |
ziyi11 | 9:a8b2086a46e5 | 14 | void Game::init(int x, int y) |
ziyi11 | 8:52e0506e98b8 | 15 | { |
ziyi11 | 9:a8b2086a46e5 | 16 | memset(_x, 0, sizeof(_x)); |
ziyi11 | 9:a8b2086a46e5 | 17 | memset(_y, 0, sizeof(_y)); |
ziyi11 | 9:a8b2086a46e5 | 18 | |
ziyi11 | 9:a8b2086a46e5 | 19 | _length = 5; |
ziyi11 | 8:52e0506e98b8 | 20 | |
ziyi11 | 9:a8b2086a46e5 | 21 | Previous_direction = 'N'; |
ziyi11 | 8:52e0506e98b8 | 22 | |
ziyi11 | 9:a8b2086a46e5 | 23 | for (int i = 0; i < _length; i++) { |
ziyi11 | 9:a8b2086a46e5 | 24 | _x[i] = x; |
ziyi11 | 9:a8b2086a46e5 | 25 | _y[i] = y + i; |
ziyi11 | 8:52e0506e98b8 | 26 | } |
ziyi11 | 9:a8b2086a46e5 | 27 | |
ziyi11 | 8:52e0506e98b8 | 28 | } |
ziyi11 | 8:52e0506e98b8 | 29 | |
ziyi11 | 9:a8b2086a46e5 | 30 | void Game::update(Direction d) |
ziyi11 | 8:52e0506e98b8 | 31 | { |
ziyi11 | 9:a8b2086a46e5 | 32 | |
ziyi11 | 8:52e0506e98b8 | 33 | for (int i = _length-1; i >= 1; i--) { |
ziyi11 | 8:52e0506e98b8 | 34 | _x[i] = _x[i-1]; |
ziyi11 | 8:52e0506e98b8 | 35 | _y[i] = _y[i-1]; |
ziyi11 | 8:52e0506e98b8 | 36 | } |
ziyi11 | 8:52e0506e98b8 | 37 | |
ziyi11 | 9:a8b2086a46e5 | 38 | |
ziyi11 | 9:a8b2086a46e5 | 39 | switch(d) { |
ziyi11 | 8:52e0506e98b8 | 40 | case N: |
ziyi11 | 9:a8b2086a46e5 | 41 | if(Previous_direction !='S') { |
ziyi11 | 9:a8b2086a46e5 | 42 | _y[0] = _y[0] - 1; |
ziyi11 | 9:a8b2086a46e5 | 43 | Previous_direction = 'N'; |
ziyi11 | 9:a8b2086a46e5 | 44 | } |
ziyi11 | 9:a8b2086a46e5 | 45 | break; |
ziyi11 | 9:a8b2086a46e5 | 46 | |
ziyi11 | 9:a8b2086a46e5 | 47 | case S: |
ziyi11 | 9:a8b2086a46e5 | 48 | if(Previous_direction !='N') { |
ziyi11 | 9:a8b2086a46e5 | 49 | _y[0] = _y[0] + 1; |
ziyi11 | 9:a8b2086a46e5 | 50 | Previous_direction = 'S'; |
ziyi11 | 9:a8b2086a46e5 | 51 | } |
ziyi11 | 9:a8b2086a46e5 | 52 | break; |
ziyi11 | 9:a8b2086a46e5 | 53 | |
ziyi11 | 9:a8b2086a46e5 | 54 | case E: |
ziyi11 | 9:a8b2086a46e5 | 55 | if(Previous_direction != 'W') { |
ziyi11 | 9:a8b2086a46e5 | 56 | _x[0] = _x[0] + 1; |
ziyi11 | 9:a8b2086a46e5 | 57 | Previous_direction = 'E'; |
ziyi11 | 8:52e0506e98b8 | 58 | } |
ziyi11 | 8:52e0506e98b8 | 59 | break; |
ziyi11 | 9:a8b2086a46e5 | 60 | case W: |
ziyi11 | 9:a8b2086a46e5 | 61 | if(Previous_direction != 'E') { |
ziyi11 | 9:a8b2086a46e5 | 62 | _x[0] = _x[0]- 1; |
ziyi11 | 9:a8b2086a46e5 | 63 | Previous_direction = 'W'; |
ziyi11 | 9:a8b2086a46e5 | 64 | } |
ziyi11 | 8:52e0506e98b8 | 65 | break; |
ziyi11 | 9:a8b2086a46e5 | 66 | } |
ziyi11 | 9:a8b2086a46e5 | 67 | if (Previous_direction == 'N') { |
ziyi11 | 9:a8b2086a46e5 | 68 | _y[0] = _y[0] - 1; |
ziyi11 | 9:a8b2086a46e5 | 69 | } else if (Previous_direction == 'S') { |
ziyi11 | 9:a8b2086a46e5 | 70 | _y[0] = _y[0] + 1; |
ziyi11 | 9:a8b2086a46e5 | 71 | } else if (Previous_direction == 'E') { |
ziyi11 | 9:a8b2086a46e5 | 72 | _x[0] = _x[0] + 1; |
ziyi11 | 9:a8b2086a46e5 | 73 | } else if (Previous_direction == 'W') { |
ziyi11 | 9:a8b2086a46e5 | 74 | _x[0] = _x[0] - 1; |
ziyi11 | 9:a8b2086a46e5 | 75 | } |
ziyi11 | 8:52e0506e98b8 | 76 | } |
ziyi11 | 8:52e0506e98b8 | 77 | |
ziyi11 | 9:a8b2086a46e5 | 78 | int Game::getLength() |
ziyi11 | 8:52e0506e98b8 | 79 | { |
ziyi11 | 8:52e0506e98b8 | 80 | return _length; |
ziyi11 | 8:52e0506e98b8 | 81 | } |
ziyi11 | 8:52e0506e98b8 | 82 | |
ziyi11 | 9:a8b2086a46e5 | 83 | void Game::grow() |
ziyi11 | 8:52e0506e98b8 | 84 | { |
ziyi11 | 8:52e0506e98b8 | 85 | _length =_length + 1; |
ziyi11 | 8:52e0506e98b8 | 86 | printf("length + 1\n"); |
ziyi11 | 8:52e0506e98b8 | 87 | } |
ziyi11 | 9:a8b2086a46e5 | 88 | |
ziyi11 | 9:a8b2086a46e5 | 89 | int Game::xcoordinate(int now) |
ziyi11 | 8:52e0506e98b8 | 90 | { |
ziyi11 | 9:a8b2086a46e5 | 91 | return _x[now]; |
ziyi11 | 8:52e0506e98b8 | 92 | } |
ziyi11 | 8:52e0506e98b8 | 93 | |
ziyi11 | 9:a8b2086a46e5 | 94 | int Game::ycoordinate(int now) |
ziyi11 | 8:52e0506e98b8 | 95 | { |
ziyi11 | 9:a8b2086a46e5 | 96 | return _y[now]; |
ziyi11 | 8:52e0506e98b8 | 97 | } |
ziyi11 | 8:52e0506e98b8 | 98 | |
ziyi11 | 8:52e0506e98b8 | 99 | |
ziyi11 | 8:52e0506e98b8 | 100 |