Compare commits

...

4 Commits

11 changed files with 60 additions and 22 deletions

BIN
Sound/hit.mp3 Normal file

Binary file not shown.

BIN
Sound/plop.mp3 Normal file

Binary file not shown.

BIN
Sound/shipdestroyed.mp3 Normal file

Binary file not shown.

View File

@ -42,6 +42,19 @@ public class Board {
return response;
}
private void propagateSunk(Point p) {
HitResponse hit = this.getHitResponseOnPoint(p);
if (hit == null || hit.getType() != HitResponseType.HIT) return;
hit.setType(HitResponseType.SUNK);
propagateSunk(new Point(p.getX() + 1, p.getY()));
propagateSunk(new Point(p.getX() - 1, p.getY()));
propagateSunk(new Point(p.getX(), p.getY() + 1));
propagateSunk(new Point(p.getX(), p.getY() - 1));
}
private void createShip(int semester){
List<ShipData> shipData = Ship.semeterList.get(semester -1);
for (int i = 0; i < shipData.size(); i++) {
@ -54,14 +67,21 @@ public class Board {
}
public synchronized boolean addHits(HitResponse hitResponse) {
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){
this.hits.add(hitResponse);
//Propagate sunk for display purposes
if (hitResponse.getType() == HitResponseType.SUNK) {
hitResponse.setType(HitResponseType.HIT);
propagateSunk(hitResponse.getPoint());
}
return true;
}
return false;
}
public synchronized HitResponse getHitResponsOnPoint(Point point) {
public synchronized HitResponse getHitResponseOnPoint(Point point) {
for (int i = 0; i < this.hits.size(); i++){
if (this.hits.get(i).getPoint().equals(point)){
return this.hits.get(i);

View File

@ -63,7 +63,7 @@ public class BoardDisplay extends JPanel {
add(rowLabel);
} else {
// Spielfeld (interaktive Zellen)
JButton field = new JButton(x + ", " + y);
JButton field = new JButton("");
field.setBackground(Color.BLUE);
field.setOpaque(true);
field.setBorderPainted(true);
@ -102,6 +102,7 @@ public class BoardDisplay extends JPanel {
} else if (SwingUtilities.isLeftMouseButton(e)) {
Point o = new Point(x, y);
handleFieldClick(o);
SoundHandler.playSound("plop");
}
}
});
@ -196,30 +197,46 @@ public class BoardDisplay extends JPanel {
fields[i][j].setEnabled(true);
}
}
if (this.enemyBoard) {
fields[i][j].setBackground(Color.WHITE);
} else {
fields[i][j].setBackground(Color.BLUE);
}
if (!this.player.isReady()) {
for(Point p : test) {
if(i==p.getX() && j==p.getY() && currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) {
if(i==p.getX() && j==p.getY()) {
if (currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) {
fields[i][j].setBackground(Color.GREEN);
}else if(i==p.getX() && j==p.getY() && !currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) {
} else {
fields[i][j].setBackground(Color.RED);
}
}
}
}
for(Ship ship: player.getBoard().getShips()) {
if(ship.isShipOnPos(new Point(i,j))) {
fields[i][j].setBackground(Color.LIGHT_GRAY);
break;
}
}
HitResponse hit = this.player.getBoard().getHitResponsOnPoint(new Point(i, j));
HitResponse hit = this.player.getBoard().getHitResponseOnPoint(new Point(i, j));
if (hit != null) {
switch (hit.getType()) {
case HIT:
fields[i][j].setBackground(Color.ORANGE);
SoundHandler.playSound("hit");
break;
case SUNK:
case VICTORY:
fields[i][j].setBackground(Color.ORANGE);
SoundHandler.playSound("destroyed");
fields[i][j].setBackground(Color.RED);
break;
case MISS:
if (this.enemyBoard) {
SoundHandler.playSound("miss");
fields[i][j].setBackground(Color.BLUE);
} else {
fields[i][j].setBackground(Color.CYAN);
}
break;
}
}

View File

@ -7,8 +7,8 @@ public class HalloSchiffeVersenken {
System.out.println("HelloSchiffeVersenekn");
System.out.println("sound");
SoundHandler.playSound("hit");
//System.out.println("sound");
//SoundHandler.playSound("hit");
Thread.sleep(10000);

View File

@ -8,8 +8,6 @@ import java.net.InetSocketAddress;
/**
* Das JoinGame Panel dient zum setzten des Ports/IP-Adresse.
* Anschließend kann das Verbinden Panel gezeigt werden.
* 51525
* ssh.lgc.sh
*/
public class JoinGame extends JPanel {
// Grafiken

View File

@ -10,7 +10,7 @@ public class LocalPlayer extends Player {
@Override
public synchronized void receiveShoot(Point point) {
HitResponse hitResponse = board.getHitResponsOnPoint(point);
HitResponse hitResponse = board.getHitResponseOnPoint(point);
if (!(hitResponse == null)){
enemy.receiveHit(hitResponse);

View File

@ -9,7 +9,7 @@ public class Point {
public Point (String str) {
if (Point.isValidSyntax(str)) {
this.setX(str.charAt(0));
this.setY(Integer.parseInt(str.substring(1)));
this.setY(Integer.parseInt(str.substring(1)) - 1);
} else {
throw new IllegalArgumentException("String ist keine gültige Koordinate");
}
@ -17,7 +17,7 @@ public class Point {
@Override
public String toString() {
return (char) ('A' + this.x) + String.valueOf(this.y);
return (char) ('A' + this.x) + String.valueOf(this.y + 1);
}
public int getX() {

View File

@ -16,7 +16,10 @@ public class SoundHandler {
// Wenn fehler beim erstellen von .jar mit sound hier gucken
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
"hit", "./Sound/water-drip.mp3"
"miss", "./Sound/water-drip.mp3",
"hit", "./Sound/hit.mp3",
"destroyed", "./Sound/hit.mp3",
"plop", "./Sound/plop.mp3"
));
public static void playSound(String soundName) {

View File

@ -15,7 +15,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
// Shoot at the enemy and receive the hit response
enemy.receiveShoot(nextShot);
HitResponse hitResponse = enemy.board.getHitResponsOnPoint(nextShot);
HitResponse hitResponse = enemy.board.getHitResponseOnPoint(nextShot);
// If it's a hit or sunk, add adjacent cells to the hitsQueue
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
addAdjacentPoints(nextShot);