Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Engine/Engine.cpp
- Revision:
- 16:4d329ce7b156
- Parent:
- 15:130900e5c268
- Child:
- 17:2a909f7da973
--- a/Engine/Engine.cpp Tue May 08 14:06:47 2018 +0000 +++ b/Engine/Engine.cpp Tue May 08 14:41:56 2018 +0000 @@ -12,10 +12,12 @@ void Engine::init() // initialise the game parameters { + + gameTest(); + _solid.init(10,15); _noodles.init(10,10); _gameOver = false; - lvlTwo() ; } void Engine::setLvl(int _levelToSet) { @@ -66,8 +68,8 @@ }; /* - To load a set level, the level is first created as a new 2d array. It is set values for each coordinate - Then the functional array has the values of _newGrid set to it individually + To load a set level, the level is first created as a new 2d array. It is set values for each coordinate + Then the functional array has the values of _newGrid set to it individually */ for(int i = 0; i < 22; i++) { for (int j = 0; j < 22; j++) { @@ -118,7 +120,7 @@ void Engine::draw(N5110 &lcd) { lcd.drawRect(0,0,48,48,FILL_TRANSPARENT); //draws a square around the 'play area' - + for (int j = 0; j < 22; j++) { //goes through each coordinate in the grid, for (int i = 0; i < 22; i++) { if (_grid[i][j] != 0) { //checks if the current square is NOT empty @@ -127,7 +129,7 @@ } } - lcd.printString("Score",50,0); + lcd.printString("Score",50,0); int _score = (_solid.getLength() - 3); //gets the score from the current snake length, it defualts to lenght 3 and increases with each food eaten. Therefor the length will allways be 3 more than the score @@ -156,22 +158,22 @@ checkGameOverAndSetGrid(); //checks if a game over state has been reached, if not set the new coordinates for the snake - _grid[_noodles.getY()][_noodles.getY()] = 2; //sets the current location of the food to the grid, stored as a 2 to differentiate it from the terrrain and tail + _grid[_noodles.getX()][_noodles.getY()] = 2; //sets the current location of the food to the grid, stored as a 2 to differentiate it from the terrrain and tail if (checkFood()) { //checks if the head has reached food - + pad.tone(750.0,0.1); //plays a beep to signify that the snake has eaten growSnake(); //grows the snake as it has eaten the food bool empty = false; //makes a varible for finding an empty square - + while (!empty) { //loops until empty square found - + _noodles.random(); //randomise position of noodles - - if ( _grid[_noodles.getY()][_noodles.getY()] == 0) { - + + if ( _grid[_noodles.getX()][_noodles.getY()] == 0) { + empty = true; //stops looping when free space is found // no need to set the grid as it will put the noodles in the new location the next time 'update' is called } @@ -194,7 +196,7 @@ } if (_grid[_solid.getX(z)][_solid.getY(z)] != 1) { //checks if the square the snake has moved to is clear - _grid[_solid.getX(z)][_solid.getY(z)] = 1; //if so sets the current square to be occupied + _grid[_solid.getX(z)][_solid.getY(z)] = 1; //if so sets the current square to be occupied } else { _gameOver = true; //if not a game over state has been reached } @@ -217,7 +219,7 @@ _solid.grow(); //makes snake longer } - + //If the snake cannot grow any more the player has won the game and filled every tile with the snake, this is extremly unlikely to happen and a game over state will be reached //I feel no need to implement a victory screen due to how unlikey this is to happen } @@ -233,3 +235,87 @@ return _gameOver; } +void Engine::gameTest() +{ + + if (!snakeTest()) { + printf("Some game tests failed!\n"); + } else if (!foodTest()) { + printf("Some game tests failed!\n"); + } else { + printf("All game tests passed!\n"); + } +} + +bool Engine::snakeTest() +{ + //checks if the grow function and initalisation works corectly + + _solid.init(12,10); + + bool _testState = true; + + _solid.grow(); + _solid.grow(); + + int _testLength = _solid.getLength(); + + if ( _testLength != 5) { //checks if the snake has grown + _testState = false; + } + if ( _solid.getY(0) != 10) { //checks the coordiantes of each snake section to check they are in the correct place + _testState = false; + } + if ( _solid.getX(0) != 12) { + _testState = false; + } + if ( _solid.getY(1) != 11) { + _testState = false; + } + if ( _solid.getX(1) != 12) { + _testState = false; + } + if ( _solid.getY(2) != 12) { + _testState = false; + } + if ( _solid.getX(2) != 12) { + _testState = false; + } + + return _testState; + +} + +bool Engine::foodTest() +{ + //checks if the radomise function works correctly for the food + + bool _testState = true; + + _noodles.init(7,3); + + _grid[_noodles.getX()][_noodles.getY()] = 2; //sets the grid value so that the randomise loop will work correctly + + + + bool empty = false; //makes a varible for finding an empty square + + while (!empty) { //loops until empty square found + + _noodles.random(); //randomise position of noodles + + if ( _grid[_noodles.getX()][_noodles.getY()] == 0) { + + empty = true; //stops looping when free space is found + // no need to set the grid as it will put the noodles in the new location the next time 'update' is called + } + + } + + + if (_noodles.getX() ==7 && _noodles.getY() == 3) { //checks to see if randomised correctly + _testState = false; //fail + } + + return _testState; +}