ELEC2645 (2017/18) / Mbed 2 deprecated ll13jrm

Dependencies:   mbed

Snake/Snake.cpp

Committer:
JRM1986
Date:
2018-04-03
Revision:
9:561e5681b7a6
Parent:
2:ea90cec2489a
Child:
10:62d8cb7742c3

File content as of revision 9:561e5681b7a6:

#include "Snake.h"


Snake::Snake()
{

}

Snake::~Snake()
{

}

void Snake::update(Direction d) 
{
    
    Vector2D values = get_snake_position();
    
    _x = values.x;
    _y = values.y;
       
}

void Snake::set_snake_direction(Direction input)
{
    StateDirection sd;
     
    int state = 1;
     
    StateDirection fsm[4] = {
        
    {N,{N,E,N,W}},  
    {E,{N,E,S,E}},  
    {S,{S,E,S,W}}, 
    {W,{N,W,S,W}} 
     
};

    sd.output = fsm[state].output;  // set ouput depending on current state
    state = fsm[state].nextState[input];  // read input and update curent state
    _direction = sd.output;
    //_direction.nextState[state] = input;

        
}

Direction Snake::get_snake_direction()
{
    
    Direction d = _direction;
    
    return d;
    
}

void Snake::set_snake_position()
{
    Direction v = get_snake_direction();
    Vector2D p;
    
    switch(v) {
        
            case(N):
        
            p.x = p.x;
            --p.y;
        
            break;
        
            case(E):
        
            ++p.x;
            p.y = p.y;
        
            break;
        
            case(S):
        
            p.x = p.x;
            ++p.y;
        
            break;
        
            case(W):
        
            --p.x;
            p.y = p.y;
        
            break;
            
            default:
            
            p.x = p.x;
            p.y = p.y;
            
            break;
            
            }
            
            _x = p.x;
            _y = p.y;
    
}

Vector2D Snake::get_snake_position()
{
    Vector2D pos = {_x, _y};
    
    return pos; 
}
/*

void Snake::set_next_dir(Direction next)
{
       
}
    
Vector2D Snake::move_snake()
{
    Direction pre; 
    Direction next;
    Vector2D i;
    
    switch(pre) {
        
        case(N):
        
            switch(next) {
        
            case(N):
        
            i.x = i.x;
            ++i.y;
        
            break;
        
            case(E):
        
            ++i.x;
            i.y = i.y;
        
            break;
        
            case(S):
        
            i.x = i.x;
            i.y = i.y;
        
            break;
        
            case(W):
        
            --i.x;
            i.y = i.y;
        
            break;
            
            case(CENTRE):
            
            i.x = i.x;
            ++i.y;
            
            break;
            
            default:
        
            i.x = i.x;
            i.y = i.y;
        
            break;
        
            }
            
            break;
        
        case(E):
        
            switch(next) {
        
            case(N):
        
            i.x = i.x;
            ++i.y;
        
            break;
        
            case(E):
        
            ++i.x;
            i.y = i.y;
        
            break;
        
            case(S):
        
            i.x = i.x;
            i.y = i.y;
        
            break;
        
            case(W):
        
            i.x = i.x;
            i.y = i.y;
        
            break;
        
            case(CENTRE):
            
            ++i.x;
            i.y = i.y;
            
            break;
            
            default:
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            }
            
            break;
            
        case(S):
            
            switch(next) {
            
            case(N):
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            case(E):
            
            ++i.x;
            i.y = i.y;
            
            break;
            
            case(S):
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            case(W):
            
            --i.x;
            i.y = i.y;
            
            break;
            
            case(CENTRE):
            
            i.x = i.x;
            --i.y;
            
            break;
            
            default:
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            }
            
            break;
            
        case(W):
        
            switch(next) {
            
            case(N):
            
            i.x = i.x;
            ++i.y;
            
            break;
            
            case(E):
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            case(S):
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            case(W):
            
            --i.x;
            i.y = i.y;
            
            break;
            
            case(CENTRE):
            
            --i.x;
            i.y = i.y;
            
            break;
            
            default:
            
            i.x = i.x;
            i.y = i.y;
            
            break;
            
            }
            
            break;
            
            }

        return i;            
    
 }*/