Skip to content

Commit

Permalink
Merge branch 'random_seed'
Browse files Browse the repository at this point in the history
  • Loading branch information
leimao committed Dec 19, 2020
2 parents 655c783 + d7f34b7 commit 02c152e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ Because the installation requires to use `git submodule`, please `git clone` ins
$ git clone https://github.com/leimao/Console_Snake.git
$ cd Console_Snake
$ git submodule update --init --recursive
$ mkdir -p build
$ cd build
$ cmake ..
$ make
$ make install
$ cmake -B build
$ cmake --build build --target install --config Release
```

## Usages
Expand Down
12 changes: 10 additions & 2 deletions src/snake.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "snake.h"
#include <string>
#include <cstdlib>


#include <ctime>
#include <iostream>


Expand Down Expand Up @@ -33,6 +32,13 @@ bool SnakeBody::operator == (const SnakeBody& snakeBody)
Snake::Snake(int gameBoardWidth, int gameBoardHeight, int initialSnakeLength): mGameBoardWidth(gameBoardWidth), mGameBoardHeight(gameBoardHeight), mInitialSnakeLength(initialSnakeLength)
{
this->initializeSnake();
this->setRandomSeed();
}

void Snake::setRandomSeed()
{
// use current time as seed for random generator
std::srand(std::time(nullptr));
}

void Snake::initializeSnake()
Expand Down Expand Up @@ -176,6 +182,8 @@ bool Snake::changeDirection(Direction newDirection)
}
}
}

return false;
}


Expand Down
2 changes: 1 addition & 1 deletion src/snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Snake
//Snake();
Snake(int gameBoardWidth, int gameBoardHeight, int initialSnakeLength);
// Set random seed
// void setRandomSeed(unsigned int seed);
void setRandomSeed();
// Initialize snake
void initializeSnake();
// Check if the snake is on the coordinate
Expand Down

0 comments on commit 02c152e

Please sign in to comment.