Skip to content

Commit

Permalink
Fix hangup issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdj20 committed Apr 10, 2017
1 parent 3a26a8f commit db2f4ea
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/main/java/cowards/HnefataflPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import javax.swing.*;
import javax.swing.filechooser.FileView;

Expand All @@ -16,6 +17,9 @@ public class HnefataflPanel extends JPanel {
private Rectangle loadGame;
private Rectangle exitGame;

// Keeps the AI and player from stepping on one another's toes.
private static Semaphore aiSem = new Semaphore(1);

/**
Constructor.
*/
Expand All @@ -42,22 +46,32 @@ public void run() {
}
}, 50, 1000 / 30);

doAiMove();
Thread thread = new Thread(HnefataflPanel::doAiMove);
thread.start();
}

/**
Does an AI move.
Does an AI move. This method should only be launched as a separate thread.
This method is not represented in the tests because in this format it is
untestable.
*/
public static void doAiMove() {
// TODO: Make AI optional
try {
// To keep the frames updating smoothly.
if (!aiSem.tryAcquire()) {
return;
}

// We can only afford to look ahead 2 moves due to the timers.
// Note the thinking time will differ depending on CPU speed.
int [] choice = Hnefalump.getNextMove(board, 2);
if (choice == null) {
aiSem.release();
return;
}
board.select(choice[2], choice[3]);
board.move(choice[0], choice[1]);
aiSem.release();
} catch (GridOutOfBoundsException gx) {
JOptionPane.showMessageDialog(null, "Critical: AI out of bounds.");
}
Expand All @@ -80,21 +94,26 @@ public void mouseReleaseEvent(MouseEvent event) {
}

try {
aiSem.acquire();
if (board.hasSelection()) {
// Attempt to move. If it fails, try changing the selection.
if (!board.move(row, col)) {
board.select(row, col);
} else {
// TODO: Make AI optional
doAiMove();
Thread thread = new Thread(HnefataflPanel::doAiMove);
thread.start();
}
} else {
board.select(row, col);
}
aiSem.release();
} catch (GridOutOfBoundsException ex) {
// Something went wrong with the geometry of the board painted.
// Since this should not happen, just log a warning.
System.out.println("WARNING: The board geometry is not synced");
} catch (InterruptedException ix) {
// Not much we can do here.
}
} else if (newGame != null && newGame.contains(event.getPoint())) {
board.pauseTimers();
Expand All @@ -106,7 +125,8 @@ public void mouseReleaseEvent(MouseEvent event) {
board = new Board();

// TODO: Make AI optional
doAiMove();
Thread thread = new Thread(HnefataflPanel::doAiMove);
thread.start();
} catch (BadAsciiBoardFormatException bx) {
JOptionPane.showMessageDialog(null, "Critical: Cannot load initial board.");
System.exit(1);
Expand Down

0 comments on commit db2f4ea

Please sign in to comment.