Need help writing a Penney\'s Game in Java. My code is: import java.util.*; publ
ID: 650243 • Letter: N
Question
Need help writing a Penney's Game in Java.
My code is:
import java.util.*;
public static void main(String[] args) {
Random rand = new Random();
String compChoice = "", playerChoice;
if (rand.nextBoolean()) {
for (int i = 0; i < 3; i++)
compChoice += "HT".charAt(rand.nextInt(2));
System.out.printf("Computer chooses %s%n", compChoice);
playerChoice = prompt(compChoice);
}
else {
playerChoice = prompt(compChoice);
compChoice = "T";
if (playerChoice.charAt(1) == 'T')
compChoice = "H";
compChoice += playerChoice.substring(0, 2);
System.out.printf("Computer chooses %s%n", compChoice);
}
String tossed = "";
while (true) {
tossed += "HT".charAt(rand.nextInt(2));
System.out.printf("Tossed %s%n" , tossed);
if (tossed.endsWith(playerChoice)) {
System.out.println("You won! But I'll win next time...");
break;
}
if (tossed.endsWith(compChoice)) {
System.out.println("I won! Beat me next time (if you can)!");
break;
}
}
}
private static String prompt(String otherChoice) {
Scanner sc = new Scanner(System.in);
String s;
do {
System.out.print("Choose a sequence: ");
s = sc.nextLine().trim().toUpperCase();
} while (!s.matches("[HT]{3}") || s.equals(otherChoice));
return s;
}
}
But now the expected output is:
Enter the # of coins (> 0): 3
Enter coin 1: ('h' for heads, 't' for tails): t
Enter coin 2: ('h' for heads, 't' for tails): t
Enter coin 3: ('h' for heads, 't' for tails): h
You chose: { T T H }
I chose: { H H T }
The flipping starts: H T T T T T H DONE!
You won! But I'll win next time...
Enter the # of coins (> 0): 5
Enter coin 1: ('h' for heads, 't' for tails): t
Enter coin 2: ('h' for heads, 't' for tails): h
Enter coin 3: ('h' for heads, 't' for tails): h
Enter coin 4: ('h' for heads, 't' for tails): t
Enter coin 5: ('h' for heads, 't' for tails): h
You chose: { T H H T H }
I chose: { H T T H H }
The flipping starts: T H H H H H H H T H H T H DONE!
You won! But I'll win next time...
Enter the # of coins (> 0): 5
Enter coin 1: ('h' for heads, 't' for tails): t
Enter coin 2: ('h' for heads, 't' for tails): h
Enter coin 3: ('h' for heads, 't' for tails): t
Enter coin 4: ('h' for heads, 't' for tails): h
Enter coin 5: ('h' for heads, 't' for tails): t
You chose: { T H T H T }
I chose: { T H T H H }
The flipping starts: T H H H H T T T H H T T H T T H H T T H T H H DONE!
I won! Beat me next time (if you can)!
Enter the # of coins (> 0): t
Invalid input; try again.
Enter the # of coins (> 0): -5
Invalid input; try again.
Enter the # of coins (> 0): 0
Invalid input; try again.
Enter the # of coins (> 0): 3
Enter coin 1: ('h' for heads, 't' for tails): t
Enter coin 2: ('h' for heads, 't' for tails): a
Invalid input; try again.
Enter coin 2: ('h' for heads, 't' for tails): b
Invalid input; try again.
Enter coin 2: ('h' for heads, 't' for tails): h
Enter coin 3: ('h' for heads, 't' for tails): t
You chose: { T H T }
I chose: { T T H }
The flipping starts: T H H H H T T H DONE!
I won! Beat me next time (if you can)!
Enter the # of coins (> 0): 3
Enter coin 1: ('h' for heads, 't' for tails): T
Enter coin 2: ('h' for heads, 't' for tails): H
Enter coin 3: ('h' for heads, 't' for tails): T
You chose: { T H T }
I chose: { H H H }
The flipping starts: H T H T DONE!
You won! But I'll win next time...
Can someone help me change my code, thx!
Explanation / Answer
import java.util.*;
//definition of class PenneysGame(Head and Tail)
public class PenneysGame
{
//declare the static variables
static boolean flipCoin[] = { true, false };
static Scanner input = new Scanner(System.in);
static boolean coins1[], coins2[];
//main method that call the functions
public static void main(String args[])
{
initFair(coins1);
play1(coins1);
play2(coins2, coins1);
System.out.print("You chose: {");
showCoins(coins1);
System.out.println();
System.out.print("I chose: {");
showCoins(coins2);
System.out.println();
boolean flag = fairFlips(coins1, coins2);
if (flag)
{
System.out.println("You win! But I'll win next time.....");
}
else
{
System.out.println("I won! Beat me next time(if you can)!");
}
}
//definition of doArraysMatch
static boolean doArraysMatch(boolean[] array1, boolean[] array2, int start2)
{
return array1[start2] == array2[start2];
}
//definition of fairFlips
static boolean fairFlips(boolean[] coins1, boolean[] coins2)
{
boolean flip;
int p1Count = 0;
int p2Count = 0;
System.out.print(" The flipping starts: ");
for (int i = 0; i < coins1.length; i++)
{
//if the arrays of player1 and player2 matches just flip the coin
if (doArraysMatch(coins1, coins2, i))
{
flip = flipCoin();
if (flip == true)
System.out.print(" H ");
else
System.out.print(" T ");
}
//if they do not match then increment the
flip = flipCoin();
if (flip == true)
System.out.print(" H ");
else
System.out.print(" T ");
//counter of respective player
if (flip == coins1[i])
{
p1Count++;
}
else if (flip == coins2[i])
{
p2Count++;
}
}
System.out.println("DONE!");
//return the winners value
if (p1Count > p2Count)
{
return true;
}
else
{
return false;
}
}
//definition of flipCoin
static boolean flipCoin()
{
return flipCoin[(int) (Math.random() * 2)];
}
//definition of getPlayer1
static boolean getPlayer1(int count)
{
return coins1[count];
}
//definition of getUserInput
static int getUserInput()
{
int count;
System.out.println("Enter the # of coins (>0): ");
count = input.nextInt();
while (count <= 0)
{
System.out.println("Invalid input; try again.");
System.out.println("Enter the # of coins (>0): ");
count = input.nextInt();
}
return count;
}
//definition of initFair
static void initFair(boolean[] coins)
{
coins1 = new boolean[getUserInput()];
coins2 = new boolean[coins1.length];
}
//definition of play1
static void play1(boolean[] coinArray)
{
int count = 0;
for (int i = 0; i < coinArray.length; i++)
{
char choice;
System.out.println("Enter coin " + (count + 1)
+ ": ('h' for heads, 't' for tails): ");
choice = input.next().charAt(0);
while (choice != 'h' && choice != 't')
{
System.out.println("Invalid input; try again.");
System.out.println("Enter coin " + (count + 1)
+ ": ('h' for heads, 't' for tails): ");
choice = input.next().charAt(0);
}
count++;
if (choice == 'h')
coinArray[i] = true;
if (choice == 't')
coinArray[i] = false;
}
System.out.println("valid");
}
//definition of play2
static void play2(boolean[] player2, boolean[] player1)
{
boolean flip;
for (int i = 0; i < player1.length; i++)
{
flip = flipCoin();
player2[i] = flip;
}
}
//definition of showCoin
static void showCoin(boolean coin)
{
if (coin == true)
System.out.print(" H ");
else
System.out.print(" T ");
}
//definition of showCoins
static void showCoins(boolean[] coinArray)
{
for (int i = 0; i < coinArray.length; i++)
{
showCoin(coinArray[i]);
if (i != coinArray.length - 1)
System.out.print(",");
}
System.out.print("}");
}
}
import java.util.*;
//definition of class PenneysGame(Head and Tail)
public class HelloWorld
{
//declare the static variables
static boolean flipCoin[] = { true, false };
static Scanner input = new Scanner(System.in);
static boolean coins1[], coins2[];
//main method that call the functions
public static void main(String args[])
{
initFair(coins1);
play1(coins1);
play2(coins2, coins1);
System.out.print("You chose: {");
showCoins(coins1);
System.out.println();
System.out.print("I chose: {");
showCoins(coins2);
System.out.println();
boolean flag = fairFlips(coins1, coins2);
if (flag)
{
System.out.println("You win! But I'll win next time.....");
}
else
{
System.out.println("I won! Beat me next time(if you can)!");
}
}
//definition of doArraysMatch
static boolean doArraysMatch(boolean[] array1, boolean[] array2, int start2)
{
return array1[start2] == array2[start2];
}
//definition of fairFlips
static boolean fairFlips(boolean[] coins1, boolean[] coins2)
{
boolean flip;
int p1Count = 0;
int p2Count = 0;
System.out.print(" The flipping starts: ");
for (int i = 0; i < coins1.length; i++)
{
//if the arrays of player1 and player2 matches just flip the coin
if (doArraysMatch(coins1, coins2, i))
{
flip = flipCoin();
if (flip == true)
System.out.print(" H ");
else
System.out.print(" T ");
}
//if they do not match then increment the
flip = flipCoin();
if (flip == true)
System.out.print(" H ");
else
System.out.print(" T ");
//counter of respective player
if (flip == coins1[i])
{
p1Count++;
}
else if (flip == coins2[i])
{
p2Count++;
}
}
System.out.println("DONE!");
//return the winners value
if (p1Count > p2Count)
{
return true;
}
else
{
return false;
}
}
//definition of flipCoin
static boolean flipCoin()
{
return flipCoin[(int) (Math.random() * 2)];
}
//definition of getPlayer1
static boolean getPlayer1(int count)
{
return coins1[count];
}
//definition of getUserInput
static int getUserInput()
{
int count;
System.out.println("Enter the # of coins (>0): ");
count = input.nextInt();
while (count <= 0)
{
System.out.println("Invalid input; try again.");
System.out.println("Enter the # of coins (>0): ");
count = input.nextInt();
}
return count;
}
//definition of initFair
static void initFair(boolean[] coins)
{
coins1 = new boolean[getUserInput()];
coins2 = new boolean[coins1.length];
}
//definition of play1
static void play1(boolean[] coinArray)
{
int count = 0;
for (int i = 0; i < coinArray.length; i++)
{
char choice;
System.out.println("Enter coin " + (count + 1)
+ ": ('h' for heads, 't' for tails): ");
choice = input.next().charAt(0);
while (choice != 'h' && choice != 't')
{
System.out.println("Invalid input; try again.");
System.out.println("Enter coin " + (count + 1)
+ ": ('h' for heads, 't' for tails): ");
choice = input.next().charAt(0);
}
count++;
if (choice == 'h')
coinArray[i] = true;
if (choice == 't')
coinArray[i] = false;
}
System.out.println("valid");
}
//definition of play2
static void play2(boolean[] player2, boolean[] player1)
{
boolean flip;
for (int i = 0; i < player1.length; i++)
{
flip = flipCoin();
player2[i] = flip;
}
}
//definition of showCoin
static void showCoin(boolean coin)
{
if (coin == true)
System.out.print(" H ");
else
System.out.print(" T ");
}
//definition of showCoins
static void showCoins(boolean[] coinArray)
{
for (int i = 0; i < coinArray.length; i++)
{
showCoin(coinArray[i]);
if (i != coinArray.length - 1)
System.out.print(",");
}
System.out.print("}");
}
}
import java.util.*;
//definition of class PenneysGame(Head and Tail)
public class HelloWorld
{
//declare the static variables
static boolean flipCoin[] = { true, false };
static Scanner input = new Scanner(System.in);
static boolean coins1[], coins2[];
//main method that call the functions
public static void main(String args[])
{
initFair(coins1);
play1(coins1);
play2(coins2, coins1);
System.out.print("You chose: {");
showCoins(coins1);
System.out.println();
System.out.print("I chose: {");
showCoins(coins2);
System.out.println();
boolean flag = fairFlips(coins1, coins2);
if (flag)
{
System.out.println("You win! But I'll win next time.....");
}
else
{
System.out.println("I won! Beat me next time(if you can)!");
}
}
//definition of doArraysMatch
static boolean doArraysMatch(boolean[] array1, boolean[] array2, int start2)
{
return array1[start2] == array2[start2];
}
//definition of fairFlips
static boolean fairFlips(boolean[] coins1, boolean[] coins2)
{
boolean flip;
int p1Count = 0;
int p2Count = 0;
System.out.print(" The flipping starts: ");
for (int i = 0; i < coins1.length; i++)
{
//if the arrays of player1 and player2 matches just flip the coin
if (doArraysMatch(coins1, coins2, i))
{
flip = flipCoin();
if (flip == true)
System.out.print(" H ");
else
System.out.print(" T ");
}
//if they do not match then increment the
flip = flipCoin();
if (flip == true)
System.out.print(" H ");
else
System.out.print(" T ");
//counter of respective player
if (flip == coins1[i])
{
p1Count++;
}
else if (flip == coins2[i])
{
p2Count++;
}
}
System.out.println("DONE!");
//return the winners value
if (p1Count > p2Count)
{
return true;
}
else
{
return false;
}
}
//definition of flipCoin
static boolean flipCoin()
{
return flipCoin[(int) (Math.random() * 2)];
}
//definition of getPlayer1
static boolean getPlayer1(int count)
{
return coins1[count];
}
//definition of getUserInput
static int getUserInput()
{
int count;
System.out.println("Enter the # of coins (>0): ");
count = input.nextInt();
while (count <= 0)
{
System.out.println("Invalid input; try again.");
System.out.println("Enter the # of coins (>0): ");
count = input.nextInt();
}
return count;
}
//definition of initFair
static void initFair(boolean[] coins)
{
coins1 = new boolean[getUserInput()];
coins2 = new boolean[coins1.length];
}
//definition of play1
static void play1(boolean[] coinArray)
{
int count = 0;
for (int i = 0; i < coinArray.length; i++)
{
char choice;
System.out.println("Enter coin " + (count + 1)
+ ": ('h' for heads, 't' for tails): ");
choice = input.next().charAt(0);
while (choice != 'h' && choice != 't')
{
System.out.println("Invalid input; try again.");
System.out.println("Enter coin " + (count + 1)
+ ": ('h' for heads, 't' for tails): ");
choice = input.next().charAt(0);
}
count++;
if (choice == 'h')
coinArray[i] = true;
if (choice == 't')
coinArray[i] = false;
}
System.out.println("valid");
}
//definition of play2
static void play2(boolean[] player2, boolean[] player1)
{
boolean flip;
for (int i = 0; i < player1.length; i++)
{
flip = flipCoin();
player2[i] = flip;
}
}
//definition of showCoin
static void showCoin(boolean coin)
{
if (coin == true)
System.out.print(" H ");
else
System.out.print(" T ");
}
//definition of showCoins
static void showCoins(boolean[] coinArray)
{
for (int i = 0; i < coinArray.length; i++)
{
showCoin(coinArray[i]);
if (i != coinArray.length - 1)
System.out.print(",");
}
System.out.print("}");
}
}
THE CLASS NAME AND THE FILE NAME SHOULD BE SAME .i.e PenneysGame is class name .. save the program PenneysGame this name .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.