Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Creating a Snake Game using Java: Two-Dimensional Arrays, SinglyLinkedList, and SnakeBoard, Lecture notes of Construction

The creation of a snake game using java, with a focus on two-dimensional arrays and a singlylinkedlist. The game's objective is to control a snake, avoiding collisions with the board's edges or its own body, while eating targets to grow longer. Background information on the game, class instructions, and an assignment to create a coordinate class, edit the snakeboard class, and edit the snakegame class.

What you will learn

  • How do you control the snake's movement and growth in the Snake game?
  • What is the role of two-dimensional arrays and SinglyLinkedList in creating a Snake game?
  • How do you create a Snake game using Java?

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

jeanette
jeanette 🇬🇧

3.7

(7)

238 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
SnakeGame.java
Objective: To use a two-dimensional arrays and a SinglyLinkedList to create the Snake game.
Background:
The Snake Game has been around since the 1970’s in one form or another. The first programs were rendered
completely with ASCII characters on a terminal, then later with more sophisticated graphics on computers and
eventually on handheld devices. The allure of the game is its simplicity in the interface and the rules while
being completely addicting to play.
Our game will be modeled on the original ASCII version played on a two-dimensional grid or “board". The
“snake” will be a series of characters with the head (@) followed by tail segments (***). An example of the
board below shows the snake with a head and a four segment tail.
The user will control the snake and command the head to move up, down, left, or right with the body trailing
behind. The snake can only move to open locations and cannot go off the board or cross over itself. In the game,
you will lose immediately (snake dies) if either move was attempted.
The objective of the game is for the snake to “eat” as many targets as it can. When a target is eaten, the snake’s
tail grows by one segment and a new target location is chosen at random on the board. The longer the tail
grows, the less open spaces are available, and the more difficult it is for the snake to move. In the figure below,
the snake’s tail has become so long it is blocking a clear path to the target.
In our game, we will use an array to represent the board and a SinglyLinkedList to represent the snake.
+ - - - - - - - - - - - - - - - +
| |
| @ * * * |
| * |
| |
| |
| |
| |
| + |
| |
| |
+ - - - - - - - - - - - - - - - +
Score: 0
+ - - - - - - - - - - - - - - - +
| * * * * * * |
| * * |
| @ * * |
| * * * * * * * * |
| * * * |
| * * * |
| * * * * |
| * * * * * |
| + * |
| * * * * * * * |
+ - - - - - - - - - - - - - - - +
Score: 30
pf3
pf4

Partial preview of the text

Download Creating a Snake Game using Java: Two-Dimensional Arrays, SinglyLinkedList, and SnakeBoard and more Lecture notes Construction in PDF only on Docsity!

SnakeGame.java

Objective : To use a two-dimensional arrays and a SinglyLinkedList to create the Snake game. Background : The Snake Game has been around since the 1970’s in one form or another. The first programs were rendered completely with ASCII characters on a terminal, then later with more sophisticated graphics on computers and eventually on handheld devices. The allure of the game is its simplicity in the interface and the rules while being completely addicting to play. Our game will be modeled on the original ASCII version played on a two-dimensional grid or “board". The “snake” will be a series of characters with the head (@) followed by tail segments (***). An example of the board below shows the snake with a head and a four segment tail. The user will control the snake and command the head to move up, down, left, or right with the body trailing behind. The snake can only move to open locations and cannot go off the board or cross over itself. In the game, you will lose immediately (snake dies) if either move was attempted. The objective of the game is for the snake to “eat” as many targets as it can. When a target is eaten, the snake’s tail grows by one segment and a new target location is chosen at random on the board. The longer the tail grows, the less open spaces are available, and the more difficult it is for the snake to move. In the figure below, the snake’s tail has become so long it is blocking a clear path to the target. In our game, we will use an array to represent the board and a SinglyLinkedList to represent the snake.

                    • - - - - - - + | | | @ * * * | | * | | | | | | | | | | + | | | | | + - - - - - - - - - - - - - - - + Score: 0 + - - - - - - - - - - - - - - - + | * * * * * * | | * * | | @ * * | | * * * * * * * * | | * * * | | * * * | | * * * * | | * * * * * | | + * | | * * * * * * * | + - - - - - - - - - - - - - - - + Score: 30

Coordinate and Snake classes : You will need to write a Coordinate class. A Coordinate object holds one location on the board. It has a row field and column field, and the constructor initializes those fields. There are also two accessor methods, getRow() and getCol() , and an equals() method. The Snake class is almost complete. Open the file and take a look. Notice the first line of the class definition. public class Snake extends SinglyLinkedList The Snake class extends the SinglyLinkedList class which holds a list of Coordinate objects. The Snake class inherits all of SinglyLinkedList ’s methods, so very little needs to be defined in this class. Make the Snake ’s constructor create a default Snake that takes up 5 vertical Coordinates on the board. SnakeBoard class : The SnakeBoard class knows how to construct the board, place a Snake and target on the board, and print the board to the screen. The SnakeBoard will be stored as a two-dimensional array of char ’s. The SnakeGame class will use the SnakeBoard whenever it needs to display the board to the screen. The SnakeBoard contains the border, the Snake , and the target. The diagram below shows a board with a 10- by-15 field, the Snake (@****), and the target (+). The head of the Snake is designated by the at-sign (@), and the Snake always moves head-first. Notice that the row numbers increase from top-to-bottom and column numbers left-to-right. Coordinates are in the form (row, column). Assignment - Coordinate and SnakeBoard :

  1. Download the zip file from Mr Greenstein’s web site. Unzip the file and it will create a directory SnakeGame. Do all of your work in this directory. In the directory you will find the files Snake.java , SnakeBoard.java , SnakeGame.java , and SnakeGame.jar. The Snake.java file is complete and should not be altered. It will be used by the other classes. The SnakeGame.jar file contains a working version of the game using the following command: java -cp SnakeGame.jar SnakeGame The remaining files SnakeBoard.java and SnakeGame.java will be used below.

Assignment - SnakeGame :

  1. Edit the SnakeGame class given in the zip file. It needs a constructor to initialize the fields. Initialize the SnakeBoard to a size of 20 rows and 25 columns. At the beginning of every game, place the Snake and randomly place the target on the board. Remember, the target cannot be placed on top of the Snake! You will need the Prompt class to accept input from the user.
  2. In SnakeGame , build a “save and restore game” feature so you can resume the game at a later time. You will need the FileUtils class to read from and write to files. Play the game in SnakeGame.jar and save it to a file ‘f’. This will create a snakeGameSave.txt file that you can use as a model for your saved game.