This is a starter repo for the 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 this project, I extended Snake game sample code with the following new features in order to satisfy the requiements of the rubric.
- Start Instruction
- You will see the start instruction at the beginning of the game, including game mode selection (
1
for single player and2
for double player), control keys, and input of player name.
- You will see the start instruction at the beginning of the game, including game mode selection (
- Game Mode
- The game supports both single player mode and double player mode chosen by the user. Under double player mode, two snakes will compete for the food, whoever get it first will have score and speed increased. Once one snake dies, it will show up in the screen for 2 seconds before disappearing.
- Record File
- After the first run, a file
records.txt
will be created in the root directory. At the finish of each run, this file will be updated to store the highest score of each player, in descending order.
- After the first run, a file
- Hash Table
- The search function used to check whether there is self-crossing or place new food has been optimized using a hash table of points, which is more efficient than iterating over a vector. This is achieved by defining an inherited class
Snake_Point
ofSDL_Point
insidesnake.h
.
- The search function used to check whether there is self-crossing or place new food has been optimized using a hash table of points, which is more efficient than iterating over a vector. This is achieved by defining an inherited class
- 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
.
The following criteria are satisfied in this project.
Loops, Functions, I/O
- The project demonstrates an understanding of C++ functions and control structures.
- For example, in
main.cpp
, I use a while loop and try-catch statement to get input of game mode.
- For example, in
- The project reads data from a file and process the data, or the program writes data to a file.
- The member function
Game::UpdateRecords
reads data from the local filerecords.txt
and updates it by including the new results.
- The member function
- The project accepts user input and processes the input.
- In
main.cpp
, the user will input the game mode and player names, which are used to configure the classes such asSnake
andGame
.
- In
Object Oriented Programming
- Classes follow an appropriate inheritance hierarchy.
- Struct
Snake_Point
is inherited fromSDL_Point
with public inheritance. The purpose is to overload the==
operator so as to support the hash table implemented bystd::unordered_set
.
- Struct
Memory Management
- The project makes use of references in function declarations.
- When the input parameter is a
std::vector
, I try to use reference. E.g. see the constructor of classGame
andRenderer::Render
.
- When the input parameter is a
- The project uses move semantics to move data, instead of copying it, where possible.
- In function
Renderer::Render
, I use move semantics to move theblock
variable into functionstd::async
.
- In function
- The project uses smart pointers instead of raw pointers.
- I use a vector of shared pointers as the member
snakes
in classGame
. The project does not use any raw pointers.
- I use a vector of shared pointers as the member
Concurrency
- The project uses multithreading.
- The update and render of each snake are handled by multiple threads. See functions
Game::Update
andRenderer::Render
.
- The update and render of each snake are handled by multiple threads. See functions
- A mutex or lock is used in the project.
- I use
std::lock_guard
in functionsGame::UpdateOneSnake
andRender::RenderOneSnake
to protect shared resources such asfood
.
- I use