This game is meant for two or more players. In the game, the players take turns
ID: 3762294 • Letter: T
Question
This game is meant for two or more players. In the game, the
players take turns flipping a coin. Before the coin is flipped
players should guess if the coin will land face up or face down
If a player guesses correctly, then that player is awarded a point. If the player guess
Incorrectly, than that player will lose a point. The first player to score five points wins.
Write a program that simulates the game being played by two players. Use coin class to simulate
the coin and a player class to simulate the players.
I have the coin class and the main method. I just need a players class to simulate the players of this game. here is the code that I have.
import java.util.Random;
class Coin {
private static String sideUp;
/*
* Default constructor
*/
public Coin() {
this.sideUp = "Heads";
toss();
}
/*
* This method will simulate the tossing of a coin. It should set the
* sideUp field to either "heads" or "tails".
* @return
*/
public int toss() {
Random rand = new Random();
// Get a random value, 0 or 1.
int value = rand.nextInt(2);
if (value == 0) {
this.sideUp = "Heads";
} else {
this.sideUp = "Tails";
}
return value;
}
/*
*
* @return The side of the coin facing up.
*/
public static String getSideUp() {
return sideUp;
}
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
int player1Score = 0,player2Score = 0;
//declaring two players
Coin firstPlayer = new Coin();
Coin secondPlayer = new Coin();
while(true){
System.out.println("Player 1, enter 1 for tails or 0 for heads ");
int guess = keyboard.nextInt();
//checking with player1 guess
if(guess == firstPlayer.toss()){
player1Score++;
System.out.println("Good guess, it was " + Coin.getSideUp());
System.out.println("Player 1 score: " + player1Score);
}
else{
player1Score--;
System.out.println("Bad guess, it was " + Coin.getSideUp());
System.out.println("Player 1 score: " + player1Score);
}
//checking with player2
System.out.println("Player 2, enter 1 for tails or 0 for heads ");
guess = keyboard.nextInt();
//checking with player1 guess
if(guess == secondPlayer.toss()){
player2Score++;
System.out.println("Good guess, it was " + Coin.getSideUp());
System.out.println("Player 2 score: " + player2Score);
}
else{
player2Score--;
System.out.println("Bad guess, it was " + Coin.getSideUp());
System.out.println("Player 2 score: " + player2Score);
}
//Checking for winning condition
if(player1Score == 5){
System.out.println();
System.out.println("Player 1 wins");
break;
}
else if(player2Score == 5){
System.out.println();
System.out.println("Player 2 wins");
break;
}
}//end while method
}//end main method
} //end program
Explanation / Answer
// Coin.java
import java.util.Random;
import java.util.Scanner;
class Coin {
private static String sideUp;
/*
* Default constructor
*/
public Coin() {
this.sideUp = "Heads";
toss();
}
/*
* This method will simulate the tossing of a coin. It should set the
* sideUp field to either "heads" or "tails".
* @return
*/
public static int toss() {
Random rand = new Random();
// Get a random value, 0 or 1.
int value = rand.nextInt(2);
if (value == 0) {
sideUp = "Heads";
} else {
sideUp = "Tails";
}
return value;
}
/*
*
* @return The side of the coin facing up.
*/
public static String getSideUp() {
return sideUp;
}
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
//declaring two players
Player player1 = new Player();
Player player2 = new Player();
while(true){
System.out.print("Player 1, enter 1 for tails or 0 for heads: ");
int guess = keyboard.nextInt();
//checking with player1 guess
if(guess == toss()){
player1.increaseScore();
System.out.println("Good guess, it was " + Coin.getSideUp());
System.out.println("Player 1 score: " + player1.getScore());
}
else{
player1.decreaseScore();
System.out.println("Bad guess, it was " + Coin.getSideUp());
System.out.println("Player 1 score: " + player1.getScore());
}
//checking with player2
System.out.print("Player 2, enter 1 for tails or 0 for heads: ");
guess = keyboard.nextInt();
//checking with player1 guess
if(guess == toss()){
player2.increaseScore();
System.out.println("Good guess, it was " + Coin.getSideUp());
System.out.println("Player 2 score: " + player2.getScore());
}
else{
player2.decreaseScore();
System.out.println("Bad guess, it was " + Coin.getSideUp());
System.out.println("Player 2 score: " + player2.getScore());
}
//Checking for winning condition
if(player1.getScore() == 5){
System.out.println();
System.out.println("Player 1 wins");
break;
}
else if(player2.getScore() == 5){
System.out.println();
System.out.println("Player 2 wins");
break;
}
}//end while method
}//end main method
} //end program
// Player.java
public class Player{
int playerScore;
Player(){
playerScore = 0;
}
int getScore(){
return playerScore;
}
void increaseScore(){
playerScore++;
}
void decreaseScore(){
playerScore--;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.