Please help me out here Chegg! Not looking for a typical Snake program, but one
ID: 3604709 • Letter: P
Question
Please help me out here Chegg! Not looking for a typical Snake program, but one specific to the requirements listed below. No food, apple, score, etc is required. There are even pictures that show what the output should look like. Thank you!
Create a text based snake game in Java without Spring or Jpanel. Will have three files (SnakeGame, SnakeBoard, & Position). The snake moves around the board until it runs into itself or goes out of bounds.
The edge of the board is drawn with #’s
The snake is drawn with S’s.
The snake has 20 spaces horizontally and 10 spaces vertically where it can move.
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).
Let the positions be 0-based. (0, 0) is the upper left.
X grows positive to the right and y grows positive going down.
The initial board, with the 2 initial snake segments, looks like:
If the snake moves to a spot already occupied by the snake or into the edge of the board, then the game is over.
Keep track of the number of moves before the game ends.
1) SnakeGame class which contains main() – essentially the code shown above in how to play the game
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)
Remove the oldest segment from the snake
For example, the first time, (1, 1) will be removed from the snake
Ask the user for a move (l for left, r for right, u for up, and d for down)
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.
Print the board
Increment the number of moves made
After the game is over, print the number of moves made
2) SnakeBoard class
Need some final instance variables (Width of the board, Height of the board)
2 instance variables (Array or ArrayList of Position objects – will contain all of the Positions of the Snake)
Flag to remember if snake has made an illegal move (moved onto itself or off the edge of the board and the game is over when this flag becomes true)
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 – you’ll want to call new a second time
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).
removeSnake method to remove the oldest Position in the Array/ArrayList
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
gameOver method that returns true if the game is over, false otherwise
validMove method that returns true if moving to x, y is okay, false otherwise
snakeHere method that returns true if the x,y you are checking is an x,y currently in your Array/ArrayList
3) Position class
Simple class that contains x, y and matching get/set methods
The following is an example of a Position class I used
package snake;
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)
Explanation / Answer
Hello, Have a good day ahead! I have done everything as per the requirements, drop me a comment if you need any help. J
Note: Forgive if the indentation is messed up
//SnakeGame.java
import java.util.Scanner;
public class SnakeGame {
public static void main(String[] args) {
/**
* creating a snakeboard of 20X10 size
*/
SnakeBoard board=new SnakeBoard(20, 10);
/**
* a counter variable for measuring number of moves taken
*/
int number_of_moves=0;
/**
* a scanner object to get user input
*/
Scanner scanner=new Scanner(System.in);
/**
* printing the board initially
*/
board.printBoard();
while(!board.isGameOver()){
System.out.println("Move l/r/u/d ? ");
String choice=scanner.nextLine();
/**
* if the choice is valid, make a move and print, else, throw an error
*/
if(choice.equalsIgnoreCase("l") || choice.equalsIgnoreCase("r") || choice.equalsIgnoreCase("u") || choice.equalsIgnoreCase("d")){
board.moveSnake(choice);
board.printBoard();
number_of_moves++;
}else{
System.out.println("Invalid choice, try again");
}
}
/**
* finally, display the number of moves after Game over
*/
if(board.isGameOver()){
System.out.println("Game over: Number of moves: "+number_of_moves);
}
}
}
//SnakeBoard.java
import java.util.ArrayList;
public class SnakeBoard {
private int width, height;
private ArrayList<Position> snake;
private boolean gameover; /*a flag variable to denote illegal move or when the game is over*/
public SnakeBoard(int width, int height) {
snake = new ArrayList<Position>();
if (width < 10 || height < 10) {
/**
* setting a minimum size for the board
*/
this.height = 10;
this.width = 10;
}
this.height = height;
this.width = width;
gameover = false;
/**
* defining initial positions and adding to the arraylist
*/
Position p1 = new Position(1, 1);
Position p2 = new Position(1, 2);
snake.add(p1);
snake.add(p2);
}
/**
* this will print the board along with the border
*/
public void printBoard() {
System.out.println();
/**
* printing the upper border
*/
for (int i = 0; i < width + 2; i++) {
System.out.print('#'); /* left border */
}
System.out.println();
for (int i = 0; i < height; i++) {
System.out.print('#');
for (int j = 0; j < width; j++) {
Position p = new Position(j, i);
/**
* if the position is occupied by a snake, print S else 'space'
*/
if (snake.contains(p)) {
System.out.print('S');
} else {
System.out.print(' ');
}
}
System.out.print('#'); /* right border */
System.out.println();
}
/**
* printing the lower border
*/
for (int i = 0; i < width + 2; i++) {
System.out.print('#');
}
System.out.println();
}
/**
* method will move the snake in the specified direction
* @param dir can be l / r / u / d
*/
public void moveSnake(String dir) {
if (dir.equalsIgnoreCase("r")) {
/**
* moving right
*/
Position p = snake.get(snake.size() - 1); /*
* getting the last position
* of snake
*/
if ((p.getX() + 2) < width) {
/**
* if the snake can move right safely
*/
Position p1 = new Position(p.getX() + 1, p.getY());
Position p2 = new Position(p.getX() + 2, p.getY());
if (snake.contains(p1) || snake.contains(p2)) {
/**
* trying to move into a position occupied by snake
*/
if(!snake.contains(p1)){
/**
* in case if one position can be moved without touching the snake
* (even though the game is over)
*/
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
snake.add(p1);
snake.add(p2);
removeSnake();
}
} else {
/**
* only one position can be moved, game over
*/
if ((p.getX() + 1) < width) {
Position p1 = new Position(p.getX() + 1, p.getY());
if (!snake.contains(p1)) {
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
/**
* cannot move, game over
*/
gameover = true;
}
}
} else if (dir.equalsIgnoreCase("l")) {
/**
* moving left
*/
Position p = snake.get(snake.size() - 1); /*
* getting the last position
* of snake
*/
if ((p.getX() - 2) >= 0) {
/**
* if the snake can move left safely
*/
Position p1 = new Position(p.getX() - 1, p.getY());
Position p2 = new Position(p.getX() - 2, p.getY());
if (snake.contains(p1) || snake.contains(p2)) {
/**
* trying to move into a position occupied by snake
*/
if(!snake.contains(p1)){
/**
* in case if one position can be moved without touching the snake
* (even though the game is over)
*/
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
snake.add(p1);
snake.add(p2);
removeSnake();
}
} else {
/**
* only one position can be moved, game over
*/
if ((p.getX() - 1) >=0) {
Position p1 = new Position(p.getX() - 1, p.getY());
if (!snake.contains(p1)) {
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
/**
* cannot move, game over
*/
gameover = true;
}
}
} else if (dir.equalsIgnoreCase("u")) {
/**
* moving up
*/
Position p = snake.get(snake.size() - 1); /*
* getting the last position
* of snake
*/
if ((p.getY() - 2) >= 0) {
/**
* if the snake can move up safely
*/
Position p1 = new Position(p.getX(), p.getY() - 1);
Position p2 = new Position(p.getX(), p.getY() - 2);
if (snake.contains(p1) || snake.contains(p2)) {
/**
* trying to move into a position occupied by snake
*/
if(!snake.contains(p1)){
/**
* in case if one position can be moved without touching the snake
* (even though the game is over)
*/
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
snake.add(p1);
snake.add(p2);
removeSnake();
}
} else {
/**
* only one position can be moved, game over
*/
if ((p.getY() - 1) >=0) {
Position p1 = new Position(p.getX(), p.getY()-1);
if (!snake.contains(p1)) {
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
/**
* cannot move, game over
*/
gameover = true;
}
}
} else if (dir.equalsIgnoreCase("d")) {
/**
* moving down
*/
Position p = snake.get(snake.size() - 1); /*
* getting the last position
* of snake
*/
if ((p.getY() + 2) < height) {
/**
* if the snake can move up safely
*/
Position p1 = new Position(p.getX(), p.getY() + 1);
Position p2 = new Position(p.getX(), p.getY() + 2);
if (snake.contains(p1) || snake.contains(p2)) {
/**
* trying to move into a position occupied by snake
*/
if(!snake.contains(p1)){
/**
* in case if one position can be moved without touching the snake
* (even though the game is over)
*/
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
snake.add(p1);
snake.add(p2);
removeSnake();
}
} else {
/**
* only one position can be moved, game over
*/
if ((p.getY() + 1) < height) {
Position p1 = new Position(p.getX(), p.getY()+1);
if (!snake.contains(p1)) {
snake.add(p1);
removeSnake();
}
gameover = true;
} else {
/**
* cannot move, game over
*/
gameover = true;
}
}
}
}
/**
* removing the oldest segment of snake ; ie. the first element on the list
*/
public void removeSnake() {
snake.remove(0);
}
/**
*
* @return
* true if the game is over,false if not
*/
public boolean isGameOver() {
return gameover;
}
}
//Position.java
public class Position {
private int x;
private int y;
public Position(int x, int y) {
this.x=x;
this.y=y;
}
public Position() {
}
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;
}
/**
* this method is important to compare two positions.
* ArrayList's contains() method will call this function
*/
@Override
public boolean equals(Object obj) {
if(obj instanceof Position){
Position p=(Position) obj;
if(p.getX()==this.getX() && p.getY()==this.getY()){
return true;
}
}
return false;
}
}
/*output*/