Skip to content

Commit

Permalink
Merge pull request #22 from BestDownLoader365/Fight_Command
Browse files Browse the repository at this point in the history
have stored map problems
  • Loading branch information
BestDownLoader365 committed Mar 21, 2024
2 parents e1cf92e + b7616f8 commit 23f634f
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 35 deletions.
6 changes: 6 additions & 0 deletions src/main/java/InteractableEntity/InteractableEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package InteractableEntity;

public abstract class InteractableEntity {

protected String name;

public abstract int getHealth();

public abstract int getDefence();

public abstract int getDamage();
}
15 changes: 12 additions & 3 deletions src/main/java/Math/MathPool.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package Math;

import java.util.Random;
import java.util.ArrayList;

Expand All @@ -7,16 +8,17 @@ public class MathPool {
private final Random random;


public MathPool(){
public MathPool() {
poolOfQuestions = new ArrayList<MathQuestion>();
random = new Random();
}

public void addMathQuestion(String wordProblem, int solution, int difficulty){
public void addMathQuestion(String wordProblem, int solution, int difficulty) {
MathQuestion problem = new MathQuestion(wordProblem, solution, difficulty);
poolOfQuestions.add(problem);
}

public MathQuestion getQuestionByDifficulty(int targetDifficulty){
public MathQuestion getQuestionByDifficulty(int targetDifficulty) {
ArrayList<MathQuestion> filteredQuestions = new ArrayList<>();
for (MathQuestion question : poolOfQuestions) {
if (question.getDifficulty() == targetDifficulty) {
Expand All @@ -31,4 +33,11 @@ public MathQuestion getQuestionByDifficulty(int targetDifficulty){
}
}

public void init() {
addMathQuestion("1 + 1 = ", 2, 0);
addMathQuestion("1 + 2 = ", 3, 0);
addMathQuestion("3 + 1 = ", 4, 0);
addMathQuestion("2 + 5 = ", 7, 0);
addMathQuestion("10 + 23 = ", 33, 0);
}
}
6 changes: 3 additions & 3 deletions src/main/java/Math/MathQuestion.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package Math;

public class MathQuestion {
private String question;
private int answer;
private int difficulty;
private final String question;
private final int answer;
private final int difficulty;

public MathQuestion(String qn, int ans, int diff){
this.question = qn;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import textbox.*;
import map.AMap;

import java.util.Scanner;


public abstract class Command {
protected TextBox textBox;
Expand All @@ -11,6 +13,9 @@ public abstract class Command {
protected AMap currentMap;

public abstract void execute();
public void execute(Scanner in){

}

public Command() {
commandDescription = "Impossible";
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/command/fight/FightingCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package command.fight;
import command.Command;

import java.util.Scanner;


public class FightingCommand extends Command {
public FightingCommand() {
Expand All @@ -11,4 +13,9 @@ public FightingCommand() {
public void execute() {

}

@Override
public void execute(Scanner in) {
currentMap.fightLoop(in);
}
}
6 changes: 1 addition & 5 deletions src/main/java/command/fight/RunningCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ public RunningCommand() {
@Override
public void execute(){
if(currentMap instanceof BattleInterface) {
AMap initMap = new FirstMap();
initMap.initMap(30, 10);
initMap.initPlayerLocation(0, 0);
initMap.placeMonsterInTheMap(2, 3);
currentMap = initMap;

}
}
}
6 changes: 4 additions & 2 deletions src/main/java/command/mapmove/MapMoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

public abstract class MapMoveCommand extends Command {
protected int commandModifier;
public MapMoveCommand(){

public MapMoveCommand() {

}

public MapMoveCommand(String userInput) {
commandDescription = "MapMove";
userInput = userInput.trim();
String[] splitUserInput = userInput.split("\\h+");
if(splitUserInput.length == 1){
if (splitUserInput.length == 1) {
commandModifier = 1;
} else {
commandModifier = Integer.parseInt(splitUserInput[1]);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/command/mapmove/MovingDownwardCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package command.mapmove;


public class MovingDownwardCommand extends MapMoveCommand {
public MovingDownwardCommand(String userInput) {
super(userInput);
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/command/mapmove/MovingRightCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@


public class MovingRightCommand extends MapMoveCommand {
public MovingRightCommand() {
super();
}

public MovingRightCommand(String userInput) {
super(userInput);
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/main/CalculaChroniclesOfTheAlgorithmicKingdom.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package main;

import command.Command;
import command.fight.FightingCommand;
import command.mapmove.MapMoveCommand;
import map.*;
import parser.Parser;
import textbox.PlayerStatus;
import textbox.TextBox;
import ui.Ui;
import Math.*;

import java.util.Map;
import java.util.Scanner;


public class CalculaChroniclesOfTheAlgorithmicKingdom {
Expand All @@ -17,6 +20,7 @@ public static void main(String[] args) {
}

public void startGame() {
Scanner in = new Scanner(System.in);
PlayerStatus playerStatus = new PlayerStatus(100, 0, 0);
TextBox textBox = new TextBox();
Parser parser = new Parser();
Expand All @@ -27,28 +31,34 @@ public void startGame() {
map.initPlayerLocation(0, 0);
map.placeMonsterInTheMap(2, 3);
textBox.initTextBox();
map.addMaps(map);

ui.printPlayerStatus(playerStatus);
ui.printMap(map);
System.out.println("Type 'h' to get the help menu.");

Command userCommand;
while (true) {
String userCommandText = parser.readInCommand();
String userCommandText = in.nextLine();

userCommand = parser.parseCommand(userCommandText);
setUserCommand(userCommand, map, playerStatus, textBox);

if (!(map instanceof FirstMap) && userCommand instanceof MapMoveCommand) {
System.out.println("Invalid Command");
} else if (userCommand.getCommandDescription().equals("FIGHT!")){
userCommand.execute(in);
} else {
userCommand.execute();
}

map.storeMaps(0, map);
map = userCommand.getCurrentMap();

if (!userCommand.getCommandDescription().equals("HelpMe!!")) {
ui.printPlayerStatus(playerStatus);
ui.printMap(map);
}

}
}

Expand Down
16 changes: 15 additions & 1 deletion src/main/java/map/AMap.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package map;

import java.util.ArrayList;
import java.util.Scanner;

public abstract class AMap {
protected static ArrayList<AMap> storedMap;
protected static ArrayList<AMap> storedMaps = new ArrayList<>();
protected int width;
protected int height;
protected ArrayList<ArrayList<Character>> currentMap;
protected int playerX;
protected int playerY;
protected String mapName;
public AMap(){

}

public abstract void fightLoop();
public void fightLoop(Scanner in){

}

public void initMap(int givenWidth, int givenHeight) {
this.width = givenWidth;
Expand Down Expand Up @@ -121,5 +129,11 @@ public int getPlayerX() {
public int getPlayerY() {
return playerY;
}
public void storeMaps(int index, AMap map){
storedMaps.set(index, map);
}
public void addMaps(AMap map){
storedMaps.add(map);
}

}
41 changes: 34 additions & 7 deletions src/main/java/map/BattleInterface/BattleInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import textbox.PlayerStatus;
import textbox.TextBox;
import ui.Ui;
import Math.*;

import java.lang.management.PlatformLoggingMXBean;
import java.util.ArrayList;
import java.util.Scanner;

public class BattleInterface extends AMap {
protected PlayerStatus currentPlayer;
Expand All @@ -25,6 +27,31 @@ public BattleInterface(PlayerStatus player, TextBox text, InteractableEntity ent

}

@Override
public void fightLoop() {

}

@Override
public void fightLoop(Scanner in) {
MathPool mathPool = new MathPool();
mathPool.init();
Ui ui = new Ui();

while (currentPlayer.getPlayerHealth() > 0 && currentEntity.getHealth() > 0) {
ui.printPlayerStatus(currentPlayer);
ui.printMap(currentMap);
MathQuestion mathQuestion = mathPool.getQuestionByDifficulty(0);
ui.printQuestion(mathQuestion);
int answer = Integer.parseInt(in.nextLine().trim());
if (mathQuestion.checkAns(answer)) {
playerHitEnemy();
} else {
enemyHitPlayer();
}
}
}

public void initMap(int givenWidth, int givenHeight) {
this.width = givenWidth;
this.height = givenHeight;
Expand All @@ -51,26 +78,26 @@ public void initMap(int givenWidth, int givenHeight) {
}


public void playerHitEnemy(){
if (currentEntity instanceof Enemy){
int dmgDone = 10 - (10 * ((Enemy) currentEntity).getDefence());
public void playerHitEnemy() {
if (currentEntity instanceof Enemy) {
int dmgDone = 10;
((Enemy) currentEntity).harmHealth(dmgDone);
}
}

public void enemyHitPlayer(){
if (currentEntity instanceof Enemy){
public void enemyHitPlayer() {
if (currentEntity instanceof Enemy) {
int dmgDone = ((Enemy) currentEntity).getDamage();
currentPlayer.harmHealth(dmgDone);
}
}


public InteractableEntity getCurrentEntity(){
public InteractableEntity getCurrentEntity() {
return currentEntity;
}

public PlayerStatus getCurrentPlayer(){
public PlayerStatus getCurrentPlayer() {
return currentPlayer;
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/map/FirstMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

public class FirstMap extends AMap{
protected String DIFFICULTY_MODIFIER = "easy"; //can use to determine question difficulty

@Override
public void fightLoop() {
System.out.println("lol");
}
}
5 changes: 0 additions & 5 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
public class Parser {


public String readInCommand() {
Scanner in = new Scanner(System.in);
return in.nextLine();
}

public CommandType analyseCommand(String userCommand) {
Pattern pattern;
Matcher matcher;
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import map.AMap;
import textbox.PlayerStatus;
import textbox.TextBox;

import Math.*;
import java.util.ArrayList;

public class Ui {
Expand Down Expand Up @@ -33,7 +33,16 @@ public void printTextBox(TextBox box) {
}
printDividingLine();
}

public void printMap(ArrayList<ArrayList<Character>> map) {
printDividingLine();
for (ArrayList<Character> row : map) {
for (char cell : row) {
System.out.print(cell + " ");
}
System.out.println();
}
printDividingLine();
}
public void printMap(AMap map) {
printDividingLine();
for (ArrayList<Character> row : map.getCurrentMap()) {
Expand All @@ -53,4 +62,7 @@ public void printHelpMenu() {
System.out.println("'run' to escape the battle interface");
printDividingLine();
}
public void printQuestion(MathQuestion mathQuestion){
System.out.println(mathQuestion.getQuestion());
}
}

0 comments on commit 23f634f

Please sign in to comment.