Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Snake Game in Java Game Description: In a snake game such as Nibbles (see https://en.wikipedia.org/wiki/Nibbles_(video_game)) the objective is to navigate a snake through a walled

Snake Game in Java

Game Description:

In a snake game such as Nibbles (see https://en.wikipedia.org/wiki/Nibbles_(video_game)) the objective is to navigate a snake through a walled space (or maze), consuming food along the way. The user must avoid colliding with walls or the snake's ever-growing body. The length of the snake increases each time food is consumed, so the difficulty of avoiding a collision increases as the game progresses.

Problem Specification:

You are going to write a GameManager class to handle your snake, wall, and food bookkeeping. You will probably want some additional classes/interfaces to accomplish this, but I leave that design up to you. You will demonstrate its functionality in a GameManagerTester class. The GameManager and related classes should be independent of any future GUI code. You will configure your GameManager object by reading in a level configuration file.

Level File Format:

A level configuration file specifies the size of the game area and the locations of the walls. It is a text file of numbers separated by whitespace. The first line in the file contains two integer values specifying the width and height of the game area. If there is additional data on this line, it may be ignored. The following lines (if any) specify the walls. A wall line contains contains four integer values: the first two are the x and y position of the upper left corner of the wall, the second two are the x and y position of the the lower right corner of the wall. If there is additional data on this line, it may be ignored.

GameManager class:

Although you may chose to use one fixed level design for your final game, your class should be able to handle arbitrary positive values for width and height of the playing area. Your class must provide the following functionality. Construct a new manager of a given size. Add walls in specified locations, as read from a level configuration file. Add at least one snake at some unoccupied location. Add at least one food item at some random unoccupied location. - The final game will use random placement for the food, but for this test program you should have reproducible results each time it is run. - One simple way to have "random" behaviour that can be repeated is to use a Random object that has been initialized with a fixed seed. Override toString in your GameManager class to provide a string representation of your board that can be printed to the terminal for debugging and testing purposes. Make it clear what locations are walls, food, snake, and empty space. Update snake position one step and check for collisions. - If the snake collides with food, the snake eats the food (removes it from game) and grows in length. A new food item is placed in at some random unoccupied location. - If the snake collides with a wall or its own body, detect this somehow. In the full game, the game would be over (or maybe the player would lose a life, depending on your game design), but for the testing code, we'll just report the collision. I fully expect that much of the GameManager logic will be handled by additional game object classes, such as Snake, Wall, and Food. You may want to make some or all of these classes extend a common abstract parent or implement a common interface.

GameManagerTester class:

The purpose of this testing class is to demonstrate to us that your game manager works correctly, so you need to put it through its paces by creating and manipulating GameManager objects as outlined below.

You should write least two GameManager objects with separate sizes and wall configurations. At least one of them must be initialized from a level configuration file. Demonstrate that they operate independently of each other. (I want to make sure you aren't locked into just one configuration and that you aren't making things static inappropriately.)

Add at least one food item and snake to each manager.

Test moving the snake around in each manager:

- Print out the string representation of the manager before you begin.

- Update snake position and print the manager after the update. - Demonstrate changing snake direction.

- Demonstrate eating food and show that snake grows.

- After testing the each manager, continue testing with the first manager again to demonstrate that it is independent of the second.

- Demonstrate collisions of the snake with a wall and the snake with itself.

Don't just copy and paste the same test code for each manager. You should try to structure your code sensibly with methods rather than writing one giant main method.

Your testing output should come from GameManagerTester, not GameManager or any other game object classes that you may write. Don't put console output in the middle of your game logic.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions