ELEC2645 (2017/18) / Mbed OS el16ajm

Snek/Snek.cpp

Committer:
Andrew_M
Date:
2018-04-29
Revision:
5:a3a9e0417e04
Parent:
4:6353f829c56c
Child:
7:c1e0593bfc99

File content as of revision 5:a3a9e0417e04:

#include "Snek.h"

// nothing doing in the constructor and destructor
Snek::Snek()
{

}

Snek::~Snek()
{

}

void Snek::init(int x, int y)
{
    //Inital values for variables
    _length = 3;
    
    _oldDirection = 'N';

    for (int i = 0; i < _length; i++) {
        _x[i] = x;
        _y[i] = y + i;
    }
}

void Snek::update(Direction d)
{
    for (int i = _length-1; i >= 1; i--) {
            _x[i] =  _x[i-1];
            _y[i] =  _y[i-1];
    }
    
    if (d == N && _oldDirection != 'S') { //checks the current direction so the snake cannot go back on itself
        _y[0] -= 1;
        _oldDirection = 'N';
    } else if (d == S && _oldDirection != 'N') {
        _y[0] += 1;
        _oldDirection = 'S';
    } else if (d == E && _oldDirection != 'W') {
        _x[0] += 1;
        _oldDirection = 'E';
    }   else if (d == W && _oldDirection != 'E') {
        _x[0] -= 1;
        _oldDirection = 'W';
    } else {
        if (_oldDirection == 'N') {
            _y[0] -= 1;
        } else if (_oldDirection == 'S') {
            _y[0] += 1;
        } else if (_oldDirection == 'E') {
            _x[0] += 1;
        }  else if (_oldDirection == 'W') {
            _x[0] -= 1;
        }
    }    
}

int Snek::getX(int ref)
{
    return _x[ref];
}

int Snek::getY(int ref)
{
    return _y[ref];
}

int Snek::getLength()
{
    return _length;
}

void Snek::grow()
{
    _length+=1;
}