Skip to content

Commit

Permalink
Bunch of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
voroscsoki committed May 22, 2024
1 parent d2769ea commit 3b6075a
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 49 deletions.
39 changes: 18 additions & 21 deletions src/main/java/Controll/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ public static void setTest(boolean value) {
* Name of the used file
*/
private static String fileName = "";
/**
* Path of the used file
*/
private static String filePath = "";

/**
* List of the test result
Expand All @@ -97,7 +93,7 @@ public static void setTest(boolean value) {
/**
* List of the commands
*/
public static final List<String> commandList = new ArrayList<>();
protected static final List<String> commandList = new ArrayList<>();

/**
* Number of new pumps with commands
Expand Down Expand Up @@ -132,7 +128,7 @@ public static void setActivePlayer(Player p) {
activePlayers.add(currentPlayer);
}

public static Player GetActivePlayer() {
public static Player getActivePlayer() {
return currentPlayer;
}

Expand All @@ -155,7 +151,7 @@ public static boolean changeActivePlayer() {
* Function for controlling the game.
* Reads a command than calls a function to execute it.
*/
public static void Run() {
public static void run() {
while (!commandList.isEmpty()) {
String command = commandList.get(0);
commandList.remove(0);
Expand Down Expand Up @@ -283,6 +279,7 @@ public static void Game() {
if (commandList.isEmpty()) {
commandList.add(stdInScanner.nextLine());
}
stdInScanner.close();
String command = commandList.get(0);
commandList.remove(0);
String[] cmd = command.split(" ");
Expand Down Expand Up @@ -375,22 +372,22 @@ public static void Game() {
step(cmd);
break;
case ("endturn"):
endturn(cmd);
endturn();
break;
case ("count"):
count(cmd);
count();
break;
case ("test"):
test(cmd);
break;
case ("setend"):
setend(cmd);
setend();
break;
case ("setpump"):
setpump(cmd);
break;
case ("restart"):
restart(cmd);
restart();
break;
case ("exit"):
return;
Expand All @@ -399,10 +396,10 @@ public static void Game() {
}
if (moves == activePlayers.size()) {
moves = 0;
endturn(cmd);
endturn();
}
}
Run();
run();
}

/**
Expand All @@ -413,7 +410,6 @@ public static void load(String cmd) {
String modifiedPath = cmd.replace("/", File.separator).replace("\\", File.separator);
try (Scanner scanner = new Scanner(new File(rootDirectory + File.separator + modifiedPath));) {
outResults.clear();
filePath = modifiedPath;
String separator = "\\";
String[] tmp = cmd.replaceAll(Pattern.quote(separator), "\\\\").split("\\\\");
fileName = tmp[tmp.length - 1];
Expand All @@ -429,7 +425,8 @@ public static void load(String cmd) {
public static void loadFileFromSrcToReader(String fileNameToOpen) {
// a text file is located in src folder in the project
Path rootDir = Paths.get(".").normalize().toAbsolutePath();
File file = new File(rootDir.toString() + "/" + fileNameToOpen);
Path filePath = rootDir.resolve(fileNameToOpen);
File file = filePath.toFile();
try (Reader input = new FileReader(file)) {
try (BufferedReader br = new BufferedReader(input)) {

Expand Down Expand Up @@ -717,11 +714,11 @@ public static void show(String[] cmd) {
switch (commands[1]) {
case "player":
if (test) outResults.add(p.toString());
else logger.log(Level.INFO, p::toString);
else logger.log(Level.INFO, p::toString); // Fix: Wrap the method call in a lambda function
break;
case "field":
if (test) outResults.add(objectReverseNames.get(p.getStandingField()));
else logger.log(Level.INFO, objectReverseNames.get(p.getStandingField()));
else logger.log(Level.INFO, () -> objectReverseNames.get(p.getStandingField())); // Fix: Wrap the method call in a lambda function
break;
default:
break;
Expand Down Expand Up @@ -1163,7 +1160,7 @@ public static void step(String[] cmd) {
* Function for ending a turn.
* Responsible for calling the step function for all steppable objects.
*/
public static void endturn(String[] cmd) {
public static void endturn() {
//elvégzi a kör végével járó lépéseket (vízszámolás, objektumok step függvényének hívása stb…)
//vízszámlálás
//water counter lehet hogy üres
Expand All @@ -1181,7 +1178,7 @@ public static void endturn(String[] cmd) {
/**
* Function for countig the points for the two sides.
*/
public static void count(String[] cmd) {
public static void count() {
waterCounter.count();
if (test) outResults.add(StringResourceController.GOOD_ACTION);
else logger.log(Level.INFO, StringResourceController.GOOD_ACTION);
Expand All @@ -1190,7 +1187,7 @@ public static void count(String[] cmd) {
/**
* Function for restaring the game.
*/
public static void restart(String[] cmd) {
public static void restart() {
random = true;
objectNames.clear();
objectReverseNames.clear();
Expand All @@ -1213,7 +1210,7 @@ public static void test(String[] cmd) {
/**
* Function for signaling to the watercounter that the game ended.
*/
public static void setend(String[] cmd) {
public static void setend() {
waterCounter.setEnd();
if (test) outResults.add(StringResourceController.GOOD_ACTION);
else logger.log(Level.INFO, StringResourceController.GOOD_ACTION);
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/Controll/Menu.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package main.java.Controll;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import javax.swing.WindowConstants;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
Expand Down Expand Up @@ -127,15 +123,15 @@ public JButton getPlayButton() {
return newGame;
}

public ArrayList<JButton> getActionButtons() {
ArrayList<JButton> actionButtons = new ArrayList<JButton>();
public List<JButton> getActionButtons() {
ArrayList<JButton> actionButtons = new ArrayList<>();
if (vg == null) return actionButtons;

actionButtons.add(vg.repairButton);
actionButtons.add(vg.breakButton);
actionButtons.add(vg.makeSlipperyButton);
actionButtons.add(vg.makeStickyButton);
actionButtons.add(vg.putDownButton);
actionButtons.add(ViewGame.repairButton);
actionButtons.add(ViewGame.breakButton);
actionButtons.add(ViewGame.makeSlipperyButton);
actionButtons.add(ViewGame.makeStickyButton);
actionButtons.add(ViewGame.putDownButton);

return actionButtons;
}
Expand All @@ -161,7 +157,7 @@ public void actionPerformed(ActionEvent e) {
Controller.commandList.add("addplayer D Sab" + i);
}
}
Controller.Run(); // egyszer fut le, felépíti a pályát, utána a függvényeit kell majd hívni
Controller.run(); // egyszer fut le, felépíti a pályát, utána a függvényeit kell majd hívni

Controller.create();
Controller.setActivePlayer(Controller.getAllPlayers().get(0));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Drawing/MechanicDraw.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MechanicDraw(int tmpX, int tmpY) {
@Override
public void Draw(JPanel panel, Graphics2D g) {
Mechanic m = (Mechanic)ViewGame.objectDrawNames.get(this);
Player current = Controller.GetActivePlayer();
Player current = Controller.getActivePlayer();
mecName = Controller.objectReverseNames.get(m);
mec.setText(mecName);
Field f = m.getStandingField();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Drawing/SaboteurDraw.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SaboteurDraw(int tmpX, int tmpY) {
@Override
public void Draw(JPanel panel, Graphics2D g) {
Saboteur s = (Saboteur)ViewGame.objectDrawNames.get(this);
Player current = Controller.GetActivePlayer();
Player current = Controller.getActivePlayer();
sabName = Controller.objectReverseNames.get(s);
sab.setText(sabName);
Field f = s.getStandingField();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Fields/ActiveFields/Cistern.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public Pipe pickUpPipe() {
@Override
public String toString() {
ArrayList<Player> players = this.getPlayers();
String playerBuilder = StringResourceController.stingBuilder(players);
String playerBuilder = StringResourceController.stringBuilder(players);

ArrayList<Pipe> pipes = this.getPipes();
String pipeBuilder = StringResourceController.stingBuilder(pipes);
String pipeBuilder = StringResourceController.stringBuilder(pipes);

return "name: "+ Controller.objectReverseNames.get(this)
+ "\noccupied: " + this.isOccupied()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Fields/ActiveFields/Pump.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public String toString() {

private String getPlayerNames() {
ArrayList<Player> players = this.getPlayers();
return StringResourceController.stingBuilder(players);
return StringResourceController.stringBuilder(players);
}


private String getPipeNames(){
ArrayList<Pipe> pipes = this.getPipes();
return StringResourceController.stingBuilder(pipes);
return StringResourceController.stringBuilder(pipes);
}
}
4 changes: 2 additions & 2 deletions src/main/java/Fields/ActiveFields/Spring.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void step() {
@Override
public String toString() {
ArrayList<Player> players = this.getPlayers();
String playerBuilder = StringResourceController.stingBuilder(players);
String playerBuilder = StringResourceController.stringBuilder(players);


ArrayList<Pipe> pipes = this.getPipes();
String pipeBuilder = StringResourceController.stingBuilder(pipes);
String pipeBuilder = StringResourceController.stringBuilder(pipes);

return "name: "+ Controller.objectReverseNames.get(this)
+ "\noccupied: " + this.isOccupied()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Fields/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ public void step(){
@Override
public String toString() {
ArrayList<Player> players = this.getPlayers();
String playerBuilder = StringResourceController.stingBuilder(players);
String playerBuilder = StringResourceController.stringBuilder(players);

List<ActiveFields> localFields = this.getFields();
String fieldBuilder = StringResourceController.stingBuilder(localFields);
String fieldBuilder = StringResourceController.stringBuilder(localFields);
return "name: " + Controller.objectReverseNames.get(this)
+ "\noccupied: " + this.isOccupied()
+ "\nwater: " + getWaterNoChange()
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/StringResource/StringResourceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ private StringResourceController(){}
public static final String GOOD_ACTION = "Sikeres művelet";
public static final String WATER = "water";
public static final String RANDOM_ON = "A véletlen események be lettek kapcsolva.";
public static final String INVALID_ACTION = "A műveletet nem lehet végrehajtani";

public static String stingBuilder(List collection){
public static String stringBuilder(List<?> collection){
StringBuilder builder = new StringBuilder("null");
if(collection != null) {
for (int i = 0; i < collection.size(); i++) {
Expand Down
1 change: 0 additions & 1 deletion test/main/java/Fields/ActiveFields/CisternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import main.java.Controll.Controller;
import main.java.Fields.Pipe;

class CisternTest {
Expand Down
3 changes: 1 addition & 2 deletions test/main/java/StepDefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -23,7 +22,7 @@ public class StepDefs {
public void gameInitialized() {
Controller.load("test/resources/testpalya.txt");

Controller.Run();
Controller.run();
Controller.create();
Controller.setActivePlayer(Controller.getAllPlayers().get(0));
}
Expand Down

0 comments on commit 3b6075a

Please sign in to comment.