Josh
Josh

Reputation: 31

Randomise button position

Having a small problem with a programme I am working on. I am making a very basic treasure hunt game where the user clicks on buttons located on a gui to attempt to find the treasure. If the user identifies the location of the button, the button changes to an image of a treasure chest if not it changes to a picture of an empty hole. My issue is I want to randomise where the treasure chest will be every time the game starts as opposed to being fixed in button 5. I thought maybe of using a random number generator but could not make the connection of how to use it.

 import java.awt.*;
 import javax.swing.*;
 import java.util.Random;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;



 public class Grid extends JFrame implements ActionListener {

 JButton but0, but1, but2, but3, but4, but5, but6, but7, but8;

 JLabel label1, label2, label3;    

  ImageIcon image1, image2, image3, image4, image5;


  JTextField textResult;    

  public static void main(String[] args) {



  new Grid();

   }


   public Grid (){

  Random rand = new Random();
  int n = rand.nextInt(9) + 1;
  System.out.println(n);


  this.setSize(700,700);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("Treasure Hunt Game");

  JPanel thePanel = new JPanel();


  thePanel.setLayout(new GridLayout(0,3,0,0));

  image1 = new ImageIcon(getClass().getResource("Treasure.jpg"));
  image2 = new ImageIcon(getClass().getResource("Pirate.jpg"));
  image3 = new ImageIcon(getClass().getResource("sand2.jpg"));
  image4 = new ImageIcon(getClass().getResource("emptyhole.jpg"));   
  image5 = new ImageIcon(getClass().getResource("map.jpg"));

  label1 = new JLabel("Click the buttons to find the Treasure!");
  label2 = new JLabel(image5); 
  label3 = new JLabel(image2);



  but0 = new JButton(image3);
  but1 = new JButton(image3);
  but2 = new JButton(image3);
  but3 = new JButton(image3);
  but4 = new JButton(image3);
  but5 = new JButton(image3);
  but6 = new JButton(image3);
  but7 = new JButton(image3);
  but8 = new JButton(image3);



  thePanel.add(but1);
  thePanel.add(but2);
  thePanel.add(but3);
  thePanel.add(but4);
  thePanel.add(but5);
  thePanel.add(but6);
  thePanel.add(but7);
  thePanel.add(but8);
  thePanel.add(but0);
  thePanel.add(label1); 
  thePanel.add(label2);
  thePanel.add(label3);



  this.add(thePanel);

  this.setVisible(true);

  but1.addActionListener(this);
  but2.addActionListener(this);
  but3.addActionListener(this);
  but4.addActionListener(this);
  but5.addActionListener(this);
  but6.addActionListener(this);
  but7.addActionListener(this);
  but8.addActionListener(this);
  but0.addActionListener(this);


    }
     public void actionPerformed(ActionEvent evt) {
     Object obj1=evt.getSource();
    if(obj1==but5){
     label1.setText("You've found the treasure!");

  }
  Object obj2=evt.getSource();
  if(obj2==but1){
     label1.setText("Empty hole!");

  }
  Object obj3=evt.getSource();
  if(obj3==but2){
     label1.setText("Empty hole!");
  }
  Object obj4=evt.getSource();
  if(obj4==but3){
     label1.setText("Empty hole!");
  }
  Object obj5=evt.getSource();
  if(obj5==but4){
     label1.setText("Empty hole!");

  }
  Object obj6=evt.getSource();
  if(obj6==but6){
     label1.setText("Empty hole!");
  }
  Object obj7=evt.getSource();
  if(obj7==but7){
     label1.setText("Empty hole!"); 

  }
  Object obj8=evt.getSource();
  if(obj8==but8){
     label1.setText("Empty hole!");
  }
  Object obj9=evt.getSource();
  if(obj9==but0){
     label1.setText("Keep looking!");
  }
  Object obj10=evt.getSource();
  if(obj10==but1){
     but1.setIcon(image4);
  }
  Object obj11=evt.getSource();
  if(obj11==but2){
     but2.setIcon(image4);
  }
  Object obj12=evt.getSource();
  if(obj12==but3){
     but3.setIcon(image4);
  }
  Object obj13=evt.getSource();
  if(obj13==but4){
     but4.setIcon(image4);
  }
  Object obj14=evt.getSource();
  if(obj14==but5){
     but5.setIcon(image1);
  }
  Object obj15=evt.getSource();
  if(obj15==but6){
     but6.setIcon(image4);
  }
  Object obj16=evt.getSource();
  if(obj16==but7){
     but7.setIcon(image4);
  }
  Object obj17=evt.getSource();
  if(obj17==but8){
     but8.setIcon(image4);
  }
  Object obj18=evt.getSource();
  if(obj18==but0){
     but0.setIcon(image4);
    }




   }
 }

Upvotes: 0

Views: 209

Answers (1)

OrangeMan
OrangeMan

Reputation: 692

I would create an array of buttons as it is more efficient and easier to randomise

JButton[] buttons = new JButton[9]
buttons[0] = new JButton(image3)
buttons[1] = new JButton(image3)
...

(or you could iterate through them)

at the start of your program import random

import java.util.Random;

then later in the program

int treasureLocation = new Random().nextInt(buttons.lenth)

This will give you a random number between 0 and 9

In the actionPerformed method

if(evt.getSource() == buttons[treasureLocation]) {
    // Do whatever, they found the treasure
}
else {
    // They didn't find the treasure
}

Hope this helps :)

Upvotes: 2

Related Questions