Loading src/edu/bu/ec504/spr19/sameGameTris/sge.java +28 −30 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.*; import java.util.Random; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; Loading @@ -30,7 +29,7 @@ import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import com.sun.deploy.security.SelectableSecurityManager; 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; Loading @@ -45,9 +44,9 @@ class sge extends GUI implements ActionListener, ItemListener { 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 public final int displayTime = 1000; // number of milliseconds to display (highlight) a move before actually making it static public final String highScoreFile = ".highscores.db"; // where high scores are kept private static final long serialVersionUID = 1L; // required for serializability 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 private final long serialVersionUID = 1L; // required for serializability // 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 Loading @@ -66,7 +65,6 @@ class sge extends GUI implements ActionListener, ItemListener { // ... ... menu items private JMenuItem changeBoard; private JMenuItem quitGame; @SuppressWarnings("FieldCanBeLocal") private JRadioButtonMenuItem noPlayerMenuItem, simplePlayerMenuItem, lazyPlayerMenuItem, // various automatic player options smarterPlayerMenuItem; private ReentrantLock GUIlock = new ReentrantLock(); // thread lock used to synchronize manipulation of circles on the board Loading Loading @@ -97,8 +95,7 @@ class sge extends GUI implements ActionListener, ItemListener { int randColumn = randGen.nextInt(width); SelfAwareCircle myCircle = circles[randColumn][0]; if (myCircle.getState() != SelfAwareCircle.Visibility.clear) { // i.e. the spot is already occupied cancelTetrisingSchedules(); doGameOver(); doGameOver("overran column "+randColumn); } else { myCircle.resetColor(); Loading Loading @@ -150,7 +147,7 @@ class sge extends GUI implements ActionListener, ItemListener { super("Ari's samegame"); try { SwingUtilities.invokeAndWait( () -> setupGUI() this::setupGUI ); } catch (Exception e) { System.out.println("Saw exception " + e); Loading Loading @@ -222,13 +219,11 @@ class sge extends GUI implements ActionListener, ItemListener { /** * @return the score achieved from clicking on a region of length <b>level</b> */ final public int score(int level, CircleColor theColor) { int tmp; final public int score(int level, CircleColor clr) { if (level == 1) return 1; else return (int) (level * level); return level * level; } // OTHER METHODS Loading Loading @@ -297,7 +292,6 @@ class sge extends GUI implements ActionListener, ItemListener { menu.add(lazyPlayerMenuItem); smarterPlayerMenuItem = new JRadioButtonMenuItem("Smarter player"); smarterPlayerMenuItem.setEnabled(false); // disabled for this version smarterPlayerMenuItem.addItemListener(this); group.add(smarterPlayerMenuItem); menu.add(smarterPlayerMenuItem); Loading Loading @@ -348,9 +342,12 @@ class sge extends GUI implements ActionListener, ItemListener { /** * Increment the number of clicks seen. * If this ends up being greater than the size of the board, the game is declared over. */ public void updateNumClicks() { numClicks++; if (numClicks>width*height) doGameOver("too many clicks - "+numClicks); } Loading Loading @@ -506,35 +503,37 @@ class sge extends GUI implements ActionListener, ItemListener { // 3. (LAST ITEM) CHECK IF THE GAME HAS ENDED // This happens if every circle is surrounded by cleared circles or circles of a different color if (gameOverQ()) doGameOver(); // all done doGameOver("exhausted all circles"); // all done } finally { GUIlock.unlock(); // release the circles for other processes } } private void doGameOver() { int score = new Integer(totalPoints.getText()); /** * The game is over - report to the user and/or save a high score, if relevant. * @param feedback Some string to present to the user; typically the reason that the game is over. */ private void doGameOver(String feedback) { cancelTetrisingSchedules(); // cancel tetrising threads int score = new Integer(totalPoints.getText()); highScore hs = new highScore(highScoreFile); hs.loadScores(); // check the high score if (hs.newRecordQ(score)) { // i.e. a new record JOptionPane.showMessageDialog(null, "Game Over - You got a new high score of " + score + " points!\n" + JOptionPane.showMessageDialog(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, "You got a new high score!\nPlease enter your name:"); if (highScoreName == null) // i.e. the user is not interested in high scores return; if (highScoreName != null) { // i.e. the user is interested in high scores // populate the high score item highScore.HSdata datum = new highScore.HSdata(highScoreName, width, height, score); hs.putScore(datum); // enter the name into the high score list hs.saveScores(); } } else JOptionPane.showMessageDialog(null, "Game Over - You did not make the high score. You had " + score + " points.\n" + JOptionPane.showMessageDialog(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)) + "."); Loading Loading @@ -609,9 +608,8 @@ class sge extends GUI implements ActionListener, ItemListener { startBrain(new simpleBrain(this)); else if (source == lazyPlayerMenuItem) startBrain(new lazyBrain(this)); /* * The smarterBrain is missing from this code :-) */ else if (source == smarterPlayerMenuItem) startBrain(new smarterBrain( this)); } else { // deselected if (theBrain != null) Loading Loading
src/edu/bu/ec504/spr19/sameGameTris/sge.java +28 −30 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.*; import java.util.Random; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; Loading @@ -30,7 +29,7 @@ import javax.swing.JTextField; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import com.sun.deploy.security.SelectableSecurityManager; 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; Loading @@ -45,9 +44,9 @@ class sge extends GUI implements ActionListener, ItemListener { 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 public final int displayTime = 1000; // number of milliseconds to display (highlight) a move before actually making it static public final String highScoreFile = ".highscores.db"; // where high scores are kept private static final long serialVersionUID = 1L; // required for serializability 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 private final long serialVersionUID = 1L; // required for serializability // 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 Loading @@ -66,7 +65,6 @@ class sge extends GUI implements ActionListener, ItemListener { // ... ... menu items private JMenuItem changeBoard; private JMenuItem quitGame; @SuppressWarnings("FieldCanBeLocal") private JRadioButtonMenuItem noPlayerMenuItem, simplePlayerMenuItem, lazyPlayerMenuItem, // various automatic player options smarterPlayerMenuItem; private ReentrantLock GUIlock = new ReentrantLock(); // thread lock used to synchronize manipulation of circles on the board Loading Loading @@ -97,8 +95,7 @@ class sge extends GUI implements ActionListener, ItemListener { int randColumn = randGen.nextInt(width); SelfAwareCircle myCircle = circles[randColumn][0]; if (myCircle.getState() != SelfAwareCircle.Visibility.clear) { // i.e. the spot is already occupied cancelTetrisingSchedules(); doGameOver(); doGameOver("overran column "+randColumn); } else { myCircle.resetColor(); Loading Loading @@ -150,7 +147,7 @@ class sge extends GUI implements ActionListener, ItemListener { super("Ari's samegame"); try { SwingUtilities.invokeAndWait( () -> setupGUI() this::setupGUI ); } catch (Exception e) { System.out.println("Saw exception " + e); Loading Loading @@ -222,13 +219,11 @@ class sge extends GUI implements ActionListener, ItemListener { /** * @return the score achieved from clicking on a region of length <b>level</b> */ final public int score(int level, CircleColor theColor) { int tmp; final public int score(int level, CircleColor clr) { if (level == 1) return 1; else return (int) (level * level); return level * level; } // OTHER METHODS Loading Loading @@ -297,7 +292,6 @@ class sge extends GUI implements ActionListener, ItemListener { menu.add(lazyPlayerMenuItem); smarterPlayerMenuItem = new JRadioButtonMenuItem("Smarter player"); smarterPlayerMenuItem.setEnabled(false); // disabled for this version smarterPlayerMenuItem.addItemListener(this); group.add(smarterPlayerMenuItem); menu.add(smarterPlayerMenuItem); Loading Loading @@ -348,9 +342,12 @@ class sge extends GUI implements ActionListener, ItemListener { /** * Increment the number of clicks seen. * If this ends up being greater than the size of the board, the game is declared over. */ public void updateNumClicks() { numClicks++; if (numClicks>width*height) doGameOver("too many clicks - "+numClicks); } Loading Loading @@ -506,35 +503,37 @@ class sge extends GUI implements ActionListener, ItemListener { // 3. (LAST ITEM) CHECK IF THE GAME HAS ENDED // This happens if every circle is surrounded by cleared circles or circles of a different color if (gameOverQ()) doGameOver(); // all done doGameOver("exhausted all circles"); // all done } finally { GUIlock.unlock(); // release the circles for other processes } } private void doGameOver() { int score = new Integer(totalPoints.getText()); /** * The game is over - report to the user and/or save a high score, if relevant. * @param feedback Some string to present to the user; typically the reason that the game is over. */ private void doGameOver(String feedback) { cancelTetrisingSchedules(); // cancel tetrising threads int score = new Integer(totalPoints.getText()); highScore hs = new highScore(highScoreFile); hs.loadScores(); // check the high score if (hs.newRecordQ(score)) { // i.e. a new record JOptionPane.showMessageDialog(null, "Game Over - You got a new high score of " + score + " points!\n" + JOptionPane.showMessageDialog(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, "You got a new high score!\nPlease enter your name:"); if (highScoreName == null) // i.e. the user is not interested in high scores return; if (highScoreName != null) { // i.e. the user is interested in high scores // populate the high score item highScore.HSdata datum = new highScore.HSdata(highScoreName, width, height, score); hs.putScore(datum); // enter the name into the high score list hs.saveScores(); } } else JOptionPane.showMessageDialog(null, "Game Over - You did not make the high score. You had " + score + " points.\n" + JOptionPane.showMessageDialog(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)) + "."); Loading Loading @@ -609,9 +608,8 @@ class sge extends GUI implements ActionListener, ItemListener { startBrain(new simpleBrain(this)); else if (source == lazyPlayerMenuItem) startBrain(new lazyBrain(this)); /* * The smarterBrain is missing from this code :-) */ else if (source == smarterPlayerMenuItem) startBrain(new smarterBrain( this)); } else { // deselected if (theBrain != null) Loading