Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Cooties App creates a cootie based on dice roll input.
- * @version 2.0
- */
- public class CootieApp
- {
- Die die;
- Cootie cootie;
- /**
- * The entry point for this application
- * @param args (ignored)
- */
- public static void main(String[] args)
- {
- new CootieApp().run();
- }
- // Keep obtaining user input until cootie is complete
- public void run()
- {
- cootie = new Cootie();
- die = new Die();
- //Loop until cootie is done
- while (!cootie.complete())
- {
- int diceRoll = die.roll();
- cootie.takeTurn(diceRoll);
- }
- //give congrats message
- cootie.showDone();
- }
- }
- import java.util.List;
- import java.util.ArrayList;
- /**
- * Cootie is an imaginary bug-like creature.
- */
- public class Cootie
- {
- // number of turns take
- int turns;
- // The main parts of a Cootie
- Tail tail;
- Head head;
- Body body;
- List<BodyPart> partsList;
- /**
- * Constructor for objects of class Cootie
- */
- public Cootie()
- {
- turns = 0;
- body = new Body();
- tail = new Tail();
- head = new Head();
- // create a list of the body parts
- partsList = new ArrayList<BodyPart>();
- partsList.add(head);
- partsList.add(body);
- partsList.add(tail);
- }
- /**
- * Perform a turn given a roll of the dice
- * @param diceRoll the input number
- */
- public void takeTurn(int diceRoll)
- {
- turns += 1;
- // See if a body can be added
- body.add(diceRoll);
- // If we have a body, try to add head and tail
- if (body.hasBody())
- {
- head.add(diceRoll);
- tail.add(diceRoll);
- }
- // Draw cootie
- for (BodyPart part: partsList)
- {
- System.out.println(part.draw());
- }
- }
- /**
- * Is the cootie finished?
- * @return true if all parts are drawn
- */
- public boolean complete()
- {
- return (body.complete() && tail.complete() && head.complete());
- }
- /**
- * Show a congratulations message.
- */
- public void showDone()
- {
- System.out.println(" Congratulations you have completed your cootie!");
- System.out.println(" It took you " + turns + " turns to finish your cootie. ");
- }
- }
- /**
- * Antenna for a cootie.
- */
- public class Antenna
- {
- private int numAntenna;
- public static final int kMaxAntenna = 2;
- /**
- * Constructor for objects of class Tail
- */
- public Antenna()
- {
- numAntenna = 0;
- }
- /**
- * Determine if a part can be added.
- */
- public void add()
- {
- if (numAntenna < kMaxAntenna)
- {
- numAntenna += 1;
- }
- }
- /**
- * Is the part completed?
- * @return true if the part has been completely assembled
- */
- public boolean complete()
- {
- return numAntenna >= kMaxAntenna;
- }
- /**
- * Draw the part.
- * @return String representation of the part.
- */
- public String draw()
- {
- String result = " ";
- if (numAntenna == 1)
- {
- result = " ! ";
- }
- if (numAntenna == 2)
- {
- result = " ! !";
- }
- return result + "\n";
- }
- }
- /**
- * Body is the core of the Cootie
- */
- public class Body implements BodyPart
- {
- private boolean hasBody;
- private Legs legs;
- /**
- * Constructor for objects of class Body
- */
- public Body()
- {
- hasBody = false;
- legs = new Legs();
- }
- @Override
- public void add(int diceRoll)
- {
- if (diceRoll == 1)
- {
- hasBody = true;
- }
- else if (diceRoll == 3)
- {
- if (hasBody)
- {
- legs.add();
- }
- }
- }
- /**
- * Has a body been added?
- * @return true if it has a body
- */
- public boolean hasBody()
- {
- return hasBody;
- }
- @Override
- public boolean complete()
- {
- return legs.complete() && hasBody;
- }
- @Override
- public String draw()
- {
- String bodyOut = "";
- //loop until all legs are created
- for (int segment = 0; segment < Legs.kMaxLegs; segment = segment + 2)
- {
- // Add a leg on left side?
- if (legs.count() > segment)
- {
- bodyOut += "- ";
- }
- else
- {
- bodyOut += " ";
- }
- if (hasBody)
- {
- bodyOut += "[ ]";
- }
- // Add a leg on right side?
- if (legs.count() > segment + 1)
- {
- bodyOut += " - ";
- }
- bodyOut += "\n";
- }
- return bodyOut.substring(0, bodyOut.length()-1);
- }
- }
- /**
- * BodyPart tells what parts of a cootie can do.
- *
- */
- public interface BodyPart
- {
- /**
- * Determine if a part can be added.
- * @param diceRoll the input number
- */
- public void add(int diceRoll);
- /**
- * Draw the part.
- * @return String representation of the part.
- */
- public String draw();
- /**
- * Is the part completed?
- * @return true if the part has been completely assembled
- */
- public boolean complete();
- }
- import java.util.Scanner;
- /**
- * Die is a cube with numbers on each side.
- */
- public class Die
- {
- Scanner console;
- /**
- * Constructor for objects of class Die
- */
- public Die()
- {
- console = new Scanner(System.in);
- }
- // Get user input
- public int roll()
- {
- System.out.println(" Input dice roll: ");
- int diceRoll = console.nextInt();
- // Repeat until a valid number is entered
- while (diceRoll < 1 || diceRoll > 6)
- {
- System.out.println(" Invalid input.");
- diceRoll = console.nextInt();
- }
- return diceRoll;
- }
- }
- /**
- * Eyes of a cootie.
- */
- public class Eyes
- {
- private int numEyes;
- public static final int kMaxEyes = 2;
- /**
- * Constructor for objects of class Tail
- */
- public Eyes()
- {
- numEyes = 0;
- }
- /**
- * Determine if a part can be added.
- */
- public void add()
- {
- if (numEyes < kMaxEyes)
- {
- numEyes += 1;
- }
- }
- /**
- * Is the part completed?
- * @return true if the part has been completely assembled
- */
- public boolean complete()
- {
- return numEyes >= kMaxEyes;
- }
- /**
- * Draw the part.
- * @return String representation of the part.
- */
- public String draw()
- {
- String result = " ";
- if (numEyes == 1)
- {
- result = "o ";
- }
- if (numEyes == 2)
- {
- result = "o o";
- }
- return result;
- }
- }
- /**
- * the head of a cootie.
- */
- public class Head implements BodyPart
- {
- private boolean hasHead;
- private Eyes eyes;
- private Antenna feelers;
- /**
- * Constructor for objects of class Head
- */
- public Head()
- {
- eyes = new Eyes();
- feelers = new Antenna();
- hasHead = false;
- }
- @Override
- public void add(int diceRoll)
- {
- // add a head?
- if (diceRoll == 2)
- {
- hasHead = true;
- }
- // add eyes?
- if (diceRoll == 5)
- {
- if (hasHead)
- {
- eyes.add();
- }
- }
- // add antenna?
- if (diceRoll == 4)
- {
- if (hasHead)
- {
- feelers.add();
- }
- }
- }
- @Override
- public String draw()
- {
- if (hasHead)
- {
- return feelers.draw() + " (" + eyes.draw() + ")";
- }
- return "";
- }
- @Override
- public boolean complete()
- {
- return hasHead && eyes.complete() && feelers.complete();
- }
- }
- /**
- * Legs of a Cootie
- */
- public class Legs
- {
- private int numLegs;
- public static final int kMaxLegs = 6;
- /**
- * Constructor for objects of class Tail
- */
- public Legs()
- {
- numLegs = 0;
- }
- /**
- * Determine if a part can be added.
- */
- public void add()
- {
- if (numLegs < kMaxLegs)
- {
- numLegs += 1;
- }
- }
- /** Accessor to number of legs
- * @return current number of legs
- */
- public int count()
- {
- return numLegs;
- }
- /**
- * Is the part completed?
- * @return true if the part has been completely assembled
- */
- public boolean complete()
- {
- return numLegs >= kMaxLegs;
- }
- }
- /**
- * Tail of a cootie.
- */
- public class Tail implements BodyPart
- {
- private boolean hasTail;
- /**
- * Constructor for objects of class Tail
- */
- public Tail()
- {
- hasTail = false;
- }
- @Override
- public void add(int diceRoll)
- {
- if (diceRoll == 6)
- {
- hasTail = true;
- }
- }
- @Override
- public String draw()
- {
- if (hasTail)
- {
- return " T";
- }
- return "";
- }
- @Override
- public boolean complete()
- {
- return hasTail;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement