Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

New Game Features The board now also contains 20 trolls that will take away all your treasure if you land on one! 20 distinct trolls

New Game Features

The board now also contains 20 trolls that will take away all your treasure if you land on one!

20 distinct trolls (not on top of each other or a treasure)

User Stories

As a player, when I click on the board, in addition to a treasure or nothing, I may encounter a troll at random positions.

As a player, when I click on the board and encounter a troll, the troll is always one position in away from a treasure in the horizontal and vertical directions.

As a player, when I click on a troll, I lose all your treasures.

As a player, when the game is over, I can see where all the treasures and trolls are located on the board.

Technical Implementation

Follow the these directions:

Implement each story one at a time in the order given.

Keep each class in a separate file.

Use the CRC card format to explain in a comment right before the class definition what the responsibilities and functions of the class are. Be sure to have a Responsibilities and a Collaborating Classes sections.

Extend the BoardButton (or JButton class, depending on what you did in the first week) to create a TrollButton class instances of which are on the board

Change the look of the button when a user clicks on it: change the text or the icon

public class TreasureGameGUI { public static void main(String[] args) { new TreasureGame().play(); //calling play method in treasure game class } }

import java.awt.BorderLayout; //border layout package for borders import java.awt.Dimension; //dimension package for dimension of board import java.awt.event.ActionEvent; //action event class for actions done import java.awt.event.ActionListener; //action listener for listening to action event import javax.swing.JFrame; //for j frames import javax.swing.JLabel; //for j labels on frames import javax.swing.SwingConstants; //for swing constants used for positioning public class TreasureGame extends JFrame { protected static final int NUM_TRIES = 50; //variable for max number of tries protected static final int NUM_TREASURES = 20; //variable for max number of treasures private static final String lastMove = "Last move: %s"; //variable to store last move private JLabel lastMoveLbl; //object named lastmoveLbl private StatsPanel statsPanel; //object named stats panel private TreasureBoardPanel boardPanel; //object named board panel private boolean gameOver; //game over variable for true or false public TreasureGame() { super("Treasure Hunt Game"); //sending string to super class setLayout(new BorderLayout()); //setting layout by calling object for borderlayout class setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //method used to specify one of several options for close button setVisible(true); //method to set the visibility } public void play() { gameOver = false; //setting gameover variable to false initially addComponents(); //calling add components method pack(); //calling pack method } private void addComponents() { JLabel headerLbl = new JLabel("Treasure Hunt"); //headerlbl object sending treasure hunt string to constructor headerLbl.setPreferredSize(new Dimension(60, 40)); //sets sizes of screen headerLbl.setHorizontalAlignment(SwingConstants.CENTER); //display area for a short string or image add(headerLbl, BorderLayout.NORTH); //add method to add theo object headerlbl onto the north side of the screen statsPanel = new StatsPanel(); //new stats panel object add(statsPanel, BorderLayout.WEST); //add stats panel to the west side boardPanel = new TreasureBoardPanel(this); //new treasure board panel object add(boardPanel, BorderLayout.CENTER); //adds board panel to the center lastMoveLbl = new JLabel(String.format(lastMove, "")); //getting the last move into lastmovelbl object lastMoveLbl.setPreferredSize(new Dimension(60, 40)); //setting lastmovelbl dimensions lastMoveLbl.setHorizontalAlignment(SwingConstants.CENTER); //sets alignment of lastmovelbl add(lastMoveLbl, BorderLayout.SOUTH); //places lastmovelbl to south side } public void updateTreasureFound(boolean treasureFound) { statsPanel.updateTreasureFound(treasureFound); //update the boolean variable to statspanel object String move = ""; //declaring string move empty if (statsPanel.getTreasuresLeft() == 0) //if statspanel is at 0 { move = String.format(lastMove, "Game over - You win"); //display you win gameOver = true; //and game is over } else if (statsPanel.getTriesLeft() == 0) //if tries left is at 0 { move = String.format(lastMove, "Game over - You lose");//diplay you lost gameOver = true; // and game is over } else { if (treasureFound) move = String.format(lastMove, "Treasure"); //updates lastmove variable with treasure found else move = String.format(lastMove, "Miss"); //updates lastmove variable with miss } lastMoveLbl.setText(move); //setting lastmove variable if (gameOver && statsPanel.getTreasuresLeft() != 0) //if game is over and still have treasures left boardPanel.displayAllTreasures(); //display all treasures } public boolean isGameOver() { return gameOver; //gets if game is over } } class ButtonListener implements ActionListener { private TreasureGame game; //object for treausre game class public ButtonListener(TreasureGame game) { this.game = game; //calls game } @Override public void actionPerformed(ActionEvent ae) { if (!game.isGameOver()) //checks if game is over or not { BoardButton btn = (BoardButton) ae.getSource(); //object for board button btn.setText(btn.getDisplayText()); //call settext method if (btn instanceof TreasureButton) //is button is pressed and treasure if found game.updateTreasureFound(true); //pass true else game.updateTreasureFound(false); //or else pass false btn.setEnabled(false); //set button enabling to false } } }

import java.awt.Dimension; //dimension package import javax.swing.Box; //box package import javax.swing.BoxLayout; //boxlayout package import javax.swing.JLabel; // for jlabels import javax.swing.JPanel; //for jpanels

public class StatsPanel extends JPanel { private static final String treasuresLeftStr = "Treasures left: %d"; //string for displaying treasures left private static final String treasuresFoundStr = "Treasures found: %d"; //string for displaying treasures found private static final String triesLeftStr = "Tries left: %d"; //string for tries left private int treasuresLeft; //variable for treasures left private int treasuresFound; //variable for treasures found private int triesLeft; //variable for tries left private JLabel treasuresLeftLbl; //object named treasure left lbl private JLabel treasuresFoundLbl; //object named treasures found lbl private JLabel triesLeftLbl; //object named tries left public StatsPanel() { super(); //calls super class setPreferredSize(new Dimension(200, 100)); //sets preferred dimensions setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // treasuresLeft = TreasureGame.NUM_TREASURES; //calls num num treasures to find amount of treasures left treasuresFound = 0; //initial treasures found triesLeft = TreasureGame.NUM_TRIES; //calls num tries to find amount treasuresLeftLbl = new JLabel(String.format(treasuresLeftStr, treasuresLeft)); //new display for treasures left treasuresFoundLbl = new JLabel(String.format(treasuresFoundStr, treasuresFound)); //new display for treasures found triesLeftLbl = new JLabel(String.format(triesLeftStr, triesLeft)); //new display for tries left add(Box.createVerticalGlue()); //specify excess space above statspanel add(treasuresLeftLbl); //add treasure left display add(treasuresFoundLbl); //add treasure found display add(triesLeftLbl); //add tries left display add(Box.createVerticalGlue()); //specift excess space below stats panel } public void updateTreasureFound(boolean treasureFound) { if (treasureFound) { treasuresFound += 1; //if treasure found + 1 to treasures found treasuresFoundLbl.setText(String.format(treasuresFoundStr, treasuresFound)); //updates treasures found treasuresLeft -= 1; //if treasure found -1 to treasures left treasuresLeftLbl.setText(String.format(treasuresLeftStr, treasuresLeft)); //updates treausres left } updateTriesLeft(); //updates how many tries left } private void updateTriesLeft() { triesLeft -= 1; //1 less try each time triesLeftLbl.setText(String.format(triesLeftStr, triesLeft)); //updates tries left } public int getTreasuresLeft() { return treasuresLeft; //gets treasure left } public int getTriesLeft() { return triesLeft; //gets tries left } }

import java.awt.GridLayout; //for grid layout import java.util.Random; //for random class import javax.swing.JPanel; //for j panel package

public class TreasureBoardPanel extends JPanel { private static final int WIDTH = 10; //variable for width of panel private static final int HEIGHT = 10; //variable for height of panel private BoardButton[] buttons; //variable array for buttons private TreasureGame game; //object for treasure game public TreasureBoardPanel(TreasureGame game) { super(); //calls super class this.game = game; //sets game setLayout(new GridLayout(WIDTH, HEIGHT)); //sets layout addButtons(); //adds button method calling } private void addButtons() { this.buttons = new BoardButton[WIDTH * HEIGHT]; //sets boardbutton to specified dimensions Random r = new Random(); //new random object for (int i = 0; i < TreasureGame.NUM_TREASURES; i++) { int index = r.nextInt(this.buttons.length); //variable index for random treasures while (this.buttons[index] != null) index = r.nextInt(this.buttons.length); //random treasure objects this.buttons[index] = new TreasureButton(); //new treasure button objects } for (int i = 0; i < this.buttons.length; i++) { if (this.buttons[i] == null) this.buttons[i] = new BoardButton(); //new button object } for (int i = 0; i < this.buttons.length; i++) { this.buttons[i].addActionListener(new ButtonListener(game)); //action listener for button(boxes) add(this.buttons[i]); //adds buttons(boxes) } } public void displayAllTreasures() { for (int i = 0; i < this.buttons.length; i++) { if (this.buttons[i] instanceof TreasureButton) this.buttons[i].setText(this.buttons[i].getDisplayText()); //displays all treasures at the end of game } } }

import java.awt.Dimension; //for setting dimensions import javax.swing.JButton; //for setting buttons import javax.swing.SwingConstants; //for swing constants

public class BoardButton extends JButton { private String displayText; //string variable for display text public BoardButton() { this("O"); //setting string to O } public BoardButton(String text) { super(); //sends string to super class this.displayText = text; //display text passed setHorizontalAlignment(SwingConstants.CENTER); //place constants on the center of button setPreferredSize(new Dimension(60, 40)); //setting dimensions for buttons } public String getDisplayText() { return displayText; //gets display text } }

public class TreasureButton extends BoardButton { public TreasureButton() { super("$$$"); //sets button to $$$ } }

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

More Books

Students also viewed these Databases questions