Using either the jMonkey SDK IDE or another Java SDK of your choice, create a ba
ID: 3864402 • Letter: U
Question
Using either the jMonkey SDK IDE or another Java SDK of your choice, create a basic coin-flip guessing game based on the following:
1. Download the repository at https://github.com/seccodingguy/CMSC325/tree/master/src/cmsc325gameconcepts
2. Using the Strategy and StrategyRandom class, create a game that prompts the user to choose heads or tails, flips a virtual coin, and then displays the results to the user. The game must continue until 100 flips have been completed. Save the results of the flip results in a format of your choosing.
3. Modify the code to include the StrategyProbability class, using the results saved from the StrategyRandom as the starting values for the setMoves() function. The game must continue until 100 flips have been completed. Save the results of the flip results in a format of your choosing and separate from the StrategyRandom results.
4. For both 2 and 3, generate statistics on the odds of getting the right guess as well as the number of heads and tails flipped, displayed, respectively, to the user as well as saved with the results.
5. The program does not have to use graphics of actual coins - this is optional.
6. The 100 flips can be generated manually, i.e. user pressing a button, or automatically.
Explanation / Answer
import java.util.Random;
// for random number
import java.util.Scanner;
//to take input from user
// Coin toss/flip game in Java
public class CoinFlipGame {
int headCount = 0;
int tailCount = 0;
private enum Coin {
Head, Tail
};
public static void main(String[] args) {
CoinFlipGame game = new CoinFlipGame()
game.startGame();
}
private void startGame() {
Scanner scanner = new Scanner(System.in);
Coin guess;
for (int i = 0; i < 1000; i++) {
System.out.print("Enter you guess (h for heads, t for tails);
if(r.nextInt(100) %2 == 0)
{
headCount++;
}
else
{
tailCount ++
}
if (choice.equalsIgnoreCase("h")) {
guess = Coin.Head;
} else if (choice.equalsIgnoreCase("t")) {
guess = Coin.Tail;
} else {
System.out.println("Wrong choice! Try again!");
continue;
}
Coin toss = tossCoin();
if (guess == toss) {
System.out.println("Congratulations! You won the toss!");
} else {
System.out.println("Sorry! You lost the toss.");
}
}
scanner.close();
System.out.println("Times head was flipped:" + headsCount);
System.out.println("Times tail was flipped:" + tailCount);
}
import java.util.Random;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.