This is a my Capstone project in the Udacity C++ Nanodegree Program. The code for this repo was inspired by this excellent StackOverflow post and set of responses.
In my project, I made food randomly update its location so the game gets more exciting. To achieve this, I add Food
class which manages its location and performs random location update by using a thread.
main.cpp
: Made it to write result to a file, not only console.Snake
: AddedIsAlive()
andPoisonPill()
so we can observe if its alive and let it die without touching member vaiables directly.Food
: As mentioned in summary, this is a representation of food, which manages its location and performs random location update by using a thread.Controller
: Now you can terminate game byESC
key.
- Has README which describes project and work.
- It compiles.
- It writes result to a file.
- The project accepts arrow keys and ESC key.
- The project uses object oriented programming techniques.
- For
Snake
class, it encapsulates private members and provides clearer methods than having accessed members directly.
- The project uses smart pointer
shared_ptr
to shareSnake
object amongGame
andFood
.
- The project uses
thread
insideFood
to run random update of its location. - For safe access to
Food
members from main game thread and thead insideFood
, the project usesstd::mutex
andstd::lock_guard
.
- cmake >= 3.7
- All OSes: click here for installation instructions
- make >= 4.1 (Linux, Mac), 3.81 (Windows)
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- SDL2 >= 2.0
- All installation instructions can be found here
- Note that for Linux, an
apt
orapt-get
installation is preferred to building from source.
- gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - install Xcode command line tools
- Windows: recommend using MinGW
- Clone this repo.
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./SnakeGame
.