Question: Package name: Proj02 File Name: SnakeGame.java SnakeBoard.java Position.java Include comments SnakeGame is a text based snake game. The snake moves around the board until it
Package name: Proj02
File Name:
SnakeGame.java
SnakeBoard.java
Position.java
Include comments
SnakeGame is a text based snake game. The snake moves around the board until it runs into itself or goes out of bounds. I realize that Snake usually has more bells and whistles, but well stick to a basic version for this project.
The edge of the board is drawn with #s
The snake is drawn with Ss.
The snake has 20 spaces horizontally and 10 spaces vertically where it can move.
o The edge of the board is outside the 20 x 10 area
Initially the snake is 2 segments long at (1, 1) and (1, 2).
o Let the positions be 0-based. (0, 0) is the upper left.
o X grows positive to the right and y grows positive going down.
o The initial board, with the 2 initial snake segments, looks like:
######################
# #
# S #
# S #
# #
# #
# #
# #
# #
# #
# #
######################
If the snake moves to a spot already occupied by the snake or into the edge of the board, then the game is over.
o Keep track of the number of moves before the game ends.
How to play the game (this is basically main()):
Initialize the board and snake
Initialize the number of moves made
Print the board
Continue until the game ends (snake runs into edge or itself)
o Remove the oldest segment from the snake
For example, the first time, (1, 1) will be removed from the snake
o Ask the user for a move (l for left, r for right, u for up, and d for down)
o Move the snake twice in that direction
For example, if the user chooses r and the last segment added to the snake was (1, 2), then add (2, 2) and (3, 2) to the snake
Right increases the x-coordinate. So (1, 2) moves to (2, 2) and (2, 2) moves to (3, 2)
I had a method in my SnakeBoard class that did a single move and called it twice. I felt this option would be more flexible if I wanted to change the game in the future.
o Print the board
o Increment the number of moves made
After the game is over, print the number of moves made
Your program should have 3 classes/files:
SnakeGame class
o Contains main() essentially the code shown above in how to play the game
SnakeBoard class
o Need some final instance variables
Width of the board
Height of the board
o 2 instance variables
Array or ArrayList of Position objects will contain all of the Positions of the Snake
I used ArrayList, but you may use a regular array if you prefer
Flag to remember if snake has made an illegal move (moved onto itself or off the edge of the board)
This instance variable is optional as it depends on how you design the solution
If you use it, the game is over when this flag becomes true
o Constructor
Initialize the Array/ArrayList
Add first 2 snake segments to the Array/ArrayList
You should create a new Position object, set its x, y, add it to your Array/ArrayList
Then you should create a second new Position, set its x, y, and add it to your Array/ArrayList
Do NOT try to reuse the same Position object youll want to call new a second time
o print method to print the board
This method will need nested loops to print the two-dimensional board
Within the loops, I checked to see if the x,y location that I was about to print was a Position in my Array/ArrayList. If it was in there, then I printed S, otherwise I printed (space).
This is where I used the snakeHere method listed below
o removeSnake method to remove the oldest Position in the Array/ArrayList
o moveSnake method to add one more Position to the Array/ArrayList
pass in a direction to move
use the last Position of the Snake and the direction to calculate the x, y of the new Position
add the new Position to your Array/ArrayList if it is a valid move
This is where I used the validMove method listed below
o gameOver method that returns true if the game is over, false otherwise
o validMove method that returns true if moving to x, y is okay, false otherwise
I also used the snakeHere method to help decide if this is a valid move or not
o snakeHere method that returns true if the x,y you are checking is an x,y currently in your Array/ArrayList
Position class
o Simple class that contains x, y and matching get/set methods
o The following is an example of a Position class feel free to use it, but please add some comments!
package proj02;
public class Position
{
private int x;
private int y;
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
}
Requirements:
Put System.out.println() after each call to next(). This will help you match the Test Program and make it easier to use it.
Sample Run #1: (the highlighted text is what the user types)
######################
# #
# S #
# S #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? u
######################
# S #
# S #
# S #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? u
######################
# S #
# S #
# #
# #
# #
# #
# #
# #
# #
# #
######################
Game over, you moved 2 times
Sample Run #2: (the highlighted text is what the user types)
######################
# #
# S #
# S #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? r
######################
# #
# #
# SSS #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? r
######################
# #
# #
# SSSS #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? r
######################
# #
# #
# SSSSS #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? r
######################
# #
# #
# SSSSSS #
# #
# #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? d
######################
# #
# #
# SSSSS #
# S #
# S #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? l
######################
# #
# #
# SSSS #
# S #
# SSS #
# #
# #
# #
# #
# #
######################
Move (l/r/u/d)? u
######################
# #
# #
# SSS #
# S S #
# SSS #
# #
# #
# #
# #
# #
######################
Game over, you moved 7 times
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
