Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Gantt-projektplanlægger.xlsx
Binary file not shown.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Define variables
SRC_DIR = woz
OUT_DIR = class_files
SRCS := $(wildcard $(SRC_DIR)/*.java)
CLASSES := $(patsubst $(SRC_DIR)/%.java,$(OUT_DIR)/%.class,$(SRCS))
JAVAC = javac
MKDIR_P = mkdir -p
MAIN_CLASS = woz.Game

# Default target to compile all .java files
all: $(OUT_DIR) $(CLASSES)

# Create the output directory if it doesn't exist
$(OUT_DIR):
$(MKDIR_P) $(OUT_DIR)

# Compile .java files into .class files
$(OUT_DIR)/%.class: $(SRC_DIR)/%.java
$(JAVAC) -d $(OUT_DIR) $<

# Run the Java program
run: all
java -cp $(OUT_DIR) $(MAIN_CLASS)

# Clean target to remove compiled .class files
clean:
rm -rf $(OUT_DIR)

.PHONY: all run clean
Binary file removed woz.zip
Binary file not shown.
Binary file added woz/BaseCommand.class
Binary file not shown.
10 changes: 9 additions & 1 deletion woz/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
*/

class BaseCommand {
String description = "Undocumented";
String description;

BaseCommand(String description) {
this.description = description;
}

BaseCommand() {
description = "Undocumented";
}

protected boolean guardEq (String[] parameters, int bound) {
return parameters.length!=bound;
Expand Down
Binary file added woz/Command.class
Binary file not shown.
Binary file added woz/CommandExit.class
Binary file not shown.
4 changes: 4 additions & 0 deletions woz/CommandExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
*/

class CommandExit extends BaseCommand implements Command {
CommandExit() {
description = "Stop spillet";
}

@Override
public void execute (Context context, String command, String parameters[]) {
context.makeDone();
Expand Down
Binary file added woz/CommandGo.class
Binary file not shown.
Binary file added woz/CommandHelp.class
Binary file not shown.
Binary file added woz/CommandResetDay.class
Binary file not shown.
11 changes: 11 additions & 0 deletions woz/CommandResetDay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CommandResetDay extends BaseCommand implements Command {

public CommandResetDay(){
description = "Reset the day";
}
@Override
public void execute (Context context, String command, String parameters[]) {
System.out.println("Resetting the day");
context.resetDay();
}
}
Binary file added woz/CommandRoomAction.class
Binary file not shown.
112 changes: 112 additions & 0 deletions woz/CommandRoomAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* Command for handlign actions in a room
*/

class CommandRoomAction extends BaseCommand implements Command {

Player player;


CommandRoomAction (String description) {
super(String.format("Rumhandlinger: %s", description));
player = Game.context.getPlayer();

}


public void pickup(Context context, String[] parameters) {

//we get the player, our current location and the amount of trash present
Space cspace = context.getCurrent();
int amountPresent = cspace.getGeneratedTrash();

//if the pickup-command has 1 parameter (amount of trash to collect) we do as follows
if (!guardEq(parameters, 1)) {
int amount = Integer.parseInt(parameters[0]); //parameters[] is a string array which we convert to int

if (amount > amountPresent) {//if you ask to collect more trah than present:
System.out.printf("%nSå meget skrald er der ikke! Der er %d", amountPresent);
}
else {//otherwise we add the trash to inventory and set the amount of trash in the location after pickup
player.addToInventory("trash", amount);
cspace.setGeneratedTrash(amountPresent - amount);
System.out.printf("%nDu har samlet %d skrald op - nu har du " + player.getInventory() + " i din taske!", amount);
}
}
else if (!guardEq(parameters, 0)) {//if you only type "pickup":
player.addToInventory("trash", amountPresent);
cspace.setGeneratedTrash(0);
System.out.printf("%nDu har samlet alt skrald op - nu har du " + player.getInventory() + " i din taske!");
} else {
context.youStupid();
}
}

// Hint can be called from everywhere
public void hint(Context context, String[] parameters) {
if (guardEq(parameters, 0)) {
context.youStupid();
return;
}
System.out.println("Congratulations, you have recieved a hint");
}

//Buy command can be executed from shop (butik). this is checked in registry w baseCommands and roomCommands
public void buy(Context context, String[] parameters) {
if (guardEq(parameters, 1)) {
context.youStupid();
return;
}
context.buyExecuter(parameters);//calls buyExecuter from context who will peform the necessary actions

}

//command to sell trash valid in shop (shouldn't it be valid at genbrugsstation only?)
public void sell(Context context, String[] parameters) {
if (guardEq(parameters, 1)) {
context.youStupid();
return;
}else{
player.removeFromInventory("trash", Integer.parseInt(parameters[0]));
System.out.println(player.getInventory());
}
System.out.printf("%nDu har solgt %s bunker skrald%n", parameters[0]);
}


//default action for commands
public void default_(Context context, String command, String[] parameters) {
if (guardEq(parameters, 0)) {
context.youStupid();
return;
}
System.out.println("From default: Have not implemented that command yet :))");
}

@Override
public void execute (Context context, String command, String[] parameters) {
switch(command) {

case "pickup" :
pickup(context, parameters);
break;

case "hint" :
hint(context, parameters);
break;

case "sell" :
sell(context, parameters);
break;

case "buy" :
buy(context, parameters);
break;

default :
default_(context, command, parameters);

}

context.getCurrent().updateExits();
}
}
Binary file added woz/CommandUnknown.class
Binary file not shown.
71 changes: 71 additions & 0 deletions woz/Commandsaveload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.io.*;
import java.util.*;

class Commandsaveload extends BaseCommand implements Command {
private final String fileName; // This variable stores the name of the file where we save and read variables.
private final Map<String, String> variables; // Use a LinkedHashMap to preserve the order

// Constructor to initialize the class with a file name.
public Commandsaveload(String fileName) {
this.fileName = fileName;
variables = new LinkedHashMap<>();
}

// Method to save a variable and its value to the text file.
public void save(String variableName, String variableValue) {
// Read existing variables, overwrite the file, and then add the new variable.
variables.put(variableName, variableValue);

try (PrintWriter writer = new PrintWriter(new FileWriter(fileName, false))) {
for (String key : variables.keySet()) {
writer.println(key + "=" + variables.get(key));
}
} catch (IOException e) {
e.printStackTrace();
}
}

// Method to read variables from the text file and store them in a map.
public Map<String, String> load() {
Map<String, String> variables = new LinkedHashMap<>();

try (BufferedReader reader = new BufferedReader(new FileReader(fileName)) ) {
// Read lines from the file and split them into variable name and value.
reader.lines().map(line -> line.split("=", 2))
.filter(parts -> parts.length == 2)
.forEach(parts -> variables.put(parts[0], parts[1]));
} catch (IOException e) {
e.printStackTrace();
// Handle any errors that occur during file reading, such as if the file doesn't exist.
}

return variables;
}

@Override
public void execute (Context context, String command, String[] parameters) {

Commandsaveload Commandsaveload = new Commandsaveload("variables.txt");

switch(command) {

case "save":


// Save variables
Commandsaveload.save("name", "Tom Smith");
Commandsaveload.save("age", "34");
Commandsaveload.save("city", "New Amsterdam");
Commandsaveload.save("city status", "Polluted");
Commandsaveload.save("lake status", "5 Trash");

break;

case "load":
// Read and print variables
Map<String, String> savedVariables = Commandsaveload.load();
savedVariables.forEach((key, value) -> System.out.println(key + " = " + value));
break;
}
}
}
Binary file added woz/Context.class
Binary file not shown.
103 changes: 97 additions & 6 deletions woz/Context.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,104 @@
/* Context class to hold all context relevant to a session.
*/

import java.util.Map;
import java.util.HashMap;

class Context {
Space current;
boolean done = false;

Context (Space node) {

private Space current;
private Player player;
private boolean done = false;
private int day;

Context (Space node, Player player) {
current = node;
this.player = player;
day = 1;
}

//resets amount of generated trash, the handleRoom(), increments the day and transitions to "Kontor"

void resetDay(){
World.park.setGeneratedTrash((int)(Math.random()*(15 - 1) + 1));
World.bymidte.setGeneratedTrash((int)(Math.random()*(15 - 1) + 1));
World.hospital.setGeneratedTrash((int)(Math.random()*(15 - 1) + 1));

World.park.makeHandled();
World.bymidte.makeHandled();
World.hospital.makeHandled();
World.genbrugsstation.makeHandled();
World.butik.makeHandled();

day++;

transition("kontor");//man "vågner op" i kontoret igen


}

public int getDay(){//gets the day-number
return day;
}


//checks if all available actions for the day are done
public boolean isDayDone(Space s1, Space s2, Space s3, Space s4, Space s5){
if((s1.isHandled && s2.isHandled && s3.isHandled && s4.isHandled && s5.isHandled)){
return true;
}else{
return false;
}
}

public Space getCurrent() {
return current;
}

public Player getPlayer() {
return player;
}

public void buyExecuter(String[] parameters) {
String item = parameters[0]; //gets the parameter from command (what to buy)
Map<String, int[]> lowerExtensions = current.getExtensions();

if (containsKey(lowerExtensions.keySet().toArray(new String[0]), item)) {
//checks if the item from the terminal is a valid product in the shop

int[] priceXP = lowerExtensions.get(item); //the value belonging to the item name is a 2D array containg price and xp

if (player.canAfford(priceXP[0])) {
//Add to inventory
player.addToInventory(item, 1);
player.subtractMoney(priceXP[0]);
player.addPoints(priceXP[1]);

System.out.printf("%nDu har købt %s. Godt gået!", item);

} else {
System.out.println("Du har ikke råd til denne udvidelse");
}
} else {
System.out.println("Denne udvidelse eksisterer ikke i shoppen");
}
System.out.println("inventory: " + player.getInventory());
}

public void transition (String direction) {
Space next = current.followEdge(direction);
if (next==null) {
if (next == null) {
System.out.println("You are confused, and walk in a circle looking for '"+direction+"'. In the end you give up 😩");
} else {
current.goodbye();
current = next;
current.welcome();
}
}

public void youStupid () {
System.out.println("You are an idiot, and have used this command wrong");
}

public void makeDone () {
done = true;
Expand All @@ -31,5 +107,20 @@ public void makeDone () {
public boolean isDone () {
return done;
}
}

//Helpers


private boolean containsKey(String[] hm, String itemName) {
//checks if the itemName is in the array
for (String name : hm) {
if (itemName.equals(name.toLowerCase().trim())) {
return true;
}
}
return false;
}



}
Binary file added woz/Game.class
Binary file not shown.
Loading