Skip to content

DeliriumInTheVoid/CppND-Capstone-Snake-Game

 
 

Repository files navigation

Submission overview

Clone the project

git clone --recurse-submodules

Udacity workspace build

make init_env
make build_debug

./build/SnakeGame

Rubric points

All points indicate at least one place in the code, but it is obvious that almost all clauses appear more than once in the code.

Loops, Functions, I/O

  • The project demonstrates an understanding of C++ functions and control structures.
    • while: game.cpp line 15 void Game::Run()
    • if: game.cpp line 31 void Game::Update()
    • else if: controller.cpp line 12 void Controller::HandleInput(bool &running, const std::unique_ptr<GameStateManager>& state_manager)
    • switch: gamefield_factory.cpp line 13 std::unique_ptr<GameField> GameFieldFactory::CreateGameField(const GameStateType type)
    • case: gamefield_factory.cpp line 15, 21, 28 std::unique_ptr<GameField> GameFieldFactory::CreateGameField(const GameStateType type)
    • default: gamefield_factory.cpp line 35 std::unique_ptr<GameField> GameFieldFactory::CreateGameField(const GameStateType type)
    • return: gameplay-manager.cpp line 18 void GamePlayManager::ChangeState(const GameStateType nextStateType)
    • for: player_game_field.cpp line 93 Winner PlayerGameField::GetWinner()
    • continue: a_star_async.cpp line 113 ResultCell AStartAsync::AStarSearch(Cell* const start, const Cell* const goal)
    • break: main_menu_state.cpp line 21, 24, 27, 30, 33 void MainMenuState::HandleScreenEvent(const MainMenuEvent event)
  • The project reads data from a file and process the data, or the program writes data to a file.
    • read from file: game_save_data.cpp line 9, 10 void Game::LoadBestScore()
    • write to file: game_save_data.cpp line 52, 53 void Game::SaveData()
  • The project accepts user input and processes the input.
    • user keyboard input: single_player_state.cpp line 21, 22 void SinglePlayerGameState::HandleInput(const SDL_Keycode keyCode)
    • user mouse input: multiplayer_screen.cpp line 44, 49, 54 void MultiplayerGameScreen::Render()
  • The project uses data structures and immutable variables.
    • data structures: a_star_async.hpp line 61 std::vector<std::vector<Cell*>> grid_{};
    • immutable variables: a_star_async.hpp line 34: void calculateHeuristic(const Cell* const goal)

Object Oriented Programming

  • One or more classes are added to the project with appropriate access specifiers for class members.
    • class attributes: game_field.hpp line 37-41
    • class methods: gameplay_manager.hpp line 23-26
  • Class constructors utilize member initialization lists.
    • member initialization: player_game_field.cpp line 8-10 PlayerGameField::PlayerGameField(std::size_t gridWidth, std::size_t gridHeight)
  • Classes abstract implementation details from their interfaces.
    • class interface: game_state_manager.hpp line 26-29
    • expected behavior: game_states.hpp line 58, 64, 70
  • Classes follow an appropriate inheritance hierarchy with virtual and override functions.
    • inheritance, polymorphism: game_field.hpp line 20-27
  • Templates generalize functions or classes in the project.
    • template class: screen.hpp line 19, 20

Memory Management

  • The project makes use of references in function declarations.
    • reference arg: game_states.hpp line 58 void Render(SDL_Renderer *sdl_renderer, SDL_Rect& block)
    • reference arg: a_star_async.hpp line 58 ResultCell AStarSearch(Cell* const start, const Cell* const goal)
    • reference attr: renderer.h line 19, 20
  • The project uses destructors appropriately.
    • destructor: a_star_async.cpp line 21-29 AStartAsync::~AStartAsync()
  • The project uses scope / Resource Acquisition Is Initialization (RAII) where appropriate.
    • allocating: a_star_async.cpp line 15 AStartAsync::AStartAsync(const std::size_t gridWidth, const std::size_t gridHeight)
    • initializing: a_star_async.cpp line 41, 42 void AStartAsync::FindPathAsync(int startX, int startY, const CellType goalCellType, const std::unordered_map<std::size_t, std::unordered_map<std::size_t, CellType>>& field)
    • destruction: a_star_async.cpp line 24 AStartAsync::~AStartAsync()
  • The project follows the Rule of 5.
    • operators, constructors, destructor: a_star_async.hpp line 42-48
  • The project uses move semantics to move data instead of copying it, where possible.
    • move semantic: game_states.cpp line 15, 16 void GameStateBase::MoveGamePlayManager(std::unique_ptr<GamePlayManager>&& manager)
  • The project uses smart pointers instead of raw pointers.
    • unique_ptr: game_states.hpp line 77

Concurrency

  • The project uses multithreading.
    • multithreading: a_star_async.cpp line 51 void AStartAsync::FindPathAsync(int startX, int startY, const CellType goalCellType, const std::unordered_map<std::size_t, std::unordered_map<std::size_t, CellType>>& field)
    • get result: a_star_async.cpp line 71 ResultCell AStartAsync::GetResult()

Dependencies

All libraries install with vcpkg. The project uses the following libraries:

  • SDL2
  • ImGui
  • nlohmann json

Snake Game

Game has the three modes:

  • Single player
  • Player vs Player
  • Player vs AI Player

Single player

Control the snake with the arrow keys. The snake will grow when it eats the food. The game ends when the snake collides with itself.

Player vs Player

Control the first snake with the arrow keys and the second snake with the WASD keys. The snakes will grow when they eat the food. The game ends when one of the snakes collides with itself or the other snake.

Player vs AI Player

Control the snake with the arrow keys. The snake will grow when it eats the food. The game ends when the snake collides with itself or the AI snake.

If snake collides with the other snake, the game will end and despite the score of the collides snake it will lose.

Controls

  • Arrow keys: Move the snake (all game modes)
  • WASD keys: Move the second snake (PvP game mode)
  • P, Esc: Pause the game (all game modes)

New features

  • Added the new game modes
  • Added the AI player
  • Added the UI
  • Added the best score save/load for all game modes

CPPND: Capstone Snake Game Example

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.

The Capstone Project gives you a chance to integrate what you've learned throughout this program. This project will become an important part of your portfolio to share with current and future colleagues and employers.

In this project, you can build your own C++ application or extend this Snake game, following the principles you have learned throughout this Nanodegree Program. This project will demonstrate that you can independently create applications using a wide range of C++ features.

Dependencies for Running Locally

Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory in the top level directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./SnakeGame.

CC Attribution-ShareAlike 4.0 International

Shield: CC BY-SA 4.0

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

CC BY-SA 4.0

About

A 2D Snake game using C++ and SDL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 92.5%
  • CMake 7.0%
  • Makefile 0.5%