<commit_before>package gui;

import gamelogic.GameArray;
import gamelogic.GameBoard;
import gamelogic.PlayerMarker;
import javafx.animation.ScaleTransition;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import logic.Controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Game board panel.
 *
 * Created by <NAME> (<EMAIL>) on 2016-05-27.
 */
public class GameBoardPanel extends BorderPane {

	private Controller controller;
	private AppWindow viewController;

	private GameBoard gameBoard;

	private GameArray gameArray;

	private int numMyMarkers;
	private int numOppMarkers;
	private long timeStart;
	private long timeEnd;

	private URL soundRes;
	private Media sound;
	private MediaPlayer mediaPlayer;
	private List<Point2D> winningRow;


	private boolean player1Turn = false;
	private List<ImageView> playerMarkers = new ArrayList<>();
	private boolean draw = false;

	public GameBoardPanel (Controller controller, AppWindow viewController) {
		this.controller = controller;
		this.viewController = viewController;
	}

	/**
	 * Starts a game.
	 *
	 * @param startPlayer boolean
	 */
	public void startGame(boolean startPlayer) {
		player1Turn = startPlayer;
		numMyMarkers = 0;
		numOppMarkers = 0;
		initGameBoard();
	}

	/**
	 * Initiates the Gameboard
	 */
	private void initGameBoard() {
		gameBoard = new GameBoard(viewController.getPanelWidth(), viewController.getPanelWidth(), 3);
		gameArray = new GameArray(gameBoard.getRows());
		gameArray.setGrowable(viewController.isGrowable());
		gameArray.setDrawable(viewController.isDrawable());
		this.setCenter(gameBoard);
		gameBoard.addEventHandler(MouseEvent.MOUSE_CLICKED, addMouseListener());
		gameBoard.setStyle("-fx-background-image: url(\"/textured_paper.png\");-fx-background-size: 600, 600;-fx-background-repeat: no-repeat;");

	}

	/**
	 * Makes a move
	 *
	 * @param col int
	 * @param row int
	 */
	public void makeMove(int col, int row) throws IOException {
		if (!checkDoubles(col, row)) {
			drawMarker(col, row);
			if (player1Turn) {
				controller.makeMove(col, row);
			}
			player1Turn = !player1Turn;
			if (isFull() && !viewController.isDrawable()) {
				playerMarkers.forEach(marker -> {
					marker.fitHeightProperty().bind(gameBoard.getCellSizeProperty().divide(2));
					marker.fitWidthProperty().bind(gameBoard.getCellSizeProperty().divide(2));
				});
				gameBoard.addEventHandler(MouseEvent.MOUSE_CLICKED, addMouseListener());
			} else if (isFull() && viewController.isDrawable()){
				this.draw = true;
			}
		}
	}

	/**
	 * Checks if gameboard is full and expands it if it's the desired behaviour.
	 *
	 * @return boolean
	 */
	private boolean isFull() throws IOException {
		if (gameArray.isGameOver()) {
			winner(0,0,numMyMarkers);
		}
		if (playerMarkers.size() == (gameBoard.getRows() * gameBoard.getRows())) {
			// Increase the gameboard's size
			gameBoard.incGameBoard();
			gameArray.growBoard(GridPane.getColumnIndex(playerMarkers.get(playerMarkers.size() - 1)),
					GridPane.getRowIndex(playerMarkers.get(playerMarkers.size() - 1)));

			int[][] tempGrid = gameArray.getGameGrid();
			playerMarkers.clear();
			gameBoard = new GameBoard(viewController.getPanelWidth(), viewController.getPanelHeight(), tempGrid[0].length);
			this.setCenter(gameBoard);
			for (int x = 0; x < tempGrid.length; x++) {
				for (int y = 0; y < tempGrid[x].length; y++) {
					if (tempGrid[x][y] == 1) {
						ImageView playerMarker = new ImageView(new PlayerMarker().placeMarker(1));
						gameBoard.add(playerMarker, x, y);
						GridPane.setHalignment(playerMarker, HPos.CENTER);
						//Circle playerMarker = new PlayerMarker().placeMarker(1);
						//gameBoard.add(playerMarker, x, y);
						playerMarkers.add(playerMarker);
					} else if (tempGrid[x][y] == 2) {
						ImageView playerMarker = new ImageView(new PlayerMarker().placeMarker(2));
						gameBoard.add(playerMarker, x, y);
						GridPane.setHalignment(playerMarker, HPos.CENTER);
						playerMarkers.add(playerMarker);
						//Circle playerMarker = new PlayerMarker().placeMarker(2);
						//gameBoard.add(playerMarker, x, y);
						//playerMarkers.add(playerMarker);
					}
				}
			}
			return true;
		}
		return false;
	}

	/**
	 * Checks if there is a marker in the desired spot allready<commit_msg>Fixed paper background after gameboard grow<commit_after>package gui;

import gamelogic.GameArray;
import gamelogic.GameBoard;
import gamelogic.PlayerMarker;
import javafx.animation.ScaleTransition;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import logic.Controller;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Game board panel.
 *
 * Created by <NAME> (<EMAIL>) on 2016-05-27.
 */
public class GameBoardPanel extends BorderPane {

	private Controller controller;
	private AppWindow viewController;

	private GameBoard gameBoard;

	private GameArray gameArray;

	private int numMyMarkers;
	private int numOppMarkers;
	private long timeStart;
	private long timeEnd;

	private URL soundRes;
	private Media sound;
	private MediaPlayer mediaPlayer;
	private List<Point2D> winningRow;


	private boolean player1Turn = false;
	private List<ImageView> playerMarkers = new ArrayList<>();
	private boolean draw = false;

	public GameBoardPanel (Controller controller, AppWindow viewController) {
		this.controller = controller;
		this.viewController = viewController;
	}

	/**
	 * Starts a game.
	 *
	 * @param startPlayer boolean
	 */
	public void startGame(boolean startPlayer) {
		player1Turn = startPlayer;
		numMyMarkers = 0;
		numOppMarkers = 0;
		initGameBoard();
	}

	/**
	 * Initiates the Gameboard
	 */
	private void initGameBoard() {
		gameBoard = new GameBoard(viewController.getPanelWidth(), viewController.getPanelWidth(), 3);
		gameArray = new GameArray(gameBoard.getRows());
		gameArray.setGrowable(viewController.isGrowable());
		gameArray.setDrawable(viewController.isDrawable());
		this.setCenter(gameBoard);
		gameBoard.addEventHandler(MouseEvent.MOUSE_CLICKED, addMouseListener());
		gameBoard.setStyle("-fx-background-image: url(\"/textured_paper.png\");-fx-background-size: 600, 600;-fx-background-repeat: no-repeat;");

	}

	/**
	 * Makes a move
	 *
	 * @param col int
	 * @param row int
	 */
	public void makeMove(int col, int row) throws IOException {
		if (!checkDoubles(col, row)) {
			drawMarker(col, row);
			if (player1Turn) {
				controller.makeMove(col, row);
			}
			player1Turn = !player1Turn;
			if (isFull() && !viewController.isDrawable()) {
				playerMarkers.forEach(marker -> {
					marker.fitHeightProperty().bind(gameBoard.getCellSizeProperty().divide(2));
					marker.fitWidthProperty().bind(gameBoard.getCellSizeProperty().divide(2));
				});
				gameBoard.addEventHandler(MouseEvent.MOUSE_CLICKED, addMouseListener());
			} else if (isFull() && viewController.isDrawable()){
				this.draw = true;
			}
		}
	}

	/**
	 * Checks if gameboard is full and expands it if it's the desired behaviour.
	 *
	 * @return boolean
	 */
	private boolean isFull() throws IOException {
		if (gameArray.isGameOver()) {
			winner(0,0,numMyMarkers);
		}
		if (playerMarkers.size() == (gameBoard.getRows() * gameBoard.getRows())) {
			// Increase the gameboard's size
			gameBoard.incGameBoard();
			gameArray.growBoard(GridPane.getColumnIndex(playerMarkers.get(playerMarkers.size() - 1)),
					GridPane.getRowIndex(playerMarkers.get(playerMarkers.size() - 1)));

			int[][] tempGrid = gameArray.getGameGrid();
			playerMarkers.clear();
			gameBoard = new GameBoard(viewController.getPanelWidth(), viewController.getPanelHeight(), tempGrid[0].length);
			gameBoard.setStyle("-fx-background-image: url(\"/textured_paper.png\");-fx-background-size: 600, 600;-fx-background-repeat: no-repeat;");
			this.setCenter(gameBoard);
			for (int x = 0; x < tempGrid.length; x++) {
				for (int y = 0; y < tempGrid[x].length; y++) {
					if (tempGrid[x][y] == 1) {
						ImageView playerMarker = new ImageView(new PlayerMarker().placeMarker(1));
						gameBoard.add(playerMarker, x, y);
						GridPane.setHalignment(playerMarker, HPos.CENTER);
						//Circle playerMarker = new PlayerMarker().placeMarker(1);
						//gameBoard.add(playerMarker, x, y);
						playerMarkers.add(playerMarker);
					} else if (tempGrid[x][y] == 2) {
						ImageView playerMarker = new ImageView(new PlayerMarker().placeMarker(2));
						gameBoard.add(playerMarker, x, y);
						GridPane.setHalignment(playerMarker, HPos.CENTER);
						playerMarkers.add(playerMarker);
						//Circle playerMarker = new PlayerMarker().placeMarker(2);
						//gameBoard.add(playerMarker, x, y);
						//playerMarkers.add(playerMarker);
					}
				}
			}
			return true;
		}
		return false;
	}

	/**
	 * Checks if there is a marker in the desired spot allready
{'fraction_non_alphanumeric': 0.12752245113576333, 'fraction_numerical': 0.008874801901743265, 'mean_word_length': 4.345002823263693, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '3005145', 'n_tokens_mistral': 3404, 'n_tokens_neox': 2945, 'n_words': 643}