Browse Source

Implemented logic after ai made move and player made move

Lorenz Hohmann 3 years ago
parent
commit
9ee90bf9c8
  1. 44
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java
  2. 1
      src/test/java/de/tims/fleetstorm/gui/LogicTest.java

44
src/main/java/de/tims/fleetstorm/gui/GameLogic.java

@ -201,11 +201,15 @@ public class GameLogic extends JPanel {
int coordX = coords[0]; int coordX = coords[0];
int coordY = coords[1]; int coordY = coords[1];
boolean shotSuccess = enemyMatchfield.getField(coordX, coordY).shoot();
gui.updateField(playerMove, enemyMatchfield);
Coordinate chosenField = enemyMatchfield.getField(coordX, coordY);
boolean shotSuccess = chosenField.shoot();
gui.updateField(enemyMatchfield);
if (shotSuccess && chosenField.getState() != Coordinate.HIT) {
nextMove(false);
}
// if (!shotSuccess)
// nextMove();
} }
}); });
} }
@ -257,6 +261,13 @@ public class GameLogic extends JPanel {
return enemy5Ship; return enemy5Ship;
} }
private JLabel getXLabel() {
JLabel x = new JLabel("x");
x.setForeground(Color.WHITE);
return x;
}
private int[] getCoordinateFromLabel(JPanel panel) { private int[] getCoordinateFromLabel(JPanel panel) {
String panelName = panel.getName(); String panelName = panel.getName();
String[] panelNameSplit = panelName.split(":"); String[] panelNameSplit = panelName.split(":");
@ -265,7 +276,7 @@ public class GameLogic extends JPanel {
return new int[] { coordX, coordY }; return new int[] { coordX, coordY };
} }
private void updateField(boolean playerMove, Matchfield matchfield) {
private void updateField(Matchfield matchfield) {
for (JPanel coordinatePanel : this.fields) { for (JPanel coordinatePanel : this.fields) {
int[] coords = this.getCoordinateFromLabel(coordinatePanel); int[] coords = this.getCoordinateFromLabel(coordinatePanel);
int coordX = coords[0]; int coordX = coords[0];
@ -287,7 +298,7 @@ public class GameLogic extends JPanel {
break; break;
case Coordinate.HIT: case Coordinate.HIT:
coordinatePanel.setBackground(new Color(91, 58, 41)); coordinatePanel.setBackground(new Color(91, 58, 41));
coordinatePanel.add(new JLabel("x"));
coordinatePanel.add(this.getXLabel());
break; break;
} }
@ -297,6 +308,7 @@ public class GameLogic extends JPanel {
// set game infos // set game infos
if (this.playerMove) { if (this.playerMove) {
System.out.println("own update");
if (this.gameState == GameLogic.PREPARATION) { if (this.gameState == GameLogic.PREPARATION) {
this.getMatchfieldValue().setText("Eigenes"); this.getMatchfieldValue().setText("Eigenes");
this.getStateValue().setText("Vorbereiten..."); this.getStateValue().setText("Vorbereiten...");
@ -305,8 +317,9 @@ public class GameLogic extends JPanel {
this.getStateValue().setText("Du bist am Zug"); this.getStateValue().setText("Du bist am Zug");
} }
} else { } else {
System.out.println("enemy update");
this.getMatchfieldValue().setText("Eigenes"); this.getMatchfieldValue().setText("Eigenes");
this.getStateValue().setText("Dein Gegner ist am Zug");
this.getStateValue().setText("Gegner am Zug");
} }
} }
@ -328,6 +341,7 @@ public class GameLogic extends JPanel {
public void start() { public void start() {
this.gameState = GameLogic.PREPARATION; this.gameState = GameLogic.PREPARATION;
this.playerMove = true;
// create player matchfield // create player matchfield
this.matchfield = new Matchfield(matchfieldSize); this.matchfield = new Matchfield(matchfieldSize);
@ -348,7 +362,7 @@ public class GameLogic extends JPanel {
this.aiLogic.setMatchfield(this.matchfield); this.aiLogic.setMatchfield(this.matchfield);
// show player own matchfield // show player own matchfield
gui.updateField(this.playerMove, this.matchfield);
gui.updateField(this.matchfield);
try { try {
Thread.sleep(3000); Thread.sleep(3000);
@ -365,10 +379,10 @@ public class GameLogic extends JPanel {
this.playerMove = playerMove; this.playerMove = playerMove;
if (this.playerMove) { if (this.playerMove) {
gui.updateField(this.playerMove, this.enemyMatchfield);
gui.updateField(this.enemyMatchfield);
// waiting for mouse click event.... // waiting for mouse click event....
} else { } else {
gui.updateField(this.playerMove, this.matchfield);
gui.updateField(this.matchfield);
try { try {
Thread.sleep(1000); Thread.sleep(1000);
@ -378,7 +392,9 @@ public class GameLogic extends JPanel {
Coordinate aiChosenField = this.aiLogic.chooseField(); Coordinate aiChosenField = this.aiLogic.chooseField();
aiChosenField.shoot(); aiChosenField.shoot();
gui.updateField(this.playerMove, this.matchfield);
gui.updateField(this.matchfield);
boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT;
try { try {
Thread.sleep(2000); Thread.sleep(2000);
@ -386,7 +402,7 @@ public class GameLogic extends JPanel {
e.printStackTrace(); e.printStackTrace();
} }
// this.nextMove();
this.nextMove(aiHasNextMove);
} }
} }
@ -398,8 +414,4 @@ public class GameLogic extends JPanel {
public boolean isPlayerMove() { public boolean isPlayerMove() {
return playerMove; return playerMove;
} }
public static void setGui(GameLogic gui) {
GameLogic.gui = gui;
}
} }

1
src/test/java/de/tims/fleetstorm/gui/LogicTest.java

@ -11,7 +11,6 @@ class LogicTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
GameLogic.setGui(new GameLogic(0, 0, 0));
guiLogic.start(); guiLogic.start();
} }

Loading…
Cancel
Save