1

So I have recently decided to recreate the game of "Dice 10,000" using Java programming and I'm having some issues getting some of the rules to work. There are 4 rules that I cannot get to work.

1) 3 of any number = 10x the face value of the 3 dice

2) A full straight (1 - 6) = 3,000 points

3) 3 pairs = 1,000 points

4) 3 One's = 1,000 points

I'm just not sure how to get these to work... I believe I would use a set, but I am very, very inexperienced with sets and how to use them and what methods they contain. I can probably figure the second one without a set, but it is the 1st, 3rd, and 4th ones giving me the most trouble.

//Andrew M
import java.util.*;
import java.io.*;
class Zilch
{

    public static Random r = new Random();
    public static ArrayList<Integer> diceList = new ArrayList<Integer>();
    public static int count = 0, humanScore = 0, compScore = 0, humanBank = 0, compBank = 0, lastMove = 0;
    public static ArrayList<Integer> diceKeep = new ArrayList<Integer>();
    public static boolean gameOn = true, x = true;
    //-----------------------------------------------------------------------------------
    public static void main(String[] args)
    {
        for (int j = 1; j <= 6; j++)
        {
            diceList.add(j);
            diceKeep.add(null);
        }
        do
        {
            do
            {
                x = true;
                for (int k = 0; k < diceKeep.size(); k++)
                {
                    if (diceKeep.get(k) != null)
                    {
                        System.out.println("Dice #" + (k + 1) + ": " + diceKeep.get(k) + "  -- kept.");
                    }
                }
                System.out.println();
                System.out.println();
                boolean b = true;
                for (int i = 0; i < diceList.size(); i++)
                {
                    if(diceList.get(i) != 0)
                    {
                        diceList.set(i, r.nextInt(6) + 1);
                        System.out.println("Dice #" + (i+1) + " is a " + diceList.get(i));
                    }
                }
                System.out.println();
                System.out.println("Select which dice to roll again... (Select one at a time, 1 - 6), then type \"go\" to complete your turn.");
                Scanner input = new Scanner(System.in);

                /*-----------UNNECCESARY CODE--------------*/
                    else
                    if (selection.equals("go"))
                    {
                        System.out.println();
                        System.out.println();
                        if (diceKeep.get(0) == 1)
                        {
                            humanBank = humanBank + 100;
                            lastMove = 100;
                        }
                        else
                        if (diceKeep.get(0) == 5)
                        {
                            humanBank = humanBank + 500;
                            lastMove = 500;
                        }

/*--------------I NEED THE RULES TO GO HERE---*/        

                        b = false;
                    }
                }
                while(b == true);
                //-----------------------------------------------------------------------
                if(gameOn == true)
                {
                    x = true;
                    //Place Score Card Here
                }
            }
            while (x == true);
        }
        while (1< 2); //Just here for testing purposes, for now
    }
}

Sorry, I know it's a lot right now. Hopefully you guys can give me some sample code to get me going? Thanks a ton.

All the Best

Andrew

EDIT: As of now, I will not be able to respond until tomorrow morning. I apologize for lack of feedback, but it is late. I'll get back with tries and responses tomorrow.

7
  • There are a lot of ways of doing this. If it were me, I would use a Map<Integer, Integer> for pairs, where the key will be the dice's number and the value the number of occurrences. Commented Nov 20, 2015 at 6:50
  • Well I'm looking to put this in an if statement, say as if if(/*There are three fives*/) Also, could you post an answer describing how to use a Map? I will gladly accept it if my problem is solved by it. for now, however, I'm off to bed. I will feedback tomorrow. Commented Nov 20, 2015 at 6:52
  • With a Map<Integer, Integer> you can iterate through the values and see if any of them is bigger then 6 ( three pairs ) or check to see if the map has only one entry ( a full straight ). Commented Nov 20, 2015 at 6:54
  • Me, I'm just starting out, so I don't know how Map works. I'd think about how to do it by hand, without thinking about the programming, yet. For instance, how could you check rule 1? I'm thinking, see how many dice are the same as the first one (to see if that count is 3 (or more?)); then see how many of 2 through 6 are the same as the second die, then how many of 3 through 6 are the same as the 3rd die, then how many of 4 through 6 are the same as the 4th. Two loops (one nested inside the other) can do this for you, I believe. Commented Nov 20, 2015 at 6:56
  • And by the way, please realize that all values in diceKeep are null until assigned. Therefore, If the player keeps dice number 5, then dice numbers 1 - 4 will be null until "kept". Because of this, I cannot use Collections.sort(diceKeep) or a BubbleSort or a Selection Sort (All throw a NullPointerException) Commented Nov 20, 2015 at 7:20

1 Answer 1

1

At the below, I add some comment. It prints count of numbers in arraylist.

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        int [] i = new int [7]; //  array starts with 0, so we can init our array with size 7 (0 to 6).
        ArrayList<Integer> arr = new ArrayList<Integer>();

        // there are 2 times "5"
        arr.add(5); 
        arr.add(5);

        //add some null
        arr.add(null);

        // there is 1 times "3"
        arr.add(3);

        // there is 1 times "6"
        arr.add(6);

        //add some null
        arr.add(null);

        System.out.println("numbers in arr: " + arr);

        for (Integer integer : arr) {
            if (integer!= null)
                i[integer]++; 
        }

        for (int c = 0 ;c<i.length ; c++){
            System.out.println("there are/is " + c + " ->" + i[c] + " times");
        }
    }
}

prints:

numbers in arr: [5, 5, null, 3, 6, null]
there are/is 0 ->0 times
there are/is 1 ->0 times 
there are/is 2 ->0 times 
there are/is 3 ->1 times 
there are/is 4 ->0 times 
there are/is 5 ->2 times 
there are/is 6 ->1 times
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.