Snake game for a 5x5 LED matrix

Dependencies:   MCP23S17 mbed

snake.cpp

Committer:
dhamilton31
Date:
2013-10-09
Revision:
0:dc906408980e
Child:
1:5fcb94bb03db

File content as of revision 0:dc906408980e:

#include "snake.h"
#include "bodyPiece.h"
#include <list>

snake::snake(char startRow, char startCol){
    bodyPiece head = bodyPiece();
    head.currRow = startRow;
    head.currCol = startCol;
    snakeBody.push_back(head);
}

void snake::move(char newHeadRow, char newHeadCol){
    
    // Variables to hold the bodyPiece's X and Y positions on the grid
    char prevRow, prevCol, tempRow, tempCol;

    // Store the snake's head into the iterator
    std::list<bodyPiece>::iterator it=snakeBody.begin();
    // Save the head's current Row and Col position
    prevRow = (*it).currRow;
    prevCol = (*it).currCol; 
    // Update the head to the new Row and Col
    (*it).currRow = newHeadRow;
    (*it).currCol = newHeadCol;
    // Check to see if we are at the tail with this while loop
    while(it != snakeBody.end()){
        // Move to the next bodyPiece
        it++;
        // Store the previous piece's location in the current piece
        tempRow = (*it).currRow;
        (*it).currRow = prevRow;
        tempCol = (*it).currCol;
        (*it).currCol = prevCol;
        prevRow = tempRow;
        prevCol = tempCol;
    }
}

void snake::addPiece(){
    snakeBody.push_back(bodyPiece());
}

food::food(char row, char col){
    currRow = row;
    currCol = col;
}

void food::moveFood(char row, char col){
    currRow = row;
    currCol = col;
}