Commit 0a36a8ee authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Cleaning up.

parent c9b092e2
Loading
Loading
Loading
Loading
+93 −73
Original line number Diff line number Diff line
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<myGUI.boardWidth(); xx++)
			for (int yy=0; yy<myGUI.boardHeight(); yy++)
				currState.modify(xx, yy, myGUI.colorAt(xx, myGUI.boardHeight()-yy-1));
				currBoard.modify(xx, yy, myGUI.colorAt(xx, myGUI.boardHeight()-yy-1));

		// Keep making moves until the game is over
		while (!allDone && !myGUI.gameOverQ()) {
				pos nextMove = chooseMove();
				Board.Pos nextMove = chooseMove();
				myGUI.makeMove(nextMove.xx, nextMove.yy); // i.e. click on the lower left corner
			}
	}

	// internal classes

	// PRIVATE METHODS
	/**
	 * Stores an (xx,yy) coordinate
	 * Chooses the next move to make.
	 * @return the move chosen, in Board coordinates
	 */
	static class pos {
		final int xx;
		int yy;
		pos(int xx, int yy) {this.xx=xx; this.yy=yy;}
	private Board.Pos chooseMove() {
		// greedy choice
		int max=0;                  // the maximum number of points for the best position found
		Board.Pos bestPos = new Board.Pos(0, 0); // the best position found
		Board currStateCopy = new Board(currBoard);

		for (int xx=0; xx< currBoard.columns(); xx++)
			for (int yy=0; yy< currBoard.rows(xx); yy++) {
				if (currStateCopy.get(xx,yy)!= CircleColor.NONE) {
					Board test = new Board(currStateCopy);
					currStateCopy.clickNodeHelper(xx, yy, test.get(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
					if (count > 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 <CircleColor>> 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
@@ -87,22 +134,22 @@ public class lazyBrain extends Brain {
		/**
		 * default constructor
		 */
		board() {
		Board() {
			this(myGUI.boardWidth(),myGUI.boardHeight());
		}

		/**
		 * copy constructor
		 * @param basis the board to copy
		 * @param baseBoard the board to copy
		 */
		board(board basis) {
			// allocate space
			this(basis.width, basis.height);
		Board(Board baseBoard) {
			// allocate memory for the new board
			this(baseBoard.width, baseBoard.height);

			// copy over all the specific items
			for (int xx=0; xx<columns(); xx++)
				for (int yy=0; yy<rows(xx); yy++)
					modify(xx,yy,basis.get(xx, yy));
					modify(xx,yy,baseBoard.get(xx, yy));
		}

		/**
@@ -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
		 * Stores an (xx,yy) position on the board.
		 */
	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<currState.columns(); xx++)
			for (int yy=0; yy<currState.rows(xx); yy++) {
				if (currStateCopy.get(xx,yy)!= CircleColor.NONE) {
					board test = new board(currStateCopy);
					currStateCopy.clickNodeHelper(xx, yy, test.get(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
					if (count > max) {
						// record a new best move
						max = count;
						bestPos = new pos(xx, yy);
					}

		static class Pos {
			final int xx;
			final int yy;
			Pos(int xx, int yy) {this.xx=xx; this.yy=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;
	}

}
Loading