<commit_before>package authoring_environment;

import gamedata.gamecomponents.Patch;
import gamedata.gamecomponents.Piece;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import authoring.data.PatchData;
import authoring.data.PatchTypeData;
import authoring.data.PieceData;
import authoring.data.PieceTypeData;

//TODO: REMOVE THE DUPLICATED CODE. SO MUCH.
/**
 * Authoring, engine, and player may all use this grid!!
 * 
 * @author <NAME>
 *
 */

public class GUIGrid extends SuperGrid implements Observer {

	private PieceData myPieceData;
	private PatchData myPatchData;

	public GUIGrid() {
		super();
	}

	public GUIGrid(int cols, int rows, double tileSize, String shape) {
		super(cols, rows, tileSize, shape);
		myPieceData = new PieceData();
		myPatchData = new PatchData();
	}

	public GUIGrid(int cols, int rows, double tileSize, String shape,
			PieceData pieceData, PatchData patchData) {
		super(cols, rows, tileSize, shape);
		myPieceData = pieceData;
		myPatchData = patchData;
	}

	public GUIGrid(int cols, int rows, double tileSize, String shape,
			GUIGrid copyGrid) {
		super(cols, rows, tileSize, shape);
		myPieceData = copyGrid.myPieceData;
		myPatchData = copyGrid.myPatchData;
		// removeRunOffPieces(cols, rows, myPieceData);
		// removeRunOffPieces(cols, rows, myPieceData);
	}

	/**
	 * Returns number of rows
	 * 
	 * @return int number of rows
	 */
	public int getNumRows() {
		return super.myHeight;
	}

	/**
	 * Returns number of columns
	 * 
	 * @return int number of columns
	 */
	public int getNumCols() {
		return super.myWidth;
	}

	public void addPiece(Piece pieceType, Point2D loc) {
		//System.out.println(myPieceData.getData().size());

		Piece clone = new Piece(pieceType, loc);
		myPieceData.add(clone);
		SuperTile myTile = myGrid.get((int) loc.getY()).get((int) loc.getX());
		myTile.addPieceImage(clone.getImageView());

	}

	public void addPatch(Patch patchType, Point2D loc) {
		//System.out.println(myPatchData.getData().size());

		Patch clone = new Patch(patchType, loc);
		myPatchData.add(clone);
		SuperTile myTile = myGrid.get((int) loc.getY()).get((int) loc.getX());
		myTile.addPatchImage(clone.getImageView());
	}

	private void replacePiece(Piece pieceType) {
		List<Point2D> pointsToReplace = myPieceData.replace(pieceType);
		for (Point2D loc : pointsToReplace) {
			SuperTile tile = super.findClickedTile(loc);
			tile.addPatchImage(pieceType.getImageView());
		}
	}

	private void replacePatch(Patch patchType) {
		List<Point2D> pointsToReplace = myPatchData.replace(patchType);
		//System.out.println(pointsToReplace.toString());
		for (Point2D loc : pointsToReplace) {
			SuperTile tile = super.findClickedTile(loc);
			tile.addPatchImage(patchType.getImageView());
		}
	}

	private void removePatch(PatchTypeData typeData) {
		List<Point2D> pointsToRemove = myPatchData.removeUnknown(typeData
				.getIdSet());
		for (Point2D loc : pointsToRemove) {
			SuperTile tile = super.findClickedTile(loc);
			tile.removePatchImage();
		}
	}

	/**
	 * Returns the piece at loc
	 * 
	 * @param loc
	 * @return
	 */
	public Piece getPiece(Point2D loc) {
		for (Piece p : myPieceData.getData()) {
			if ((p.getLoc().getX() == loc.getX())
					& (p.getLoc().getY() == loc.getY())) {
				return p;
			}
		}
		return null;
	}

	/**
	 * Returns the patch at loc
	 * 
	 * @param loc
	 * @return
	 */
	public Patch getPatch(Point2D loc) {
		for (Patch p : myPatchData.getData()) {
			if ((p.getLoc().getX() == loc.getX())
					& (p.getLoc().getY() == loc.getY())) {
				return p;
			}
		}
		return null;
	}

	public void removePiece(Piece p) {
		SuperTile currentTile = findClickedTile(p.getLoc());
		//System.out.println(currentTile.getLocation().getY());
		myPieceData.remove(p);
		currentTile.clearPieceImage();

	}

	/**
	 * Removes a piece at the given coordinates. NOTE: Point2D coordinates given
	 * as X = column number, Y = row number
	 * 
	 * @param coor
	 *            - Point2D containing coordinates of the piece to remove given
	 *            as [Col, Row]
	 */
	public void removePiece(Point2D coor) {
		// Piece toRemove = getPiece(Point2D coor) {
		//
		// }
	}

	/**
	 * Removes a piece at the given coordinates. NOTE: Point2D coordinates given
	 * as X = column number, Y = row number
	 * 
	 * @param coor
	 *            - Point2D containing coordinates of the piece to remove given
	 *            as [Col, Row]
	 */
	public void removePatch(Point2D loc) {

	}

	/**
	 * Gets Pieces that have been tagged for removal
	 * 
	 * @return
	 */
	public List<Piece> getRemovedPieces() {
		List<Piece> l = new ArrayList<Piece>();
		for (Piece p : myPieceData.getData()) {

			// TODO: FOR TESTING ONLY
			if (p.getStats().getValue("health") <= 0) {
				p.markForRemoval();
			}

			if (p.shouldRemove()) {
				l.add(p);
			}
		}
		return l;
	}

	/**
	 * Returns all pieces that belong to a given player
	 * 
	 * @param playerId
	 *            - ID of player
	 * @return List of pieces belonging to the player
	 */
	public List<Piece> getPlayerPieces(int playerId) {
		List<Piece> l = new ArrayList<Piece>();
		for (Piece p : myPieceData.getData()) {
			if (p.getPlayerID() == playerId) {
				l.add(p);
			}
		}
		return l;
	}

	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof PieceTypeData) {
			PieceTypeData typeData = (PieceTypeData) o;
			if (arg == null) {
				myPieceData.removeUnknown(typeData.getIdSet());
			}

			if (arg instanceof Piece) {
				Piece pieceType = (Piece) arg;
				replacePiece(pieceType);
			}
		}
		if (o instanceof PatchTypeData) {
			PatchTypeData typeData = (PatchTypeData) o;<commit_msg>fixing merge conflicts<commit_after>package authoring_environment;

import gamedata.gamecomponents.Patch;
import gamedata.gamecomponents.Piece;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import authoring.data.PatchData;
import authoring.data.PatchTypeData;
import authoring.data.PieceData;
import authoring.data.PieceTypeData;

//TODO: REMOVE THE DUPLICATED CODE. SO MUCH.
/**
 * Authoring, engine, and player may all use this grid!!
 * 
 * @author <NAME>
 *
 */

public class GUIGrid extends SuperGrid implements Observer {

	private PieceData myPieceData;
	private PatchData myPatchData;

	public GUIGrid() {
		super();
	}

	public GUIGrid(int cols, int rows, double tileSize, String shape) {
		super(cols, rows, tileSize, shape);
		myPieceData = new PieceData();
		myPatchData = new PatchData();
	}

	public GUIGrid(int cols, int rows, double tileSize, String shape,
			PieceData pieceData, PatchData patchData) {
		super(cols, rows, tileSize, shape);
		myPieceData = pieceData;
		myPatchData = patchData;
	}

	public GUIGrid(int cols, int rows, double tileSize, String shape,
			GUIGrid copyGrid) {
		super(cols, rows, tileSize, shape);
		myPieceData = copyGrid.myPieceData;
		myPatchData = copyGrid.myPatchData;
		// removeRunOffPieces(cols, rows, myPieceData);
		// removeRunOffPieces(cols, rows, myPieceData);
	}

	/**
	 * Returns number of rows
	 * 
	 * @return int number of rows
	 */
	public int getNumRows() {
		return super.myHeight;
	}

	/**
	 * Returns number of columns
	 * 
	 * @return int number of columns
	 */
	public int getNumCols() {
		return super.myWidth;
	}

	public void addPiece(Piece pieceType, Point2D loc) {
		//System.out.println(myPieceData.getData().size());

		Piece clone = new Piece(pieceType, loc);
		myPieceData.add(clone);
		SuperTile myTile = myGrid.get((int) loc.getY()).get((int) loc.getX());
		myTile.addPieceImage(clone.getImageView());

	}

	public void addPatch(Patch patchType, Point2D loc) {
		//System.out.println(myPatchData.getData().size());

		Patch clone = new Patch(patchType, loc);
		myPatchData.add(clone);
		SuperTile myTile = myGrid.get((int) loc.getY()).get((int) loc.getX());
		myTile.addPatchImage(clone.getImageView());
	}

	private void replacePiece(Piece pieceType) {
		List<Point2D> pointsToReplace = myPieceData.replace(pieceType);
		for (Point2D loc : pointsToReplace) {
			SuperTile tile = super.findClickedTile(loc);
			tile.addPatchImage(pieceType.getImageView());
		}
	}

	private void replacePatch(Patch patchType) {
		List<Point2D> pointsToReplace = myPatchData.replace(patchType);
		//System.out.println(pointsToReplace.toString());
		for (Point2D loc : pointsToReplace) {
			SuperTile tile = super.findClickedTile(loc);
			tile.addPatchImage(patchType.getImageView());
		}
	}

	private void removePatch(PatchTypeData typeData) {
		List<Point2D> pointsToRemove = myPatchData.removeUnknown(typeData
				.getIdSet());
		for (Point2D loc : pointsToRemove) {
			SuperTile tile = super.findClickedTile(loc);
			tile.removePatchImage();
		}
	}

	/**
	 * Returns the piece at loc
	 * 
	 * @param loc
	 * @return
	 */
	public Piece getPiece(Point2D loc) {
		for (Piece p : myPieceData.getData()) {
			if ((p.getLoc().getX() == loc.getX())
					& (p.getLoc().getY() == loc.getY())) {
				return p;
			}
		}
		return null;
	}

	/**
	 * Returns the patch at loc
	 * 
	 * @param loc
	 * @return
	 */
	public Patch getPatch(Point2D loc) {
		for (Patch p : myPatchData.getData()) {
			if ((p.getLoc().getX() == loc.getX())
					& (p.getLoc().getY() == loc.getY())) {
				return p;
			}
		}
		return null;
	}

	public void removePiece(Piece p) {
		SuperTile currentTile = findClickedTile(p.getLoc());
		//System.out.println(currentTile.getLocation().getY());
		myPieceData.remove(p);
		currentTile.clearPieceImage();

	}

	/**
	 * Removes a piece at the given coordinates. NOTE: Point2D coordinates given
	 * as X = column number, Y = row number
	 * 
	 * @param coor
	 *            - Point2D containing coordinates of the piece to remove given
	 *            as [Col, Row]
	 */
	public void removePiece(Point2D coor) {
		// Piece toRemove = getPiece(Point2D coor) {
		//
		// }
	}

	/**
	 * Removes a piece at the given coordinates. NOTE: Point2D coordinates given
	 * as X = column number, Y = row number
	 * 
	 * @param coor
	 *            - Point2D containing coordinates of the piece to remove given
	 *            as [Col, Row]
	 */
	public void removePatch(Point2D loc) {

	}

	/**
<<<<<<< HEAD
	 * Gets Pieces that have been tagged for removal
	 * 
	 * @return
	 */
	public List<Piece> getRemovedPieces() {
		List<Piece> l = new ArrayList<Piece>();
		for (Piece p : myPieceData.getData()) {

			// TODO: FOR TESTING ONLY
			if (p.getStats().getValue("health") <= 0) {
				p.markForRemoval();
			}

			if (p.shouldRemove()) {
				l.add(p);
			}
		}
		return l;
	}

	/**
=======
>>>>>>> 81dff75686db2e323819d437531f2a5876287f04
	 * Returns all pieces that belong to a given player
	 * 
	 * @param playerId
	 *            - ID of player
	 * @return List of pieces belonging to the player
	 */
	public List<Piece> getPlayerPieces(int playerId) {
		List<Piece> l = new ArrayList<Piece>();
		for (Piece p : myPieceData.getData()) {
			if (p.getPlayerID() == playerId) {
				l.add(p);
			}
		}
		return l;
	}

	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof PieceTypeData) {
			PieceTypeData typeData = (PieceTypeData) o;
			if (arg == null) {
				myPieceData.removeUnknown(typeData.getIdSet());
			}

			if (arg instanceof Piece) {
				Piece pieceType = (Piece) arg;
				replacePiece(pieceType);
			}
		}
		if (o instanceof PatchTypeData) {
			PatchTypeData typeData = (PatchTypeData) o;
{'fraction_non_alphanumeric': 0.12929623567921442, 'fraction_numerical': 0.005857524334568007, 'mean_word_length': 3.5210280373831777, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '115998', 'n_tokens_mistral': 4418, 'n_tokens_neox': 3928, 'n_words': 973}