Last active
June 14, 2023 14:29
Tamagotchi - reddit dailyprogramming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class AllInOne { | |
static AllInOne instance = new AllInOne(); | |
static final int GAME_SPEED = 5; //The lower the faster. | |
static final int MONSTER_MEDIUM_AGE = 70; | |
public static void main(String[] args) { | |
//Getting some utilities.. | |
Scanner scanner = new Scanner(System.in); | |
Random random = new Random(); | |
Timer timer = new Timer(); | |
Monster monster = instance.setupGame(scanner, timer); | |
while(monster.alive){ | |
System.out.println("Insert operation : (1) EAT (2) SLEEP"); | |
int operation = scanner.nextInt(); | |
synchronized(monster){ | |
if(operation == 1) monster.hunger -= random.nextInt(25); | |
else if(operation == 2) monster.sleepiness -= random.nextInt(25); | |
else System.out.println("I don't know this operation"); | |
} | |
} | |
scanner.close(); | |
timer.cancel(); | |
} | |
public Monster setupGame(Scanner scanner, Timer timer){ | |
System.out.println("Insert monster name"); | |
String name = scanner.next(); | |
Monster monster = new Monster(name); | |
StatsUpdater updater = new StatsUpdater(monster); | |
timer.scheduleAtFixedRate(updater, 0, 1000 * GAME_SPEED); | |
return monster; | |
} | |
class Monster{ | |
public String name; | |
public int hunger , age , sleepiness; | |
public boolean alive; | |
public Monster(String name) { | |
this.name = name; | |
hunger = sleepiness = 50; | |
sleepiness = 90; | |
age = 0; | |
alive = true; | |
} | |
public void printStats(){ | |
System.out.println("=========================================================================================================="); | |
System.out.println("Mosnter NAME : " + name + " || Age : " + age + " || Hunger: " + hunger + " || Sleepiness : " + sleepiness); | |
if(alive == true) System.out.println(":) ALIVE :)"); | |
else System.out.println("X_X Dead - RIP X_X"); | |
System.out.println("=========================================================================================================="); | |
} | |
} | |
static class StatsUpdater extends TimerTask{ | |
Monster theMonster; | |
int nCounts; | |
public StatsUpdater(Monster monster){ | |
this.theMonster = monster; | |
} | |
@Override | |
public void run() { | |
if(theMonster.alive == true){ | |
synchronized(theMonster){ | |
//Aging once every three calls. | |
if(nCounts++ == 3){ theMonster.age++; nCounts = 0;} | |
//Random death based on the age. | |
if(Math.random() * theMonster.age > MONSTER_MEDIUM_AGE) theMonster.alive = false; | |
//Randomizing stats decrease. | |
Random r = new Random(); | |
theMonster.hunger += r.nextInt(5) + 1; | |
theMonster.sleepiness += r.nextInt(5) + 1; | |
if(theMonster.sleepiness >= 100 ){ | |
try{ | |
System.out.println("Your monster is exhausted!!! Now he has to sleep a lot to recover!"); | |
Thread.sleep(10000); | |
theMonster.hunger += r.nextInt(10) +10; | |
theMonster.sleepiness = 50; | |
} catch(InterruptedException e){ | |
e.printStackTrace(); | |
} | |
} | |
if(theMonster.hunger >= 100) theMonster.alive = false; | |
} | |
} | |
theMonster.printStats(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment