Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that allows a single Player (the user) to play a simple three di

ID: 3607526 • Letter: W

Question

Write a program that allows a single Player (the user) to play a simple three dice game of chance against a single Opponent (the computer).

Game Description:

• There is a single Player (human), with three dice.

• There is a single Opponent (computer), with three dice.

• Each die is 4 sided, and each of its sides is labeled with a unique number: 2, 3, 5 or 7.

• When a die is rolled it stops with one of its sides facing down, we will call this the value of the die roll.

• A game is made up of rounds, a single round is played as such:

1. The computer Opponent rolls its dice

2. The dice are displayed

3. The Player rolls their dice

4. The dice are displayed

5. A determination is made as to who won the round, this determination is made via the following rules:

– A Triple is when all the dice have the same number on their top faces, the value of the Triple is this common value.

– A Pair is when exactly two of the dice have the same number on their top faces, the value of the pair is this common value.

– A Straight is when the three dice values can be arranged into a consecutive sequence, there are exactly two possible Straights 2 3 5 in any order and 3 5 7 in any order. – Junk is anything that is not a Triple, a Pair or a Straight.

– Larger values beat smaller values, same values are a tie.

– Any Triple beats any Straight.

– Any Triple beats any Pair.

– Any Triple beats any Junk.

– Two Triples are compared by their respective Triple values.

– Any Straight beats any Pair.

– Any Straight beats any Junk.

– Any 3 5 7 Straight beats any 2 3 5 Straight.

– Any Pair beats any Junk.

– Two Pairs are first compared by their respective Pair values, and then by their respective non-common die values. 6. The result of the round (with respect to the Player) is announced.

7. The result of the round (with respect to the Player) is tallied, by increasing the number of player wins, ties, or loses by one.

• The player is asked if they wish to play another round.

• When the game is over, the results of the game are announced: how many rounds the player played; how many they won; how many they tied; how many they lost.

Explanation / Answer

Solution:

code:

import java.util.*;
public class dice
{public static void main(String [] args)
    {Scanner in=new Scanner(System.in);
    int pMajor,pMinor,oMajor,oMinor,tp,tc,win=0,lost=0,tie=0,d1,d2,res;
    int low=1,high=6;
    String yes;
    char y;
    welcome();
    do
    {d1=rollDie(low,high);
    d2=rollDie(low,high);
    oMajor=majorDie(d1,d2);
    oMinor=minorDie(d1,d2);
    System.out.println("opponent --------");
    printDice(oMajor,oMinor);
    d1=rollDie(low,high);
    d2=rollDie(low,high);
    pMajor=majorDie(d1,d2);
    pMinor=minorDie(d1,d2);
   System.out.println("player ------");
    printDice(pMajor,pMinor);
    res=result(oMajor,oMinor,pMajor,pMinor);
    if(res==0)
        {tie++;
        System.out.println("It's a tie!");
        }
    else if(res==1)
       {win++;
        System.out.println("Congrats, you win!");
        }
    else
       {lost++;
        System.out.println("Sorry, you lose!");
        }
    System.out.print("Do you wish to play again [y, n]: ");
    yes=in.next();
    y=Character.toUpperCase(yes.charAt(0));
    }while(y=='Y');
report(win,tie,lost);   
    }
public static int result(int oMajor, int oMinor, int pMajor, int pMinor)
{
boolean pPair=isPair(pMajor,pMinor);
boolean oPair=isPair(oMajor,oMinor);
if(pPair)
   {if(oPair)
        {if(pMajor==oMajor)
              return 0;
        else if(pMajor>oMajor)
              return 1;
        else
             return -1;
        }
    else
        return 1;
   }
else if(oPair)
    return -1;
else if(pMajor==oMajor)
         {if(pMinor==oMinor)
                return 0;
           else if(pMinor>oMinor)
                      return 1;
                else
                      return -1;
                }
else if(pMajor>oMajor)
                 return 1;
return -1;
}

public static void report(int wins, int ties, int loses)
{System.out.println("PLAYER SUMMARY");
System.out.println("Games Won: "+wins);
System.out.println("Games Lost: "+loses);
System.out.println("Games Tied: "+ties);
}

public static boolean isPair(int d1, int d2)
{if(d1==d2)
     return true;
return false;
}
public static void printDice(int d1, int d2)
{System.out.println(d1+" "+d2);
}
public static int rollDie(int low, int high)
{int d;
d=(int)(Math.random()*high)+low;
return d;
}
public static int majorDie(int d1, int d2)
{if(d1>d2)
    return d1;
return d2;
}
public static int minorDie(int d1, int d2)
{if(d1<d2)
    return d1;
return d2;
}
public static void welcome()
{System.out.println("RULES");
System.out.println("- Larger values beat smaller values, same values are a tie.");
System.out.println("-A pair is when both dice have the same number on their top faces, the ");
System.out.println("value of the pair is this common value.");
System.out.println("-A non-pair is then when both dice have a different number on their top");
System.out.println("faces, the major die is the one with the larger value and the minor die the");
System.out.println("one with the lesser value.");
System.out.println("-Any pair beats any non-pair.");
System.out.println("-Any two pairs are compared by their respective pair values.");
System.out.println("-Any two non-pairs are first compared by their major die values and only in");
System.out.println("the case of a tie in majors, are compared by their minor die values.");
}
}

Output:

Success #stdin #stdout 0.06s 4386816KB

I hope this helps, if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote