-1
\$\begingroup\$

I have write a program to determine the color of last bean from a tin (char[] beans). "Beans" in a tin are only green or blue.

The rule is: If two beans taken out are both greens or blues, put 1 blue "bean" back, else put the green one back. Repeat until there are only 1 "bean" left. I don't know whether my code is right or not.

public class CoffeeTinGame {
    /** constant value for the green bean*/
    private static final char GREEN = 'G';
    /** constant value for the blue bean*/
    private static final char BLUE = 'B';
    /** constant for removed beans */
    private static final char REMOVED = '-';

    public static void main(String[] args) {
        char[] beans = { GREEN, BLUE, BLUE, GREEN, GREEN };

        // count number of greens
        int greens = 0;
        for (char b : beans) { 
            if (b == GREEN)
                greens++;
        }
        // the expected last bean
        final char last = (greens % 2 == 1) ? GREEN : BLUE;

        // print the content of tin before the game
        TextIO.putf("tin before: %s %n", Arrays.toString(beans));

        // perform the game
        char lastBean = tinGame(beans);

        // print the content of tin and last bean
        TextIO.putf("tin after: %s %n", Arrays.toString(beans));

        // check if last bean as expected and print 
        if (lastBean == last) { 
            TextIO.putf("last bean: %c ", lastBean);      
        } else {
            TextIO.putf("Oops, wrong last bean: %c (expected: %c)%n",lastBean,last);
        }
    }

    private static int takeTwo(char[] tin)  {
        do {
            int bi1= (int)(Math.random()*tin.length); 
        } while (tin[bi1] != REMOVED);
        return b1;

        do {
            int bi2= (int)(Math.random()*tinLength);
        } while (tin[bi2]!=REMOVED || bi2!=bi1);
        return bi2;
    }


    private static char tinGame(char[] tin) {    
        int count = tin.length;
        while (count >= 2) {
            // remove b1, b2 from tin
            int bi1, bi2= takeTwo(beans);
            b1 = tin[bi1];             
            b2 = tin[bi2];          
            tin[bi1] = REMOVED; 
            tin[bi2] = REMOVED;     
            if (b1 == BLUE && b2 == BLUE|| b1 == GREEN && b2 == GREEN) { 
                // put B in bin
                tin[bi2] = BLUE;        
            } else { // BG, GB    
                // put G in bin
                tin[bi2] = GREEN;   
            }
            count = count-1;        
        }
        return tin;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

There are a number of issues with you code:

The primary one that will make it not work is:

int bi1, bi2= takeTwo(beans);

This does not do what it looks like it does. It only assigns to bi2 and leaves bi1 unassigned and therefore zero.


Also in takeTwo you have:

return b1;

None of the code following will ever be executed.


I would suggest that instead of takeTwo (which would need to return a Pair which would be a bit advanced for a beginner) you implement a pickOneAtRandom - perhaps something like:

private static int pickOneAtRandom(char[] tin, int notThisOne) {
    int bi1;
    do {
        bi1 = (int) (Math.random() * tin.length);
    } while (bi1 == notThisOne || tin[bi1] == REMOVED);
    return bi1;
}

You can then use it to choose your bi1 and bi2 like:

    int bi1 = pickOneAtRandom(beans, -1);
    int bi2 = pickOneAtRandom(beans, bi1);

Your use of Random is a little strange. The normal way to get a arndom in a specific range would be something like this:

private static final Random random = new Random();

private static int pickOneAtRandom(char[] tin, int notThisOne) {
    int bi1;
    do {
        bi1 = (int) (random.nextInt(tin.length));
    } while (bi1 == notThisOne || tin[bi1] == REMOVED);
    return bi1;
}
\$\endgroup\$
4
  • \$\begingroup\$ My main question is that the way I call takeTwo is right or not. If it is not true, can you edit for mr. I am Java begiiner \$\endgroup\$
    – ʚïɞ Gemini ʚïɞ
    Apr 23, 2015 at 8:54
  • \$\begingroup\$ @ʚïɞGeminiʚïɞ - I just noticed a mistake in my pickOneAtRandom - it should read while (bi1 == notThisOne || tin[bi1] == REMOVED);. \$\endgroup\$ Apr 23, 2015 at 10:14
  • \$\begingroup\$ I have some problem. There is a mistake in return tin[bi1] in this code. can you help me?? It is the last :) \$\endgroup\$
    – ʚïɞ Gemini ʚïɞ
    Apr 23, 2015 at 10:29
  • \$\begingroup\$ @ʚïɞGeminiʚïɞ - Sorry but I cannot do your homework for you. \$\endgroup\$ Apr 23, 2015 at 10:32

Your Answer

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