commit 204f0548cdd89164a3bf422c38530252d7b32e9d Author: Michel Joris Hombrecher <3012477@stud.hs-mannheim.de> Date: Tue Jan 9 02:11:06 2024 +0100 Dateien nach "main" hochladen diff --git a/main/GameControls.java b/main/GameControls.java new file mode 100644 index 0000000..c11e292 --- /dev/null +++ b/main/GameControls.java @@ -0,0 +1,284 @@ +package minigame; + +import java.io.BufferedReader; +import java.io.IOException; + +public class GameControls { + + private int raceCount; + private boolean hire; + private static Player player; + + public GameControls() { + this.raceCount = 0; + + } + + public String toString() { + return "Races: "+this.raceCount; + } + + public static void startInto(GameControls game, BufferedReader ter) throws InterruptedException, IOException { + String intro = "Welcome to Hoof-Hustle\n\nGet CREDITS by having your HORSE win RACES.\nBuy UPGRADES as well as new RACES with the CREDITS Earned.\n\nBut be careful.\nDont lose all your CREDITS!!!\n"; + Tools.printSlow30(intro); + while (true) { + System.out.print("\nSYSTEM | Choose a Username: "); + String uname = ter.readLine(); + if (uname.toCharArray().length > 15) { + System.out.println("\nSYSTEM | Please Choose a Shorter Username"); + } else { + player = new Player(uname); + String info1 = "\nSTRANGER | Welcome to Hoof-Hustle " + player.username + "!"; + Tools.printSlow30(info1); + break; + } + } + + startGame(game, ter); + } + + private static void startGame(GameControls game, BufferedReader ter) { + Tools.printSlow30("\nSTRANGER | Im Jones btw, I sell horses and train riders for a living! I will guide you through the first few steps of Horse Racing."); + while (true) { + Tools.printSlow30("\n\nSYSTEM | Hire Jones for free? y/n: "); + String choice; + try { + choice = String.valueOf(ter.readLine()); + if (choice.equalsIgnoreCase("y")) { + game.hire = true; + Tools.printSlow30("\nJONES | Thanks for Hiring me!\n"); + break; + } else if (choice.equalsIgnoreCase("n")){ + game.hire = false; + break; + } else { + Tools.printSlow30("\nPossible options: y/n"); + } + } catch (Exception e) { + Tools.printSlow30("\nPossible options: y/n"); + } + } + if (game.hire) { + Tools.printSlow30("JONES | Lets put your racing Skills to the test! But first we need to choose you a horse. How many credits do you have right now?\n"); + Tools.waitT(1, "s"); + Tools.printSlow30("\nSYSTEM | Check your current CREDITS with \"/p cr\"\nSYSTEM | Try it out: "); + while (true) { + try { + if (ter.readLine().equalsIgnoreCase("/p cr")) { + Tools.printSlow30("SYSTEM | "); + player.showBalance(); + break; + } else { + Tools.printSlow30("\nSYSTEM | Tipp: use \"/p cr\": "); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + Tools.printSlow30("\nJONES | With that little money you won't really be able to buy a good horse.\nJONES | I can give you a friends-deal on one of my cheaper horses though.\n\n"); + Shop.showHorses(); + } else { + Tools.printSlow30("\nJONES | No problem mate. See you around!\n\nSYSTEM | Choose a Horse from Jones Store to get started...\n\n"); + Shop.showHorses(); + } + Tools.printSlow30("\nChoose a horse with \"/buy \": "); + while (true) { + try { + String [] full_com = ter.readLine().split(" "); + String com = full_com[0]; + System.out.println(); + if (com.equalsIgnoreCase("/buy")) { + if (!(Integer.valueOf(full_com[1])>4)) { + player.buyHorse(Integer.valueOf(full_com[1])); + break; + } else { + Tools.printSlow30("SYSTEM | Use one of the IDs in range: "); + } + } else { + Tools.printSlow30("SYSTEM | Use one of the IDs in range: "); + } + + } catch (Exception e) { + Tools.printSlow30("SYSTEM | Use one of the IDs in range: "); + } + } + Tools.printSlow30("\nJONES | Excelent Choice " + player.username + ".\nJONES | Alright let's see how you do on the track!\nJONES | Your first Race will take place here in Mannheim, but if you do well you can work your way up to known tracks like those in Tokyo and New York!\n\n"); + Race.firstRace(player,ter, Player.player_horses.get(1)); + runMenu(0, ter); + } + + static void runMenu(int status, BufferedReader ter) { + if (status == 0) { + //get help + Tools.printSlow30("\n------------------------------------------------------------------------------------------------------\nSYSTEM | Welcome to the Hoof-Hustle Main Menu " +player.username+"!"); + help(); + takeInput(ter); + } else if (status == 1){ + //no help + takeInput(ter); + } + + } + + private static void takeInput(BufferedReader ter) { + Tools.printSlow30("\n\nMENU | Player Input: "); + try { + String input = ter.readLine(); + String [] full_com = input.split(" "); + String com = full_com[0]; + switch (com) { + case "/p": + //info cr name + switch (full_com[1]) { + case "info": + Tools.printSlow30("\nSYSTEM | "+player.toString()+"\n------------------------------------------------------------------------------------------------------"); + player.visLevelProgress(); + runMenu(1,ter); + break; + case "cr": + Tools.printSlow30("PLAYER | "+String.valueOf(player.getCred())); + runMenu(1,ter); + break; + case "name": + String newName; + if ((newName = player.changeName(ter))!=null) { + player.username = newName; + Tools.printSlow30("SYSTEM | "+player.toString()); + } else { + Tools.printSlow30("\nSYSTEM | Canceled \"Name change\""); + runMenu(1,ter); + } + break; + default: + Tools.printSlow30("\nSYSTEM | Unknown Command. Use \"/g help\""); + runMenu(1,ter); + break; + } + break; + case "/h": + //all show sell + switch (full_com[1]) { + case "all": + System.out.println(); + player.showHorses(); + runMenu(1,ter); + break; + case "show": + try { + player.showOneHorse(Integer.valueOf(full_com[2])); + } catch (Exception e) { + Tools.printSlow30("\nSYSTEM | That didnt seem to work. Use \"/g help\""); + } + runMenu(1,ter); + break; + case "sell": + try { + System.out.println(); + player.sellHorse(Integer.valueOf(full_com[2])); + runMenu(1,ter); + break; + } catch (Exception e) { + Tools.printSlow30("\nSYSTEM | That didnt seem to work. Use \"/g help\""); + runMenu(1,ter); + break; + } + default: + Tools.printSlow30("\nSYSTEM | Unknown Command. Use \"/g help\""); + runMenu(1,ter); + break; + } + break; + case "/s": + //open + switch (full_com[1]) { + case "open": + Shop.openShop(player, ter); + break; + default: + Tools.printSlow30("\nSYSTEM | Unknown Command. Use \"/g help\""); + runMenu(1,ter); + break; + } + break; + case "/r": + //hs next start end + switch (full_com[1]) { + case "hs": + Race.showHistory(); + runMenu(1,ter); + break; + case "next": + Race.genNextRace(player, ter); + break; + default: + Tools.printSlow30("\nSYSTEM | Unknown Command. Use \"/g help\""); + runMenu(1,ter); + break; + } + break; + case "/g": + //help quit + switch (full_com[1]) { + case "help": + help(); + runMenu(1,ter); + break; + case "quit": + Tools.printSlow30("SYSTEM | Saving Progress..."); + saveProgress(); + Tools.printSlow80("\n\n::::::::::::::::::::::::::::::::::::::::::::::::: | "); + Tools.printSlow15("Save complete!"); + Tools.waitT(1, "s"); + Tools.printSlow30("\n\nSYSTEM | Quitting the game"); + Tools.waitT(800, "m"); + Tools.printSlow800("..."); + System.exit(0); + break; + } + break; + default: + Tools.printSlow30("\nSYSTEM | Please use a command from the list above or use \"g help\""); + runMenu(0, ter); + } + } catch (Exception e) { + e.printStackTrace();; + } + + } + + public static void help() { + //Player commands + Tools.printSlow15("\n------------------------------------------------------------------------------------------------------\n\nSYSTEM | Player commands\n"); + Tools.explainCommand("p info","Displays all important information about the player"); + Tools.explainCommand("p cr","Shows your current Balance"); + Tools.explainCommand("p name","Lets you change your current Username"); + + //HorseCommands + Tools.printSlow15("\n\n------------------------------------------------------------------------------------------------------\n\nSYSTEM | Horse commands\n"); + Tools.explainCommand("h all","Shows all the horses you own"); + Tools.explainCommand("h show ","Shows the horse with that name"); + Tools.explainCommand("h sell ","Sell one of your horses for 80% Original Value"); + + //Shop commands + Tools.printSlow15("\n\n------------------------------------------------------------------------------------------------------\n\nSYSTEM | Shop commands\n"); + Tools.explainCommand("s open","Enter the shop"); + + //Race commands + Tools.printSlow15("\n\n------------------------------------------------------------------------------------------------------\n\nSYSTEM | Race commands\n"); + Tools.explainCommand("r hs","Shows your Race History"); + Tools.explainCommand("r next","Starts the next Race"); + Tools.explainCommand("r start","Starts the Race"); + Tools.explainCommand("r end","Ends the Race"); + + //Game commands + Tools.printSlow15("\n\n------------------------------------------------------------------------------------------------------\n\nSYSTEM | Game commands\n"); + Tools.explainCommand("g quit","Ends the current Game"); + Tools.explainCommand("g help","Shows the list of available commands"); + } + + + private static void saveProgress() { + //save mechanism + } + +} diff --git a/main/Horse.java b/main/Horse.java new file mode 100644 index 0000000..2e2b523 --- /dev/null +++ b/main/Horse.java @@ -0,0 +1,76 @@ +package minigame; + +import java.util.ArrayList; +import java.util.stream.IntStream; + +public class Horse { + + public int price; + public double speedx; + public int stamina; + public String name; + public String tag = "None"; + public double current_pos; + + public Horse(int price, double speed, int stamina, String name, String tag){ + this.price = price; + this.speedx = speed; + this.stamina = stamina; + this.name = name; + this.tag = tag; + this.current_pos = 0; + } + + public String toString() { + return String.format("Price: %d, Name: %s, Speed: %.1f, Stamina: %d", this.price, this.name, this.speedx * 10, this.stamina); + } + + public String toOwnedString() { + return String.format("Name: %s, Speed: %.1f, Stamina: %d", this.name, this.speedx * 10, this.stamina); + } + + public String toBotString() { + return String.format("%s, Sp: %.1f, St: %d", this.name, this.speedx * 10, this.stamina); + } + + public String toRacePosString() { + return String.format("%s, Distance: %.1f", this.name, this.current_pos); + } + + public static ArrayList genHorses(int max, int level, String tag){ + ArrayList horses = new ArrayList<>(); + IntStream.rangeClosed(1, max).forEach(x-> horses.add(new Horse((int)((Math.random()*(level*5*level))+3),(Math.random()*level/5)+0.1,(int)(Math.random()*5*(level*2)),genName(),tag))); + return horses; + } + + public double getCurrentPos() { + return current_pos; + } + + private static String genName() { + String [] names = { + "Maverick", + "Luna", + "Shadow", + "Echo", + "Willow", + "Blaze", + "Atlas", + "Harmony", + "Dakota", + "Nova", + "Titan", + "Luna", + "Diesel", + "Indie", + "Bella", + "Jasper", + "Zoe", + "Titan", + "Misty", + "Ace" + }; + int x = (int)(Math.random()*names.length); + return names[x]; + } +} diff --git a/main/Main.java b/main/Main.java new file mode 100644 index 0000000..86c1b14 --- /dev/null +++ b/main/Main.java @@ -0,0 +1,19 @@ +package minigame; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Main { + + public static void main(String[] args) throws IOException { + + BufferedReader ter = new BufferedReader(new InputStreamReader(System.in)); +// Race.readLocations("minigame/locations2.txt"); + GameControls game1 = new GameControls(); + try {GameControls.startInto(game1, ter);} + catch (Exception e) {e.printStackTrace();} + + } + +} diff --git a/main/Player.java b/main/Player.java new file mode 100644 index 0000000..2625dae --- /dev/null +++ b/main/Player.java @@ -0,0 +1,135 @@ +package minigame; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.TreeMap; + +public class Player { + + public static TreeMap player_horses = new TreeMap<>(); + private static int maxMapId = 0; + + public int credits; + public String username; + public int level; + public int lvlpg; + + public Player(String uname) { + this.username = uname; + this.credits = 50; + this.level = 1; + this.lvlpg = 0; + } + + public String toString() { + return this.username + ", Credits: " + this.credits + ", Lvl: " + this.level + ", Lvl Progress: " + this.lvlpg + "%."; + } + + public int getCred() { + return this.credits; + } + + public void showBalance() { + System.out.println("Credits: " + this.credits); + } + + public void levelUp() { + this.lvlpg-=100; + this.level+=1; + } + + public void visLevelProgress() { + System.out.println("\nSYSTEM | Level Progress:"); + String header = "0%["; + String ending = "]100%"; + String con10 = "::::"; + String pas10 = " "; + StringBuilder out1 = new StringBuilder(); + int con10am = lvlpg/10; + out1.append(header); + for (int i = 0; i 15) { + System.out.println("\nSYSTEM | Please Choose a Shorter Username"); + } else { + return uname; + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + public void buyHorse(int id) { + int price = Shop.shop_horses.get(id).price; + if ((credits-price)>=0) { + credits-=price; + System.out.println(); + Horse h = Shop.shop_horses.get(id); + h.tag = "Player"; + maxMapId++; + player_horses.put(maxMapId, h); + Shop.shop_horses.remove(id); + Tools.printSlow30("SYSTEM | Bought " + player_horses.get(maxMapId).name+". "); + System.out.println(player_horses.get(1)); + } else { + Tools.printSlow30("\nSYSTEM | You dont have enough credits for that right now!\n"); + } + + } + + public void sellHorse(int id) { + if (player_horses.size()==1) { + Tools.printSlow30("SYSTEM | You cant sell your only horse."); + } else { + try { + int price = calcSellPrice(player_horses.get(id)); + String name = player_horses.get(id).name; + player_horses.remove(id); + credits+=price; + Tools.printSlow30("SYSTEM | You sold " + name + " for " + price + " credits."); + } catch (Exception e) { + Tools.printSlow30("SYSTEM | ID: "+id+" didnt seem to work. Please try again."); + } + } + } + + private int calcSellPrice(Horse horse) { + return (int)(horse.price * 0.8); + + } + + public void showHorses() { + for (Integer key : player_horses.keySet()) { + System.out.print("PLAYER | ID: " + key + ", " + player_horses.get(key)+"\n"); + } + } + + public void showOneHorse(int id) { + try { + Tools.printSlow30("\n"+player_horses.get(id)); + } catch (Exception e) { + Tools.printSlow30("SYSTEM | Couldnt find: "+id+".\n"); + } + } + + +} diff --git a/main/Race.java b/main/Race.java new file mode 100644 index 0000000..8e1605c --- /dev/null +++ b/main/Race.java @@ -0,0 +1,229 @@ +package minigame; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.TreeMap; + +public class Race { + + private static ArrayList past_races = new ArrayList(); + public ArrayList end = new ArrayList<>(); + public ArrayList cur_horses = new ArrayList<>(); + public static TreeMap races = new TreeMap<>(); + public static int curmaxRace = 0; + private int rlen; + private String rname; + private int max_horses; + private int status; + public int placement; + public int reward; + public Horse ch_horse; + + public Race(Player player, String name) { + this.rlen = player.level * 10 + (2*player.level); + this.rname = name; + this.max_horses = Player.player_horses.values().size() * ((int)(Math.random()*4))+4; + this.cur_horses = Horse.genHorses(max_horses, player.level, "Race"); + this.status = 0; + this.reward = 0; + } + + public String toStartString() { + return "Get ready for " + this.rname + ". Race length: " + this.rlen + ". Horses: " + (this.max_horses+1); + } + + public String toString() { + return String.format("Location: %s, Length: %d, FINISH POSITION: %d, Horses: %d, Reward: %d",this.rname,this.rlen,this.placement,(this.max_horses+1),this.reward); + } + + public static void showHistory() { + System.out.println(); + past_races.forEach(race -> Tools.printSlow30("RACE | " + race)); + } + + public static void firstRace(Player player,BufferedReader ter, Horse c_horse) { + Race firstRace = new Race(player, "Mannheim"); + past_races.add(firstRace); + curmaxRace++; + races.put(curmaxRace,firstRace); + firstRace.getParts(player, 1); + Tools.printSlow30("\nSYSTEM | Type \"/r start\" to start the Race: "); + while(true) { + try { + String input = ter.readLine(); + String [] full_com = input.split(" "); + String com = full_com[0]; + if (com.equalsIgnoreCase("/r")) { + if (full_com[1].equalsIgnoreCase("start")) { + break; + } else { + Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: "); + } + } else { + Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: "); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + firstRace.doRace(player, c_horse); + firstRace.finishRace(player, c_horse, ter); + } + + public static void genNextRace(Player player, BufferedReader ter) throws IOException { + Tools.printSlow30("\n"+Tools.lineGap()+"\n"); + curmaxRace++; + races.put(curmaxRace, new Race(player,getNextName())); + Tools.printSlow30("INFO | Prepare for the Race and choose your horse!\n"); + Tools.printSlow30(Tools.lineGap()+"\n"); + Tools.waitT(1, "s"); + while (true) { + Tools.printSlow30("\nSYSTEM | Choose a horses with \"/c \"\n\n"); + player.showHorses(); + Tools.printSlow30("\nSYSTEM | Player Input:"); + String input = ter.readLine(); + String [] parts = input.split(" "); + String com = parts[0]; + if (com.equalsIgnoreCase("/c")) { + try { + if (Player.player_horses.containsKey(Integer.valueOf(parts[1]))) { + Tools.printSlow30("\n\n"+Tools.lineGap()+"\n\n"); + races.get(curmaxRace).getParts(player, Integer.valueOf(parts[1])); + Tools.printSlow30("\nSYSTEM | Type \"/r start\" to start the Race: "); + while(true) { + try { + String input1 = ter.readLine(); + String [] full_com = input1.split(" "); + String com1 = full_com[0]; + if (com1.equalsIgnoreCase("/r")) { + if (full_com[1].equalsIgnoreCase("start")) { + break; + } else { + Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: "); + } + } else { + Tools.printSlow30("\nSYSTEM | Use \"/r start\" to start the Race: "); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + races.get(curmaxRace).doRace(player, Player.player_horses.get(Integer.valueOf(parts[1]))); + races.get(curmaxRace).finishRace(player,Player.player_horses.get(Integer.valueOf(parts[1])) ,ter); + break; + } else { + Tools.printSlow30("\nSYSTEM | Couldnt find a Horse with the ID: "+parts[1]+"\n\n------------------------------------------------------------------------------------------------------\n"); + } + } catch (Exception e) { + Tools.printSlow30("\nChoose one of your horses with \"/c \"\n"); + } + } else { + Tools.printSlow30("\nChoose one of your horses with \"/c \"\n"); + } + } + } + + private void finishRace(Player player, Horse c_horse, BufferedReader ter) { + try (BufferedReader ter2 = new BufferedReader(new InputStreamReader(System.in))){ + c_horse.current_pos = 0; //Resest for next Race + System.out.println(Tools.lineGap()+"\n\nRACE | Rew: "+this.reward+" crd. Pos: "+this.placement+""); + player.visLevelProgress(); + while (true) { + Tools.printSlow30("\n\nSYSTEM | Type \"/r end\" to leave the end screen: "); + String input = ter2.readLine(); + if (input.equalsIgnoreCase("/r end")) { + Tools.printSlow30("SYSTEM | Updating Data"); + Shop.updateShop(player); + Tools.printSlow800("..."); + break; + } + } + GameControls.runMenu(0, ter); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String getNextName() { + return String.format("Race %d",curmaxRace); + } + + public void getParts(Player player, int c_horse) { + cur_horses.add(Player.player_horses.get(c_horse)); + Tools.printSlow30("INFO | Contestants:\n\n"); + cur_horses.forEach(horse -> System.out.println((horse.tag.equalsIgnoreCase("Race") ? "RACE | " : player.username + " | ") + horse.toBotString())); + } + + private void doRace(Player player, Horse c_horse) { + Tools.printSlow30("\n"+toStartString()+"\n\n"); + Tools.waitT(1, "s"); + int cyc_count = 0; + while(status==0) { + cyc_count++; + doCycle(player, cyc_count); + Tools.waitT(1000, "m"); + } + } + + private void doCycle(Player player, int cyc_count) { + System.out.println("---------------------------------------------------\nRACE | Time from start: "+cyc_count+"\n"); + Collections.sort(cur_horses, Comparator.comparingDouble(Horse::getCurrentPos).reversed()); + for (Horse horse : cur_horses) { + if (end.contains(horse)) { + continue; + } else { + horse.current_pos += calcStep(horse, cyc_count); + if (horse.current_pos >= this.rlen) { + horse.current_pos = this.rlen; + System.out.println("FINISH | "+(horse.tag.equalsIgnoreCase("Race") ? "BOT | " : player.username + " | ") + horse.toRacePosString()); + end.add(horse); + if (horse.tag.equalsIgnoreCase("Player")) { + status = 1; + this.placement = end.size(); + calcRaceRewards(horse, player); + System.out.println(); + break; // exit the loop when status is 1 + } + } else { + System.out.println((cur_horses.indexOf(horse)+1)+" | " + (horse.tag.equalsIgnoreCase("Race") ? "OPP | " : player.username + " | ") + horse.toRacePosString()); + } + } + } + } + + private double calcStep(Horse horse,int cyc_count) { + double speed = (((Math.random()*10)+5)*horse.speedx); + double bonus = horse.stamina/10 - cyc_count/10; + return (bonus>=0) ? speed + bonus : speed; + } + + private void calcRaceRewards(Horse horse, Player player) { + int reward = (int) (player.level*(Math.random()+2))+((10/this.placement)*3); + this.reward = reward; + player.credits += reward; + player.lvlpg+=(100/this.placement)-(player.level<90 ? player.level : 0); + if (player.lvlpg >= 100) { + player.levelUp(); + } + } + +// public static void readLocations(String filename) throws IOException { +// try (BufferedReader file1 = new BufferedReader(new FileReader(filename))) { +// String line; +// while ((line = file1.readLine()) != null) { +// raceId++; +// races.put(raceId, line); +// } races.values().forEach(x -> System.out.println(x)); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// +// } + +}