This is an extended version of the Snake Game, a project in the Udacity C++ Nanodegree Program.
As the original README states, "The code for this repo was inspired by this excellent StackOverflow post and set of responses."
Truthfully, I didn't intend to use this, but as I had experimented immediately after first seeing it, I had some ideas about changes I thought would be fun to try. Namely, adding another snake to compete against.
I assumed I'd be able to find a way to use multiple threads in order to run a computer player, and starting poking that idea. Originally, I had started with the intent that there could perhaps be more than just one, but in the end, I stayed with just the one competitor. I just wanted to see if I could get a snake running and finding food autonimously.
I also wondered how changing the goal would impact the way I needed to code it. Instead of growing, the snakes, get smaller (and faster).
The game has a few ways to end, so I also strugged with how to send the right messages to the queue, and when, to ensure that threads were closed down as the program needed to end. I went down a few git branches trying various ideas, and backtracked a number of times, before make a version that is a bit simpler than some of my attempts, but I feel would still statify much of the projects goals.
The original dependancies are still present. See the dependency section below.
- Clone this repo.
cd
into the folder you cloned (it should have this README.md file).cd
into the build folder.- Use the command
"cmake .."
followed by"make"
to compile the src folder into an executable. - There should now be an executable called
SnakeGame
. - Run the file from your terminal with the command
./SnakeGame
Much of the oringal code structure remains, with the addition of class to extend the Snake class, to make a ComputerSnake.
All source files are in the {project root}/src
directory.
Most classes have a distinct headder file(.h) and code file (.cpp). There's a simple gameDebug.h
file that doesn't hava a .cpp
file to go along.
The main.cpp
file contains the main function, and includes the game.h
header, render.h
and, and controller.h
.
Main initials the Game::Run function.
The game.h
file included most other headers for the rest of the classes.
-
The project compiles and runs, not only on my local system, but also in the dev environment provided for the project. It compiles utilizing cmake and make, as suggested.
-
The project code is organized into classes with class attributes to hold the data, and class methods to perform tasks.
-
The project accepts input from a user as part of the necessary operation of the program.
- Input is required to move the snake.
-
A variety of control structures are used in the project.
-
The project code is clearly organized into functions.
- While I'm sure it could be inmproved, examples of this are found in the game.cpp. I separated the
Update
functions, breaking out smaller functions, and functiones needed for the computer snake, vs the player. See also the Game::GameOver() function, and Game::PlaceFood() function, as some examples.
- While I'm sure it could be inmproved, examples of this are found in the game.cpp. I separated the
-
The project code is organized into classes with class attributes to hold the data, and class methods to perform tasks.
-
The Snake class holds a '_won' boolean value, with a public IsWinner() function to retrieve it.
-
All class data members are explicitly specified as public, protected, or private.
- The "Snake" class shows some example of this. protected was used to enable ineheritance where needed.
-
All class members that are set to argument values are initialized through member initialization lists.
- There are few left TODO here.
-
Appropriate data and functions are grouped into classes. Member data that is subject to an invariant is hidden from the user. State is accessed via member functions.
-
One function is overloaded with different signatures for the same function name.
- Renderer::Render() has two signatures.
-
Inheritance hierarchies are logical. Composition is used instead of inheritance when appropriate. Abstract classes are composed of pure virtual functions. Override functions are specified.
- The ComputerSnake needs to use most of the features of the SnakeClass, and augment or change.
- It adds new functions, Release() and UpdateLastKnownFoodPoint().
- However, it also overrides the ShrinkBody() function to effect the computer snake with unique changes.
- The ComputerSnake needs to use most of the features of the SnakeClass, and augment or change.
-
One function is declared with a template that allows it to accept a generic parameter.
- See the MessageQueue. Its utilized by the Game class to send and receive
gameMessages
of type FoodStatus.
- See the MessageQueue. Its utilized by the Game class to send and receive
- At least two variables are defined as references, or two functions use pass-by-reference in the project code.
- ComputerSnake::UpdateLastKnownFoodPoint passes a reference to a
food
object. - Game::UpdateComp passes a reference to the Snake object.
- ComputerSnake::UpdateLastKnownFoodPoint passes a reference to a
-
The project uses multiple threads in the execution.
- See ComputerSnake::Release(), and Game::Run(), line 32.
-
A mutex or lock (e.g. std::lock_guard or `std::unique_lock) is used to protect data that is shared across multiple threads in the project code.
- See ComputerSnake::Hunt(), Game::Update().
-
A std::condition_variable is used in the project code to synchronize thread execution.
- See the use of the MessageQueue.
- Also these functions Game::EatFood() Game::waitForFoodEaten() Game::GameOver()
- 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
.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
This code was adapted and changed by Beau Kinstler for fun (and hopefully a nanodegree.)