Commit 2239bc6c authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

added IntelliJ project metadata

parent 8afa7385
Loading
Loading
Loading
Loading

.idea/encodings.xml

0 → 100644
+4 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
 No newline at end of file
+1 −2
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager">
    <output url="file://$MODULE_DIR$/bin" />
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+480 −471
Original line number Diff line number Diff line
package edu.bu.ec504.spr19.sameGame;

/*
   An implementation of the "samegnome" game, possibly with a computer-aided solver.
   Written by Prof. Ari Trachtenberg, January 20, 2019.
*/


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
@@ -30,23 +29,22 @@ import edu.bu.ec504.spr19.Brain.Brain;
import edu.bu.ec504.spr19.Brain.lazyBrain;
import edu.bu.ec504.spr19.Brain.simpleBrain;

// Ari's implementation of the "samegnome" game, possibly with a computer-aided solver.

class sg extends GUI implements ActionListener,ItemListener {

	// constants
    private static final int DEFAULT_WIDTH =15;
    private static final int DEFAULT_HEIGHT =10; // sizes in terms of numbers of circles
    private static final int DEFAULT_WINDOW_WIDTH =500;
    private static final int DEFAULT_WINDOW_HEIGHT =300; // default window size, if run as a standalone application
    private static final int NUM_COLORS = 3; // the number of colors available for circles
    private static final int DISPLAY_TIME = 1000; // number of milliseconds to display (highlight) a move before actually making it
    static private final int SINGLE_CIRCLE_PENALTY = -10; // the penalty for clicking on a single circle
    private static final String HIGH_SCORE_FILE =".highscores.db"; // where high scores are kept
	static public final int defaultWidth=15, defaultHeight=10; // sizes in terms of numbers of circles
	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

    // fields
	// 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 = DEFAULT_WIDTH, height= DEFAULT_HEIGHT; // initial width and height of the array of circles
	private int width = defaultWidth, height=defaultHeight; // initial width and height of the array of circles
	private SelfAwareCircle circles[][];
	private String highScoreName = null;    // the name to use for the high score

@@ -60,19 +58,23 @@ class sg extends GUI implements ActionListener,ItemListener {
		smarterPlayerMenuItem;

	// ... Brain elements
    private Brain theBrain;                   // the Brain (automated player) for the game
    private Thread brainThread;               // the thread that will run the Brain
	Brain theBrain;                   // the Brain (automated player) for the game
	Thread brainThread;               // the thread that will run the Brain

	// Public access methods

	/*
	 * Default no-args constructor
	 */
    private sg() {
        super("Ari's sameGame");
	public sg() {
        super("Ari's samegame");
		try {
			SwingUtilities.invokeAndWait(
                    this::setupGUI
					new Runnable() {
						public void run() {
							setupGUI();
						}
					}
			);
		}
		catch (Exception e) { System.out.println("Saw exception "+e); }
@@ -132,8 +134,8 @@ class sg extends GUI implements ActionListener,ItemListener {

		circles[xx][yy].mouseEntered(null);  // pretend the mouse was pressed at location (xx,yy)
		try {
            Thread.sleep(DISPLAY_TIME);          // wait a bit for the user to register the move
        } catch (InterruptedException ignored) { }
			Thread.sleep(displayTime);          // wait a bit for the user to register the move
		} catch (InterruptedException e) { }
		circles[xx][yy].mouseExited(null);
		circles[xx][yy].mousePressed(null);
		circles[xx][yy].mouseReleased(null); // pretend that the mouse button was released at the location
@@ -146,7 +148,7 @@ class sg extends GUI implements ActionListener,ItemListener {
	    int tmp;

        if (level==1)
            return -SINGLE_CIRCLE_PENALTY;
            return -10;
        else
            tmp = (int) (level * Math.log(level));
        switch (theColor) {
@@ -195,7 +197,7 @@ class sg extends GUI implements ActionListener,ItemListener {
		menu.add(changeBoard);

		quitGame = new JMenuItem("Quit");
        quitGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.META_MASK));
		quitGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.META_MASK));
		quitGame.setMnemonic('Q');
		quitGame.addActionListener(this);
		menu.add(quitGame);
@@ -241,7 +243,7 @@ class sg extends GUI implements ActionListener,ItemListener {
		for (int jj=0; jj<height; jj++) 
			for (int ii=0; ii<width; ii++) 
			{
                CircleColor foreColor = CircleColor.randColor(NUM_COLORS);
				CircleColor foreColor = CircleColor.randColor(numColors);

				// establish the button
				circles[ii][jj] = new SelfAwareCircle(foreColor,ii,jj,this);
@@ -402,7 +404,7 @@ class sg extends GUI implements ActionListener,ItemListener {
		// if we've gotten here, it means that the game has ended
		int score = new Integer(totalPoints.getText());

        highScore hs = new highScore(HIGH_SCORE_FILE);
		highScore hs = new highScore(highScoreFile);
		hs.loadScores();

		// check the high score
@@ -438,8 +440,8 @@ class sg extends GUI implements ActionListener,ItemListener {
			System.exit(0);
		else if (source == changeBoard) {
			// modified from http://www.vbforums.com/showthread.php?t=513699
            JTextField width = new JTextField(); width.setText(""+ DEFAULT_WIDTH);
            JTextField height = new JTextField(); height.setText(""+ DEFAULT_HEIGHT);
			JTextField width = new JTextField(); width.setText(""+defaultWidth);
			JTextField height = new JTextField(); height.setText(""+defaultHeight);
			Object[] msg = {"Width:", width, "Height:", height};

			JOptionPane op = new JOptionPane(
@@ -452,7 +454,14 @@ class sg extends GUI implements ActionListener,ItemListener {
			JDialog dialog = op.createDialog(this, "Enter new board size ...");
			dialog.setVisible(true);

            int result = (Integer) op.getValue();
			int result = JOptionPane.OK_OPTION;

			try
			{
				result = (Integer) op.getValue();
			}
			catch(Exception uninitializedValue)
			{}

			if(result == JOptionPane.OK_OPTION) // i.e. effect the change
			{
@@ -505,7 +514,7 @@ class sg extends GUI implements ActionListener,ItemListener {
	public static void main(String args[]) {
		JFrame myApp = new sg();
		myApp.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        myApp.setSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
		myApp.setSize(defaultWindowWidth, defaultWindowHeight);
		myApp.setVisible(true);
	}
}
 No newline at end of file