SnakeGame  1
Implementing a game of Snake
 All Classes Functions Variables Pages
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
com.gradescope.spampede.SpampedeBrain Class Reference

SpampedeBrain - The "Controller" in MVC, which includes all of the AI and handling key presses. More...

Inheritance diagram for com.gradescope.spampede.SpampedeBrain:
com.gradescope.spampede.SpampedeBrainParent com.gradescope.spampede.SpampedeBrainParent

Public Member Functions

void startNewGame ()
 Starts a new game.
 
void gameOver ()
 Declares the game over.
 
void cycle ()
 Moves the game forward one step (i.e., one frame of animation, which occurs every Preferences.SLEEP_TIME milliseconds)
 
void keyPressed (KeyEvent evt)
 Reacts to characters typed by the user. More...
 
void advanceTheSnake (BoardCell nextCell)
 Move the snake into the next cell (and possibly eat spam) More...
 
BoardCell getNextCellFromBFS ()
 Uses BFS to search for the spam closest to the snake head. More...
 
void reverseSnake ()
 Reverses the snake back-to-front and updates the movement mode appropriately.
 
void playSound_spamEaten ()
 Plays crunch noise.
 
void playSound_spam ()
 Plays spam noise.
 
void playSound_meow ()
 Plays meow noise.
 
String testing_toStringParent ()
 
BoardCell testing_getNextCellInDir ()
 
String testing_toStringSpampedeData ()
 
void startNewGame ()
 Starts a new game.
 
void gameOver ()
 Declares the game over.
 
void cycle ()
 Moves the game forward one step (i.e., one frame of animation, which occurs every Preferences.SLEEP_TIME milliseconds)
 
void keyPressed (KeyEvent evt)
 Reacts to characters typed by the user. More...
 
void advanceTheSnake (BoardCell nextCell)
 Move the snake into the next cell (and possibly eat spam) More...
 
BoardCell getNextCellFromBFS ()
 Uses BFS to search for the spam closest to the snake head. More...
 
void reverseSnake ()
 Reverses the snake back-to-front and updates the movement mode appropriately.
 
void playSound_spamEaten ()
 Plays crunch noise.
 
void playSound_spam ()
 Plays spam noise.
 
void playSound_meow ()
 Plays meow noise.
 
String testing_toStringParent ()
 
BoardCell testing_getNextCellInDir ()
 
String testing_toStringSpampedeData ()
 
- Public Member Functions inherited from com.gradescope.spampede.SpampedeBrainParent
void init ()
 
void actionPerformed (ActionEvent evt)
 
void keyReleased (KeyEvent evt)
 
void keyTyped (KeyEvent evt)
 
void run ()
 
synchronized void go ()
 
synchronized void stop ()
 
abstract void keyPressed (KeyEvent evt)
 
void init ()
 
void actionPerformed (ActionEvent evt)
 
void keyReleased (KeyEvent evt)
 
void keyTyped (KeyEvent evt)
 
void run ()
 
synchronized void go ()
 
synchronized void stop ()
 
abstract void keyPressed (KeyEvent evt)
 

Static Public Member Functions

static SpampedeBrain getTestGame (TestGame gameNum)
 
static SpampedeBrain getTestGame (TestGame gameNum)
 

Private Member Functions

BoardCell getFirstCellInPath (BoardCell start)
 Follows parent pointers back from the closest spam cell to decide where the head should move. More...
 
BoardCell getFirstCellInPath (BoardCell start)
 Follows parent pointers back from the closest spam cell to decide where the head should move. More...
 

Private Attributes

SpampedeDisplay theDisplay
 The "View" in MVC.
 
SpampedeData theData
 The "Model" in MVC.
 
int cycleNum = 0
 Number of animated frames displayed so far.
 

Static Private Attributes

static final char REVERSE = 'r'
 
static final char UP = 'i'
 
static final char DOWN = 'k'
 
static final char LEFT = 'j'
 
static final char RIGHT = 'l'
 
static final char AI_MODE = 'a'
 
static final char PLAY_SPAM_NOISE = 's'
 
static final long serialVersionUID = 1L
 

Additional Inherited Members

- Public Attributes inherited from com.gradescope.spampede.SpampedeBrainParent
Image image
 
Graphics screen
 
AudioClip audioSpam
 
AudioClip audioCrunch
 
AudioClip audioMeow
 

Detailed Description

SpampedeBrain - The "Controller" in MVC, which includes all of the AI and handling key presses.

Author
CS60 instructors

Member Function Documentation

void com.gradescope.spampede.SpampedeBrain.advanceTheSnake ( BoardCell  nextCell)

Move the snake into the next cell (and possibly eat spam)

This method should probably be private, but we make it public anyway to permit unit testing.

Parameters
nextCellNew location of the snake head (which must be horizontally or vertically adjacent to the old location of the snake head).
void com.gradescope.spampede.SpampedeBrain.advanceTheSnake ( BoardCell  nextCell)

Move the snake into the next cell (and possibly eat spam)

This method should probably be private, but we make it public anyway to permit unit testing.

Parameters
nextCellNew location of the snake head (which must be horizontally or vertically adjacent to the old location of the snake head).
BoardCell com.gradescope.spampede.SpampedeBrain.getFirstCellInPath ( BoardCell  start)
private

Follows parent pointers back from the closest spam cell to decide where the head should move.

Specifically, follows the parent pointers back from the spam until we find the cell whose parent is the snake head (and which must therefore be adjacent to the previous snake head location).

Recursive or looping solutions are possible.

Parameters
startwhere to start following spam pointers; this will be (at least initially, if you use recursion) the location of the spam closest to the head.
Returns
the new cell for the snake head.
BoardCell com.gradescope.spampede.SpampedeBrain.getFirstCellInPath ( BoardCell  start)
private

Follows parent pointers back from the closest spam cell to decide where the head should move.

Specifically, follows the parent pointers back from the spam until we find the cell whose parent is the snake head (and which must therefore be adjacent to the previous snake head location).

Recursive or looping solutions are possible.

Parameters
startwhere to start following spam pointers; this will be (at least initially, if you use recursion) the location of the spam closest to the head.
Returns
the new cell for the snake head.
BoardCell com.gradescope.spampede.SpampedeBrain.getNextCellFromBFS ( )

Uses BFS to search for the spam closest to the snake head.

Returns
Where to move the snake head, if we want to head one step along the shortest path to (the nearest) spam cell.
BoardCell com.gradescope.spampede.SpampedeBrain.getNextCellFromBFS ( )

Uses BFS to search for the spam closest to the snake head.

Returns
Where to move the snake head, if we want to head one step along the shortest path to (the nearest) spam cell.
void com.gradescope.spampede.SpampedeBrain.keyPressed ( KeyEvent  evt)

Reacts to characters typed by the user.

The provided code registers the SpampedeBrain as an "observer" for key presses on the keyboard.

So, whenever the user presses a key, Java automatically calls this keyPressed method and passes it a KeyEvent describing the specific keypress.

void com.gradescope.spampede.SpampedeBrain.keyPressed ( KeyEvent  evt)

Reacts to characters typed by the user.

The provided code registers the SpampedeBrain as an "observer" for key presses on the keyboard.

So, whenever the user presses a key, Java automatically calls this keyPressed method and passes it a KeyEvent describing the specific keypress.


The documentation for this class was generated from the following file: