Daste-was-me

asgm1

Apr 12th, 2022 (edited)
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.37 KB | None | 0 0
  1. package asg1;
  2. import java.util.Arrays;
  3. import utils.TextIO;
  4.    
  5.     /**
  6.     * @overview A program that performs the coffee tin game on a
  7.     * tin of beans and display result on the standard output.
  8.     *
  9.     */
  10. public class CoffeeTinGame {
  11.     /** constant value for the green bean*/
  12.     private static final char GREEN = 'G';
  13.     /** constant value for the blue bean*/
  14.     private static final char BLUE = 'B';  
  15.     /** constant for removed beans */
  16.     private static final char REMOVED = '-';
  17.     /** the null character*/
  18.     private static final char NULL = '\u0000';
  19.     /** constant that represents the bag of available beans*/
  20.     private static final char[] BeansBag =
  21.                                     {BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
  22.                                      GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN, GREEN,
  23.                                      REMOVED, REMOVED, REMOVED, REMOVED, REMOVED, REMOVED, REMOVED, REMOVED};
  24.     /**
  25.     * the main procedure
  26.     * @effects
  27.     * initialise a coffee tin
  28.     * {@link TextIO#putf(String, Object...)}: print the tin content
  29.     * {@link @tinGame(char[])}: perform the coffee tin game on tin
  30.     * {@link TextIO#putf(String, Object...)}: print the tin content again
  31.     * if last bean is correct
  32.     * {@link TextIO#putf(String, Object...)}: print its colour
  33.     * else
  34.     * {@link TextIO#putf(String, Object...)}: print an error message
  35.     */
  36.     public static void main(String args) {
  37.         // initialise some beans
  38.         char[] tin =
  39.         { BLUE, BLUE, BLUE, GREEN, GREEN, GREEN};
  40.            
  41.         // count number of greens
  42.         int greens = 0;
  43.         for (char bean : tin) {
  44.         if (bean == GREEN)
  45.             greens++;
  46.         }
  47.  
  48.         final char last = (greens % 2 == 1) ? GREEN : BLUE;
  49.         // p0 = green parity /\
  50.         // (p0=1 -> last=GREEN) /\ (p0=0 -> last=BLUE)
  51.  
  52.         // print the content of tin before the game
  53.         TextIO.putf("tin before: %s %n", Arrays.toString(tin));
  54.  
  55.         // perform the game
  56.         char lastBean = tinGame(tin);
  57.         // lastBean = last \/ lastBean != last
  58.    
  59.         // print the content of tin and last bean
  60.         TextIO.putf("tin after: %s %n", Arrays.toString(tin));
  61.  
  62.         // check if last bean as expected and print
  63.         if (lastBean == last) {
  64.             TextIO.putf("last bean: %c ", lastBean);
  65.         } else {
  66.             TextIO.putf("Oops, wrong last bean: %c (expected: %c)%n", lastBean,last);
  67.         }
  68.     }
  69.    
  70.     /**
  71.     * Performs coffee tin game to determine the last bean.
  72.     *
  73.     * @requires tin neq null /\ tin.length > 0
  74.     * @modifies tin
  75.     * @effects <pre>
  76.     *   repeat take out two beans from ti
  77.     *       if same colour
  78.     *           throw both away, put one blue bean back
  79.     *       else
  80.     *           put green bean back
  81.     *   until tin has less than 2 beans left
  82.     *   let p0 = initial number of green beans
  83.     *   if p0 = 1
  84.     *       result = 'G'
  85.     *   else
  86.     *       result = 'B'
  87.     * </pre>
  88.     */
  89.      
  90.     public static char tinGame(char[] tin) {
  91.         while (hasAtLeastTwoBeans(tin)) {
  92.             // take two beans from tin  
  93.             char[] takeTwo = takeTwo(tin);
  94.             char b1 = takeTwo[0];
  95.             char b2 = takeTwo[1];
  96.             // process beans to update tin
  97.             /*if (b1 == BLUE && b2 == BLUE) {
  98.                 // put B in bin
  99.                 putIn(tin, BLUE);
  100.             } else if (b1 == GREEN && b2 == GREEN) {
  101.                 // put B in bin
  102.                 putIn(tin, BLUE);
  103.             } else { // BG, GB
  104.                 // put G in bin
  105.                 putIn(tin, GREEN);
  106.             }*/
  107.             updateTin(tin, b1, b2);
  108.         }
  109.         return anyBean(tin);
  110.     }
  111.    
  112.         /**
  113.     *@requires an interger n as input
  114.     *@effects
  115.     *   return an interger number randomly selected from the range [0, n)
  116.     */
  117.     private static int randInt (int n) {
  118.         return (int) (Math.random()*n);
  119.     }
  120.    
  121.     /**
  122.     *@effects
  123.     * if they are the same colour
  124.     *    throw them both away
  125.     *    put a blue bean back in (may be taken from an extra bag of blue beans)
  126.     *else
  127.     *    throw away the blue one
  128.     *    put the green one back
  129.     */
  130.     private static char getBean(char[] BeansBag, char binType) {
  131.         for (char bean: BeansBag) {
  132.             if (bean == binType)
  133.                 bean = REMOVED;
  134.                 return binType;
  135.             }
  136.         return NULL;
  137.     }
  138.        
  139.     /**
  140.     *@effects
  141.     * if they are the same colour
  142.     *    throw them both away
  143.     *    put a blue bean back in (may be taken from an extra bag of blue beans)
  144.     *else
  145.     *    throw away the blue one
  146.     *    put the green one back
  147.     */
  148.     private static void updateTin (char[] tin, char b1, char b2) {
  149.        
  150.         if (b1 == b2) {
  151.             // put B in bin
  152.             putIn(tin, getBean(BeansBag, BLUE));
  153.         } else { // BG, GB
  154.                 // put G in bin
  155.                 putIn(tin, GREEN);
  156.         }
  157.     }    
  158.        
  159.     /**
  160.     *
  161.     * @effects
  162.     * if tin has at least two beans
  163.     *   return true
  164.     * else
  165.     *   return false
  166.     */
  167.     private static boolean hasAtLeastTwoBeans(char[] tin) {
  168.         int count = 0;
  169.         for (char bean : tin) {
  170.             if (bean != REMOVED) {
  171.                 count++;
  172.             }
  173.         if (count >= 2) // enough beans
  174.                 return true;
  175.         }
  176.         // not enough beans
  177.         return false;
  178.     }
  179.        
  180.     /**
  181.     * @requires tin has at least 2 beans left
  182.     * @modifies tin
  183.     * @effects
  184.     *   remove any two beans from tin and return them
  185.     *   i.e.
  186.     *   char b1 = {@link takeOne(char[])} on tin
  187.     *   char b2 = {@link takeOne(char[])} on tin
  188.     *   result = [b1, b2]
  189.     */
  190.     private static char[] takeTwo(char[] tin) {
  191.         char first = takeOne(tin);
  192.         char second = takeOne(tin);
  193.         return new char[] {first, second};
  194.     }
  195.        
  196.     /**
  197.     * @requires tin has at least one bean
  198.     * @modifies tin
  199.     * @effects
  200.     *    randomly remove a bean from tin and return it
  201.     */
  202.     public static char takeOne(char[] tin) {
  203.        while (hasAtLeastTwoBeans(tin)) {
  204.            // find bean in random positions
  205.            int index = randInt(tin.length);
  206.             char bean = tin[index];
  207.            
  208.             if (bean != REMOVED) { // found one
  209.                 tin[index] = REMOVED;
  210.                 return bean;
  211.             }
  212.         }
  213.         // no beans left
  214.         return NULL;
  215.     }
  216.    
  217.     /**
  218.     * @requires tin has vacant positions for new beans
  219.     * @modifies tin
  220.     * @effects
  221.     * place bean into any vacant position in tin
  222.     */
  223.     private static void putIn(char[] tin, char bean) {
  224.         for (int i = 0; i < tin.length; i++) {
  225.             if (tin[i] == REMOVED) { // vacant position
  226.                 tin[i] = bean;
  227.                 break;
  228.             }
  229.         }
  230.     }
  231.        
  232.     /**
  233.     * @effects
  234.     * if there are beans in tin
  235.     * return any such bean
  236.     * else
  237.     * return NULL
  238.     */
  239.     private static char anyBean(char[] tin) {
  240.         for (char bean : tin) {
  241.             if (bean != REMOVED) {
  242.             return bean;
  243.         }
  244.     }
  245.         // no beans left
  246.         return NULL;
  247.     }
  248. }
  249.      
Add Comment
Please, Sign In to add comment