Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Homework help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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
    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! :-(");
    	}		
    }	
    }
    }

    Please help.


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help

    this was my latest development:
    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.
    Last edited by helloworld922; October 11th, 2009 at 05:46 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.
    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?
         }
    }

Similar Threads

  1. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  2. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  3. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  4. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM