Results 1 to 4 of 4
  1. #1
    mindfcked is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Comparing two coinflips, in JFrame, lots of problems need advice!!!

    Hello all, thanks for coming!

    I'm having tremendous issues today with making a program to compare the results of two flipped coins. Users are to be prompted for a number of coinflips, and the program must compare the results of each flip. Heads always wins, so if P1 (player 1) is heads and P2 is tails then P1 wins. If P1 is Heads and P2 is Heads, then it would output a tie. Also, I have to keep a going track on the number of wins for each player and output this number after all coin flips are done.

    Right now I can't figure out how to compare the outputs of the two coinflips. I have them returned as a string value. There's only so much I can explain with words, but right now I'm really confused as to how I compare these coinflips, and then output them to the Jframe. Any and all help or advice is appreciated!! Let me know if anything is unclear also.

    Java Code:
    package coinflip;
    
    public class Coin
    {
       private final int HEADS = 0;
       private final int TAILS = 1;
    
       private int face;
    
       //-----------------------------------------------------------------
       //  Sets up the coin by flipping it initially.
       //-----------------------------------------------------------------
       public Coin ()
       {
          flip();
       }
    
       //-----------------------------------------------------------------
       //  Flips the coin by randomly choosing a face value.
       //-----------------------------------------------------------------
       public void flip ()
       {
          face = (int) (Math.random() * 2);
       }
    
       //-----------------------------------------------------------------
       //  Returns true if the current face of the coin is heads.
       //-----------------------------------------------------------------
       public boolean isHeads ()
       {
          return (face == HEADS);
       }
    
       //-----------------------------------------------------------------
       //  Returns the current face of the coin as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          String faceName;
    
          if (face == HEADS)
             faceName = "Heads";
          else
             faceName = "Tails";
    
          return faceName;
       }
    }
    Java Code:
    package coinflip;
    
    public class CoinFlip
    {
       //-----------------------------------------------------------------
       //  Creates first coin object, and flips it. 
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          Coin myCoin = new Coin();
    
          myCoin.flip();
    
          System.out.println (myCoin);
    
          if (myCoin.isHeads()) {
    	   System.out.println ("You win.");
           }
          else {
    	   System.out.println ("Better luck next time.");
           }
       }
       
       //creates second coin object , and flips it. 
       {
           Coin newCoin = new Coin ();
          
           newCoin.flip();
          
           System.out.println (newCoin);
       
       }
    }
    Java Code:
    package coinflip;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CoinFlipPanel extends JPanel
    {
        
        private JButton flip;
        private JLabel resultone, resulttwo, instructions;
        private JTextField input; // number of times coins are flipped, and compared
        
    
        //-----------------------------------------------------------------
        //  Constructor: Sets up the GUI.
        //-----------------------------------------------------------------
        public CoinFlipPanel ()
        {
    	
           instructions = new JLabel ("Enter the number of coinflips.");
            
            input = new JTextField(5); //input for number of times to flip
            input.setBounds(100,20,100,20); //input for number of times to flip
            
    	flip = new JButton ("Flip both coins!"); //flips both coins
    	flip.addActionListener (new CoinFlipPanel.ButtonListener());
    
    	resultone = new JLabel ("Player 1 coin:" );
            resulttwo = new JLabel ("Player 2 coin:" );
    	setPreferredSize (new Dimension(300, 40));
    	setBackground (Color.cyan);
        }
    
        //*****************************************************************
        //  Represents a listener for button push (action) events.
        //*****************************************************************
        private class ButtonListener implements ActionListener
        {
    	//--------------------------------------------------------------
    	//  Updates the counter and label when the button is pushed.
    	//--------------------------------------------------------------
    	@Override
    	public void actionPerformed (ActionEvent event)
    	{
    	   
    	}
        }
    }
    
    /// if coin 1 < coin 2, player 1 wins. 
    // if coin 1 = coin 2, play 1 ties.
    // if coin 1 > coin 2, player 1 loses.
    // Heads = 0 
    // tai;s = 1
    Thanks for reading. ANY advice is appreciated!!!

  2. #2
    mindfcked is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    Can I get any feedback at all? How to make my post more user friendly so people will respond? something? haha

  3. #3
    mindfcked is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    bumpbumpbump

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Comparing two coinflips, in JFrame, lots of problems need advice!!!

    Read the forum etiquette advice in your crosspost:
    Comparing two coinflips, in JFrame, lots of problems need advice!!!

Similar Threads

  1. Replies: 3
    Last Post: 04-15-2011, 03:30 AM
  2. Comparing values problems
    By mari.beka in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 09:15 PM
  3. MultiPle JFrame problems
    By sysout in forum New To Java
    Replies: 2
    Last Post: 08-26-2009, 03:55 AM
  4. problems with JPanel and JFrame
    By v1nsai in forum New To Java
    Replies: 13
    Last Post: 04-08-2009, 07:49 PM
  5. problems with jDialog in a JFrame
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 04:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •