Skip to content

Instantly share code, notes, and snippets.

@ansisec
Last active April 3, 2024 20:32
Show Gist options
  • Save ansisec/b6e7b896dca97d1c2f64808e340e4d1b to your computer and use it in GitHub Desktop.
Save ansisec/b6e7b896dca97d1c2f64808e340e4d1b to your computer and use it in GitHub Desktop.
Aula "Mosquito" - Ficheiros Auxiliares
Ficheiros Auxiliares
public class MosquitoData {
private enum LandingZone {NONE, ANIMAL, OBJECT}
private int numberMoves = 0;
private int numberCrushAttempts = 0;
private int numberBites = 0;
private LandingZone landedOn = LandingZone.NONE;
private boolean isDead = false;
public int getNumberMoves() {
return numberMoves;
}
public int getNumberCrushAttempts() {
return numberCrushAttempts;
}
public int getNumberBites() {
return numberBites;
}
public boolean isOnAnimal() {
return landedOn == LandingZone.ANIMAL;
}
public boolean isOnObject() {
return landedOn == LandingZone.OBJECT;
}
public boolean isDead() {
return isDead;
}
public boolean tryToCrush() {
numberCrushAttempts++;
if (isDead)
return true;
isDead = Math.random()>=0.5;
return isDead;
}
public void move() {
if (isDead)
return;
numberMoves++;
double r = Math.random();
if (r <= .2)
landedOn=LandingZone.ANIMAL;
else if (r <= .5)
landedOn=LandingZone.OBJECT;
else
landedOn=LandingZone.NONE;
}
public void bite() {
if (isOnAnimal() && !isDead)
numberBites++;
}
public boolean takeOff() {
if (isDead)
return false;
landedOn=LandingZone.NONE;
return true;
}
}
public class MosquitoUI {
MosquitoContext mosquito;
public MosquitoUI(MosquitoContext mosquito) {
this.mosquito = mosquito;
}
public void start() {
while (switch (mosquito.getState()) {
case IN_FLIGHT -> inFlightUI();
case DEAD -> deadUI();
case READY_TO_BITE -> readyToBiteUI();
case LANDED -> landedUI();
}) {
System.out.println();
System.out.printf("Number of moves: %d\n", mosquito.getNumberMoves());
System.out.printf("Number Crush Attempts: %d\n", mosquito.getNumberCrushAttempts());
System.out.printf("Number of bites: %d\n", mosquito.getNumberBites());
System.out.println();
}
}
private boolean inFlightUI() {
System.out.println("***** In flight *****\n");
switch(PAInput.chooseOption("Actions:", "Move","Try to crush","Exit")) {
case 1 -> mosquito.move();
case 2 -> mosquito.crush();
default -> {
return false;
}
}
return true;
}
private boolean deadUI() {
System.out.println("***** Dead *****\n");
System.out.println("Press Enter to finish... ");
try {
System.in.read();
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
private boolean readyToBiteUI() {
System.out.println("***** Ready To Bite *****\n");
switch(PAInput.chooseOption("Actions:", "Take off","Try to crush","Bite","Exit")) {
case 1 -> mosquito.takeOff();
case 2 -> mosquito.crush();
case 3 -> mosquito.bite();
default -> {
return false;
}
}
return true;
}
private boolean landedUI() {
System.out.println("***** Landed *****\n");
switch(PAInput.chooseOption("Actions:", "Take off","Try to crush","Exit")) {
case 1 -> mosquito.takeOff();
case 2 -> mosquito.crush();
default -> {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment