Sign in to follow this  
White Wold

Board Game Problem(Java)

Recommended Posts

Hello, In my Board.Java class there is a function which creates a random number from 1 to 10 when the button "take a spin" is clicked .If you press the button 10 times, 10 random numbers are generated but the problem is I need to be able to access those random numbers in my main class. So in my main I have: while(stop == false){ System.out.println("The random number is" + b.getSpin()); } thats an infite loop and should technically output each number by calling the getSpin() method from Board.Java,problem is it will get the number and print it infinite times which cannot happen. Here is my source code :)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

public class CoolioMain{
  static Board b = new Board();
  static int x = 0;
  static boolean stop = false;

  public static void main(String[] args){
    b.setSize(720,550);
    b.setVisible(true);

    while(stop == false){
      System.out.println("The random number is" + b.getSpin());
    }
  }
} 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;


class Board extends JFrame{
  private JLabel lb1,lb2;
  private JTextField  tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9;
  private JRadioButton rd1, rd2, rd3, rd4;
  private JButton spin;
  private ButtonGroup bgroup = new ButtonGroup();
  private TitledBorder messagePanelBorder; //TitledBorder for the msg panel
  private JTextField outputTF;
  public int n;
  private String nStr;
  private int currentPlayer;

  public Board(){
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setTitle("The Game of Life");
    JPanel p1 = new JPanel(); //create new Panel for the Instruction
    p1.setLayout(new FlowLayout());
    p1.add(lb1 = new JLabel("Instructions"));
    p1.add(tf1 = new JTextField(50));               //create TextField
    tf1.setEditable(false);                          //textField is editable
    p1.setBorder(new TitledBorder("Instructions"));
    p1.setBackground(Color.red);

    JPanel p2 = new JPanel();
    p2.setLayout(new GridLayout(4,0));
    p2.add(rd1 = new JRadioButton("Player1"));
    rd1.setBackground(Color.pink);
    p2.add(rd2 = new JRadioButton("Player2"));
    rd2.setBackground(Color.pink);
    p2.add(rd3 = new JRadioButton("Player3"));
    rd3.setBackground(Color.pink);
    p2.add(rd4 = new JRadioButton("Player4"));
    rd4.setBackground(Color.pink);
    p2.setBorder(new TitledBorder("Players"));
    p2.setBackground(Color.red);
    bgroup.add(rd1);
    bgroup.add(rd2);
    bgroup.add(rd3);
    bgroup.add(rd4);

    JPanel p3 = new JPanel();
    p3.setLayout(new GridLayout(10,0));

    outputTF =  new JTextField(5);
    outputTF.setEditable(false);

    spin = new JButton("Take a Spin");

    spin.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
        if ("Take a Spin".equals(actionCommand)){
          Random r = new Random(); // don't make it here!
          for (int i = 1; i < 2; ++i){ // loop only once !!! what for???
            n = r.nextInt(10) + 1;
          }
          nStr = Integer.toString(n);
          outputTF.setText(nStr);
          getSpin(); // useless, what for???
        }
      }
    });

    p2.add(spin) ;
    p2.add(outputTF) ;
    p2.add(outputTF, BorderLayout.EAST);

    JPanel rec = new JPanel();
    rec.setLayout(new BorderLayout());
    JPanel box = new JPanel();
    box.setLayout(new BorderLayout());
    box.add(p1, BorderLayout.CENTER);
    box.add(p2, BorderLayout.WEST);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(p3, BorderLayout.WEST);
    getContentPane().add(box, BorderLayout.SOUTH);
  }

  public int getSpin(){
    return n;
  }
} 


Share this post


Link to post
Share on other sites
you are only generating a random number once, when you assign its value to "n". You need to make sure you generate a new number everytime the getSpin() method is called:


public int getSpin(){
return r.nextInt(10)+1;
}

Share this post


Link to post
Share on other sites
0) Are those "useless" etc. comments your own? If so, better deal with them now, while you still can. After all, you don't want to be made fun of. If not, then presumably you have some sort of mentor; maybe you should be asking him/her for help instead?

1) Well, of course it loops infinitely. How exactly are you expecting 'stop' to get set to true? There's no code in place to change it. It continually outputs the number because, well, it doesn't get changed unless you invoke the ActionListener. If you only wanted to output the number when it changes, then output it as part of the ActionListener processing. If your concern is just about accessing the value, then your setup is fine - you just need to be able to write the main loop in such a way that it can deal with getSpin() working the way that it does. What exactly do you want to *happen* in main()?

2) Scope your variables as tightly as you can, in general. That means *not* using member variables for something that is only relevant to that one function; use a local instead. Actually, for simple expressions that aren't reused, don't even bother declaring a variable. For example, you can and should write things like "outputTF.setText(Integer.toString(n));", and get rid of nStr. (Anyone else who needs a the string version of n can recreate it themselves anyway.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this