Jump to content
  • Advertisement
Sign in to follow this  
White Wold

Board Game Problem(Java)

This topic is 4443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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){

    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(){

    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"));

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

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

    outputTF =  new JTextField(5);

    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);
          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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!