diff --git a/.idea/misc.xml b/.idea/misc.xml index 865b294f3b7ff03d520ce4f4ffeeebf55f45ba7b..606422566bc24975bc6482f2f89b4703a8cf41b0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - - - + + + + + - - + - { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "WebServerToolWindowFactoryState": "false", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "project.structure.last.edited": "Project", + "project.structure.proportion": "0.0", + "project.structure.side.proportion": "0.36091954", + "run.code.analysis.last.selected.profile": "pProject Default", + "vue.rearranger.settings.migration": "true" } -}]]> +} @@ -96,6 +100,8 @@ + + 1580084914114 @@ -170,12 +176,12 @@ file://$PROJECT_DIR$/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java - 658 + 697 file://$PROJECT_DIR$/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java - 169 + 208 diff --git a/src/edu/bu/ec504/spr24/brain/Board.java b/src/edu/bu/ec504/spr24/brain/Board.java index 177e8aeb1f6b96ec214365e0ab76dd85557489fb..e49116747fe7611688c4a523a234d1830f9079d0 100644 --- a/src/edu/bu/ec504/spr24/brain/Board.java +++ b/src/edu/bu/ec504/spr24/brain/Board.java @@ -174,10 +174,12 @@ class Board { return data.get(xx).size(); } + /** * @return a "pretty-printed" version of the data structure */ - public String print() { + @Override + public String toString() { String temp = data.toString(); return temp.replace("], [", "]\n["); } @@ -186,7 +188,6 @@ class Board { * Stores an (xx,yy) position on the board. */ static class Pos { - final int xx; final int yy; @@ -194,5 +195,10 @@ class Board { this.xx = xx; this.yy = yy; } + + @Override + public String toString() { + return "[" + xx + ", " + yy +']'; + } } } diff --git a/src/edu/bu/ec504/spr24/brain/LazyBrain.java b/src/edu/bu/ec504/spr24/brain/LazyBrain.java index 9fd6e4f81ed81bf1640149680e45929af18aa27e..eee3534b6f6c0b63d9f3447501433a4c796ca1c2 100644 --- a/src/edu/bu/ec504/spr24/brain/LazyBrain.java +++ b/src/edu/bu/ec504/spr24/brain/LazyBrain.java @@ -59,9 +59,12 @@ public class LazyBrain extends Brain { for (int yy = 0; yy < currBoard.rows(xx); yy++) { if (currStateCopy.getAt(xx, yy) != CircleColor.NONE) { Board test = new Board(currStateCopy); - currStateCopy.clickNodeHelper(xx, yy, test.getAt(xx, - yy)); // mark all other nodes in the region as "clear" (but does not delete anything) - int count = test.clickNode(xx, yy); // try removing the region to see what is left over + + // mark all other nodes in the region as "clear" (but does not delete anything) + currStateCopy.clickNodeHelper(xx, yy, test.getAt(xx, yy)); + + // try removing the region to see what is left over + int count = test.clickNode(xx, yy); if (count > max) { // record a new best move max = count; @@ -71,10 +74,8 @@ public class LazyBrain extends Brain { } } - // 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); + bestPos = new Board.Pos(bestPos.xx, myGUI.boardHeight() - 1 - bestPos.yy); // return the result to the GUI return bestPos; diff --git a/src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java b/src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java index ffb55b2ddcfe60b9f0c1375098f80a4805a0d438..d932fc66019b07392b96f346762a0ff91f4ea71d 100644 --- a/src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java +++ b/src/edu/bu/ec504/spr24/sameGameTris/CircleColor.java @@ -32,11 +32,11 @@ public enum CircleColor { * Generate a random color from the first num possible colors (excluding NONE) */ static CircleColor randColor() { - if (SameGameTris.numColors < 1 || SameGameTris.numColors >= CircleColor.values().length) + if (SameGameTris.NUM_COLORS < 1 || SameGameTris.NUM_COLORS >= CircleColor.values().length) throw new IndexOutOfBoundsException("Only " + CircleColor.values().length - + " colors are available. You requested choosing one of " + SameGameTris.numColors + " colors."); + + " colors are available. You requested choosing one of " + SameGameTris.NUM_COLORS + " colors."); - int randNum = 1 + Math.abs(randGen.nextInt()) % SameGameTris.numColors; + int randNum = 1 + Math.abs(randGen.nextInt()) % SameGameTris.NUM_COLORS; return CircleColor.values()[randNum]; } } diff --git a/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java b/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java index 2c728a94ce672bcac4560f60df48c54e3ec958dd..5f5403477fa2b0875446f2d5dbbbfe657cc6b5ba 100644 --- a/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java +++ b/src/edu/bu/ec504/spr24/sameGameTris/SameGameTris.java @@ -40,21 +40,59 @@ import static java.util.concurrent.Executors.newScheduledThreadPool; public class SameGameTris extends GUI implements ActionListener, ItemListener { // CONSTANTS - static public final int defaultWidth = 15, defaultHeight = 10; // sizes in terms of numbers of circles - static public final int defaultEmptyRows = - defaultHeight / 3; // number of empty rows on the top of the game - static public final int defaultWindowWidth = 500, defaultWindowHeight = 300; // default window size, if run as a standalone application - static public final int numColors = 3; // the number of colors available for circles - static private final int displayTime = 1000; // number of milliseconds to display (highlight) a move before actually making it - static private final String highScoreFile = ".highscores.db"; // where high scores are kept + static public final int DEFAULT_WIDTH = 15, DEFAULT_HEIGHT = 10; // sizes in terms of numbers of circles + /** + * Rate at which new "Tetrising" circles are being produced, in microseconds. + * Production rate increases invesely proportional to the number of circle selections clicked so far. + */ + static private long PRODUCT_CICLE_US = 5000000; + /** + * Rate at which new "Tetrising" circles descend on the board, in microseconds. + * Production rate increases invesely proportional to the number of circle selections clicked so far. + */ + static private long DROP_CIRCLE_US = 5000000; + + /** + * The number of empty rows on the top of the game. + */ + static public final int DEFAULT_EMPTY_ROWS = DEFAULT_HEIGHT / 3; + + /** + * The default window size, if run as a standalone application. + */ + static public final int DEFAULT_WINDOW_WIDTH = 500, DEFAULT_WINDOW_HEIGHT = 300; + + /** + * The number of colors available for circles. + */ + static public final int NUM_COLORS = 3; + + /** + * The number of milliseconds to display (highlight) a move before actually making it. + */ + static private final int DISPLAY_TIME_MS = 1000; + + /** + * The file where high scores are stored. + */ + static private final String HIGH_SCORE_FILE = ".highscores.db"; + @Serial - static private final long serialVersionUID = 1L; // required for serializability + static private final long serialVersionUID = 1L; - // FIELDS (all static - only one instance of the game should be running at a time) - final JLabel regionPoints = new JLabel("0"); // keeps track of the number of selected points - final JLabel totalPoints = new JLabel("0"); // keeps track of the total number of points so far - private int width = defaultWidth, height = defaultHeight; // initial width and height of the array of circles - private final int emptyRows = defaultEmptyRows; // number of rows on top of screen with no circles + /** + * Keeps track of the number of points for the selected region. + */ + final JLabel regionPoints = new JLabel("0"); + + /** + * Keeps track of the total number of points so far + */ + final JLabel totalPoints = new JLabel("0"); + + // FIELDS + private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; // initial width and height of the array of circles + private final int emptyRows = DEFAULT_EMPTY_ROWS; // number of rows on top of screen with no circles 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). @@ -83,6 +121,7 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener { private ScheduledFuture produceCircleFuture = null; // schedule for producing circles for the app private ScheduledFuture dropCircleFuture = null; // schedule for dropping circles for the app + // SUBCLASSES /** @@ -227,7 +266,7 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener { circles[xx][yy].mouseEntered(null); // pretend the mouse was pressed at location (xx,yy) try { - Thread.sleep(displayTime); // wait a bit for the user to register the move + Thread.sleep(DISPLAY_TIME_MS); // wait a bit for the user to register the move } catch (InterruptedException ignored) { } circles[xx][yy].mouseExited(null); @@ -396,8 +435,8 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener { // start up the new ones produceCircleFuture = tetrisExec.scheduleAtFixedRate(new initDropCircles(), 0, - 5000000 / numClicks, TimeUnit.MICROSECONDS); - dropCircleFuture = tetrisExec.scheduleAtFixedRate(new moveDropCircles(), 0, 2000000 / numClicks, + PRODUCT_CICLE_US / numClicks, TimeUnit.MICROSECONDS); + dropCircleFuture = tetrisExec.scheduleAtFixedRate(new moveDropCircles(), 0, DROP_CIRCLE_US / numClicks, TimeUnit.MICROSECONDS); } @@ -546,7 +585,7 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener { cancelTetrisingSchedules(); // cancel Tetrising threads int score = Integer.parseInt(totalPoints.getText()); - highScore hs = new highScore(highScoreFile); + highScore hs = new highScore(HIGH_SCORE_FILE); // check the high score if (hs.newRecordQ(score)) { // i.e. a new record @@ -585,9 +624,9 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener { else if (source == changeBoard) { // modified from http://www.vbforums.com/showthread.php?t=513699 JTextField width = new JTextField(); - width.setText("" + defaultWidth); + width.setText("" + DEFAULT_WIDTH); JTextField height = new JTextField(); - height.setText("" + defaultHeight); + height.setText("" + DEFAULT_HEIGHT); Object[] msg = {"Width:", width, "Height:", height}; JOptionPane op = new JOptionPane( @@ -658,7 +697,7 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener { public static void main(String[] args) { JFrame myApp = SameGameTris.getInstance(); myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - myApp.setSize(defaultWindowWidth, defaultWindowHeight); + myApp.setSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT); myApp.setVisible(true); } }