Question: New Features Finding a treasure gives the user 1, 2, or 3 points randomly. As a player, when I click on a treasure, I am

New Features

Finding a treasure gives the user 1, 2, or 3 points randomly.

As a player, when I click on a treasure, I am randomly given one, two or three points, which is shown in the Last Move field.

As a player, I can tell how many points I have accumulated in an on-screen Points field.

As a player, I can exit the game at any point using a menu.

Implement a method in the button classes that return the number of points they are worth.

Implement field to show how many points the user has next to the Tries Left field and update it when ever the user finds a treasure.

Implement a menu in the game window.

Add comments to new code

------BoardButton.java-----

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() //constructor  { 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  } } ----TreasureButton.java----
public class TreasureButton extends BoardButton { public TreasureButton() { super("$$$"); //sets button to $$$  } }

----StatsPanel.java----

import javax.swing.*; import java.awt.*; public class StatsPanel extends JPanel { // string for displaying treasures left   private static final String treasuresLeftStr = "Treasures left: %d"; // string for displaying treasures found   private static final String treasuresFoundStr = "Treasures found: %d"; // string for displaying trolls encountered   private static final String trollsEncounteredStr = "Trolls encountered: %d"; // string for tries left   private static final String triesLeftStr = "Tries left: %d"; private int treasuresLeft; // variable for treasures left   private int treasuresFound; // variable for treasures found   private int trollsEncountered; // variable for trolls encountered   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 trollsEncounteredLbl; // 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   trollsEncountered = 0; // initial trolls encountered   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   trollsEncounteredLbl = new JLabel(String.format(trollsEncounteredStr, trollsEncountered)); // new display for trolls encountered   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(trollsEncounteredLbl); // add trolls encountered 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   } public void updateTrollEncountered(boolean trollEncountered) { if (trollEncountered) { //checking if button is a troll   trollsEncountered += 1; // updating value of encountered troll   trollsEncounteredLbl.setText(String.format(trollsEncounteredStr, trollsEncountered)); //setting text of the JLabel   treasuresFound = 0; // lose all treasure if troll encountered   treasuresFoundLbl.setText(String.format(treasuresFoundStr, treasuresFound)); // updates treasures found   } updateTriesLeft(); // updates how many tries left   } private void updateTriesLeft() { triesLeft -= 1; // 1 less try each time   triesLeftLbl.setText(String.format(triesLeftStr, triesLeft)); // updates left tries   } public int getTreasuresLeft() { return treasuresLeft; // gets treasure left   } public int getTriesLeft() { return triesLeft; // gets tries left   } } 

----TreasureBoardPanel.java----

import javax.swing.*; import java.awt.*; import java.util.Random; 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 board button to specified dimensions   Random r = new Random(); // new random object   for (int i = 0; i < TreasureGame.NUM_TREASURES; i++) { // for loop to run from 0 to the number of tries defined in the constant NUM_TREASURES i.e. 20   int index = r.nextInt(this.buttons.length); // variable index for random treasures   while (this.buttons[index] != null) // loop till index i.e. random treasures is not NULL   index = r.nextInt(this.buttons.length); // random treasure objects   this.buttons[index] = new TreasureButton(); // new treasure button objects   // the troll is always one position in away from a treasure in the horizontal and vertical directions.   if(index+1 < buttons.length && this.buttons[index+1] == null) { this.buttons[index+1] = new TrollButton(); } else if (index+WIDTH < buttons.length && this.buttons[index+WIDTH] == null) { this.buttons[index+WIDTH] = new TrollButton(); } } for (int i = 0; i < this.buttons.length; i++) { if (this.buttons[i] == null) //checks if button at the index is 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)   } } // displays all treasures at the end of game.  public void displayAllTreasures() { for (int i = 0; i < this.buttons.length; i++) { if (this.buttons[i] instanceof TreasureButton) //checks if the button is a treasure button   this.buttons[i].setText(this.buttons[i].getDisplayText()); // displays all treasures at the end of game.   } } // displays all trolls at the end of game.  public void displayAllTrolls() { for (int i = 0; i < this.buttons.length; i++) { if (this.buttons[i] instanceof TrollButton) //checks if the button is a troll button   this.buttons[i].setText(this.buttons[i].getDisplayText()); // displays troll buttons   } } } 

----TreasureGame.java----

import javax.swing.*; import java.awt.*; 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   } //updating the treasure count as per the treasures found  public void updateTreasureFound(boolean treasureFound) { statsPanel.updateTreasureFound(treasureFound); // update the boolean variable tostatspanel 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) { //check if treasure is found   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  boardPanel.displayAllTrolls();// display all trolls   } } //updating the troll count as per the encountered trolls  public void updateTrollEncountered(boolean trollEncountered) { statsPanel.updateTrollEncountered(trollEncountered); // update the boolean variable tostatspanel 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");// display you lost   gameOver = true; // and game is over   } else { if (trollEncountered) { move = String.format(lastMove, "Troll"); // 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  boardPanel.displayAllTrolls(); // display all trolls   } } 
 public boolean isGameOver() { return gameOver; // gets if game is over   } } 
---TrollButton.java---- 
public class TrollButton extends BoardButton { public TrollButton() { super(""); // sets button to the icon  symbolising troll   } }
// --------- TreasureGameGUI.java - No change --------------  import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class TreasureGameGUI { public static void main(String[] args) { new TreasureGame().play(); // calling play method in treasure game class   } } 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 TrollButton) // is button is pressed and troll encountered   { game.updateTrollEncountered(true); } 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   } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!