ZIYI CHEN ml17z4c 201214999

Dependencies:   mbed

Game/Game.cpp

Committer:
ziyi11
Date:
2019-05-08
Revision:
9:a8b2086a46e5
Parent:
8:52e0506e98b8

File content as of revision 9:a8b2086a46e5:

#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];
}