Changed cpu mode from string to enum
This commit is contained in:
parent
c02c98f11f
commit
0e7a771cf3
|
@ -196,17 +196,17 @@ public class Chess {
|
|||
JCheckBoxMenuItem blackAutomaticSmart = new JCheckBoxMenuItem("Black / Automatic smart");
|
||||
|
||||
Runnable updateWhite = () -> {
|
||||
String cpu = chessboard.getPlayer1().getCPU();
|
||||
CPUMode cpu = chessboard.getPlayer1().getCPU();
|
||||
whiteDisabled.setState(cpu == null);
|
||||
whiteAutomaticSmart.setState(cpu != null && cpu.equals("smart"));
|
||||
whiteAutomaticRandom.setState(cpu != null && cpu.equals("random"));
|
||||
whiteAutomaticSmart.setState(cpu == CPUMode.SMART);
|
||||
whiteAutomaticRandom.setState(cpu == CPUMode.RANDOM);
|
||||
};
|
||||
|
||||
Runnable updateBlack = () -> {
|
||||
String cpu = chessboard.getPlayer2().getCPU();
|
||||
CPUMode cpu = chessboard.getPlayer2().getCPU();
|
||||
blackDisabled.setState(cpu == null);
|
||||
blackAutomaticSmart.setState(cpu != null && cpu.equals("smart"));
|
||||
blackAutomaticRandom.setState(cpu != null && cpu.equals("random"));
|
||||
blackAutomaticSmart.setState(cpu == CPUMode.SMART);
|
||||
blackAutomaticRandom.setState(cpu == CPUMode.RANDOM);
|
||||
};
|
||||
|
||||
whiteDisabled.addActionListener(l -> {
|
||||
|
@ -216,13 +216,13 @@ public class Chess {
|
|||
menuCPU.add(whiteDisabled);
|
||||
|
||||
whiteAutomaticRandom.addActionListener(l -> {
|
||||
setCPU(chessboard.getPlayer1(), "random");
|
||||
setCPU(chessboard.getPlayer1(), CPUMode.RANDOM);
|
||||
updateWhite.run();
|
||||
});
|
||||
menuCPU.add(whiteAutomaticRandom);
|
||||
|
||||
whiteAutomaticSmart.addActionListener(l -> {
|
||||
setCPU(chessboard.getPlayer1(), "smart");
|
||||
setCPU(chessboard.getPlayer1(), CPUMode.SMART);
|
||||
updateWhite.run();
|
||||
});
|
||||
menuCPU.add(whiteAutomaticSmart);
|
||||
|
@ -236,13 +236,13 @@ public class Chess {
|
|||
menuCPU.add(blackDisabled);
|
||||
|
||||
blackAutomaticRandom.addActionListener(l -> {
|
||||
setCPU(chessboard.getPlayer2(), "random");
|
||||
setCPU(chessboard.getPlayer2(), CPUMode.RANDOM);
|
||||
updateBlack.run();
|
||||
});
|
||||
menuCPU.add(blackAutomaticRandom);
|
||||
|
||||
blackAutomaticSmart.addActionListener(l -> {
|
||||
setCPU(chessboard.getPlayer2(), "smart");
|
||||
setCPU(chessboard.getPlayer2(), CPUMode.SMART);
|
||||
updateBlack.run();
|
||||
});
|
||||
menuCPU.add(blackAutomaticSmart);
|
||||
|
@ -277,10 +277,10 @@ public class Chess {
|
|||
/**
|
||||
* Set player's CPU mode
|
||||
* @param player selected player
|
||||
* @param mode null, "random" or "smart"
|
||||
* @param mode CPU mode or null
|
||||
*/
|
||||
public static void setCPU(Player player, String mode) {
|
||||
if(mode != null && mode.equals("smart")) {
|
||||
public static void setCPU(Player player, CPUMode mode) {
|
||||
if(mode == CPUMode.SMART) {
|
||||
String binary = Stockfish.findBinary();
|
||||
if(binary == null) {
|
||||
JOptionPane.showMessageDialog(chessboard, "Stockfish engine not found! (stockfishchess.org)\nDownload 'stockfish' or 'stockfish.exe' to the program's folder or the system's path.");
|
||||
|
|
|
@ -28,7 +28,7 @@ public class Player {
|
|||
/**
|
||||
* Active CPU mode
|
||||
*/
|
||||
private String cpu;
|
||||
private CPUMode cpu;
|
||||
|
||||
/**
|
||||
* Waiting for CPU move
|
||||
|
@ -87,8 +87,8 @@ public class Player {
|
|||
@Override
|
||||
public void run() {
|
||||
if (cpu != null) {
|
||||
if (cpu.equals("random")) randomMove();
|
||||
if (cpu.equals("smart")) smartMove();
|
||||
if (cpu == CPUMode.RANDOM) randomMove();
|
||||
if (cpu == CPUMode.SMART) smartMove();
|
||||
}
|
||||
isPlaying = false;
|
||||
t.cancel();
|
||||
|
@ -209,7 +209,7 @@ public class Player {
|
|||
* Set player's CPU mode
|
||||
* @param cpu CPU mode
|
||||
*/
|
||||
public void setCPU(String cpu) {
|
||||
public void setCPU(CPUMode cpu) {
|
||||
// TODO: use enums
|
||||
this.cpu = cpu;
|
||||
if(chessboard.getActivePlayer() == this && !isPlaying) play();
|
||||
|
@ -219,7 +219,7 @@ public class Player {
|
|||
* Get player's CPU mode
|
||||
* @return CPU mode
|
||||
*/
|
||||
public String getCPU() {
|
||||
public CPUMode getCPU() {
|
||||
return cpu;
|
||||
}
|
||||
|
||||
|
@ -240,3 +240,9 @@ public class Player {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
enum CPUMode {
|
||||
|
||||
RANDOM, SMART;
|
||||
|
||||
}
|
Loading…
Reference in a new issue