2 Replies - 887 Views - Last Post: 13 October 2012 - 12:54 AM Rate Topic: -----

#1 sarmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-March 12

Question about my dice game

Posted 12 October 2012 - 09:11 PM

Im trying to get better at programming. so I came up with this game, its like 21 but using dice. Little help would be great, tips, hints, etc thank you

  • The first thing that i havent been able to fix is it only rolls 1 die when its supposed to be 2.
  • Second I have no idea how to do make my counter, i know its supposed to be something like playerTotal++
  • Third once i get the counter i could make the if statement for it over 21 you lose. then make the comp play.


import java.util.Scanner;

public class AGameOfTwentyOne
{
    public static void main(String [] args)
    {
        String rollDie = "y";
        int playerTotal = 0;
        Scanner kb = new Scanner(System.in);

        Die playerDie = new Die();
        Die compDie = new Die();

        do
        {

            while(rollDie.equalsIgnoreCase("y"))
            {
                System.out.print("Roll the dice? (y/n) : ");
                rollDie = kb.nextLine();
                playerDie.roll();
                playerDie.roll();
                System.out.println("Your score is: "+ playerDie.getValue());

            }

        }while(rollDie == "y");
    }
}



import java.util.Random;

public class Die
{
    private final int SIDES = 6;
    private int value;

    Die()
    {
        roll();
    }

    public void roll()
    {
        Random randomValue = new Random();
        value = randomValue.nextInt(SIDES) + 1;
    }

    public int getValue()
    {
        return value;
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Question about my dice game

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Question about my dice game

Posted 13 October 2012 - 12:02 AM

So for your die roll problem you are not adding to the value, your resting it every time you roll:
value = randomValue.nextInt(SIDES) + 1;


should be
value += randomValue.nextInt(SIDES) + 1;


Second, you can place the playerTotal++ inside the end of your while loop. That will keep track of the number of times you said yes to another roll.

By fixing the code above you will now have a running total that you can check against to see when the player hits a score of 13 or greater. Probably have something inside the last line of the do/while loop.

if( score >= 13 )
{
  print out "You lose"
  break;
}

Was This Post Helpful? 0
  • +
  • -

#3 sarmen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-March 12

Re: Question about my dice game

Posted 13 October 2012 - 12:54 AM

Wow I cant believe I didnt notice that.

I meant to do the += but I guess I missed it. Always great to have a second pair of eyes. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1