Hallo ich möchte, dass ein bestimmte Sound bei einem Zug des Spielers erklingt, und ein bestimmter Sound für den Zug des Computers. Ich habe nun für beide eine separate Methode geschrieben. playerSound und computerSound. Diese beiden Methoden sind aber ziemlich lang. Gibt es eine Möglichkeit, wie ich diese Methoden um ein einiges verkleinern kann.
public class Sound {
public static int playerSound (char[][] gameBoard) {
try (AudioInputStream inStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("Spieler x.wav")) ){
Clip clip = AudioSystem.getClip();
clip.open(inStream);
clip.start();
} catch (LineUnavailableException lue) {
System.out.println("Spieler x.wav" + lue.getMessage());
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
System.out.println("Spieler x.wav" + uafe.getMessage());
uafe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Spieler x.wav" + ioe.getMessage());
ioe.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
return 0;
}
public static int computerSound (char[][] gameBoard) {
try (AudioInputStream inStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("Bot.wav")) ){
Clip clip = AudioSystem.getClip();
clip.open(inStream);
clip.start();
} catch (LineUnavailableException lue) {
System.out.println("Bot.wav" + lue.getMessage());
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
System.out.println("Bot.wav" + uafe.getMessage());
uafe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Bot.wav" + ioe.getMessage());
ioe.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
return 0;
}
public class Sound {
public static int playerSound (char[][] gameBoard) {
try (AudioInputStream inStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("Spieler x.wav")) ){
Clip clip = AudioSystem.getClip();
clip.open(inStream);
clip.start();
} catch (LineUnavailableException lue) {
System.out.println("Spieler x.wav" + lue.getMessage());
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
System.out.println("Spieler x.wav" + uafe.getMessage());
uafe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Spieler x.wav" + ioe.getMessage());
ioe.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
return 0;
}
public static int computerSound (char[][] gameBoard) {
try (AudioInputStream inStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("Bot.wav")) ){
Clip clip = AudioSystem.getClip();
clip.open(inStream);
clip.start();
} catch (LineUnavailableException lue) {
System.out.println("Bot.wav" + lue.getMessage());
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
System.out.println("Bot.wav" + uafe.getMessage());
uafe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Bot.wav" + ioe.getMessage());
ioe.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
return 0;
}