Results 1 to 3 of 3
  1. #1
    mambalamba is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Dice game issue- any Java gamblers ableto help?:P

    Hello everyone- Iím a new kid here,

    As most of the beginners Iím experiencing some issues with my first Java programÖ

    Basically I need to write a Pig Dice Game. Rules are as below:

    ∑ Itís a two players game- first to reach 100 or more points wins
    ∑ Players take turn- on each turn player rolls six-sided dice
    ∑ If the player rolls 1, then the player gets no new points and it becomes other playerís turn
    ∑ If the player rolls 2 through to 6, he/she can:
    o Roll again
    o Hold- at this point the sum of all rolls is added to the playerís score and it becomes the other playerís turn
    ∑ The first player is a human and the other is the computer
    ∑ When itís the human turn, the program should show the score of both players and the previous roll. Allow human to put ďrĒ to roll again and ďhĒ to hold
    ∑ The computer should play according to the following rule:
    o Keep rolling when it is the computerís turn until it has accumulated 20 or more points. Then hold, If the computer wins or rolls 1, then the turn ends immediately.
    ∑ Allow the human to roll first.

    Guys below is the code I came up with after I donít know how many hours spent, thatís the best I could do so farÖ

    The small success for me is the fact that finally the file complies and I donít get any error messages, however the problem is that Iím not getting any output- just the message Process Completed (I use the Jcreator). Can you PLEASE have a look and help me out with that?
    I know I still need to come up with the victory conditions- at the moment they are put as a comment- after putting them in I started to get again errors, but at least I want to have a peace of mind that the main part is done. Iím close to a nervous breakdown, because this thing is driving me madÖ J

    Any help will be much appreciated!!


    import java.util.Scanner;

    public class Assignment
    {

    int humanScore, computerScore = 0;
    Boolean humanTurn = true;
    Boolean computerTurn = false;
    int totalHuman, totalComputer = 0;
    int dice;

    public void roll()
    {
    dice = (int)(Math.random()*6) + 1;
    }

    public int getDice()
    {
    return dice;
    }

    public int HumanGame(){

    do{
    roll();


    if(dice == 1)
    {
    System.out.println("You got 1 and you lost your turn.");
    humanTurn = false;
    computerTurn = true;
    }
    else if(dice != 1)
    {
    System.out.println("You scored: " + getDice());
    System.out.println("You can hold or roll again.");
    System.out.println("To roll again press 'r' or'h' to hold.");
    Scanner keyboard = new Scanner(System.in);
    String choice = keyboard.nextLine();
    totalHuman = totalHuman + dice;
    System.out.println(totalHuman);
    if(choice.equalsIgnoreCase("r")){
    {
    System.out.println("You pressed 'r'.");
    System.out.println("Lets roll again.");
    roll();
    }
    if(choice.equalsIgnoreCase("h")){
    System.out.println("You pressed 'h' and loose your turn.");
    humanTurn = false;
    computerTurn = true;
    }
    }
    if(totalHuman >= 100)
    {
    System.out.println("Congragtulations, you win!");
    System.exit(0);
    }
    }


    }while(humanTurn = true);
    return getDice();
    }
    public int computerTurn()
    {
    System.out.println("Now it's computer turn.");
    do {
    roll();

    if(dice == 1) {
    System.out.println("Computer thrown 1 therefore it's your turn now.");
    totalComputer = totalComputer + dice;
    System.out.println(totalComputer);
    computerTurn = false;
    humanTurn = true;
    }
    if(totalComputer >= 20){
    System.out.println("Computer scored already 20 points, you'd better start to focus.");
    totalComputer = totalComputer + dice;
    System.out.println(totalComputer);
    computerTurn = false;
    humanTurn = true;
    }
    if(totalComputer >=100)
    {
    System.out.println("Game Over! the computer wins");
    System.exit(0);
    }

    }while (computerTurn = true);
    return dice;

    }


    public static void main(String[] args)
    {
    new Assignment();
    }


    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    When posting code, highlight it and click the "code" button (#). That way the code will be readable when it appears here.

    You don't seem to be invoking the HumanGame() method anywhere.

    Write one very small step at a time. Be sure you have a clear idea, not of what the code should be but why you are writing that code (ie how you intend it to fit in with your overall plan). Then check that it compiles and does what you expect before moving on.

    A start might look like:

    Java Code:
    public class Assignment
    {
        public static void main(String[] args)
        {
            Assignment test = new Assignment();
            test.HumanGame();
        }
    
        void HumanGame()
        {
            System.out.println("Output appearing!");
        }
    }

  3. #3
    mambalamba is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi Pal,

    Great thanks for looking at my post and replying!

    Sorry for not putting my code in the proper way- will be better the next time though, promise!

    Thanks God, I managed to crack the issue and now my program runs. I still have to overcome some task order issues but it shouldn't be a problem.

    In case it will be I will ask for help once again!

    In the meantime once again THANKS for help!
    Lucas

Similar Threads

  1. Unreachable statement Pig Dice Game
    By noturn10 in forum New To Java
    Replies: 2
    Last Post: 11-22-2009, 12:36 AM
  2. Help with a dice game.
    By hero in forum AWT / Swing
    Replies: 14
    Last Post: 07-26-2009, 11:50 AM
  3. Help with dice game...student seeking advice
    By waparson in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 03:31 PM
  4. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 01:01 AM
  5. help debugging a dice game
    By Windoze in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •