Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class CoffeeTinGame
- /**
- * @overview: This is program that performs the 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 = '-';
- /**
- * @overview: This is the main procedure
- * @effect:
- *
- */
- public static void main(String[] args)
- {
- char [] tin = {GREEN, BLUE, BLUE, GREEN, GREEN};
- TextIO.putf("The original tin: %s %n", Arrays.toString(tin));
- // take 2 beans random from the original tin and update the tin
- int [] x = takeTwo(tin);
- char [] a = updateTin(tin, takeTwo(tin));
- // continue to take 2 beans from the updated tin
- do
- {
- a = updateTin(a, takeTwo(a));
- } while(a.length > 1);
- //print out the last bean's color
- TextIO.putf("The last bean: %s %n", Arrays.toString(a));
- }
- public static int [] takeTwo(char [] tin)
- {
- int count = tin.length;
- int pos1,pos2;
- // generate random position in the coffee tin
- pos1 = (int)(Math.random()*count);
- do
- {
- pos2 = (int)(Math.random()*count);
- } while(pos1 == pos2);
- //print out the random position
- TextIO.putln("Position " +pos1);
- TextIO.putln("Position " +pos2);
- int [] rand = {pos1, pos2};
- return rand;
- }
- /**
- * @overview: Procedure that include both the "takeTwo" and "updateTin" procedures
- * @effect:
- *
- */
- public static char [] updateTin(char [] tin, int [] rand)
- {
- int count = tin.length;
- int b1 = rand[0];
- int b2 = rand[1];
- // perform the game
- if(b1 == b2)
- {
- tin[b2] = BLUE;
- tin[b1] = REMOVED;
- // push the removed element to the end of the array
- tin[b1] = tin[count - 1];
- tin[count - 1] = REMOVED;
- }
- else
- {
- tin[b2] = GREEN;
- tin[b1] = REMOVED;
- tin[b1] = tin[count - 1];
- tin[count - 1] = REMOVED;
- }
- // print out the result after each removal
- for(int x = 0; x < tin.length; x++)
- TextIO.put("[" +tin[x]+ "]");
- TextIO.putln(" ");
- TextIO.putln("============");
- // create a new array tp return the result
- char [] newTin = new char [count - 1];
- for (int i = 0; i<count-1; i++)
- {
- newTin[i] = tin[i];
- }
- return newTin;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement