sound
This commit is contained in:
parent
fdc7af9940
commit
bf15b4da2c
|
|
@ -1,2 +1 @@
|
|||
/windowInterface/
|
||||
/backend/
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -17,6 +17,9 @@ public class Board {
|
|||
private Integer selectedX = null;
|
||||
private Integer selectedY = null;
|
||||
|
||||
// Add Sound instance
|
||||
private Sound sound;
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
this.height = lineNum;
|
||||
|
|
@ -25,6 +28,7 @@ public class Board {
|
|||
this.pieces = new ArrayList<>();
|
||||
this.highlightedPositions = new ArrayList<>();
|
||||
this.boardHistory = new ArrayList<>();
|
||||
this.sound = new Sound(); // Initialize Sound instance
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
|
|
@ -175,7 +179,7 @@ public class Board {
|
|||
if (move.isValid()) {
|
||||
move.execute();
|
||||
|
||||
Sound.getInstance().playMoveSound();
|
||||
sound.playMoveSound(); // Use instance method
|
||||
|
||||
if (move.putsOpponentInCheckmate()) {
|
||||
System.out.println("Checkmate! " + (isWhiteTurn ? "Black" : "White") + " wins!");
|
||||
|
|
@ -234,6 +238,7 @@ public class Board {
|
|||
this.height = 8;
|
||||
this.pieces = new ArrayList<>();
|
||||
this.boardHistory = new ArrayList<>();
|
||||
this.sound = new Sound(); // Initialize Sound instance
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
if (y >= array.length) {
|
||||
|
|
@ -314,6 +319,9 @@ public class Board {
|
|||
|
||||
// Initialize board history for copy constructor
|
||||
this.boardHistory = new ArrayList<>();
|
||||
|
||||
// Initialize Sound instance (don't copy the sound instance)
|
||||
this.sound = new Sound();
|
||||
}
|
||||
|
||||
private void clearHighlights() {
|
||||
|
|
@ -346,8 +354,8 @@ public class Board {
|
|||
|
||||
move.execute();
|
||||
|
||||
// Play the move sound
|
||||
Sound.getInstance().playMoveSound();
|
||||
// Play the move sound using instance method
|
||||
sound.playMoveSound();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -386,4 +394,11 @@ public class Board {
|
|||
public boolean canUndo() {
|
||||
return !boardHistory.isEmpty();
|
||||
}
|
||||
|
||||
// Add cleanup method to properly dispose of sound resources
|
||||
public void cleanup() {
|
||||
if (sound != null) {
|
||||
sound.cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package backend;
|
||||
|
||||
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.Clip;
|
||||
|
|
@ -9,23 +8,14 @@ import javax.sound.sampled.UnsupportedAudioFileException;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public class Sound {
|
||||
private static Sound instance;
|
||||
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();
|
||||
}
|
||||
|
||||
public static Sound getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new Sound();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void loadSound() {
|
||||
try {
|
||||
File soundFile = new File(SOUND_FILE_PATH);
|
||||
|
|
@ -50,7 +40,6 @@ public class Sound {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void cleanup() {
|
||||
if (moveSound != null) {
|
||||
moveSound.close();
|
||||
|
|
|
|||
Loading…
Reference in New Issue