Coder Social home page Coder Social logo

cppnd-capstone-snake-game's Introduction

CPPND: Capstone AI Snake Game

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.

Class Structure

  • Game Class
    • AI needs to know where the food is. So In Game::Run() function, controller.HandleInput() call has been modified to pass food as well as running and snake.
  • Snake Class
    • bool _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() and void setAI(bool ai) has been added. When switching _ai, debugging messages are printed to console.
    • AI in Controller class needs to know grid_width and grid_height, which is a private member variable. So getter for those items, int getGridWidth() and int getGridHeight(), are added.
  • Renderer Class
    • In Render() function, the color of snake head is changed to Pink, #CC006D, when AI is enabled.
  • Controller Class
    • HandleInput()
      • 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.
    • 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.

Rubrics

  • Loops, Functions, I/O
    • The project demonstrates an understanding of C++ functions and control structures.
      • Controller::ControlAI() uses switch-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.
  • Object Oriented Programming
    • The project uses Object Oriented Programming techniques.
      • Class attributes and class methods of Snake and Controller classes are modified and newly defined.
    • Classes use appropriate access specifiers for class members.
      • Controller::ControlAI() and Controller::TestMove() functions are only used in Controller object. So it is defined private.
    • Classes encapsulate behavior.
      • To access _ai from Snake, bool isAIEnabled() and void setAI(bool ai) has been added.
  • Memory Management
    • The project makes use of references in function declarations.
      • Newly defined functions Controller::ControlAI() and Controller::TestMove() takes advantage of pass-by-refence.

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.

cppnd-capstone-snake-game's People

Contributors

swwelch avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.