Advertisement
jdalbey

Cootie - Functional solution

Apr 3rd, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.55 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Cooties class draws a cootie based on dice roll input.
  5.  *
  6.  * @author Ayan Patel
  7.  * @author Ryan Cook
  8.  * @author J. Dalbey
  9.  * @version 1.1
  10.  */
  11. public class Cooties_functional
  12. {
  13.     /**
  14.      * The entry point for this application
  15.      * @param args (ignored)
  16.      */
  17.     public static void main(String[] args)
  18.     {
  19.    
  20.         // parts of a cootie
  21.         int i = 0;
  22.         int numLegs = 0;
  23.         int maxLegs = 6;
  24.         int numEyes = 0;
  25.         int maxEyes = 2;
  26.         int head = 0;
  27.         int numAnt = 0;
  28.         int maxAnt = 2;
  29.         int tail = 0;
  30.         int body = 0;
  31.         boolean done = false;
  32.         boolean error = false;
  33.         int diceRoll = 0;
  34.         int turns = 0;
  35.        
  36.         //Create a scanner to read from standard input
  37.         Scanner console = new Scanner(System.in);
  38.  
  39.         //Loop until cootie is done
  40.         while (!done)
  41.         {
  42.             error = false;
  43.            
  44.             //Get input from user
  45.             System.out.println(" Input dice roll: ");
  46.             diceRoll = console.nextInt();
  47.            
  48.             //If input is body
  49.             if (diceRoll == 1)
  50.             {
  51.                 if (body == 0)
  52.                 {
  53.                     body = 1;
  54.                 }
  55.                 else
  56.                 {
  57.                     error = true;
  58.                 }
  59.             }
  60.             //If input is head
  61.             else if (diceRoll == 2)
  62.             {
  63.                 if (body == 1 && head == 0)
  64.                 {
  65.                     head = 1;
  66.                 }
  67.                 else
  68.                 {
  69.                     error = true;
  70.                 }
  71.             }
  72.             //If input is leg
  73.             else if (diceRoll == 3)
  74.             {
  75.                 if (body == 1 && numLegs < maxLegs)
  76.                 {
  77.                     numLegs += 1;
  78.                 }
  79.                 else
  80.                 {
  81.                     error = true;
  82.                 }
  83.             }
  84.             //If input is antenna
  85.             else if (diceRoll == 4)
  86.             {
  87.                 if (head >= 1 && numAnt < maxAnt)
  88.                 {
  89.                     numAnt += 1;
  90.                 }
  91.                 else
  92.                 {
  93.                     error = true;
  94.                 }
  95.             }
  96.             //If input is eyes
  97.             else if (diceRoll == 5)
  98.             {
  99.                 if (head >= 1 && numEyes < maxEyes)
  100.                 {
  101.                     numEyes += 1;
  102.                 }
  103.                 else
  104.                 {
  105.                     error = true;
  106.                 }
  107.             }
  108.             //If input is tail
  109.             else if (diceRoll == 6)
  110.             {
  111.                 if (body == 1 && tail == 0)
  112.                 {
  113.                     tail = 1;
  114.                 }
  115.                 else
  116.                 {
  117.                     error = true;
  118.                 }
  119.             }
  120.             else
  121.             {
  122.                 System.out.println(" Invalid input.");
  123.             }
  124.             // Print the current cootie
  125.            
  126.             // Print antenna
  127.             String antOut = "";
  128.             if (numAnt > 0)
  129.             {
  130.                 antOut += "  !";
  131.             }
  132.             if (numAnt == 2)
  133.             {
  134.                 antOut += " !";
  135.             }
  136.             System.out.println(antOut);
  137.            
  138.             // Print head and eys
  139.             String headOut = "";
  140.             if (head >= 1)
  141.             {
  142.                 headOut += " (";
  143.             }
  144.             if (numEyes > 0)
  145.             {
  146.                 headOut += "o ";
  147.             }
  148.             else
  149.             {
  150.                 headOut += "  ";
  151.             }
  152.             if (numEyes == 2)
  153.             {
  154.                 headOut += "o";
  155.             }
  156.             else
  157.             {
  158.                 headOut += " ";
  159.             }
  160.             if (head >= 1)
  161.             {
  162.                 headOut += ")";
  163.             }
  164.             System.out.println(headOut);
  165.            
  166.             //loop unitl all legs are created
  167.             for (i = 0; i < maxLegs; i = i + 2)
  168.             {
  169.                 String bodyOut = "";
  170.                 if (numLegs > i)
  171.                 {
  172.                     bodyOut += "- ";
  173.                 }
  174.                 else
  175.                 {
  176.                     bodyOut += "  ";
  177.                 }
  178.                 if (body == 1)
  179.                 {
  180.                     bodyOut += "[ ]";
  181.                 }
  182.                 if (numLegs > i + 1)
  183.                 {
  184.                     bodyOut += " - ";
  185.                 }
  186.                 else
  187.                 {
  188.                     bodyOut += " ";
  189.                 }
  190.                 System.out.println(bodyOut);
  191.             }
  192.            
  193.             //Create tail
  194.             if (tail == 1)
  195.             {
  196.                 System.out.println("   T");
  197.             }
  198.            
  199.             // A part could not be added with that roll.
  200.             if (error)
  201.             {
  202.                 System.out.println(" Can't add a part. \n");
  203.             }
  204.            
  205.             turns += 1;
  206.            
  207.             //Once everything is complete, display message
  208.             if (body == 1 && numLegs == maxLegs && numEyes == maxEyes && numAnt == maxAnt && tail == 1)
  209.             {
  210.                 System.out.println(" Congratulations you have completed your cootie!");
  211.                 System.out.println(" It took you " + turns + " turns to finish your cootie. ");
  212.                 done = true;
  213.             }
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement