This commit is contained in:
romca 2025-05-23 09:57:49 +02:00
parent fdc7af9940
commit bf15b4da2c
6 changed files with 20 additions and 17 deletions

View File

@ -1,2 +1 @@
/windowInterface/
/backend/ /backend/

Binary file not shown.

Binary file not shown.

View File

@ -17,6 +17,9 @@ public class Board {
private Integer selectedX = null; private Integer selectedX = null;
private Integer selectedY = null; private Integer selectedY = null;
// Add Sound instance
private Sound sound;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
this.height = lineNum; this.height = lineNum;
@ -25,6 +28,7 @@ public class Board {
this.pieces = new ArrayList<>(); this.pieces = new ArrayList<>();
this.highlightedPositions = new ArrayList<>(); this.highlightedPositions = new ArrayList<>();
this.boardHistory = new ArrayList<>(); this.boardHistory = new ArrayList<>();
this.sound = new Sound(); // Initialize Sound instance
} }
public int getWidth() { public int getWidth() {
@ -175,7 +179,7 @@ public class Board {
if (move.isValid()) { if (move.isValid()) {
move.execute(); move.execute();
Sound.getInstance().playMoveSound(); sound.playMoveSound(); // Use instance method
if (move.putsOpponentInCheckmate()) { if (move.putsOpponentInCheckmate()) {
System.out.println("Checkmate! " + (isWhiteTurn ? "Black" : "White") + " wins!"); System.out.println("Checkmate! " + (isWhiteTurn ? "Black" : "White") + " wins!");
@ -234,6 +238,7 @@ public class Board {
this.height = 8; this.height = 8;
this.pieces = new ArrayList<>(); this.pieces = new ArrayList<>();
this.boardHistory = new ArrayList<>(); this.boardHistory = new ArrayList<>();
this.sound = new Sound(); // Initialize Sound instance
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
if (y >= array.length) { if (y >= array.length) {
@ -314,6 +319,9 @@ public class Board {
// Initialize board history for copy constructor // Initialize board history for copy constructor
this.boardHistory = new ArrayList<>(); this.boardHistory = new ArrayList<>();
// Initialize Sound instance (don't copy the sound instance)
this.sound = new Sound();
} }
private void clearHighlights() { private void clearHighlights() {
@ -346,8 +354,8 @@ public class Board {
move.execute(); move.execute();
// Play the move sound // Play the move sound using instance method
Sound.getInstance().playMoveSound(); sound.playMoveSound();
} }
} }
@ -386,4 +394,11 @@ public class Board {
public boolean canUndo() { public boolean canUndo() {
return !boardHistory.isEmpty(); return !boardHistory.isEmpty();
} }
// Add cleanup method to properly dispose of sound resources
public void cleanup() {
if (sound != null) {
sound.cleanup();
}
}
} }

View File

@ -1,6 +1,5 @@
package backend; package backend;
import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip; import javax.sound.sampled.Clip;
@ -9,23 +8,14 @@ import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
public class Sound { public class Sound {
private static Sound instance;
private Clip moveSound; private Clip moveSound;
private static final String SOUND_FILE_PATH = "movesound.wav"; private final String SOUND_FILE_PATH = "movesound.wav";
private Sound() { public Sound() {
loadSound(); loadSound();
} }
public static Sound getInstance() {
if (instance == null) {
instance = new Sound();
}
return instance;
}
private void loadSound() { private void loadSound() {
try { try {
File soundFile = new File(SOUND_FILE_PATH); File soundFile = new File(SOUND_FILE_PATH);
@ -49,7 +39,6 @@ public class Sound {
moveSound.start(); moveSound.start();
} }
} }
public void cleanup() { public void cleanup() {
if (moveSound != null) { if (moveSound != null) {