Browse Source

Implemented logic after ai made move and player made move

fleetstorm
Lorenz Hohmann 3 years ago
parent
commit
d472759a1c
  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 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;
}
private JLabel getXLabel() {
JLabel x = new JLabel("x");
x.setForeground(Color.WHITE);
return x;
}
private int[] getCoordinateFromLabel(JPanel panel) {
String panelName = panel.getName();
String[] panelNameSplit = panelName.split(":");
@ -265,7 +276,7 @@ public class GameLogic extends JPanel {
return new int[] { coordX, coordY };
}
private void updateField(boolean playerMove, Matchfield matchfield) {
private void updateField(Matchfield matchfield) {
for (JPanel coordinatePanel : this.fields) {
int[] coords = this.getCoordinateFromLabel(coordinatePanel);
int coordX = coords[0];
@ -287,7 +298,7 @@ public class GameLogic extends JPanel {
break;
case Coordinate.HIT:
coordinatePanel.setBackground(new Color(91, 58, 41));
coordinatePanel.add(new JLabel("x"));
coordinatePanel.add(this.getXLabel());
break;
}
@ -297,6 +308,7 @@ public class GameLogic extends JPanel {
// set game infos
if (this.playerMove) {
System.out.println("own update");
if (this.gameState == GameLogic.PREPARATION) {
this.getMatchfieldValue().setText("Eigenes");
this.getStateValue().setText("Vorbereiten...");
@ -305,8 +317,9 @@ public class GameLogic extends JPanel {
this.getStateValue().setText("Du bist am Zug");
}
} else {
System.out.println("enemy update");
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() {
this.gameState = GameLogic.PREPARATION;
this.playerMove = true;
// create player matchfield
this.matchfield = new Matchfield(matchfieldSize);
@ -348,7 +362,7 @@ public class GameLogic extends JPanel {
this.aiLogic.setMatchfield(this.matchfield);
// show player own matchfield
gui.updateField(this.playerMove, this.matchfield);
gui.updateField(this.matchfield);
try {
Thread.sleep(3000);
@ -365,10 +379,10 @@ public class GameLogic extends JPanel {
this.playerMove = playerMove;
if (this.playerMove) {
gui.updateField(this.playerMove, this.enemyMatchfield);
gui.updateField(this.enemyMatchfield);
// waiting for mouse click event....
} else {
gui.updateField(this.playerMove, this.matchfield);
gui.updateField(this.matchfield);
try {
Thread.sleep(1000);
@ -378,7 +392,9 @@ public class GameLogic extends JPanel {
Coordinate aiChosenField = this.aiLogic.chooseField();
aiChosenField.shoot();
gui.updateField(this.playerMove, this.matchfield);
gui.updateField(this.matchfield);
boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT;
try {
Thread.sleep(2000);
@ -386,7 +402,7 @@ public class GameLogic extends JPanel {
e.printStackTrace();
}
// this.nextMove();
this.nextMove(aiHasNextMove);
}
}
@ -398,8 +414,4 @@ public class GameLogic extends JPanel {
public boolean isPlayerMove() {
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
void setup() {
GameLogic.setGui(new GameLogic(0, 0, 0));
guiLogic.start();
}

Loading…
Cancel
Save