Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this project, you will create a MineWalker game that lets a user start from the lower-left corner on a grid and attempt to walk

In this project, you will create a MineWalker game that lets a user start from the lower-left corner on a grid and attempt to walk to the upper-right corner while avoiding hidden mines.

Classes that you will create

MineWalker.java (driver)

MineWalkerPanel.java (and, potentially, other classes

) Existing class that you will use RandomWalk.java (you will use your version from p3).

Objectives

Write a program with a Graphical User Interface (GUI)

Design a good user interface

Use classes from the AWT and Swing packages

Use existing classes

Use events and listeners

Specification

User interface design.

Design a layout for the user interface. This is best done on a white board or on paper! The main visual element will be the grid of tiles that represents the mine field. This can be a two-dimensional array of JButton objects. You will also need buttons for starting a new game, a text field for the grid size, buttons for showing/hiding the mines and labels for the current score and number of lives as well as other optional elements. Plan on changing labels on buttons and disabling them when it doesn't make sense to use them. For example, the grid size cannot be changed while the game is going on. Once the user clicks on the Show Mines button, its label should change to Hide Mines. Once the game starts, the label for the New Game button should change to Give Up. Note that the labels can be different if that makes more sense to you.

Sample ScreenShot:

http://cs.boisestate.edu/~cs121/projects/p5/images/new-game.png http://cs.boisestate.edu/~cs121/projects/p5/images/show-mines-path.png http://cs.boisestate.edu/~cs121/projects/p5/images/win.png

http://cs.boisestate.edu/~cs121/projects/p5/images/small-grid.png

Creating the game.

The New Game button creates a new game as follows.

Generate a new grid of the size specified by the user (have reasonable limits on the size).

The default size is 10x10.

Generate a random walk on this grid from the lower-right corner to the upper-left corner.

This path will be used to ensure that there is a solution to the game.

Generate the hidden mines on the grid.

Use a random number generator to place mines. 25% of the tiles that are not in the random walk should be mines. You may allow the user to change from the 25% default value but that is extra credit and not required.

Game play.

At each step, the user can move one step to the north, south, west or east of the current position (except around the edges). If a user clicks on a tile that isn't valid, the program doesn't let them move there (no jumping allowed!). The game hints at the number of mines near by with a color code defined as follows: Green: There are zero mines in the four adjacent tiles Yellow: There is one mine in the four adjacent tiles Orange: There are two mines in the four adjacent tiles Red: There are three mines in the four adjacent tiles If the user reaches the goal, the game is over. Notify the user appropriately using a JOptionPane popup window . When the game is over, show all the mines on the grid along with the path that the user took. Do not allow the user to play anymore unless they start a new game. Show current square by a blinking tile color or label. For this you need to create a timer thread for the animation (similar to one we used in Project 1 or Project 3 (in the GridMap class)).

Use the following code for the startAnimation() method and call it from the constructor in MineWalkerPanel class. The animation method creates a Timer that will periodically fire an ActionEvent that needs to be handled by an ActionListener. You need to toggle the color of the button (or its label) in the actionPerformed method. Here is a template for the startAnimation method and the corresponding ActionListener: If the user steps on a mine, they die and lose one life. The game is over after number of lives reaches zero. The user gets five lives in total. After each death, the user is reborn on the last tile they were on before stepping on a mine. They can continue walking from that spot. Stepping on an already exploded mine should not result in an additional death, but it is not a valid move. Stay out of the craters. The user can give up on a game anytime by pressing the Give Up button. The score and the number of lives are updated after every move in a label somewhere on the display. Scoring. Come up with a scoring scheme. For example, the initial score can be 500. Each life costs the user a 100 points. Each step costs them 1 point. The goal is to lose as few points as possible. Or invent your own scoring scheme! Show/hide mines: Add a button that allows the user to show or hide the mines. This is very useful for debugging! Show/hide walk: Add a button that allows the user to show or hide the original random walk used to create the game. Color key: Add a useful key so the user knows what the colors mean in the game. Extra Credit (up to 10 points) Settable level of difficulty: Provide a sliding bar that allows the user to set the level of difficulty for the game. Sound effects: Set sound effects for walking, invalid moves, reaching the goal and, of course, for explosions!

RandomWalk Class:

import java.awt.*; import java.util.ArrayList; import java.util.Random;

public class RandomWalk {

private int largeness; private boolean geordie; private Random rand; private Point begin; private ArrayList sidewalk; private int x; private int y; private int INdex = 0 ; public RandomWalk(int gridsize) { this.largeness = gridsize; this.sidewalk = new ArrayList(); this.geordie = false; this.INdex = 0; this.begin = new Point(0, largeness - 1); this.x = begin.x; this.y = begin.y; sidewalk.add(begin); this.rand = new Random(); }

public RandomWalk(int gridSize, long seed) { this.largeness = gridSize; this.sidewalk = new ArrayList(); this.geordie = false; this.INdex = 0; this.begin = new Point(0, largeness - 1); this.x = begin.x; this.y = begin.y; sidewalk.add(begin); this.rand = new Random(seed); } public void step() { Point current = sidewalk.get(INdex); boolean random = rand.nextBoolean(); Point newPoint; if (random == true) { newPoint = new Point (current.x, current.y-1); if (newPoint.y < 0) { newPoint.setLocation(current.x, 0); } } else { newPoint = new Point (current.x+1, current.y); if (newPoint.x > largeness) { newPoint.setLocation(largeness - 1, current.y); } } sidewalk.add(newPoint); INdex ++; if (newPoint.x == largeness - 1 && newPoint.y == 0) { geordie = true; } }

public void createWalk() { while (geordie == false) { step(); } } public boolean isDone() { return geordie; } public ArrayList getPath() { return sidewalk; } public String toString() { String str = ""; for (Point p: sidewalk) { str += "[" + p.x + ", " + p.y + "]"; } return str; } }

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

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

What is paper chromatography?

Answered: 1 week ago

Question

Explain the cost of capital.

Answered: 1 week ago

Question

Define capital structure.

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago