diff --git a/.idea/misc.xml b/.idea/misc.xml
index 42fa23dbe26c042d6ef473dba9b8e9424109e73d..865b294f3b7ff03d520ce4f4ffeeebf55f45ba7b 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -5,7 +5,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d99de8d5abe9465c7f6750fab05b2f1be10f196d..7679cf52fab5030c48f7172ea8a82b57cf84b419 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,9 +5,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -24,24 +40,35 @@
+
+
+
+ {
+ "associatedIndex": 0
+}
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
@@ -49,13 +76,14 @@
-
+
+
@@ -68,6 +96,8 @@
1548370618181
+
+ 1580084914114
@@ -94,7 +124,7 @@
-
+
@@ -102,102 +132,4 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/edu/bu/ec504/spr19/Brain/Brain.java b/src/edu/bu/ec504/spr24/Brain/Brain.java
similarity index 80%
rename from src/edu/bu/ec504/spr19/Brain/Brain.java
rename to src/edu/bu/ec504/spr24/Brain/Brain.java
index f3a3d154e2889d46a2d0008975efa2782cfc08ec..63da5cfff3a9134f0e3d1e38385c0cd932ce26e7 100644
--- a/src/edu/bu/ec504/spr19/Brain/Brain.java
+++ b/src/edu/bu/ec504/spr24/Brain/Brain.java
@@ -1,6 +1,6 @@
-package edu.bu.ec504.spr19.Brain;
+package edu.bu.ec504.spr24.Brain;
-import edu.bu.ec504.spr19.sameGameTris.GUI;
+import edu.bu.ec504.spr24.sameGameTris.GUI;
/**
* The Brain is the artificial intelligence that tries to come up with the
@@ -9,26 +9,28 @@ import edu.bu.ec504.spr19.sameGameTris.GUI;
* It typically runs in its own thread so that it will not interfere with other processing.
*/
public abstract class Brain implements Runnable {
- // fields
+ // FIELDS
protected final GUI myGUI; // the GUI class attached to this Brain
- // methods
+ // METHODS
+
/**
- * A constructor that accepts a parameter representing the GUI interface on which the game is played.
+ * Constructs the brain
+ * @param myGUI A reference to the Graphical User Interface on which the game is being played.
*/
Brain(GUI myGUI) { this.myGUI = myGUI; }
-
- /**
- * This is called when the Brain is being asked to close down (i.e., the game is over).
- * It should clean up any data structures and/or signal threads to close, etc.
- */
- public abstract void allDone();
-
+
/**
* Each Brain should have a name, which is provided by this method.
* @return the name of the brain
*/
public abstract String myName();
+
+ /**
+ * This is called when the Brain is being asked to close down (i.e., the game is over).
+ * It should clean up any data structures and/or signal threads to close, etc.
+ */
+ public abstract void allDone();
/**
* Starts the Brain a'thinking.
@@ -36,5 +38,4 @@ public abstract class Brain implements Runnable {
* @see java.lang.Runnable#run()
*/
public abstract void run();
-
}
diff --git a/src/edu/bu/ec504/spr19/Brain/lazyBrain.java b/src/edu/bu/ec504/spr24/Brain/LazyBrain.java
similarity index 70%
rename from src/edu/bu/ec504/spr19/Brain/lazyBrain.java
rename to src/edu/bu/ec504/spr24/Brain/LazyBrain.java
index 127a6f0ee95461dc41f5a33b2f1a2dcc4a03801b..7c429134ddeae73ef754834117d86f5ee7fb00c5 100644
--- a/src/edu/bu/ec504/spr19/Brain/lazyBrain.java
+++ b/src/edu/bu/ec504/spr24/Brain/LazyBrain.java
@@ -1,23 +1,34 @@
-package edu.bu.ec504.spr19.Brain;
+package edu.bu.ec504.spr24.Brain;
import java.util.LinkedList;
import java.util.Objects;
-import edu.bu.ec504.spr19.sameGameTris.GUI;
-import edu.bu.ec504.spr19.sameGameTris.CircleColor;
+import edu.bu.ec504.spr24.sameGameTris.GUI;
+import edu.bu.ec504.spr24.sameGameTris.CircleColor;
/**
* Does whatever a lazy brain does ...
*/
-public class lazyBrain extends Brain {
- // fields
- private volatile boolean allDone = false; // when set to true, the Brain should stop what it's doing and exit (at an appropriate time)
- private board currState;
+public class LazyBrain extends Brain {
- /** Instantiates a Brain linked to a specified myGUI
+ // FIELDS
+
+ /**
+ * When set to true, the Brain should stop what it's doing and exit as soon as is appropriate.
+ */
+ private volatile boolean allDone = false;
+
+ /**
+ * The Brain's recording of the current state of the board.
+ */
+ private Board currBoard;
+
+ // METHODS
+
+ /** Constructs a Brain linked to a specified myGUI
* @param myGUI The GUI that will be instantiating the Brain
*/
- public lazyBrain(GUI myGUI) {
+ public LazyBrain(GUI myGUI) {
super(myGUI);
}
@@ -32,48 +43,84 @@ public class lazyBrain extends Brain {
* {@inheritDoc}
*/
public String myName() {
- return "Greedy Brain";
+ return "Lazy Brain";
}
/**
* {@inheritDoc}
*/
public void run() {
- // Initialize and set up the board with the current position
- currState = new board();
+ // Initialize and set up the internal board state with the current position
+ currBoard = new Board();
for (int xx=0; xx max) {
+ // record a new best move
+ max = count;
+ bestPos = new Board.Pos(xx, yy);
+ }
+
+ }
+ }
+
+ // register the selected move on the board
+ currBoard.clickNode(bestPos.xx, bestPos.yy);
+ // convert bestPos to GUI coordinates
+ bestPos = new Board.Pos(bestPos.yy, myGUI.boardHeight() - 1 - bestPos.yy);
+
+ // return the result to the GUI
+ return bestPos;
}
+
+ // INTERNAL CLASSES
+
/**
* Stores a board set up
*/
- private class board {
+ private class Board {
+
+ /**
+ * A 2-d Linked list of colors on the board.
+ */
final LinkedList< LinkedList > data;
+
+ /**
+ * The width and height of the board.
+ */
final private int width, height;
// constructs a board of specified width and height
- board(int width, int height) {
+ Board(int width, int height) {
this.width = width; this.height = height;
- // allocate the data structure
+ // allocate the data structure for storing board contents
data = new LinkedList<>();
// set up the data structure
@@ -82,27 +129,27 @@ public class lazyBrain extends Brain {
for (int jj=0; jjxx
*/
@@ -225,42 +272,15 @@ public class lazyBrain extends Brain {
String temp = data.toString();
return temp.replace("], [", "]\n[");
}
- }
-
- // internal methods
- /**
- * Chooses the next move to make.
- * @return the move chosen, in GUI coordinates
- */
- private pos chooseMove() {
- // greedy choice
- int max=0; // the maximum number of points for the best position found
- pos bestPos = new pos(0, 0); // the best position found
- board currStateCopy = new board(currState);
-
- for (int xx=0; xx max) {
- // record a new best move
- max = count;
- bestPos = new pos(xx, yy);
- }
-
- }
- }
-
- // register the selected move on the board
- currState.clickNode(bestPos.xx, bestPos.yy);
- // convert bestPos to GUI coordinates
- bestPos.yy = myGUI.boardHeight() - 1 - bestPos.yy;
- // return the result to the GUI
- return bestPos;
+ /**
+ * Stores an (xx,yy) position on the board.
+ */
+ static class Pos {
+ final int xx;
+ final int yy;
+ Pos(int xx, int yy) {this.xx=xx; this.yy=yy;}
+ }
}
-
}
diff --git a/src/edu/bu/ec504/spr19/Brain/simpleBrain.java b/src/edu/bu/ec504/spr24/Brain/SimpleBrain.java
similarity index 78%
rename from src/edu/bu/ec504/spr19/Brain/simpleBrain.java
rename to src/edu/bu/ec504/spr24/Brain/SimpleBrain.java
index 3f78b344c6cabe0239e0108899990dd32e4dae10..a20c245d2d3890993706e7642804e24c2a765694 100644
--- a/src/edu/bu/ec504/spr19/Brain/simpleBrain.java
+++ b/src/edu/bu/ec504/spr24/Brain/SimpleBrain.java
@@ -1,16 +1,16 @@
-package edu.bu.ec504.spr19.Brain;
+package edu.bu.ec504.spr24.Brain;
-import edu.bu.ec504.spr19.sameGameTris.GUI;
+import edu.bu.ec504.spr24.sameGameTris.GUI;
/**
* A very simple Brain for the game.
*/
-public class simpleBrain extends Brain {
+public class SimpleBrain extends Brain {
// fields
private volatile boolean allDone = false; // when set to true, the Brain should stop what it's doing and exit (at an appropriate time)
- public simpleBrain(GUI myGUI) {
+ public SimpleBrain(GUI myGUI) {
super(myGUI);
}
@@ -36,4 +36,3 @@ private volatile boolean allDone = false; // when set to true, the Brain should
myGUI.makeMove(0, myGUI.boardHeight()-1); // i.e. click on the lower left corner
}
}
-
\ No newline at end of file
diff --git a/src/edu/bu/ec504/spr19/Brain/smarterBrain.java b/src/edu/bu/ec504/spr24/Brain/SmarterBrain.java
similarity index 68%
rename from src/edu/bu/ec504/spr19/Brain/smarterBrain.java
rename to src/edu/bu/ec504/spr24/Brain/SmarterBrain.java
index fd3f82be64ea136171d791ea795a2520d5a7b2d5..0d72fd9ddee5bbe28e5b16fe9d8696f27013f984 100644
--- a/src/edu/bu/ec504/spr19/Brain/smarterBrain.java
+++ b/src/edu/bu/ec504/spr24/Brain/SmarterBrain.java
@@ -1,14 +1,14 @@
-package edu.bu.ec504.spr19.Brain;
+package edu.bu.ec504.spr24.Brain;
-import edu.bu.ec504.spr19.sameGameTris.GUI;
+import edu.bu.ec504.spr24.sameGameTris.GUI;
import javax.swing.*;
/**
* A smarter brain, for you to produce.
*/
-public class smarterBrain extends Brain {
- public smarterBrain(GUI myGUI) {
+public class SmarterBrain extends Brain {
+ public SmarterBrain(GUI myGUI) {
super(myGUI);
}
diff --git a/src/edu/bu/ec504/spr19/highScores/highScore.java b/src/edu/bu/ec504/spr24/highScores/highScore.java
similarity index 86%
rename from src/edu/bu/ec504/spr19/highScores/highScore.java
rename to src/edu/bu/ec504/spr24/highScores/highScore.java
index e9403ec30c131c74f274e34fbb3ed5f83bf0d2a3..00ac8691141c8f94b11180ff15a778e4e6bbb9d7 100644
--- a/src/edu/bu/ec504/spr19/highScores/highScore.java
+++ b/src/edu/bu/ec504/spr24/highScores/highScore.java
@@ -1,4 +1,4 @@
-package edu.bu.ec504.spr19.highScores;
+package edu.bu.ec504.spr24.highScores;
import java.io.BufferedReader;
import java.io.BufferedWriter;
@@ -25,8 +25,8 @@ public class highScore {
/*
* where the high score database is kept
*/
- private final String dataFile;
- private final TreeSet db;
+ private final String _dataFile;
+ private final TreeSet _db;
// SUBCLASSES
/**
@@ -72,9 +72,9 @@ public class highScore {
* @param fileName The name of the file to use for high score information.
*/
public highScore(String fileName) {
- dataFile = fileName;
- db = new TreeSet<>(new hsComp());
- loadScores();
+ _dataFile = fileName;
+ _db = new TreeSet<>(new hsComp());
+ _loadScores();
}
@@ -85,10 +85,10 @@ public class highScore {
*/
public boolean putScore(HSdata foo) {
if (newRecordQ(foo.score)) {
- db.add(foo);
- while (db.size()>maxScores)
- db.remove(db.last());
- saveScores();
+ _db.add(foo);
+ while (_db.size()>maxScores)
+ _db.remove(_db.last());
+ _saveScores();
return true;
}
else
@@ -100,10 +100,10 @@ public class highScore {
* @return true iff score is a new record on the high score list.
*/
public boolean newRecordQ(int score) {
- if (db.isEmpty()) // nothing in the db so far
+ if (_db.isEmpty()) // nothing in the db so far
return true;
else
- return (db.size() db.last().score);
+ return (_db.size() _db.last().score);
}
@@ -112,7 +112,7 @@ public class highScore {
*/
public String display() {
StringBuilder temp= new StringBuilder();
- for (HSdata foo: db) {
+ for (HSdata foo: _db) {
temp.append(hsComp.weightedScore(foo)).append(" [weighted]: ").append(foo.score).append("[actual] (").append(foo.name).append(" on ").append(foo.boardSizeX).append("x").append(foo.boardSizeY).append(")\n");
}
@@ -123,10 +123,10 @@ public class highScore {
/**
* Load high scores from the associated file.
*/
- private void loadScores() {
+ private void _loadScores() {
FileInputStream fis;
try {
- fis = new FileInputStream(dataFile);
+ fis = new FileInputStream(_dataFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
@@ -134,7 +134,7 @@ public class highScore {
while ((strLine = br.readLine()) != null) {
String[] res = strLine.split("\t");
HSdata temp = new HSdata(res[0],new Integer(res[1]),new Integer(res[2]), new Integer(res[3]));
- db.add(temp);
+ _db.add(temp);
}
} catch (FileNotFoundException e) {
System.err.println("High score file not found - Starting fresh!");
@@ -146,14 +146,14 @@ public class highScore {
/**
* Save current database to the associated file.
*/
- private void saveScores() {
+ private void _saveScores() {
FileOutputStream fos;
try {
- fos = new FileOutputStream(dataFile);
+ fos = new FileOutputStream(_dataFile);
DataOutputStream out = new DataOutputStream(fos);
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
- for (HSdata foo: db) {
+ for (HSdata foo: _db) {
br.write(foo.name+"\t"+foo.boardSizeX+"\t"+foo.boardSizeY+"\t"+foo.score+"\n");
}
diff --git a/src/edu/bu/ec504/spr19/sameGameTris/CircleColor.java b/src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java
similarity index 57%
rename from src/edu/bu/ec504/spr19/sameGameTris/CircleColor.java
rename to src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java
index 2ead8bea22cf5301dd8419b9100e79544a3a0ae5..83748e687e4da538d9e4fac0d6b59025ea469368 100644
--- a/src/edu/bu/ec504/spr19/sameGameTris/CircleColor.java
+++ b/src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java
@@ -1,8 +1,8 @@
-package edu.bu.ec504.spr19.sameGameTris;
+package edu.bu.ec504.spr24.sameGameTris;
import java.awt.Color;
-import static edu.bu.ec504.spr19.sameGameTris.sge.randGen;
+import static edu.bu.ec504.spr24.sameGameTris.sge.randGen;
/*
* Records the color of one circle
@@ -11,21 +11,21 @@ import static edu.bu.ec504.spr19.sameGameTris.sge.randGen;
public enum CircleColor {
NONE(null),
Red(new Color(255, 0, 0)),
- Green(new Color(0, 255, 0)),
- Blue(new Color(0, 0, 255)),
- Pink(new Color(255,0,255));
+ Green(new Color(0, 255, 0)),
+ Blue(new Color(0, 0, 255)),
+ Pink(new Color(255, 0, 255));
- // fields
- private final Color myColor;
+ // FIELDS
+ private final Color _myColor;
- // Constructor
+ // CONSTRUCTOR
CircleColor(Color myColor) {
- this.myColor = myColor;
+ this._myColor = myColor;
}
- // Accessors
+ // ACCESSOR
Color getColor() {
- return myColor;
+ return _myColor;
}
/*
@@ -33,10 +33,10 @@ public enum CircleColor {
*/
static CircleColor randColor() {
if (sge.numColors < 1 || sge.numColors >= CircleColor.values().length)
- throw new IndexOutOfBoundsException("Only " + CircleColor.values().length + " colors are available. You requested choosing one of " + sge.numColors + " colors.");
+ throw new IndexOutOfBoundsException("Only " + CircleColor.values().length
+ + " colors are available. You requested choosing one of " + sge.numColors + " colors.");
int randNum = 1 + Math.abs(randGen.nextInt()) % sge.numColors;
return CircleColor.values()[randNum];
-
}
}
diff --git a/src/edu/bu/ec504/spr19/sameGameTris/CircleRolloverEvent.java b/src/edu/bu/ec504/spr24/sameGameTris/CircleRolloverEvent.java
similarity index 66%
rename from src/edu/bu/ec504/spr19/sameGameTris/CircleRolloverEvent.java
rename to src/edu/bu/ec504/spr24/sameGameTris/CircleRolloverEvent.java
index 0f60337ec43d01f86aa0472dec0dde2104321b8a..0a5c1e55c1bac6c4a88148e07ad4558c13051ec4 100644
--- a/src/edu/bu/ec504/spr19/sameGameTris/CircleRolloverEvent.java
+++ b/src/edu/bu/ec504/spr24/sameGameTris/CircleRolloverEvent.java
@@ -1,4 +1,4 @@
-package edu.bu.ec504.spr19.sameGameTris;
+package edu.bu.ec504.spr24.sameGameTris;
import java.util.EventObject;
@@ -12,31 +12,35 @@ class CircleRolloverEvent extends EventObject {
/*
* the location of the button broadcasting the event
*/
- private final int xx;
- private final int yy;
+ private final int _xx;
+ private final int _yy;
private final CircleColor clr;
private final SelfAwareCircle.Visibility visibility;
-
+
// Constructor(s)
/*
* Records a rollover event on a specific button
+ * @param source The "source" upon which the event initially occured
* @param xx The x coordinate (in the gridlayout of buttons) of the button that was rolled over
* @param yy The y coordinate (in the gridlayout of buttons) of the button that was rolled over
* @param clr The color of the button that initiated the event
- * @param visibility Should circles be highlighted or unhighlighted
- * @param source The "source" upon which the event initially occured
+ * @param vis Should circles be highlighted or unhighlighted
*/
- public CircleRolloverEvent(Object source, int xx, int yy, CircleColor clr, SelfAwareCircle.Visibility toDo) {
+ public CircleRolloverEvent(Object source, int xx, int yy, CircleColor clr,
+ SelfAwareCircle.Visibility vis) {
super(source); // register with the superclass
- this.xx=xx; this.yy=yy; this.clr=clr; this.visibility = toDo;
+ this._xx = xx;
+ this._yy = yy;
+ this.clr = clr;
+ this.visibility = vis;
}
-
- // Methods
- // ... accessor
- int getXX() { return xx; }
- int getYY() { return yy; }
- SelfAwareCircle.Visibility getAction() { return visibility; }
- CircleColor getColor() { return clr; }
+ // ACCESSORS
+ int getXX() {return _xx;}
+
+ int getYY() {return _yy;}
+
+ SelfAwareCircle.Visibility getVisibility() {return visibility;}
+ CircleColor getColor() {return clr;}
}
diff --git a/src/edu/bu/ec504/spr19/sameGameTris/GUI.java b/src/edu/bu/ec504/spr24/sameGameTris/GUI.java
similarity index 90%
rename from src/edu/bu/ec504/spr19/sameGameTris/GUI.java
rename to src/edu/bu/ec504/spr24/sameGameTris/GUI.java
index f03dd778b5145b0e81c6a8f476dee7fb2f6d9bf2..b11b7d6089889f30ac2b7169a1d65ea3b6e09468 100644
--- a/src/edu/bu/ec504/spr19/sameGameTris/GUI.java
+++ b/src/edu/bu/ec504/spr24/sameGameTris/GUI.java
@@ -1,4 +1,4 @@
-package edu.bu.ec504.spr19.sameGameTris;
+package edu.bu.ec504.spr24.sameGameTris;
import javax.swing.*;
@@ -7,10 +7,17 @@ import javax.swing.*;
* It contains all the interfaces your Brain might need to make its moves.
*/
public abstract class GUI extends JFrame {
+
+ /**
+ * A number used for serialization.
+ */
private static final long serialVersionUID = 1L;
- GUI(String s) {
- super(s);
+ /**
+ * @param title Creates a new GUI with the given title.
+ */
+ GUI(String title) {
+ super(title);
}
/*
diff --git a/src/edu/bu/ec504/spr19/sameGameTris/SelfAwareCircle.java b/src/edu/bu/ec504/spr24/sameGameTris/SelfAwareCircle.java
similarity index 74%
rename from src/edu/bu/ec504/spr19/sameGameTris/SelfAwareCircle.java
rename to src/edu/bu/ec504/spr24/sameGameTris/SelfAwareCircle.java
index 765c1e7768d16788d3679ce1266efe308e1a5e81..58541a632f9b60cc39a5cbbb24f867cc8b5981f5 100644
--- a/src/edu/bu/ec504/spr19/sameGameTris/SelfAwareCircle.java
+++ b/src/edu/bu/ec504/spr24/sameGameTris/SelfAwareCircle.java
@@ -1,4 +1,4 @@
-package edu.bu.ec504.spr19.sameGameTris;
+package edu.bu.ec504.spr24.sameGameTris;
import java.awt.Color;
import java.awt.Component;
@@ -15,25 +15,27 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
class SelfAwareCircle extends Component implements MouseListener, SelfAwareListener {
// Constants
- private final Color hltColor = new Color(200,200,200); // the color to use for highlighting a button
+ private final Color _hltColor = new Color(200,200,200); // the color to use for highlighting a button
- // Data fields
+ // FIELDS
public final int xx;
public final int yy;
public CircleColor clr;
private static final long serialVersionUID = 1L;
private final ArrayList _listeners = new ArrayList<>(); // who is listening to events fired by this circle
- private static int id=0;
- private final int myid; // the circle's ID
- private Visibility state = Visibility.plain; // the current state of the circle
- private final sge sgeGui; // a link to the GUI that created this circle
+ private static int _id =0;
+ private final int _myID; // the circle's ID
+ private Visibility _state = Visibility.plain; // the current state of the circle
+ private final sge sgeGUI; // a link to the GUI that created this circle
- // concurrency variables
+ /**
+ * Used to manage concurrency.
+ */
private static final AtomicInteger regionLength = new AtomicInteger(0);
// SUBCLASSES
/*
- * Records the length of the selected region
+ * Records the visibility of an object.
*/
public enum Visibility{
/*
@@ -65,9 +67,9 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
this.clr = clr; // record the color of the button
if (clr == CircleColor.NONE) // circles with no color should be cleared
setClear();
- this.sgeGui = sgeGui; // record the GUI that created this button
+ this.sgeGUI = sgeGui; // record the GUI that created this button
addMouseListener(this); // register for mouse events
- myid = id++;
+ _myID = _id++;
}
/**
@@ -86,11 +88,11 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
}
public int getId() {
- return myid;
+ return _myID;
}
- Visibility getState() {
- return state;
+ Visibility get_state() {
+ return _state;
}
public CircleColor getColor() {
@@ -104,20 +106,20 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
clr=CircleColor.randColor();
}
- void setState (Visibility state) {
- if (state==Visibility.clear)
+ void set_state(Visibility _state) {
+ if (_state ==Visibility.clear)
setClear(); // i.e. there is extra overhead in clearing
else
- this.state = state;
+ this._state = _state;
repaint();
}
boolean isCleared() {
- return this.state == Visibility.clear;
+ return this._state == Visibility.clear;
}
void setClear() {
- state = Visibility.clear;
+ _state = Visibility.clear;
repaint();
}
@@ -125,18 +127,18 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
* Copies immutable elements of this SelfAwareCircle to "recipient"
* *except* position-dependent items (e.g. _listeners, xx, yy)
*/
- void copy(SelfAwareCircle recipient) {
+ void copyTo(SelfAwareCircle recipient) {
recipient.clr=clr;
- if (recipient.state == state)
+ if (recipient._state == _state)
return; // i.e. nothing to do
// special cases
// ... copying a cleared circle to an uncleared circle
- if (state == Visibility.clear)
+ if (_state == Visibility.clear)
recipient.setClear(); // includes removing a mouse listener
// copy the state
- recipient.setState(state);
+ recipient.set_state(_state);
}
// ... Mouse methods
@@ -146,7 +148,7 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
* Highlights the button by changing its icon and firing off an event
* @param state determines whether to highlight or unhighlight the circle
*/
- private void highlightButton(Visibility stt) {
+ private void _highlightButton(Visibility stt) {
if (isCleared()) // don't go further with already cleared circles
return;
@@ -154,7 +156,7 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
if (stt==Visibility.clear) // clear has its own special treatment
setClear(); // properly clear this circle (including removal of mouse listener)
else
- setState(stt); // set the non-cleared state appropriately
+ set_state(stt); // set the non-cleared state appropriately
// deal with the region length
regionLength.getAndIncrement(); // update the region length by the newly highlighted item
@@ -171,30 +173,43 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
*/
public void mouseEntered(MouseEvent e) {
regionLength.set(0); // i.e. reinitialize
- highlightButton(Visibility.highlight);
+ _highlightButton(Visibility.highlight);
}
+ /**
+ * Mouse exited the object area.
+ * * Change its button icon
+ * * Broadcast the change to other buttons
+ */
public void mouseExited(MouseEvent e) {
regionLength.set(0); // i.e. reinitialize
- highlightButton(Visibility.plain);
+ _highlightButton(Visibility.plain);
}
- public void mousePressed(MouseEvent e) {
+
+ /**
+ * The mouse button was pressed.
+ */
+ public void mousePressed(MouseEvent e) {
regionLength.set(0); // i.e. reinitialize
}
- public void mouseReleased(MouseEvent e) {
- highlightButton(Visibility.clear);
+
+ /**
+ * The mouse button was released.
+ */
+ public void mouseReleased(MouseEvent e) {
+ _highlightButton(Visibility.clear);
// register the score
- sgeGui.totalPoints.setText("" +
- (Integer.parseInt(sgeGui.totalPoints.getText()) +
- sgeGui.score(SelfAwareCircle.getRegionLength(), getColor())));
+ sgeGUI.totalPoints.setText("" +
+ (Integer.parseInt(sgeGUI.totalPoints.getText()) +
+ sgeGUI.score(SelfAwareCircle.getRegionLength(), getColor())));
// request that the board be shifted appropriately
- sgeGui.shiftCircles();
+ sgeGUI.shiftCircles();
// update tetrising
- sgeGui.updateNumClicks();
- sgeGui.updateTetrising();
+ sgeGUI.updateNumClicks();
+ sgeGUI.updateTetrising();
}
@@ -225,22 +240,22 @@ class SelfAwareCircle extends Component implements MouseListener, SelfAwareListe
* @param e the event broadcast by the other button
*/
public void rollingOver(CircleRolloverEvent e) {
- if (state == e.getAction() || isCleared()) // i.e. this circle has already been processed
+ if (_state == e.getVisibility() || isCleared()) // i.e. this circle has already been processed
return;
if (e.getColor().equals(clr)) { // check if the color is the same as mine
- highlightButton(e.getAction());
+ _highlightButton(e.getVisibility());
}
// in all other cases, only update the text field (i.e. stop the recursion)
- sgeGui.regionPoints.setText(""+(SelfAwareCircle.getRegionLength())); // update the gui text field
+ sgeGUI.regionPoints.setText(""+(SelfAwareCircle.getRegionLength())); // update the gui text field
}
// ... drawing methods
public void paint(Graphics g) {
// set the color, depending on whether this is being highlighted
- switch (state) {
+ switch (_state) {
case highlight:
- g.setColor(hltColor); // change to highlighting color
+ g.setColor(_hltColor); // change to highlighting color
break;
case plain:
g.setColor(clr.getColor()); // the default color
diff --git a/src/edu/bu/ec504/spr19/sameGameTris/SelfAwareListener.java b/src/edu/bu/ec504/spr24/sameGameTris/SelfAwareListener.java
similarity index 83%
rename from src/edu/bu/ec504/spr19/sameGameTris/SelfAwareListener.java
rename to src/edu/bu/ec504/spr24/sameGameTris/SelfAwareListener.java
index 7a0be616c34f738e453cd654f7872d6c7a9e01f4..27f88bfe623b0d3355e68bdb3937daab92997218 100644
--- a/src/edu/bu/ec504/spr19/sameGameTris/SelfAwareListener.java
+++ b/src/edu/bu/ec504/spr24/sameGameTris/SelfAwareListener.java
@@ -1,4 +1,4 @@
-package edu.bu.ec504.spr19.sameGameTris;
+package edu.bu.ec504.spr24.sameGameTris;
/*
* Listener for events of interest to SelfAwareJButtons
diff --git a/src/edu/bu/ec504/spr19/sameGameTris/sge.java b/src/edu/bu/ec504/spr24/sameGameTris/sge.java
similarity index 94%
rename from src/edu/bu/ec504/spr19/sameGameTris/sge.java
rename to src/edu/bu/ec504/spr24/sameGameTris/sge.java
index 77013c7a07a2760f726f78ddaef7eda2b6213ab4..047f815e4b2329799436e0675862b59ddbfd899a 100644
--- a/src/edu/bu/ec504/spr19/sameGameTris/sge.java
+++ b/src/edu/bu/ec504/spr24/sameGameTris/sge.java
@@ -1,9 +1,4 @@
-package edu.bu.ec504.spr19.sameGameTris;
-
-/* An implementation of the "samegnome" game, possibly with a computer-aided solver.
- Written by Prof. Ari Trachtenberg for EC504 at Boston University.
-*/
-
+package edu.bu.ec504.spr24.sameGameTris;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
@@ -29,14 +24,18 @@ import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
-import edu.bu.ec504.spr19.Brain.smarterBrain;
-import edu.bu.ec504.spr19.highScores.highScore;
-import edu.bu.ec504.spr19.Brain.Brain;
-import edu.bu.ec504.spr19.Brain.lazyBrain;
-import edu.bu.ec504.spr19.Brain.simpleBrain;
+import edu.bu.ec504.spr24.Brain.SmarterBrain;
+import edu.bu.ec504.spr24.highScores.highScore;
+import edu.bu.ec504.spr24.Brain.Brain;
+import edu.bu.ec504.spr24.Brain.LazyBrain;
+import edu.bu.ec504.spr24.Brain.SimpleBrain;
import static java.util.concurrent.Executors.newScheduledThreadPool;
+/**
+ * A Singleton Graphical User Interface for the game.
+ * @author Ari Trachtenberg
+ */
class sge extends GUI implements ActionListener, ItemListener {
// CONSTANTS
@@ -56,8 +55,11 @@ class sge extends GUI implements ActionListener, ItemListener {
private SelfAwareCircle[][] circles;
private String highScoreName = null; // the name to use for the high score
static final Random randGen = new Random(); // package-private random number generator used for the entire package (i.e., repeatedly seeding with the same value produces the same randomness).
- private int numClicks = 1; // Must be a positive integer; counts the number of clicks on circles since the last time the board was reset.
-
+ static private int numClicks = 1; // Must be a positive integer; counts the number of clicks on circles since the last time the board was reset.
+ /**
+ * An instane of the SGE GUI.
+ */
+ private static sge Instance = null;
// ... GUI elements
private JPanel circlePanel; // the panel containing the circles
@@ -94,12 +96,12 @@ class sge extends GUI implements ActionListener, ItemListener {
try {
int randColumn = randGen.nextInt(width);
SelfAwareCircle myCircle = circles[randColumn][0];
- if (myCircle.getState() != SelfAwareCircle.Visibility.clear) { // i.e. the spot is already occupied
+ if (myCircle.get_state() != SelfAwareCircle.Visibility.clear) { // i.e. the spot is already occupied
doGameOver("overran column "+randColumn);
}
else {
myCircle.resetColor();
- myCircle.setState(SelfAwareCircle.Visibility.plain);
+ myCircle.set_state(SelfAwareCircle.Visibility.plain);
}
} finally {
GUIlock.unlock();
@@ -124,8 +126,8 @@ class sge extends GUI implements ActionListener, ItemListener {
for (int yy = height - 2; yy >= 0; yy--) { // go down to up, so as not to move circles more than once
SelfAwareCircle orig = circles[xx][yy],
dest = circles[xx][yy + 1];
- if (orig.getState() != SelfAwareCircle.Visibility.clear && // a colored circle
- dest.getState() == SelfAwareCircle.Visibility.clear) { // above a cleared circle
+ if (orig.get_state() != SelfAwareCircle.Visibility.clear && // a colored circle
+ dest.get_state() == SelfAwareCircle.Visibility.clear) { // above a cleared circle
// move it down
moveCircle(orig, dest);
}
@@ -138,12 +140,12 @@ class sge extends GUI implements ActionListener, ItemListener {
}
}
- // PUBLIC ACCESS METHODS
+ // METHODS
/*
* Default no-args constructor
*/
- public sge() {
+ private sge() {
super("Ari's samegame");
try {
SwingUtilities.invokeAndWait(
@@ -154,6 +156,13 @@ class sge extends GUI implements ActionListener, ItemListener {
}
}
+ public static sge getInstance() {
+ if (sge.Instance == null) {
+ sge.Instance = new sge();
+ }
+ return Instance;
+ }
+
/*
* Returns the color of the circle at location [xx][yy], or NONE if the circle has been cleared
* @param xx must be between 0 and width
@@ -422,7 +431,7 @@ class sge extends GUI implements ActionListener, ItemListener {
*/
synchronized private void moveCircle(SelfAwareCircle orig, SelfAwareCircle dest) {
// copy the immutable, position independent values
- orig.copy(dest);
+ orig.copyTo(dest);
// clear the top item
orig.setClear();
@@ -605,11 +614,11 @@ class sge extends GUI implements ActionListener, ItemListener {
if (source == noPlayerMenuItem)
return; // i.e. no players
else if (source == simplePlayerMenuItem)
- startBrain(new simpleBrain(this));
+ startBrain(new SimpleBrain(this));
else if (source == lazyPlayerMenuItem)
- startBrain(new lazyBrain(this));
+ startBrain(new LazyBrain(this));
else if (source == smarterPlayerMenuItem)
- startBrain(new smarterBrain( this));
+ startBrain(new SmarterBrain( this));
} else {
// deselected
if (theBrain != null)
@@ -624,9 +633,9 @@ class sge extends GUI implements ActionListener, ItemListener {
* Runs the application
*/
public static void main(String[] args) {
- JFrame myApp = new sge();
+ JFrame myApp = sge.getInstance();
myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myApp.setSize(defaultWindowWidth, defaultWindowHeight);
myApp.setVisible(true);
}
-}
\ No newline at end of file
+}