3. Tresure Hunt (GUI application). Your program should display a grid of buttons
ID: 3634682 • Letter: 3
Question
3. Tresure Hunt (GUI application). Your program should display a grid ofbuttons that represent an area to be searched for treasure. Specications:
(a) The grid should be 12 rows 12 columns.
(b) The computer should randomly assign 8 dierent cells where treasure
is located.
(c) The button should change colour when it has been selected. You
may choose your own colours: I chose black for selected and red if
treasure was found.
(d) A label should display how many treasure items have been found. If
a gem has been uncovered and is clicked on again, this should NOT
increase the count of gems found.
(e) Hints:
i. Store the buttons in a 2-D array
ii. There are a couple of possible implementations for recording
where the treasure is located: parallel arrays for x and y co-
ordinates; or a 2-D array of booleans. The choice is up to you.
iii. Develop this application incrementally. It is easiest to start with
hard-coded values for the treasure locations.
(f ) Feel free to get creative with this program. You could make use of
Icons to display treasure locations, for instance
Explanation / Answer
package treasurehunt; import com.phidgets.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import treasurehunt.listeners.*; import java.util.*; /** * * @author Ben Ken */ public class TreasureHunt extends JFrame implements TimerListener, TextActionListener { // Can't use the serial number as this refers to the board and we now have only // one board for the two servos! // public static final int TIMER_SERVO_SERIAL = 14475; public static final boolean DEBUG_LCD = true; public static final boolean DEBUG_SERVO = false; public static final boolean DEBUG_LEDS = true; public static final String TEXT_BEGIN_INSTRUCT1 = "Flick the switch"; public static final String TEXT_BEGIN_INSTRUCT2 = "to begin"; public static final String TEXT_GOODBYE = "Goodbye!"; public static final String TEXT_NEW_GAME = "New Game?"; public static final String TEXT_WELCOME1 = "Treasure Hunt"; public static final String TEXT_WELCOME2 = "Find the treasures";// in the right order to win the game. Mind out there is a time limit!"; public static final String TEXT_RETUNE_REQUEST = "You must retune!"; public static final String TEXT_WRONG_TREASURE = "WRONG TREASURE"; public static final String TEXT_READY = "READY?"; public static final String TEXT_SET = "SET?"; public static final String TEXT_GO = "GO!"; public static final String TEXT_INSTRUCTION = "FIND THE TREASURE!"; public static final String TEXT_PICKUP = "Press the button to pickup the treasure"; public static final String TEXT_CONGRATS = "CONGRATULATIONS!"; public static final String TEXT_CORRECT = "You picked up the correct treasure!"; public static final String TEXT_GAME_OVER = "GAME OVER"; public static final String TEXT_YOU_LOSE = "You ran out of time!"; public static final String BANK_NAME = "Bank"; public static final String BEACH_NAME = "Beach"; public static final String CHURCH_NAME = "Church"; public static final String COBBLERS_NAME = "Cobblers"; public static final String GARAGE_NAME = "Garage"; public static final String FARM_NAME = "Farm"; public static final String PUB_NAME = "Pub"; public static final String THEATRE_NAME = "Theatre"; public static final String BANK_CLUE = "Bank Clue"; public static final String BEACH_CLUE = "Beach Clue"; public static final String CHURCH_CLUE = "Church Clue"; public static final String COBBLERS_CLUE = "Cobblers Clue"; public static final String GARAGE_CLUE = "Garage Clue"; public static final String FARM_CLUE = "Farm Clue"; public static final String PUB_CLUE = "If its a thirst you wish to quench, a pint you should clench"; public static final String THEATRE_CLUE = "Theatre Clue"; public static final int BANK_TAG = 1480962580; public static final int BEACH_TAG = 1480933111; public static final int CHURCH_TAG = 903049665; public static final int COBBLERS_TAG = 903018769; public static final int GARAGE_TAG = 1480917898; public static final int FARM_TAG = 677651345; public static final int PUB_TAG = 1482139245; public static final int THEATRE_TAG = 34491233; public static final Treasure BEACH = new Treasure(BEACH_NAME, BEACH_CLUE, BEACH_TAG, InterfacePhidgetModule.BEACH_LED); public static final Treasure COBBLERS = new Treasure(COBBLERS_NAME, COBBLERS_CLUE, COBBLERS_TAG, InterfacePhidgetModule.COBBLERS_LED); public static final Treasure PUB = new Treasure(PUB_NAME, PUB_CLUE, PUB_TAG, InterfacePhidgetModule.PUB_LED); public static final Treasure BANK = new Treasure(BANK_NAME, BANK_CLUE, BANK_TAG, InterfacePhidgetModule.BANK_LED); public static final Treasure GARAGE = new Treasure(GARAGE_NAME, GARAGE_CLUE, GARAGE_TAG, InterfacePhidgetModule.GARAGE_LED); public static final Treasure THEATRE = new Treasure(THEATRE_NAME, THEATRE_CLUE, THEATRE_TAG, InterfacePhidgetModule.THEATRE_LED); public static final Treasure CHURCH = new Treasure(CHURCH_NAME, CHURCH_CLUE, CHURCH_TAG, InterfacePhidgetModule.CHURH_LED); public static final Treasure FARM = new Treasure(FARM_NAME, FARM_CLUE, FARM_TAG, InterfacePhidgetModule.FARM_LED); public static final Treasure[] TREASURE_COLLECTION = {BEACH, COBBLERS, PUB, BANK, GARAGE, THEATRE, CHURCH, FARM}; public static final int TEXT_TYPE_PLAIN = 0; public static final int TEXT_TYPE_FLASHING = 1; private InterfacePhidgetModule pInterface; private LCDPhidgetModule pLCD; private RFIDPhidgetModule pRFID; // There is one ServoPhidgetModule per board and the new board has up to // four servos controlled using the index parameter private ServoPhidgetModule pServos; // Used to indicate when text action has completed private boolean[] textActionCompleted = { false, false }; private boolean tuned; private int generatedTuneValue; private boolean timerExpired; private boolean newGame; private java.util.List treasures; private Treasure currentTreasure; private int currentTag; private int currentPuzzle; private JButton[] buttons = new JButton[] { null, null, null, null, null, null, null, null, null, null }; private JPanel mainPanel; private JTextField textfield; public TreasureHunt() { setTitle("FrameDemo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); newGame = true; timerExpired = false; generatePuzzleOrder(); currentPuzzle = 0; currentTag = -1; connectHardware(); initFrame(); displayInstructions(); // Wait for switch to be flicked to start the game pInterface.waitingForSwitch(true); } private void initFrame() { int button = 0; buttons[button] = new JButton("Flick Switch"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pInterface.switchChanged(); } }); button++; buttons[button] = new JButton("Scan Beach"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(BEACH_TAG); } } }); button++; buttons[button] = new JButton("Scan Cobblers"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(COBBLERS_TAG); } } }); button++; buttons[button] = new JButton("Scan Pub"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(PUB_TAG); } } }); button++; buttons[button] = new JButton("Scan Bank"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(BANK_TAG); } } }); button++; buttons[button] = new JButton("Scan Garage"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(GARAGE_TAG); } } }); button++; buttons[button] = new JButton("Scan Theatre"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(THEATRE_TAG); } } }); button++; buttons[button] = new JButton("Scan Church"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(CHURCH_TAG); } } }); button++; buttons[button] = new JButton("Scan Farm"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (pRFID.isWaitingForRFIDScan()) { processTagHashCode(FARM_TAG); } } }); button++; buttons[button] = new JButton("Touch Touch-Sensor"); buttons[button].setAlignmentX(Component.LEFT_ALIGNMENT); buttons[button].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pInterface.touchSensorChanged(); } }); mainPanel = new JPanel(); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); for (button = 0; button < 10; button++) { mainPanel.add(buttons[button]); } textfield = new JTextField(""); textfield.setEditable(false); Container mainPane = getContentPane(); mainPane.setLayout(new BorderLayout()); mainPane.add(mainPanel, BorderLayout.NORTH); mainPane.add(textfield, BorderLayout.SOUTH); pack(); setVisible(true); } public void generatePuzzleOrder() { java.util.List puzzleOrder = new ArrayList(); treasures = new ArrayList(); Random generator = new Random(); for(int i = 0; i < 8; i++) { int tempVal = generator.nextInt(8); while(puzzleOrder.indexOf(tempVal) != -1) { tempVal = generator.nextInt(8); } puzzleOrder.add(tempVal); treasures.add(TREASURE_COLLECTION[tempVal]); } System.out.println(puzzleOrder); } public void runGame() { pInterface.waitingForSwitch(false); pInterface.waitingForTouchSensor(false); // Test LEDs - to be removed pInterface.setLEDState(pInterface.BANK_LED, true); pInterface.setLEDState(pInterface.BEACH_LED, true); pInterface.setFlashingLED(pInterface.CHURH_LED, 500, 1, null); pInterface.setFlashingLED(pInterface.COBBLERS_LED, 400, 2, null); pInterface.setFlashingLED(pInterface.FARM_LED, 300, 3, null); pInterface.setFlashingLED(pInterface.GARAGE_LED, 500, pInterface.REPEAT_FOREVER, null); pInterface.setLEDState(pInterface.PUB_LED, true); pInterface.setLEDState(pInterface.THEATRE_LED, true); resetTextActionStatus(); pLCD.displayText(TEXT_WELCOME1, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_PLAIN, this); pLCD.displayText(TEXT_WELCOME2, LCDPhidgetModule.DISPLAY_LINE_1, TEXT_TYPE_PLAIN, this); waitForAllTextActionCompleted(); resetTextActionStatus(); pLCD.timedClearLCDText(500, this); waitForAllTextActionCompleted(); resetTextActionStatus(); pLCD.displayText(TEXT_READY, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_PLAIN, 1000, this); waitForTextActionCompleted(LCDPhidgetModule.DISPLAY_LINE_0); resetTextActionStatus(); pLCD.displayText(TEXT_SET, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_PLAIN, 1000, this); waitForTextActionCompleted(LCDPhidgetModule.DISPLAY_LINE_0); resetTextActionStatus(); pLCD.displayText(TEXT_GO, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_PLAIN, 1000, this); waitForTextActionCompleted(LCDPhidgetModule.DISPLAY_LINE_0); // Test LEDs - to be removed pInterface.resetAllLEDs(); //Start Timer pServos.startTimer(this); displayPuzzleClue(0); pRFID.waitingForRFIDScan(true); //Finish flag //Finish timer //Finish Text } private void displayPuzzleClue(int index) { pLCD.displayText(TEXT_INSTRUCTION, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_PLAIN); pLCD.displayText(treasures.get(index).getClue(), LCDPhidgetModule.DISPLAY_LINE_1, TEXT_TYPE_PLAIN); } private void displayInstructions() { pLCD.displayText(TEXT_BEGIN_INSTRUCT1, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_PLAIN); pLCD.displayText(TEXT_BEGIN_INSTRUCT2, LCDPhidgetModule.DISPLAY_LINE_1, TEXT_TYPE_PLAIN); } private void connectHardware() { try { pInterface = new InterfacePhidgetModule(this); } catch(PhidgetException e) { System.out.println("Interface Kit could not connect, exiting..."); System.exit(1); } try { pLCD = new LCDPhidgetModule(this); } catch (PhidgetException ex) { ex.printStackTrace(); System.out.println("LCD could not connect, exiting..."); System.exit(1); } try { pRFID = new RFIDPhidgetModule(this); } catch(PhidgetException e) { System.out.println("RFID could not connect, exiting..."); System.exit(1); } try { pServos = new ServoPhidgetModule(this); } catch(PhidgetException ex) { ex.printStackTrace(); System.out.println("Servos could not connect, exiting..."); System.exit(1); } try { Thread.sleep(500); } catch(InterruptedException e) { } // pInterface.setReady(true); } private void reset() { pInterface.resetAllLEDs(); pLCD.clearLCDText(); pServos.reset(1); pServos.reset(0); /* catch(PhidgetException e) { System.out.println("Cannot reset the phidgets, exiting..."); System.exit(0); } */ } public void untune() { pLCD.displayText(TEXT_RETUNE_REQUEST, LCDPhidgetModule.DISPLAY_LINE_1); pLCD.displayFlashingText(TEXT_WRONG_TREASURE, LCDPhidgetModule.DISPLAY_LINE_0, 500, 3, this); pInterface.untune(); } private void retune() { generateRandomNumber(); boolean tuned = false; // Display retuning stuff on LCD } public void timerExpired(int servoIndex) { //TODO Game Over pInterface.waitingForTouchSensor(false); pRFID.waitingForRFIDScan(false); pLCD.displayText(TEXT_GAME_OVER, LCDPhidgetModule.DISPLAY_LINE_0, TEXT_TYPE_FLASHING); pLCD.displayText(TEXT_YOU_LOSE, LCDPhidgetModule.DISPLAY_LINE_1, TEXT_TYPE_PLAIN); pInterface.waitingForSwitch(true); } public void tagStopTimer() { pServos.stopTimer(pServos.TIMER_SERVO_INDEX); } public void textActionCompleted(int displayLine) { synchronized (textActionCompleted) { textActionCompleted[displayLine] = true; } } private void resetTextActionStatus() { synchronized (textActionCompleted) { textActionCompleted[LCDPhidgetModule.DISPLAY_LINE_0] = false; textActionCompleted[LCDPhidgetModule.DISPLAY_LINE_1] = false; } } private void waitForTextActionCompleted(int displayLine) { while (!textActionCompleted[displayLine]) { try { Thread.sleep(100); } catch(InterruptedException e) { } } } private void waitForAllTextActionCompleted() { while ((!textActionCompleted[LCDPhidgetModule.DISPLAY_LINE_0]) || (!textActionCompleted[LCDPhidgetModule.DISPLAY_LINE_1])) { try { Thread.sleep(100); } catch(InterruptedException e) { } } } private void generateRandomNumber() { double rand; rand = Math.random() * 1000; generatedTuneValue = (int)rand; } private Treasure findTreasure(int rFIDValue) { for(int i = 0; i 7) { //YOU WIN! //Raise flag } } public static void main(String args[]) { new TreasureHunt(); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.