Created
June 9, 2014 22:37
-
-
Save zhantongz/f12911065d080be0fb29 to your computer and use it in GitHub Desktop.
Computer Science 30 Final Project
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
public class Building { | |
String name; | |
int cost; | |
int monthlyCost; | |
int price; | |
double attraction; | |
int area; | |
} |
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
public class Employee { | |
String position; | |
int salary; | |
double attraction; | |
double accidentRate; | |
Park park; | |
// regular employee | |
final static String[] positions = { "Cleaner", "Engineer", "Clerk", | |
"Lawyer", "EMT" }; | |
final static int[] salaries = { 700, 1200, 700, 1550, 1250 }; | |
final static double[] attractions = { 0.005, 0, 0.001, 0, 0.001 }; | |
static double[] accidentRates = { 0, -0.1, 0, 0.05, -0.01 }; | |
public Employee(String position, int salary, double attraction, | |
double accidentRate) { | |
this.position = position; | |
this.salary = salary; | |
this.attraction = attraction; | |
this.accidentRate = accidentRate; | |
} | |
public static Employee hire(int number) { | |
return new Employee(positions[number - 1], salaries[number - 1], | |
attractions[number - 1], accidentRates[number - 1]); | |
} | |
} |
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.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.StreamCorruptedException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Random; | |
import java.util.Scanner; | |
import java.util.StringTokenizer; | |
// | |
/** | |
* Main game instance | |
* | |
* @author Zhang, Zhantong | |
* @author Zhou, Jade | |
*/ | |
public class Main { | |
public static Scanner input = new Scanner(System.in); | |
public static Random rnd = new Random( | |
(long) (Math.random() * Math.random() * 10000000000L)); | |
/** game calendar */ | |
static Calendar gameCal = Calendar.getInstance(); | |
public static int money = 0; | |
static Park park; | |
public static final String[] weathers = { "sunny", "cloudy", "stormy", | |
"windy", "rainy", "snowy" }; | |
static String name; | |
static String petName; | |
static double accidentRate; | |
/** | |
* return true at a chance of one in a specified number | |
* | |
* @param divisor | |
* the specified number | |
* @return true if a random number in [1, divisor] is equal to divisor; | |
* false otherwise | |
*/ | |
public static boolean takeChance(int divisor) { | |
return randomNum(1, divisor) == divisor; | |
} | |
/** | |
* return true at a specified chance (a double less than 1) | |
* | |
* @param chance | |
* the specified chance | |
* @return true if a random number in [1, 1000] is less than or equal to | |
* 1000 times the chance | |
*/ | |
public static boolean takeChance(double chance) { | |
return 1000 * chance >= randomNum(1, 1000); | |
} | |
/** | |
* Show a string in typewriter style | |
* | |
* @param message | |
* the string need to be displayed | |
*/ | |
public static void typeString(String message) throws InterruptedException { | |
char[] mes = message.toCharArray(); | |
for (int i = 0; i < mes.length; i++) { | |
System.out.print(mes[i]); | |
if (!(mes[i] == ' ') || (i < mes.length - 1 && mes[i + 1] == ' ')) | |
Thread.sleep(50); | |
} | |
System.out.println(); | |
} | |
/** | |
* Show a string in typewriter style | |
* | |
* @param speed | |
* the time between each character in ms | |
* @param message | |
* the string need to be displayed | |
*/ | |
public static void typeString(String message, int speed) | |
throws InterruptedException { | |
char[] mes = message.toCharArray(); | |
for (int i = 0; i < mes.length; i++) { | |
System.out.print(mes[i]); | |
if (!(mes[i] == ' ') || (i < mes.length - 1 && mes[i + 1] == ' ')) | |
Thread.sleep(speed); | |
} | |
System.out.println(); | |
} | |
/** | |
* Break lines in a long string with maximum length of 80 | |
* | |
* @param input | |
* the string need to be broken | |
* @return the string with proper breaks | |
*/ | |
public static String addLinebreaks(String input) { | |
return addLinebreaks(input, 80); | |
} | |
public static void displayBuildings(Building[] builds) { | |
for (int i = 0; i < builds.length; i++) { | |
if (builds[i].name.toCharArray().length > 12) | |
System.out.println((i + 1) + ". " + builds[i].name + "\t" | |
+ builds[i].cost + "\t" + builds[i].area); | |
else | |
System.out.println((i + 1) + ". " + builds[i].name + "\t\t" | |
+ builds[i].cost + "\t" + builds[i].area); | |
} | |
} | |
/** | |
* Generate a random number in a certain range | |
* | |
* @param min | |
* the minimum for the number, inclusive | |
* @param max | |
* the maximum for the number, inclusive | |
* @return the generated number | |
*/ | |
public static int randomNum(int min, int max) { | |
// ensure the max and min are correct | |
if (max < min) { | |
int temp = min; | |
min = max; | |
max = temp; | |
} | |
return (int) (rnd.nextInt(max - min + 1) + min); | |
} | |
/** | |
* Break lines in a long string to make display nicer | |
* | |
* ref: http://stackoverflow.com/questions/7528045/large-string-split-into- | |
* lines-with-maximum-length-in-java | |
* | |
* @param input | |
* the string need to be broken | |
* @param maxLineLength | |
* the maxium line length to insert break line | |
* @return the string with proper breaks | |
*/ | |
public static String addLinebreaks(String input, int maxLineLength) { | |
StringTokenizer tok = new StringTokenizer(input, " "); | |
StringBuilder output = new StringBuilder(input.length()); | |
int lineLen = 0; | |
while (tok.hasMoreTokens()) { | |
String word = tok.nextToken() + " "; | |
if (lineLen + word.length() > maxLineLength) { | |
output.append("\n"); | |
lineLen = 0; | |
} | |
output.append(word); | |
lineLen += word.length(); | |
} | |
return output.toString(); | |
} | |
/** | |
* Display the date today (as in the game) | |
* | |
* @return the current date in the game | |
*/ | |
public static String displayDate() { | |
return new SimpleDateFormat("MMMMM d, yyyy").format(gameCal.getTime()); | |
} | |
public static String displayWeather(int num) { | |
String weatherS = weathers[num]; | |
return weatherS.substring(0, 1).toUpperCase() + weatherS.substring(1); | |
} | |
/** | |
* Get the user's input with space as the delimiter | |
* | |
* @param prompt | |
* prompt for the user | |
* @return the string user entered | |
*/ | |
public static String input(String prompt) throws IOException { | |
System.out.println(prompt); | |
System.out.print(">> "); | |
String data = input.next(); | |
input.nextLine(); | |
return data; | |
} | |
/** | |
* Get the user's input with line break as the delimiter | |
* | |
* @param prompt | |
* prompt for the user | |
* @return the string user entered | |
*/ | |
public static String inputLn(String prompt) { | |
System.out.println(prompt); | |
System.out.print(">> "); | |
return input.nextLine(); | |
} | |
/** | |
* Check if the user's input is valid and return a valid data | |
* | |
* @param prompt | |
* prompt for the user | |
* @param min | |
* the minimum value for a valid data | |
* @param max | |
* the maximum value for a valid data | |
* @return a valid integer data between min and max | |
*/ | |
public static int input(String prompt, int min, int max) { | |
// ensure the max and min are correct | |
if (max < min) { | |
int temp = min; | |
min = max; | |
max = temp; | |
} | |
boolean passed; | |
int data = min - 1; | |
do { | |
System.out.println(prompt); | |
System.out.print(">> "); | |
passed = true; | |
try { | |
data = input.nextInt(); | |
if (data < min || data > max) { | |
passed = false; | |
System.out.println("Invalid range, enter a number between " | |
+ min + " and " + max + "."); | |
} | |
} catch (Exception e) { | |
passed = false; | |
System.out.println("Invalid type, enter a number between " | |
+ min + " and " + max + "."); | |
input.next(); | |
} | |
} while (!passed); | |
input.nextLine(); | |
return data; | |
} | |
/** | |
* Add 1 day to the calendar | |
* | |
* @throws InterruptedException | |
*/ | |
public static void passDay() throws InterruptedException { | |
typeString("Zzzz..."); | |
gameCal.add(Calendar.DATE, 1); | |
} | |
/** | |
* Load a serialized file | |
* | |
* @param prompt | |
* prompt for the user | |
* @return the object included in the file | |
*/ | |
public static Object loadFile(String prompt) throws IOException { | |
boolean errorExists = false; | |
Object object = null; | |
do { | |
errorExists = false; | |
try { | |
ObjectInputStream inobj = new ObjectInputStream( | |
new FileInputStream(input(prompt))); | |
object = inobj.readObject(); | |
inobj.close(); | |
} catch (FileNotFoundException e) { | |
System.out.println("Error. The file doesn't exist."); | |
errorExists = true; | |
} catch (ClassNotFoundException e) { | |
System.out.println("Error. File type may be wrong."); | |
errorExists = true; | |
} catch (StreamCorruptedException e) { | |
System.out.println("Error. File type may be wrong."); | |
errorExists = true; | |
} catch (NullPointerException e) { | |
errorExists = true; | |
} | |
} while (errorExists); | |
return object; | |
} | |
public static void clear() throws IOException { | |
Runtime.getRuntime().exec("clear"); | |
/* | |
* char c = '\n'; int length = 128; char[] chars = new char[length]; | |
* Arrays.fill(chars, c); System.out.print(String.valueOf(chars)); | |
*/ | |
} | |
// check if an object is in an array | |
public static boolean inArray(Object obj, Object[] arr) { | |
for (int i = 0; i < arr.length; i++) | |
if (obj == arr[i]) | |
return true; | |
return false; | |
} | |
// add item to an partially-empty object array | |
public static void attachArray(Object obj, Object[] arr) { | |
Object cur = arr[0]; | |
int index = 0; | |
while (cur != null) { | |
index++; | |
cur = arr[index]; | |
} | |
arr[index] = obj; | |
} | |
public static void main(String[] args) throws InterruptedException, | |
IOException { | |
System.out | |
.print(" ___ _ ______ _\n"); | |
System.out | |
.print(" / _ \\ | | | ___ \\ | |\n"); | |
System.out | |
.print("/ /_\\ \\_ __ ___ _ _ ___ ___ _ __ ___ ___ _ __ | |_ | |_/ /_ _ _ __| | __\n"); | |
System.out | |
.print("| _ | '_ ` _ \\| | | / __|/ _ \\ '_ ` _ \\ / _ \\ '_ \\| __| | __/ _` | '__| |/ /\n"); | |
System.out | |
.print("| | | | | | | | | |_| \\__ \\ __/ | | | | | __/ | | | |_ | | | (_| | | | < \n"); | |
System.out | |
.print("\\_| |_/_| |_| |_|\\__,_|___/\\___|_| |_| |_|\\___|_| |_|\\__| \\_| \\__,_|_| |_|\\_\\\n"); | |
System.out.print("\n\n\n"); // the space between ASCII art and the | |
// message | |
System.out.println("\t\t\t" + "================================="); // [TAB] | |
// for | |
// centre | |
System.out.println("\t\t\t" + "Welcome to Play Amusement Tycoon!"); // [TAB] | |
// for | |
// centre | |
System.out.println("\t\t\t" + "================================="); // [TAB] | |
// for | |
// centre | |
System.out.println("1. Instructions" + '\n' + "2. Play the Game" + '\n' | |
+ "0. Exit"); // mainMenu | |
int choice = input("", 0, 2); | |
switch (choice) { | |
// instructions | |
case 1: | |
typeString(addLinebreaks("Your mission is to make enough money to build a rocket for your alien pet to go home.")); | |
int endIns = input("Enter 1 to start game; 2 to end the game", 1, 2); | |
if (endIns == 2) | |
System.exit(0); | |
// play game | |
case 2: { | |
// initialization | |
name = input("Please enter your name: "); | |
petName = input("Please enter your pet's name: "); | |
System.out | |
.println("=================================================="); | |
System.out.println("LOCATION\t\t\tAREA\tMAX AREA"); | |
System.out.println("1. Countryside\t\t\t120\t175"); | |
System.out.println("2. Downtown\t\t\t90\t120"); | |
System.out.println("3. Prince Esperplena Island\t100\t150"); | |
System.out.println("4. Cropper Mills Mall\t\t100\t150"); | |
System.out | |
.println("=================================================="); | |
int place = input("Choose your location: ", 1, 4); | |
park = Park.place(place); | |
int parkchoice = 0; | |
money = randomNum(7500, 10000); | |
// rides, employees and services | |
Ride[] rides = new Ride[Ride.names.length]; | |
Employee[] employees = new Employee[Employee.positions.length]; | |
Service[] services = new Service[Service.names.length]; | |
for (int i = 0; i < rides.length; i++) { | |
rides[i] = Ride.build(i + 1); | |
} | |
for (int i = 0; i < employees.length; i++) { | |
employees[i] = Employee.hire(i + 1); | |
} | |
for (int i = 0; i < services.length; i++) { | |
services[i] = Service.addService(i + 1); | |
} | |
// game loop | |
do { | |
// determine the weather | |
// in 100 days, 80 are sunny, 8 are cloudy, 2 are stormy | |
// 3 are windy, 5 are rainy and 2 are snowy | |
int weather; | |
if (takeChance(0.8)) | |
weather = 0; | |
else if (takeChance(0.4)) | |
weather = 1; | |
else if (takeChance(6)) | |
weather = 2; | |
else if (takeChance(0.3)) | |
weather = 3; | |
else if (takeChance((double) 5 / 7)) | |
weather = 4; | |
else | |
weather = 5; | |
// interface | |
System.out | |
.println("=================================================="); | |
System.out.println(displayDate() + "\t\t Money: " + money); | |
System.out.println("Weather: " + displayWeather(weather)); | |
System.out.println("1. Open amusement park\n" | |
+ "2. Build rides\n" + "3. Hire employees\n" | |
+ "4. Map\n" + "5. Adjust park\n" + "6. Build rocket\n" | |
+ "7. Add services\n" + "8. Pass the day\n" | |
+ "0. Exit game"); | |
System.out | |
.println("=================================================="); | |
parkchoice = input("Enter your choice", 0, 8); | |
switch (parkchoice) { | |
case 1: { | |
int openChoice = 0; | |
double cost = 1000; // basic cost | |
if (weather != 0) | |
openChoice = input( | |
"Today is " | |
+ displayWeather(weather) | |
+ ", do you want to open the park? (1 for yes, 2 for no)", | |
1, 2); | |
else | |
openChoice = 1; | |
if (openChoice == 1) { | |
double attraction = 1 + (double) randomNum(1, 50) / 1000; | |
accidentRate = (double) randomNum(1, 20) / 1000; | |
int customers = 0; | |
switch (weather) { | |
case 0: | |
customers = randomNum(300, 400); | |
break; | |
case 1: | |
customers = randomNum(200, 300); | |
break; | |
case 2: | |
customers = randomNum(0, 10); | |
break; | |
case 3: | |
customers = randomNum(150, 200); | |
break; | |
case 4: | |
customers = randomNum(50, 150); | |
break; | |
case 5: | |
customers = randomNum(10, 30); | |
} | |
// + rides & services effect | |
if (park.map.count != 0) { | |
Node cur = park.map.first; | |
Building[] added = new Building[park.map.count]; | |
int curIndex = 0; | |
while (cur != null) { | |
if (!inArray(cur.building, added) | |
&& cur.building != null) { | |
added[curIndex] = cur.building; | |
attraction += added[curIndex].attraction; | |
cost += (double) added[curIndex].monthlyCost / 30; | |
curIndex++; | |
} | |
cur = cur.next; | |
} | |
} else { | |
customers = randomNum(0, 15); | |
} | |
// + employee effect | |
{ | |
for (int i = 0; i < park.employees.size(); i++) { | |
attraction += park.employees.get(i).attraction; | |
accidentRate += park.employees.get(i).accidentRate; | |
cost += (double) park.employees.get(i).salary / 30; | |
} | |
} | |
// + accident effect | |
attraction -= accidentRate; | |
// + park effect | |
attraction += park.attraction; | |
// extreme situations | |
if (park.ticket > (double) park.map.area / 3) | |
attraction = 0; | |
else if (park.ticket > (double) park.map.area / 4) | |
attraction -= 0.75; | |
customers *= attraction; | |
int revenue = customers * park.ticket; | |
int income = (int) (revenue - cost); | |
money += income; | |
// amusement park image | |
typeString( | |
" o", | |
10); | |
typeString( | |
" o |", | |
15); | |
typeString( | |
" |", | |
10); | |
typeString( | |
" . . ._._. _ .===.", | |
5); | |
typeString( | |
" |` |` ..'\\ /`.. |H| .--. .:' `:.", | |
20); | |
typeString( | |
" //\\-...-/|\\ |- o -| |H|`. /||||\\ || ||", | |
50); | |
typeString( | |
" ._.'//////,'|||`._. '`./|\\.'` |\\\\||:. .'||||||`. `:. .:'", | |
30); | |
typeString( | |
" ||||||||||||[ ]|||| /_T_\\ |:`:.--'||||||||||`--..`=:='... ", | |
20); | |
System.out | |
.println("--------------------------------------------------"); | |
// output | |
System.out.println("CUSTOMERS: " + customers); | |
System.out.println("REVENUE: " + revenue); | |
System.out.println("COST: " + cost); | |
// accident happening | |
if (takeChance(accidentRate)) { | |
String text = ""; | |
int moneyLost = 0; | |
if (takeChance(5)) { | |
text = "You are fined by the police for safety reason."; | |
boolean lawyerUp = false; | |
for (int i = 0; i < park.employees.size(); i++) { | |
if (park.employees.get(i).position | |
.equals("Lawyer")) | |
lawyerUp = true; | |
} | |
if (!lawyerUp) { | |
moneyLost = randomNum(500, 1000); | |
text += " You don't have any lawyer, your appeal is dismissed."; | |
} else { | |
moneyLost = randomNum(0, 100); | |
if (moneyLost == 0) | |
text += " Your lawyer appeals to the court, the fine is dismissed."; | |
else | |
text += " Your lawyer appeals to the court, the fine is decreased."; | |
} | |
} else if (takeChance(3)) { | |
text = "Your park is vandalized by some stupid teenagers."; | |
moneyLost = randomNum(100, 150); | |
} else if (takeChance(0.2)) { | |
text = "You forget to lock a safe in the office and a thief entered into your office."; | |
moneyLost = randomNum(600, 750); | |
} else { | |
text = "A ride stopped working when people were on it. " | |
+ randomNum(0, 3) | |
+ " are killed and " | |
+ randomNum(5, 15) | |
+ " are injured. Victims and their families filed a class suit."; | |
moneyLost = randomNum(1500, 2500); | |
} | |
// display accident info | |
System.out.println("Ooops!"); | |
typeString(addLinebreaks(text)); | |
typeString(addLinebreaks("You lost $" | |
+ moneyLost | |
+ ". You should consider employing special personnels to help you avoid accidents.")); | |
} | |
System.out.println("TOTAL INCOME TODAY: " + income); | |
System.out.println("MONEY: " + money); | |
input("Enter any CHARACTER to continue."); | |
passDay(); | |
} else | |
passDay(); | |
} | |
break; | |
case 2: { | |
// list all rides | |
System.out | |
.println("=================================================="); | |
System.out.println("RIDE\t\t\tCOST\tAREA"); | |
displayBuildings(rides); | |
System.out | |
.println("=================================================="); | |
// save default order | |
Ride[] oriR = rides.clone(); | |
int rChoice = 0; | |
do { | |
System.out | |
.println("1. Build a ride\n" | |
+ "2. List the rides by its cost (cheapest to dearest)\n" | |
+ "3. List the rides by its area (smallest to largest)\n" | |
+ "0. Return"); | |
rChoice = input("What do you want to do?", 0, 3); | |
if (rChoice == 1) { | |
int rideNum = input( | |
"Choose the ride you want to build: ", 1, | |
rides.length); | |
park.map.push(rides[rideNum - 1]); | |
money -= services[rideNum - 1].cost; | |
} else if (rChoice == 2) { // sorting for cost | |
SearchSortEngine.qckSortCost(rides, 0, | |
rides.length - 1); | |
System.out | |
.println("=================================================="); | |
System.out.println("RIDE\t\t\tCOST\tAREA"); | |
displayBuildings(rides); | |
System.out | |
.println("=================================================="); | |
} else if (rChoice == 3) { // sorting for area | |
SearchSortEngine.qckSortArea(rides, 0, | |
rides.length - 1); | |
System.out | |
.println("=================================================="); | |
System.out.println("RIDE\t\t\tCOST\tAREA"); | |
displayBuildings(rides); | |
System.out | |
.println("=================================================="); | |
} | |
} while (rChoice != 1 && rChoice != 0); | |
rides = oriR; | |
} | |
break; | |
case 3: { | |
// list all employees | |
System.out | |
.println("=================================================="); | |
System.out.println("Employee \t\t Salary"); | |
for (int x = 0; x < Employee.positions.length; x++) { | |
if (x == 4) | |
System.out.println((x + 1) + ". " | |
+ Employee.positions[x] + "\t\t\t" | |
+ Employee.salaries[x]); | |
else | |
System.out.println((x + 1) + ". " | |
+ Employee.positions[x] + "\t\t" | |
+ Employee.salaries[x]); | |
} | |
System.out | |
.println("=================================================="); | |
int eChoice = input( | |
"HIRE employee number (or press 0 to exit): ", 0, | |
Employee.positions.length); | |
if (eChoice != 0) { | |
Employee emp = Employee.hire(eChoice); | |
park.employees.add(emp); | |
} | |
} | |
break; | |
case 4: | |
// print map | |
System.out.println("O means no building is at that spot;\n" | |
+ "* means there is a building at that spot."); | |
park.map.display(); | |
input("ENTER 0 TO RETURN: "); | |
break; | |
case 5: { | |
int landPrice = randomNum(25, 100); | |
System.out.println("Current Status"); | |
System.out | |
.println("=================================================="); | |
System.out.println("Location: " + park.location); | |
System.out.println("Ticket: $" + park.ticket); | |
System.out.println("Area: " + park.map.area | |
+ " cubic unit(s)"); | |
System.out.println("Max Area: " + park.maxArea | |
+ " cubic units"); | |
System.out.println("Land Price: $" + landPrice | |
+ " per cubic unit"); | |
System.out | |
.println("=================================================="); | |
System.out.println("1. Adjust ticket price\n" | |
+ "2. Expand the park\n" + "3. Sell land'n" | |
+ "0. Return"); | |
int pChoice = input("Choose your action: ", 0, 3); | |
switch (pChoice) { | |
case 1: | |
System.out.println("Current price is $" + park.ticket | |
+ "."); | |
int newP = input("Enter the new price: ", 0, | |
Integer.MAX_VALUE); | |
park.ticket = newP; | |
case 2: { | |
System.out.println("Current land price is $" | |
+ landPrice + " per cubic unit."); | |
int newA = input("Enter the area: ", 0, | |
Integer.MAX_VALUE); | |
if (newA + park.map.area > park.maxArea) { | |
System.out | |
.println("Sorry, the land resource is limited. You can't expand the park to this extent."); | |
} else { | |
System.out.println("You need $" + landPrice * newA | |
+ "."); | |
int sure = input( | |
"Are you sure? (1 for yes; 0 for no)", 0, 1); | |
if (sure == 1) { | |
if (money - landPrice * newA < 0) | |
System.out | |
.println("You don't have enough money."); | |
else { | |
money -= landPrice * newA; | |
park.map.push(newA); | |
} | |
} | |
} | |
} | |
break; | |
case 3: | |
System.out.println("Current land price is $" | |
+ landPrice + " per cubic unit."); | |
int newA = input("Enter the area you want to sell: ", | |
1, Integer.MAX_VALUE); | |
if (newA + park.map.area > park.maxArea) { | |
System.out | |
.println("Sorry, the land resource is limited. You can't expand the park to this extent."); | |
} else { | |
System.out.println("You will lose " + newA | |
+ " cubic unit(s)."); | |
int sure = input( | |
"Are you sure? (1 for yes; 0 for no)", 0, 1); | |
if (sure == 1) { | |
if (park.map.last.building != null) { | |
System.out | |
.println("The portion you want to sell has building."); | |
int sure2 = input( | |
"do you want to remove them? (1 for yes; 0 for no)", | |
0, 1); | |
if (sure2 == 1) { | |
money += landPrice * newA; | |
park.map.push(park.map.last.building); | |
park.map.pop(newA); | |
} | |
} else { | |
money += landPrice * newA; | |
park.map.pop(newA); | |
} | |
} | |
} | |
} | |
} | |
break; | |
case 6: | |
if (money < 120000) | |
typeString(addLinebreaks(":( You don't have enough money to build the rocket...Continue your amusement park for your " | |
+ petName + ". It really wants to go home!")); | |
else { | |
int engg = 0; | |
for (int i = 0; i < park.employees.size(); i++) { | |
if (park.employees.get(i).position | |
.equals("Engineer")) | |
engg++; | |
if (engg < 3) | |
typeString(addLinebreaks(":( You don't have enough engineer to build the rocket..." | |
+ "Continue your amusement park for your " | |
+ petName | |
+ ". It really wants to go home!")); | |
else { | |
typeString(addLinebreaks("Your cute little " | |
+ petName | |
+ " can finally go back its lovely home! The rocket will" | |
+ "be launched in several minutes. See goodbye to it!")); | |
typeString("5! 4! 3! 2! 1!", 5000 / 14); | |
typeString("Launch!", 55); | |
} | |
} | |
} | |
break; | |
case 7: { | |
// list all services | |
System.out | |
.println("=================================================="); | |
System.out.println("SERV\t\t\tCOST\tAREA"); | |
displayBuildings(services); | |
System.out | |
.println("=================================================="); | |
Service[] oriS = services.clone(); | |
int sChoice = 0; | |
do { | |
System.out | |
.println("1. Add a service\n" | |
+ "2. List the services by its cost (cheapest to dearest)\n" | |
+ "3. List the services by its area (smallest to largest)\n" | |
+ "0. Return"); | |
sChoice = input("What do you want to do?", 0, 3); | |
if (sChoice == 1) { | |
int servNum = input( | |
"Choose the ride you want to build: ", 1, | |
services.length); | |
park.map.push(services[servNum - 1]); | |
money -= services[servNum - 1].cost; | |
} else if (sChoice == 2) { | |
SearchSortEngine.qckSortCost(services, 0, | |
services.length - 1); | |
System.out | |
.println("=================================================="); | |
System.out.println("SERV\t\t\tCOST\tAREA"); | |
displayBuildings(services); | |
System.out | |
.println("=================================================="); | |
} else if (sChoice == 3) { | |
SearchSortEngine.qckSortArea(services, 0, | |
services.length - 1); | |
System.out | |
.println("=================================================="); | |
System.out.println("SERV\t\t\tCOST\tAREA"); | |
displayBuildings(services); | |
System.out | |
.println("=================================================="); | |
} | |
} while (sChoice != 1 && sChoice != 0); | |
services = oriS; | |
} | |
break; | |
case 8: | |
passDay(); | |
break; | |
case 0: | |
System.exit(0); | |
} | |
if (money <= 0) { // end game when no money | |
typeString(addLinebreaks("YOU DON'T HAVE ANY MONEY AND ARE IN DEBT! " | |
+ "YOU HAVE TO FILE BANKRUTPCY! TRY AGAIN NEXT TIME!")); | |
System.exit(0); | |
} | |
} while (choice != 0); | |
} | |
break; | |
} | |
} | |
} |
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
public class Map { | |
int area; | |
int count; // counts of buildings | |
Node first; | |
Node last; | |
public Map(int area) { | |
count = 0; | |
push(area); | |
} | |
public void push(Node newNode) { // add a node to the list | |
newNode.next = null; | |
if (this.area == 0) { | |
this.first = newNode; | |
this.last = newNode; | |
newNode.prev = null; | |
} else { | |
newNode.prev = this.last; | |
this.last.next = newNode; | |
this.last = newNode; | |
} | |
this.area++; | |
} | |
public void push(int value) { // add a certain number of nodes | |
for (int i = 0; i < value; i++) | |
push(new Node()); | |
} | |
public void pop(Node node) { // remove one node to the list | |
this.area--; | |
if (node == this.last) { | |
this.last = node.prev; | |
this.last.next = null; | |
} else if (node == this.first) { | |
this.first = node.next; | |
this.first.prev = null; | |
} else { | |
node.next.prev = node.prev; | |
node.prev.next = node.next; | |
} | |
} | |
public void pop(int value) { // remove a certain number of nodes | |
for (int i = 0; i < value; i++) | |
push(this.last); | |
} | |
public void pop(Building building) { // remove a building | |
Node cur = first; | |
while (cur != null) { | |
if (cur.building == building) | |
cur.building = null; | |
cur = cur.next; | |
} | |
count--; | |
} | |
public void display() { // display the map | |
Node cur = first; | |
int index = 1; | |
while (cur != null) { | |
if (cur.building != null) | |
System.out.print("* "); | |
else | |
System.out.print("O "); | |
if (index % 15 == 0) | |
System.out.println(); | |
cur = cur.next; | |
index++; | |
} | |
} | |
public boolean push(Building building) { // add a building | |
Node cur = first; | |
int area = building.area; | |
while (!(cur == null || area == 0)) { | |
if (cur.building == null) { | |
cur.building = building; | |
area--; | |
} | |
cur = cur.next; | |
} | |
if (area == 0) { | |
count++; | |
return true; | |
} else | |
return false; | |
} | |
} | |
class Node { | |
Building building; | |
Node next; | |
Node prev; | |
public Node() { | |
building = null; | |
next = null; | |
prev = null; | |
} | |
public Node(Building building, Node next, Node prev) { | |
this.building = building; | |
this.next = next; | |
this.prev = prev; | |
} | |
} |
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.LinkedList; | |
public class Park { | |
String location; | |
int maxArea; | |
int ticket; | |
double attraction; | |
LinkedList<Employee> employees; | |
Map map; | |
public static final String[] locations = { "Countryside", "Downtown", | |
"Prince Esperplena Island", "Cropper Mills Mall" }; | |
final static int[] areas = { 120, 90, 100, 100 }; | |
final static int[] maxAreas = { 175, 120, 150, 150 }; | |
final static int[] tickets = { 10, 15, 10, 10 }; | |
final static double[] attractions = { 0.1, 0.2, 0.1, 0.1 }; | |
public Park(String location, int area, int maxArea, int ticket, | |
double attraction) { | |
this.location = location; | |
this.maxArea = maxArea; | |
this.ticket = ticket; | |
this.attraction = attraction; | |
this.map = new Map(area); | |
this.employees = new LinkedList<Employee>(); | |
} | |
public static Park place(int number) { | |
return new Park(locations[number - 1], areas[number - 1], | |
maxAreas[number - 1], tickets[number - 1], | |
attractions[number - 1]); | |
} | |
} |
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
public class Ride extends Building { | |
int[] forbidWeather; | |
Park park; | |
// regular rides | |
final static String[] names = { "Roller Coaster", "Bumper Cars", "Octopus", | |
"Drop Tower", "Ferris Wheels", "Sky Flyer", "Carousel" }; | |
final static int[] costs = { 2200, 1500, 888, 1000, 1250, 1000, 500 }; | |
final static int[] areas = { 20, 25, 10, 5, 15, 15, 10 }; | |
final static int[] prices = { 15, 5, 8, 10, 25, 10, 3 }; | |
final static double[] attractions = { 0.08, 0.045, 0.005, 0.03, 0.01, 0.05, | |
0.07 }; | |
final static int[][] forbidWeathers = { { 2, 3, 5 }, { -1 }, { 2 }, | |
{ 3, 2, 4, 5 }, { 3 }, { 2, 3, 4, 5 }, { -1 } }; // 0-sunny,1-cloudy,2-stormy,3-windy,4-rainy,5-snowy | |
public Ride(String name, int cost, int price, int area, double attraction, | |
int[] forbidWeather) { | |
this.name = name; | |
this.cost = cost; | |
this.price = price; | |
this.area = area; | |
this.attraction = attraction; | |
this.forbidWeather = forbidWeather; | |
this.monthlyCost = (int) (area * price * attraction); | |
} | |
public static Ride build(int number) { | |
return new Ride(names[number - 1], costs[number - 1], | |
prices[number - 1], areas[number - 1], attractions[number - 1], | |
forbidWeathers[number - 1]); | |
} | |
} |
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; | |
public class SearchSortEngine { | |
public static Scanner in = new Scanner(System.in); // scanner from the | |
// keyboard | |
public static Random rnd = new Random((long) (Math.random() * 10000444400L)); // random | |
// number | |
// generator | |
// quick sort for cost | |
public static void qckSortCost(Building[] a, int lm, int rm) { | |
// end of recursion | |
if (lm < rm) { | |
int pivot = a[lm + (rm - lm) / 2].cost; // pivot in the middle | |
// divide et conquer | |
int lf = lm, rf = rm; // mark flags be the markers | |
while (lf <= rf) { | |
while (a[lf].cost < pivot) | |
// increase left flag till it is greater/equal to/than pivot | |
lf++; | |
while (a[rf].cost > pivot) | |
// decrease rf till it is less/equal to/than pivot | |
rf--; | |
if (lf < rf) { // swap lf and rf | |
Building temp = a[rf]; | |
a[rf] = a[lf]; | |
a[lf] = temp; | |
lf++; | |
rf--; | |
} else if (lf == rf) { // if equal, continue to next flag | |
lf++; | |
rf--; | |
} | |
} | |
// recursion | |
if (rm > lf) | |
qckSortCost(a, lf, rm); | |
if (lm < rf) | |
qckSortCost(a, lm, rf); | |
} | |
} | |
// quick sort | |
public static void qckSortArea(Building[] a, int lm, int rm) { | |
// end of recursion | |
if (lm < rm) { | |
int pivot = a[lm + (rm - lm) / 2].area; // pivot in the middle | |
// divide et conquer | |
int lf = lm, rf = rm; // mark flags be the markers | |
while (lf <= rf) { | |
while (a[lf].area < pivot) | |
// increase left flag till it is greater/equal to/than pivot | |
lf++; | |
while (a[rf].area > pivot) | |
// decrease rf till it is less/equal to/than pivot | |
rf--; | |
if (lf < rf) { // swap lf and rf | |
Building temp = a[rf]; | |
a[rf] = a[lf]; | |
a[lf] = temp; | |
lf++; | |
rf--; | |
} else if (lf == rf) { // if equal, continue to next flag | |
lf++; | |
rf--; | |
} | |
} | |
// recursion | |
if (rm > lf) | |
qckSortArea(a, lf, rm); | |
if (lm < rf) | |
qckSortArea(a, lm, rf); | |
} | |
} | |
} |
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
public class Service extends Building { | |
final static String[] names = { "Food Counter", "Information Counter", | |
"EM Center" }; | |
final static int[] costs = { 350, 200, 500 }; | |
final static int[] monthlyCosts = { 50, 30, 100 }; | |
final static int[] prices = { 15, 0, 0 }; | |
final static int[] areas = { 5, 1, 10 }; | |
final static double[] attractions = { 0.05, 0.001, 0.03 }; | |
public Service(String name, int cost, int monthlyCost, int price, | |
double attraction, int area) { | |
this.name = name; | |
this.cost = cost; | |
this.price = price; | |
this.attraction = attraction; | |
} | |
public static Service addService(int number) { | |
return new Service(names[number - 1], costs[number - 1], | |
monthlyCosts[number - 1], prices[number - 1], | |
attractions[number - 1], areas[number - 1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment