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

Using Python: Design the logic for the dice game Pig, in which each player compe

ID: 643353 • Letter: U

Question

Using Python:

Design the logic for the dice game Pig, in which each player competes with the computer. The object of the game is to be the first to score 100 points. The user and computer take turns "rolling" a pair of dice following these rule: On a turn, each player rolls two dice. If no '1' is rolled, the dice values are aded to a running total for the turn, and the player can choose to roll again or pass to the other player. When a player passes, the accumulated turn total is added to the player's game total. Next, if a '1' is rolled on one die, the player's turn total becomes 0; in other words, nothing more is added tothe player's game total for that turn, and it becomes the other player's turn. Also, If a '1' is rolled on both dice, not only is the player's turn over, but the player's entire accumulated game total is reset to 0. Also, when the computer does not roll a '1' and can choose whether to roll again, generate a random value from 1 to 2. The computer will continue if the value is 1 and will quit and pass the turn when the value is not 1.

Explanation / Answer

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class PIGGame extends JFrame implements ActionListener
{
static int totalp,totalc;
JButton jb;
JTextArea jta;
PIGGame()
{
Container c=getContentPane();
jb=new JButton("Play");
jb.addActionListener(this);
jta=new JTextArea(25,25);
c.setLayout(new BorderLayout());
c.add(jb,BorderLayout.NORTH);
c.add(jta,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e)
{
String s=JOptionPane.showInputDialog(this,"Enter 1 for You to play || Enter 2 for Computer to Play");

int i=Integer.parseInt(s);

if(i==1 || i==2)
{
if(i==1)
playerplay();
if(i==2)
computerplay();
}
else
jta.setText("Please Enter 1 or 2");
}

void playerplay()
{
int pr;
if(totalp==100)
jta.append("Player Win");

jta.append(" Player Playing");
Random r=new Random();

pr=showRandomInteger(1,6,r);
if(pr==1)
{
totalp=0;
computerplay();
}
else
{
totalp=totalp+pr;
jta.append("Total:"+totalp);
}
}

void computerplay()
{
int pr;
if(totalc==100)
jta.append("Computer Win");

jta.append(" Computer Playing");
Random r=new Random();
pr=showRandomInteger(1,6,r);
if(pr==1)
{
totalc=0;
playerplay();
}
else
{
totalc=totalc+pr;
jta.append("Total:"+totalc);
}

}


private int showRandomInteger(int aStart, int aEnd, Random aRandom)
{
if ( aStart > aEnd ) {
throw new IllegalArgumentException("Start cannot exceed End.");
}

long range = (long)aEnd - (long)aStart + 1;
long fraction = (long)(range * aRandom.nextDouble());
int randomNumber = (int)(fraction + aStart);
jta.append("You Played:"+randomNumber);

return randomNumber;
}

public static void main(String args[])
{
PIGGame pg=new PIGGame();
pg.setSize(300,400);
pg.setVisible(true);
}
}   

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