Title | Program 4e Gameof Pigs |
---|---|
Author | Sandra Thomas |
Course | Introduction to Programming |
Institution | San Diego State University |
Pages | 5 |
File Size | 50.8 KB |
File Type | |
Total Downloads | 3 |
Total Views | 130 |
This is the 'GameofPigs' extra credit code from ZyBooks (Patty Kraft)...
/* Program 4e GameofPigs * CS 107-3 * Fall 2017 * @author Sandra Thomas */
import java.util.Scanner; import java.util.Random;
public class GameOfPigs { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); Random num1 = new Random(140L); Random num2 = new Random(340L); Random option = new Random(140L); int input = 0; int dice1 = 0; int dice2 = 0; int turn; int turnNum = 0; int compNum = 0; int participantNum = 0;
System.out.println("Welcome of the Game of Pigs");
if (option.nextInt(2) == 0) turn = 0; else turn = 1; do { System.out.println(); dice1 = num1.nextInt(6) + 1; dice2 = num2.nextInt(6) + 1; switch (turn % 2) { case 1: System.out.println("Your turn (current points: " + participantNum + ")"); if (dice1 != 1 || dice2 != 1) System.out.print("You rolled " + dice1 + " and " + dice2 + ", "); if (dice1 != 1 && dice2 != 1){ turnNum += dice1 + dice2; // if participant rolls numbers other than 1 on both dices, the values will be added to the overall points. System.out.println("points earned this turn:" + turnNum); System.out.println("Press 0 to roll again or 1 to start computer's turn"); input = scnr.nextInt();
if (input == 0) break; else participantNum += turnNum; }
else if (dice1 == 1 && dice2 == 1) { // if 1 appears on both dices, participant's turn is over and now it's the turn of computer System.out.println("You rolled two ones, points reset and computer's turn"); participantNum = 0; // total points reset to 0 } else System.out.println("no points earned and computer's turn");
turnNum = 0; turn++; //turn continues till 1 appears on both dices break;
case 0: System.out.println("Computer's turn (current points: " + compNum + ")"); if (dice1 != 1 || dice2 != 1) System.out.print("Computer rolled " + dice1 + " and " + dice2 + ", "); if(dice1 != 1 && dice2 != 1){ turnNum += dice1 + dice2; System.out.println("points earned this turn:" + turnNum); if (option.nextInt(2) == 0) { //random number generator to determine if the computer or participant will go first System.out.println("The computer selected choice 0"); // computer choose to roll again break; } else {
System.out.println("The computer selected choice 1"); compNum += turnNum; // if the computer choose 1, it pass the turn over } }
else if (dice1 == 1 && dice2 == 1) { System.out.println("Computer rolled two ones, points reset and your turn"); //if 1 appears on both dices of computer, it's the participant's turn next. compNum = 0; // points reset to 0 } else System.out.println("no points earned and your turn");
turnNum = 0; turn++; break;
} } while (compNum < 100 && participantNum < 100); if(compNum >= 100) System.out.println("\nYou lose!"); //the participant loses if computer gains 100 or more points else
System.out.println("\nCongratulations! You won!");
return; } }...