|
@ -35,6 +35,7 @@ public class GameLogic extends JPanel { |
|
|
public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) { |
|
|
public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) { |
|
|
this.playerFields = new ArrayList<>(); |
|
|
this.playerFields = new ArrayList<>(); |
|
|
this.enemyFields = new ArrayList<>(); |
|
|
this.enemyFields = new ArrayList<>(); |
|
|
|
|
|
|
|
|
setSize(640, 480); |
|
|
setSize(640, 480); |
|
|
|
|
|
|
|
|
JPanel fieldWrapper = new JPanel(); |
|
|
JPanel fieldWrapper = new JPanel(); |
|
@ -64,8 +65,9 @@ public class GameLogic extends JPanel { |
|
|
|
|
|
|
|
|
private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween, |
|
|
private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween, |
|
|
JPanel panel, ArrayList<JPanel> fields, boolean registerMouseListener) { |
|
|
JPanel panel, ArrayList<JPanel> fields, boolean registerMouseListener) { |
|
|
for (int x = 0; x < 10; x++) { |
|
|
|
|
|
for (int y = 0; y < 10; y++) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < this.matchfieldSize; x++) { |
|
|
|
|
|
for (int y = 0; y < this.matchfieldSize; y++) { |
|
|
JPanel field = new JPanel(); |
|
|
JPanel field = new JPanel(); |
|
|
|
|
|
|
|
|
int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween); |
|
|
int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween); |
|
@ -74,17 +76,16 @@ public class GameLogic extends JPanel { |
|
|
field.setBounds(xPos, yPos, fieldWidth, fieldWidth); |
|
|
field.setBounds(xPos, yPos, fieldWidth, fieldWidth); |
|
|
field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); |
|
|
field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); |
|
|
field.setName(x + ":" + y); |
|
|
field.setName(x + ":" + y); |
|
|
|
|
|
|
|
|
fields.add(field); |
|
|
fields.add(field); |
|
|
panel.add(field); |
|
|
panel.add(field); |
|
|
|
|
|
|
|
|
if (registerMouseListener) { |
|
|
|
|
|
|
|
|
if (!registerMouseListener) |
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
field.addMouseListener(new MouseAdapter() { |
|
|
field.addMouseListener(new MouseAdapter() { |
|
|
|
|
|
|
|
|
public void mouseClicked(MouseEvent e) { |
|
|
public void mouseClicked(MouseEvent e) { |
|
|
System.out.println("mouse clicked"); |
|
|
|
|
|
if (!playerMove) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
int[] coords = getCoordinateFromLabel(field); |
|
|
int[] coords = getCoordinateFromLabel(field); |
|
|
int coordX = coords[0]; |
|
|
int coordX = coords[0]; |
|
|
int coordY = coords[1]; |
|
|
int coordY = coords[1]; |
|
@ -92,7 +93,6 @@ public class GameLogic extends JPanel { |
|
|
Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); |
|
|
Coordinate chosenField = enemyMatchfield.getField(coordX, coordY); |
|
|
|
|
|
|
|
|
boolean shotSuccess = chosenField.shoot(); |
|
|
boolean shotSuccess = chosenField.shoot(); |
|
|
gui.updateFields(); |
|
|
|
|
|
|
|
|
|
|
|
if (shotSuccess && chosenField.getState() != Coordinate.HIT) { |
|
|
if (shotSuccess && chosenField.getState() != Coordinate.HIT) { |
|
|
nextMove(false); |
|
|
nextMove(false); |
|
@ -102,19 +102,14 @@ public class GameLogic extends JPanel { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public ArrayList<JPanel> getFields() { |
|
|
|
|
|
return playerFields; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private JLabel getXLabel() { |
|
|
private JLabel getXLabel() { |
|
|
JLabel x = new JLabel("x"); |
|
|
JLabel x = new JLabel("x"); |
|
|
x.setForeground(Color.WHITE); |
|
|
x.setForeground(Color.WHITE); |
|
|
|
|
|
|
|
|
return x; |
|
|
return x; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -185,7 +180,7 @@ public class GameLogic extends JPanel { |
|
|
this.gameState = GameLogic.PREPARATION; |
|
|
this.gameState = GameLogic.PREPARATION; |
|
|
this.playerMove = true; |
|
|
this.playerMove = true; |
|
|
|
|
|
|
|
|
// create player matchfield |
|
|
|
|
|
|
|
|
// create player matchfield and set ships |
|
|
this.matchfield = new Matchfield(matchfieldSize); |
|
|
this.matchfield = new Matchfield(matchfieldSize); |
|
|
this.matchfield.createMatchfield(); |
|
|
this.matchfield.createMatchfield(); |
|
|
this.matchfield.setShip(new Coordinate(4, 6), 2, 0); |
|
|
this.matchfield.setShip(new Coordinate(4, 6), 2, 0); |
|
@ -193,15 +188,17 @@ public class GameLogic extends JPanel { |
|
|
this.matchfield.setShip(new Coordinate(9, 1), 4, 1); |
|
|
this.matchfield.setShip(new Coordinate(9, 1), 4, 1); |
|
|
this.matchfield.setShip(new Coordinate(0, 0), 5, 0); |
|
|
this.matchfield.setShip(new Coordinate(0, 0), 5, 0); |
|
|
|
|
|
|
|
|
// create enemy matchfield and initialize AI (with player's matchfield) |
|
|
|
|
|
|
|
|
this.aiLogic = new Logic(); |
|
|
|
|
|
this.aiLogic.setMatchfield(this.matchfield); |
|
|
|
|
|
|
|
|
|
|
|
// create enemy matchfield, set ships and initialize AI (with player's |
|
|
|
|
|
// matchfield) |
|
|
this.enemyMatchfield = new Matchfield(matchfieldSize); |
|
|
this.enemyMatchfield = new Matchfield(matchfieldSize); |
|
|
this.enemyMatchfield.createMatchfield(); |
|
|
this.enemyMatchfield.createMatchfield(); |
|
|
this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); |
|
|
this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); |
|
|
this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); |
|
|
this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); |
|
|
this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); |
|
|
this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); |
|
|
this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); |
|
|
this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); |
|
|
this.aiLogic = new Logic(); |
|
|
|
|
|
this.aiLogic.setMatchfield(this.matchfield); |
|
|
|
|
|
|
|
|
|
|
|
// enter game loop |
|
|
// enter game loop |
|
|
this.gameState = GameLogic.RUNNING; |
|
|
this.gameState = GameLogic.RUNNING; |
|
@ -213,7 +210,7 @@ public class GameLogic extends JPanel { |
|
|
|
|
|
|
|
|
if (this.playerMove) { |
|
|
if (this.playerMove) { |
|
|
gui.updateFields(); |
|
|
gui.updateFields(); |
|
|
// waiting for mouse click event.... |
|
|
|
|
|
|
|
|
// waiting here for mouse click event.... |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
|
|
|
Coordinate aiChosenField = null; |
|
|
Coordinate aiChosenField = null; |
|
@ -221,12 +218,11 @@ public class GameLogic extends JPanel { |
|
|
aiChosenField = this.aiLogic.chooseField(); |
|
|
aiChosenField = this.aiLogic.chooseField(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
aiChosenField.print(); |
|
|
|
|
|
aiChosenField.shoot(); |
|
|
aiChosenField.shoot(); |
|
|
|
|
|
|
|
|
gui.updateFields(); |
|
|
gui.updateFields(); |
|
|
|
|
|
|
|
|
boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; |
|
|
boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; |
|
|
|
|
|
|
|
|
this.nextMove(aiHasNextMove); |
|
|
this.nextMove(aiHasNextMove); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -235,8 +231,4 @@ public class GameLogic extends JPanel { |
|
|
public int getGameState() { |
|
|
public int getGameState() { |
|
|
return gameState; |
|
|
return gameState; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public boolean isPlayerMove() { |
|
|
|
|
|
return playerMove; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |