# Homework help

• October 10th, 2009, 01:02 PM
cd247
Homework help
I have to make the game of pig. here is a link: The Game of Pig
The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions:

* roll - If the player rolls a
o 1: the player scores nothing and it becomes the opponent's turn.
o 2 - 6: the number is added to the player's turn total and the player's turn continues.
* hold - The turn total is added to the player's score and it becomes the opponent's turn.
I have to have:
two players (one human and one computer)
the computer can hold after >=20
i have to input 'r' to roll and 'h' to hold
Code :

```import java.util.Random; import java.util.Scanner;   public class piggame {   /** * @param args */ public static void main(String[] args) { int dice3 = 0, dice2 = 0, player1 = 0, computer = 0, dice4 = 0, dice = 0; Scanner di = new Scanner(System.in); Random diceP = new Random(); Random diceC = new Random(); System.out.println("Player 1, Please press r to Roll"); char letter = di.next().charAt(0); if(letter == 'r'){ do{ // Player 1's turn player1 = dice3; System.out.println("Roll or hold"); dice2 = (int) (diceP.nextInt(6)) + 1; System.out.println("you have " + dice2); dice3 = dice3 + dice2; System.out.println(dice3); letter = di.next().charAt(0); break; }while(dice2 != 1); if (dice2 == 1){ letter = 'h'; dice3 = 0;   if (letter == 'h'){ do{ // computer's turn dice = (int) (diceC.nextInt(6)) + 1; dice4 = dice4 + dice; computer = dice4; System.out.println(dice3); break;   }while(dice != 1); } if (dice2 == 1){ //letter = 'r'; dice4 = 0;   } if (computer >= 100){ System.out.println("Computer has won! :-(");   }else if(player1 >= 100) System.out.println("Player 1 has won! :-("); } } } }```

• October 10th, 2009, 10:40 PM
cd247
Re: Homework help
this was my latest development:
Code :

```import java.util.Random; import java.util.Scanner;   public class PigGame {   /** * @param args */ public static void main(String[] args) { int dice = 0, dice2 = 0, dice3 = 0, dice4 = 0, player1 = 0, dice5 = 0, computer = 0; Scanner di = new Scanner(System.in); Random diceP = new Random(); boolean pturn = true, cturn = false; if (pturn == true) { System.out.println("Player 1, Please press r to Roll"); char letter = di.next().charAt(0);   if (letter == 'r' || letter == 'R') { dice2 = (diceP.nextInt(6)) + 1; if (dice2 == 1) { dice4 = 0; pturn = false; cturn = true; } if (dice2 != 1) { do { // Player 1's turn // player1 = dice3; System.out.println("Roll or hold"); dice2 = (diceP.nextInt(6)) + 1; System.out.println("you have " + dice2); dice4 += dice2; System.out.println(dice3); pturn = true; } while (dice2 != 1);   if (letter == 'h' || letter == 'H') { dice5 = dice5 + dice4; player1 = dice5; pturn = false; cturn = true; } } if (cturn == true) { if (dice2 != 1) { if (dice4 < 20) { dice2 = (diceP.nextInt(6)) + 1; dice4 += dice2; cturn = true;   } if (dice2 == 1) { dice4 = 0; cturn = false; pturn = true; } if (dice4 >= 20) { dice = dice4; computer = dice; cturn = false; pturn = true; } } } } } if (computer >= 100) { System.out.println("Computer has won! :-(");   } else if (player1 >= 100) { System.out.println("Player 1 has won! :-)"); } }   }```
This does not return a number.
• October 11th, 2009, 05:56 PM
helloworld922
Re: Homework help
? I don't understand why it should return a number. In Java, main methods are declared to have a void return type, where as in c/c++, it's common practice to have the main function return an int.

I would strongly recommend using helper methods, it'll make coding much easier.

Here's the general flow of how the game works, I'll leave implementing the helper methods up to you.
Code :

```public class PigGame { public static void main(String[] args) { int playerScore = 0, compScore = 0; boolean playerTurn = true; while (playerScore < 100 && compScore < 100) { int tempScore = 0; if (playerTurn) { if (getIfPlayerWantsToRollAgain()) { int roll = (int)(Math.random()*5+1); if (roll == 1) { playerTurn = false; } else { tempScore += roll; } } else { playerScore += tempScore; playerTurn = false; } else { compScore = doComputersTurn(compScore); playerTurn = true; } } }   public static boolean getIfPlayerWantsToRollAgain() { // what goes here? }   public static int doComputersTurn(int compCurrentScore) { // what goes here? } }```