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!!
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; } }
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); } }
Thanks for reading. ANY advice is appreciated!!! also pls let me know if anything is unclear.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