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

The game of Pig is a simple two-player dice game in which the first player to re

ID: 3529761 • Letter: T

Question

The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die:

Explanation / Answer

Siva_code: import javax.swing.JOptionPane; 002 import java.util.*; 003 004 public class Pig 005 { 006 007 String Play; 008 boolean playerTurn = true; 009 final int WIN = 100; 010 final int COMPMAX = 20; 011 int play, playerScore = 0, compScore = 0, compEnd, points, roundScore; 012 int die1Roll, die2Roll; 013 014 Die die1 = new Die(); 015 Die die2 = new Die(); 016 017 //--------------------------------------------------------------------------------- 018 //Main method creates object of class and calls the play method to initiate gameplay. 019 //--------------------------------------------------------------------------------- 020 021 public static void main (String[] args) 022 { 023 024 JOptionPane.showMessageDialog(null, "Welcome to M@'s Pig game."); 025 026 Pig pig = new Pig(); 027 028 pig.play(); 029 030 } 031 032 //--------------------------------------------------------------------------------- 033 //Loops game and controls who has dice. 034 //--------------------------------------------------------------------------------- 035 036 public void play() 037 { 038 039 while(playerTurn) 040 { 041 042 Play = JOptionPane.showInputDialog("Please enter 1 to roll the die or 2 to let the computer roll the die."); 043 044 play = Integer.parseInt (Play); 045 046 if (play == 1) 047 { 048 049 playerTurn = true; 050 051 } 052 053 else 054 { 055 056 playerTurn = false; 057 058 } 059 060 if (playerTurn == false) 061 { 062 063 computerRoll(); 064 065 } 066 067 else 068 { 069 070 playerRoll(); 071 072 } 073 074 } 075 076 } 077 078 //--------------------------------------------------------------------------------- 079 //Rolls for the computer and gives corresponding points. 080 //--------------------------------------------------------------------------------- 081 082 public void computerRoll() 083 { 084 085 while (playerTurn == false) 086 { 087 088 die1Roll = die1.roll(); 089 die2Roll = die2.roll(); 090 091 JOptionPane.showMessageDialog(null, "Computer rolled: " + die1Roll + " + " + die2Roll); 092 093 compScore = points(die1Roll, die2Roll, compScore); 094 095 if (die1Roll == 1 || die2Roll == 1) 096 { 097 098 playerTurn = true; 099 100 } 101 102 JOptionPane.showMessageDialog(null, "Player Score: "+ playerScore + " " + "Computer Score: " + compScore); 103 104 if (compEnd >= COMPMAX) 105 { 106 107 playerTurn = true; 108 109 compEnd = 0; 110 111 } 112 113 } 114 115 if (compScore >= WIN) 116 { 117 118 win(); 119 120 } 121 122 } 123 124 //--------------------------------------------------------------------------------- 125 //Rolls for the player and gives corresponding points. 126 //--------------------------------------------------------------------------------- 127 128 public void playerRoll() 129 { 130 131 die1Roll = die1.roll(); 132 die2Roll = die2.roll(); 133 134 JOptionPane.showMessageDialog(null, "You rolled: " + die1Roll + " + " + die2Roll); 135 136 playerScore = points(die1Roll, die2Roll, playerScore); 137 138 JOptionPane.showMessageDialog(null, "Player Score: " + playerScore + " " + "Computer Score: " + compScore); 139 140 if (die1Roll == 1 || die2Roll == 1) 141 { 142 143 playerTurn = false; 144 computerRoll(); 145 146 } 147 148 if (playerScore >= WIN) 149 { 150 151 win(); 152 153 } 154 155 } 156 157 //--------------------------------------------------------------------------------- 158 //Calculates points for players and checks to see they roll 1 or double 1s. 159 //--------------------------------------------------------------------------------- 160 161 public int points(int die1Roll, int die2Roll, int points) 162 { 163 164 if(roundCont(die1Roll, die2Roll)); 165 { 166 167 roundScore += die1Roll + die2Roll; 168 169 } 170 171 if (die1Roll == 1 && die2Roll == 1) 172 { 173 174 JOptionPane.showMessageDialog(null, "Lost all points!"); 175 176 points = 0; 177 178 compEnd = 0; 179 180 roundScore = 0; 181 182 return points; 183 184 } 185 186 else if (die1Roll == 1 || die2Roll == 1) 187 { 188 189 JOptionPane.showMessageDialog(null, "Received no points!"); 190 191 192 roundScore = 0; 193 194 195 return points; 196 197 } 198 199 else 200 { 201 202 compEnd += die1Roll + die2Roll; 203 204 points = roundScore; 205 206 return points; 207 208 } 209 210 } 211 212 //--------------------------------------------------------------------------------- 213 //Checks to see if the round should end or not. 214 //--------------------------------------------------------------------------------- 215 216 public boolean roundCont(int die1Roll, int die2Roll) 217 { 218 219 if (die1Roll == 1 && die2Roll == 1) 220 { 221 222 return false; 223 224 } 225 226 else if (die1Roll == 1 || die2Roll == 1) 227 { 228 229 return false; 230 231 } 232 233 else 234 { 235 236 return true; 237 238 } 239 240 } 241 242 //--------------------------------------------------------------------------------- 243 //Tests whether or not player or computer won. 244 //--------------------------------------------------------------------------------- 245 246 public void win() 247 { 248 249 if (playerScore >= 100) 250 { 251 252 JOptionPane.showMessageDialog(null, "Congratulations! You Win!"); 253 254 System.exit(0); 255 256 } 257 258 if (compScore >= 100) 259 { 260 261 JOptionPane.showMessageDialog(null, "You lose!"); 262 263 System.exit(0); 264 265 } 266 267 } 268 269 }

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