Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asg1;
- import java.util.Arrays;
- import utils.TextIO;
- /**
- * @overview A program that performs the coffee tin game on a
- * tin of beans and display result on the standard output.
- *
- */
- 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 = '-';
- /** the null character*/
- private static final char NULL = '\u0000';
- /** constant that represents the bag of available beans*/
- private static final char[] BeansBag =
- {BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
- GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN,
- REMOVED, REMOVED, REMOVED, REMOVED, REMOVED, REMOVED, REMOVED, REMOVED};
- /**
- * the main procedure
- * @effects
- * initialise a coffee tin
- * {@link TextIO#putf(String, Object...)}: print the tin content
- * {@link @tinGame(char[])}: perform the coffee tin game on tin
- * {@link TextIO#putf(String, Object...)}: print the tin content again
- * if last bean is correct
- * {@link TextIO#putf(String, Object...)}: print its colour
- * else
- * {@link TextIO#putf(String, Object...)}: print an error message
- */
- public static void main(String args) {
- // initialise some beans
- char[] tin =
- { BLUE, BLUE, BLUE, GREEN, GREEN, GREEN};
- // count number of greens
- int greens = 0;
- for (char bean : tin) {
- if (bean == GREEN)
- greens++;
- }
- final char last = (greens % 2 == 1) ? GREEN : BLUE;
- // p0 = green parity /\
- // (p0=1 -> last=GREEN) /\ (p0=0 -> last=BLUE)
- // print the content of tin before the game
- TextIO.putf("tin before: %s %n", Arrays.toString(tin));
- // perform the game
- char lastBean = tinGame(tin);
- // lastBean = last \/ lastBean != last
- // print the content of tin and last bean
- TextIO.putf("tin after: %s %n", Arrays.toString(tin));
- // 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);
- }
- }
- /**
- * Performs coffee tin game to determine the last bean.
- *
- * @requires tin neq null /\ tin.length > 0
- * @modifies tin
- * @effects <pre>
- * repeat take out two beans from ti
- * if same colour
- * throw both away, put one blue bean back
- * else
- * put green bean back
- * until tin has less than 2 beans left
- * let p0 = initial number of green beans
- * if p0 = 1
- * result = 'G'
- * else
- * result = 'B'
- * </pre>
- */
- public static char tinGame(char[] tin) {
- while (hasAtLeastTwoBeans(tin)) {
- // take two beans from tin
- char[] takeTwo = takeTwo(tin);
- char b1 = takeTwo[0];
- char b2 = takeTwo[1];
- // process beans to update tin
- /*if (b1 == BLUE && b2 == BLUE) {
- // put B in bin
- putIn(tin, BLUE);
- } else if (b1 == GREEN && b2 == GREEN) {
- // put B in bin
- putIn(tin, BLUE);
- } else { // BG, GB
- // put G in bin
- putIn(tin, GREEN);
- }*/
- updateTin(tin, b1, b2);
- }
- return anyBean(tin);
- }
- /**
- *@requires an interger n as input
- *@effects
- * return an interger number randomly selected from the range [0, n)
- */
- private static int randInt (int n) {
- return (int) (Math.random()*n);
- }
- /**
- *@effects
- * if they are the same colour
- * throw them both away
- * put a blue bean back in (may be taken from an extra bag of blue beans)
- *else
- * throw away the blue one
- * put the green one back
- */
- private static char getBean(char[] BeansBag, char binType) {
- for (char bean: BeansBag) {
- if (bean == binType)
- bean = REMOVED;
- return binType;
- }
- return NULL;
- }
- /**
- *@effects
- * if they are the same colour
- * throw them both away
- * put a blue bean back in (may be taken from an extra bag of blue beans)
- *else
- * throw away the blue one
- * put the green one back
- */
- private static void updateTin (char[] tin, char b1, char b2) {
- if (b1 == b2) {
- // put B in bin
- putIn(tin, getBean(BeansBag, BLUE));
- } else { // BG, GB
- // put G in bin
- putIn(tin, GREEN);
- }
- }
- /**
- *
- * @effects
- * if tin has at least two beans
- * return true
- * else
- * return false
- */
- private static boolean hasAtLeastTwoBeans(char[] tin) {
- int count = 0;
- for (char bean : tin) {
- if (bean != REMOVED) {
- count++;
- }
- if (count >= 2) // enough beans
- return true;
- }
- // not enough beans
- return false;
- }
- /**
- * @requires tin has at least 2 beans left
- * @modifies tin
- * @effects
- * remove any two beans from tin and return them
- * i.e.
- * char b1 = {@link takeOne(char[])} on tin
- * char b2 = {@link takeOne(char[])} on tin
- * result = [b1, b2]
- */
- private static char[] takeTwo(char[] tin) {
- char first = takeOne(tin);
- char second = takeOne(tin);
- return new char[] {first, second};
- }
- /**
- * @requires tin has at least one bean
- * @modifies tin
- * @effects
- * randomly remove a bean from tin and return it
- */
- public static char takeOne(char[] tin) {
- while (hasAtLeastTwoBeans(tin)) {
- // find bean in random positions
- int index = randInt(tin.length);
- char bean = tin[index];
- if (bean != REMOVED) { // found one
- tin[index] = REMOVED;
- return bean;
- }
- }
- // no beans left
- return NULL;
- }
- /**
- * @requires tin has vacant positions for new beans
- * @modifies tin
- * @effects
- * place bean into any vacant position in tin
- */
- private static void putIn(char[] tin, char bean) {
- for (int i = 0; i < tin.length; i++) {
- if (tin[i] == REMOVED) { // vacant position
- tin[i] = bean;
- break;
- }
- }
- }
- /**
- * @effects
- * if there are beans in tin
- * return any such bean
- * else
- * return NULL
- */
- private static char anyBean(char[] tin) {
- for (char bean : tin) {
- if (bean != REMOVED) {
- return bean;
- }
- }
- // no beans left
- return NULL;
- }
- }
Add Comment
Please, Sign In to add comment