This is based on 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.
This Snake Game features AI player on top of Capstone Snake Game. You can play normal Snake Game. But once you press the space bar, the AI player will take over your controller. See how good the AI plays. If you want to take control from AI player, you can press space bar again, or just start controlling with arrow keys. AI will give you the full control.
Game
Class- AI needs to know where the food is. So In
Game::Run()
function,controller.HandleInput()
call has been modified to passfood
as well asrunning
andsnake
.
- AI needs to know where the food is. So In
Snake
Classbool _ai
private member variable has been added to indicate if the snake is AI enabled or not.- To access private member variabl e
_ai
,bool isAIEnabled()
andvoid setAI(bool ai)
has been added. When switching_ai
, debugging messages are printed to console. - AI in
Controller
class needs to knowgrid_width
andgrid_height
, which is a private member variable. So getter for those items,int getGridWidth()
andint getGridHeight()
, are added.
Renderer
Class- In
Render()
function, the color of snake head is changed to Pink,#CC006D
, when AI is enabled.
- In
Controller
ClassHandleInput()
- This function has been modified to receive
food
. Since the AI needs to know where the food is to decide next move. - If AI is enabled, it calls
ControlAI()
to control with AI. - If space bar has entered, it toggles AI.
- If there is user input, arrow keys, it disables AI.
- This function has been modified to receive
void ControlAI()
- This function sets direction of snake for next move.
- It tests current direction with
TestMove()
function. If it is okay, then AI does nothing. Otherwise, it tests left and right move respectively and choose from the result.
bool TestMove()
- With the given direction, it estimates next position and see if it collides.
- It returns false if it collides. Otherwise, it continues calculating distance between food.
- If the distance between food gets closer, it returns true. If not, it returns false.
- Loops, Functions, I/O
- The project demonstrates an understanding of C++ functions and control structures.
Controller::ControlAI()
usesswitch-case
control structures.
- The project accepts user input and processes the input.
Controller::HandleInput()
accepts user input space bar, and process it to toggle AI mode.
- The project demonstrates an understanding of C++ functions and control structures.
- Object Oriented Programming
- The project uses Object Oriented Programming techniques.
- Class attributes and class methods of
Snake
andController
classes are modified and newly defined.
- Class attributes and class methods of
- Classes use appropriate access specifiers for class members.
Controller::ControlAI()
andController::TestMove()
functions are only used inController
object. So it is defined private.
- Classes encapsulate behavior.
- To access
_ai
fromSnake
,bool isAIEnabled()
andvoid setAI(bool ai)
has been added.
- To access
- The project uses Object Oriented Programming techniques.
- Memory Management
- The project makes use of references in function declarations.
- Newly defined functions
Controller::ControlAI()
andController::TestMove()
takes advantage of pass-by-refence.
- Newly defined functions
- The project makes use of references in function declarations.
- 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
.