ELEC2645 (2018/19) / Mbed 2 deprecated ml17z4c_attempt2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Game.cpp Source File

Game.cpp

00001 #include "Game.h"
00002 
00003 
00004 Game::Game()
00005 {
00006 
00007 }
00008 
00009 Game::~Game()
00010 {
00011 
00012 }
00013 
00014 void Game::init(int x, int y)
00015 {
00016     memset(_x, 0, sizeof(_x));
00017     memset(_y, 0, sizeof(_y));
00018 
00019     _length = 5;
00020 
00021     Previous_direction = 'N';
00022 
00023     for (int i = 0; i < _length; i++) {
00024         _x[i] = x;
00025         _y[i] = y + i;
00026     }
00027 
00028 }
00029 
00030 void Game::update(Direction d)
00031 {
00032 
00033     for (int i = _length-1; i >= 1; i--) {
00034         _x[i] =  _x[i-1];
00035         _y[i] =  _y[i-1];
00036     }
00037 
00038 
00039     switch(d) {
00040         case N:
00041             if(Previous_direction !='S') {
00042                 _y[0] = _y[0] - 1;
00043                 Previous_direction = 'N';
00044             }
00045             break;
00046 
00047         case S:
00048             if(Previous_direction !='N') {
00049                 _y[0] = _y[0] + 1;
00050                 Previous_direction = 'S';
00051             }
00052             break;
00053 
00054         case E:
00055             if(Previous_direction != 'W') {
00056                 _x[0] = _x[0] + 1;
00057                 Previous_direction = 'E';
00058             }
00059             break;
00060         case W:
00061             if(Previous_direction != 'E') {
00062                 _x[0] = _x[0]- 1;
00063                 Previous_direction = 'W';
00064             }
00065             break;
00066     }
00067     if (Previous_direction == 'N') {      
00068         _y[0] = _y[0] - 1;                    
00069     } else if (Previous_direction == 'S') {
00070         _y[0] = _y[0] + 1;
00071     } else if (Previous_direction == 'E') {
00072         _x[0] = _x[0] + 1;
00073     }  else if (Previous_direction == 'W') {
00074         _x[0] = _x[0] - 1;
00075     }
00076 }
00077 
00078 int Game::getLength()
00079 {
00080     return _length;
00081 }
00082 
00083 void Game::grow()
00084 {
00085     _length =_length + 1;  
00086     printf("length + 1\n");
00087 }
00088 
00089 int Game::xcoordinate(int now)
00090 {
00091     return _x[now];
00092 }
00093 
00094 int Game::ycoordinate(int now)
00095 {
00096     return _y[now];
00097 }
00098 
00099 
00100