Commit 3dadd3ab authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Working on concurrency issues.

parent fdaced20
Loading
Loading
Loading
Loading
+17 −7
Original line number Diff line number Diff line
@@ -12,8 +12,9 @@
    <option name="autoReloadType" value="SELECTIVE" />
  </component>
  <component name="ChangeListManager">
    <list default="true" id="78671b20-6a1e-42c8-be28-50b7f38fa7ff" name="Default Changelist" comment="">
    <list default="true" id="78671b20-6a1e-42c8-be28-50b7f38fa7ff" name="Default Changelist" comment="Working on concurrency issues.">
      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
      <change beforePath="$PROJECT_DIR$/src/edu/bu/ec504/spr24/brain/Brain.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/edu/bu/ec504/spr24/brain/Brain.java" afterDir="false" />
      <change beforePath="$PROJECT_DIR$/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java" afterDir="false" />
    </list>
    <option name="SHOW_DIALOG" value="false" />
@@ -45,7 +46,7 @@
  </component>
  <component name="PropertiesComponent"><![CDATA[{
  "keyToString": {
    "Application.EC504 SameGameTris.executor": "Debug",
    "Application.EC504 SameGameTris.executor": "Run",
    "RunOnceActivity.OpenProjectViewOnStart": "true",
    "RunOnceActivity.ShowReadmeOnStart": "true",
    "WebServerToolWindowFactoryState": "false",
@@ -108,7 +109,7 @@
      <workItem from="1706042051986" duration="6820000" />
      <workItem from="1706057188033" duration="218000" />
      <workItem from="1706070762735" duration="2123000" />
      <workItem from="1706112889389" duration="1257000" />
      <workItem from="1706112889389" duration="6798000" />
    </task>
    <task id="LOCAL-00001" summary="Apparently working version.">
      <created>1580084914114</created>
@@ -163,7 +164,15 @@
      <option name="project" value="LOCAL" />
      <updated>1706051180665</updated>
    </task>
    <option name="localTasksCounter" value="8" />
    <task id="LOCAL-00008" summary="Working on concurrency issues.">
      <option name="closed" value="true" />
      <created>1706114342828</created>
      <option name="number" value="00008" />
      <option name="presentableId" value="LOCAL-00008" />
      <option name="project" value="LOCAL" />
      <updated>1706114342829</updated>
    </task>
    <option name="localTasksCounter" value="9" />
    <servers />
  </component>
  <component name="TypeScriptGeneratedFilesManager">
@@ -176,15 +185,16 @@
    <MESSAGE value="Cleaning up." />
    <MESSAGE value="Working version ..." />
    <MESSAGE value="Nice and clean ..." />
    <option name="LAST_COMMIT_MESSAGE" value="Nice and clean ..." />
    <MESSAGE value="Working on concurrency issues." />
    <option name="LAST_COMMIT_MESSAGE" value="Working on concurrency issues." />
  </component>
  <component name="XDebuggerManager">
    <breakpoint-manager>
      <breakpoints>
        <line-breakpoint enabled="true" type="java-line">
          <url>file://$PROJECT_DIR$/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java</url>
          <line>143</line>
          <option name="timeStamp" value="1" />
          <line>626</line>
          <option name="timeStamp" value="3" />
        </line-breakpoint>
      </breakpoints>
    </breakpoint-manager>
+10 −1
Original line number Diff line number Diff line
@@ -11,6 +11,12 @@ import edu.bu.ec504.spr24.sameGameTris.SameGameTris;
 */
public abstract class Brain implements Runnable {

	// CONSTANTS
	/**
	 * The number of milliseconds between Brain moves
	 */
	private static final long BRAIN_WAIT_MS = 1000;

	// FIELDS
	protected final GUI myGUI; // stores the GUI class attached to this Brain
	/**
@@ -41,6 +47,9 @@ public abstract class Brain implements Runnable {
		while (!allDone && !myGUI.gameOverQ()) {
			Board.Pos theNextMove = nextMove();
			myGUI.makeMove(theNextMove.xx, theNextMove.yy); // i.e. click on the lower left corner
      try {
        Thread.sleep(BRAIN_WAIT_MS);
      } catch (InterruptedException ignored) {}
    }
	}

+36 −22
Original line number Diff line number Diff line
package edu.bu.ec504.spr24.sameGameTris;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.Serial;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
@@ -70,9 +72,10 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
  static public final int NUM_COLORS = 3;

  /**
   * The number of milliseconds to display (highlight) a move before actually making it.
   * The number of milliseconds to wait after selecting a region before disappearing it.
   * This allows the user to see the region being selected
   */
  static private final int DISPLAY_TIME_MS = 1000;
  static private final int DISPLAY_WAIT_MS = 700;

  /**
   * The file where high scores are stored.
@@ -271,21 +274,14 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
    TimerTask tt = new TimerTask() {
      @Override
      public void run() {
        circles[xx][yy].mouseExited(null);
        circles[xx][yy].mousePressed(null);
        circles[xx][yy].mouseReleased(
        final SelfAwareCircle theCircle = circles[xx][yy];
        theCircle.mouseExited(null);
        theCircle.mousePressed(null);
        theCircle.mouseReleased(
            null); // pretend that the mouse button was released at the location
        synchronized(brainThread) {
          brainThread.notify(); // restart thinking
        }
      }
    };
    t.schedule(tt, DISPLAY_TIME_MS);
    synchronized(brainThread) {
      try {
        brainThread.wait();  // pause thinking between moves
      } catch (InterruptedException ignored) {}
    }
    t.schedule(tt, DISPLAY_WAIT_MS);
  }

  /**
@@ -432,7 +428,7 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
   * Cancel schedules for adding / dropping circles.
   */
  private void cancelTetrisingSchedules() {
    if (produceCircleFuture != null)
    if (produceCircleFuture != null) {
      produceCircleFuture.cancel(true);
    if (dropCircleFuture != null)
      dropCircleFuture.cancel(true);
@@ -602,12 +598,12 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {

    // check the high score
    if (hs.newRecordQ(score)) { // i.e. a new record
      JOptionPane.showMessageDialog(null,
      _showMessage(null,
          "Game Over (" + feedback + ") - You got a new high score of " + score + " points!\n" +
              "Your weighted score for this sized board was " + highScore.hsComp.weightedScore(
              new highScore.HSdata("", width, height, score)) + ".");
      if (highScoreName == null)
        highScoreName = JOptionPane.showInputDialog(null,
        highScoreName = _showInput(null,
            "You got a new high score!\nPlease enter your name:");

      if (highScoreName != null) { // i.e. the user is interested in high scores
@@ -615,20 +611,38 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
        highScore.HSdata datum = new highScore.HSdata(highScoreName, width, height, score);
        hs.putScore(datum); // enter the name into the high score list
      }
    } else
      JOptionPane.showMessageDialog(null,
    } else {
      _showMessage(null,
          "Game Over (" + feedback + ") - You did not make the high score.  You had " + score
              + " points.\n" +
              "Your weighted score for this sized board was " + highScore.hsComp.weightedScore(
              new highScore.HSdata("", width, height, score)) + ".");

    JOptionPane.showMessageDialog(null, "Current high scores:\n" + hs.display());
    }
    _showMessage(null, "Current high scores:\n" + hs.display());

    // i.e. time for a new game
    cleanUp();
    setupGUI();
  }

  /**
   * Shows a message on the parent component and waits for the user to click "OK".
   * @param parent The component on which to show the dialog box.
   * @param message The message to display.
   */
  private void _showMessage(Component parent, String message) {
   JOptionPane.showMessageDialog(parent, message);
  }

  /**
   * Shows an input request on the parent component and returns the user's input.
   * @param parent The component on which to show the dialog box.
   * @param message The input request
   */
  private String _showInput(Component parent, String message) {
    return JOptionPane.showInputDialog(parent, message);
  }

  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();