Question

Write java program that implements a game in which the computer plays against the human. GAME...

Write java program that implements a game in which the computer plays against the human.

GAME OF CHANCE RULES:

  1. Both players start with a score of 0.
  2. The game lasts at most 10 rounds.
  3. During each round, each player throws a fair virtual die.
  4. The player who threw the higher number gains the sum of points thrown by both players during that round.
    • Example: If computer throws '5' and human throws '3', computer gains 8 points.
  5. If both players throw the same number, both lose the sum of points thrown that round.
    • Example: If computer throws '5' and human also throws '5', they each lose 10 points.
  6. If one of the player has a negative number of points, the game ends.
  7. If at any point after the third round one of the players has more than 5 times as many points as the other, the game ends.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java program to simulate a game of chance between 2 players

import java.util.Random;

import java.util.Scanner;

public class GameOfChance {

       // method to return the result of rolling a fair virtual die

       public static int dieRoll()

       {

             Random ran = new Random();

             return(ran.nextInt(6)+1);

       }

      

      

       public static void main(String[] args) {

            

             int rounds = 0;

             final int MAX_ROUNDS = 10;

             int player1_score =0, player2_score=0;

             int player1_die, player2_die;

             Scanner scan = new Scanner(System.in);

             // loop continues at most MAX_ROUNDS rounds

             while(rounds < MAX_ROUNDS)

             {

                    rounds++;

                    // get the die rolls for both the players

                    player1_die = dieRoll();

                    player2_die = dieRoll();

                    System.out.println("ROUND: "+rounds+"\nPlayer1 Score : "+player1_score+" Player2 Score : "+player2_score);

                    System.out.println("Player 1 rolls : "+player1_die+" Player 2 rolls : "+player2_die);

                    // update the scores of the players

                    if(player1_die > player2_die)

                    {

                           player1_score += player1_die+player2_die;

                    }else if(player1_die < player2_die)

                    {

                           player2_score += player1_die+player2_die;

                    }else

                    {

                           player1_score -= (player1_die+player2_die);

                           player2_score -= (player1_die+player2_die);

                    }

                   

                    // check if game is over or not

                    if((player1_score < 0) || (player2_score < 0))

                           break;

                    if(rounds > 3 && ((player1_score > 5*player2_score) || (player2_score > 5*player1_score)))

                           break;

                    System.out.println("Enter any key to continue... ");

                    scan.nextLine();

             }

            

             scan.close();

             System.out.println("\nPlayer1 Score : "+player1_score+" Player2 Score : "+player2_score);

             // determine the winner

             if(player1_score == player2_score)

                    System.out.println("Its a tie");

             else if(player1_score > player2_score)

                    System.out.println("Player 1 wins");

             else

                    System.out.println("Player 2 wins");

       }

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Write java program that implements a game in which the computer plays against the human. GAME...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT