From 109440bd1d2e955769886d7cb03edea0ab024fc2 Mon Sep 17 00:00:00 2001 From: Ian Danahy Date: Mon, 5 Apr 2021 06:05:32 -0400 Subject: [PATCH 1/3] Added functionality for moving black pieces so that players can play against the computer --- src/main/java/game/Main.java | 111 ++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/src/main/java/game/Main.java b/src/main/java/game/Main.java index 7b2b755..2aba9e0 100644 --- a/src/main/java/game/Main.java +++ b/src/main/java/game/Main.java @@ -1,42 +1,69 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package game; - -import java.awt.EventQueue; -import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.swing.JFrame; - -/** - * - * @author gersc - */ -public class Main extends JFrame { - - public Main() { - try { - this.add( new Board()); - } catch (IOException ex) { - Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); - } - this.pack(); - this.setResizable(false); - this.setTitle( "CHESS" ); - this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); - this.setLocationRelativeTo( null ); - } - - public static void main(String[] args) { - - EventQueue.invokeLater(() -> { - Main main = new Main(); - main.setVisible( true ); - }); - - } - -} +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package game; + +import java.awt.BorderLayout; +import java.awt.EventQueue; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +/** + * + * @author gersc + */ +public class Main extends JFrame { + + public Main() { + + /// \ref T7_1 Popup will ask users to choose the number of players + boolean onePlayer = isOnePlayerGame(); + + try { + this.add( new Board(onePlayer)); + } catch (IOException ex) { + Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + } + + this.pack(); + this.setResizable(false); + this.setTitle( "CHESS" ); + this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); + this.setLocationRelativeTo( null ); + + } + + /// \ref T7_2 Popup for users to choose number of players + //will return true for One player, false for Two player + public boolean isOnePlayerGame() { + String[] options = {"1 Player", "2 Players"}; + int playerChoice = JOptionPane.showOptionDialog(null, "Please choose number of Players", + "How Many Players?", + JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); + if (playerChoice == 0) { + return true; + } + else { + return false; + } + } + + + public static void main(String[] args) { + + EventQueue.invokeLater(() -> { + Main main = new Main(); + main.setVisible( true ); + }); + + } + +} From 5e3afe80b09678b89a5426c3714c8541648efd45 Mon Sep 17 00:00:00 2001 From: Ian Danahy Date: Thu, 15 Apr 2021 19:30:44 -0400 Subject: [PATCH 2/3] Added functionality to play against a computer player. This allows the game to be played without a second person. --- src/main/java/game/Opponent.java | 206 +++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 src/main/java/game/Opponent.java diff --git a/src/main/java/game/Opponent.java b/src/main/java/game/Opponent.java new file mode 100644 index 0000000..a6155c8 --- /dev/null +++ b/src/main/java/game/Opponent.java @@ -0,0 +1,206 @@ +package game; + +import java.util.ArrayList; + +/// \imp \ref T1_1 \ref T1_2 \ref T1_3 +/// Class that allows the game to be played single player. +/// Holds the methods for the computer choosing a piece to move and moving that piece. +/// These functions allow us to simulate a computer player so that the game can +/// be played without a second person. +public class Opponent { + /// \ref T1_1 \ref T1_2 + ///The computer player must have a list of which pieces it can move so that it can + ///choose a piece to move. This cuts down on time checking possible moves on pieces that + ///cannot move at all. + private ArrayList
moveablePieces = new ArrayList(); + /// \ref T1_1 \ref T1_2 + ///The computer player must know where the player's pieces are in order to choose a proper move + private ArrayList
playerPieces; + /// \ref T1_1 \ref T1_2 + ///The computer player needs to know the current board state in order to choose the + ///correct move to make. + private Field[][] board; + + public Opponent(Field[][] arrayBoard) { + this.board = arrayBoard; + } + + /// \imp \ref T1_1 \ref T1_2 \ref T1_3 + ///The computer player must be able to choose a piece to move, choose where to move it, + ///and then move that piece + public void takeTurn(ArrayList
computerPieces, ArrayList
playerPieces) { + this.playerPieces = playerPieces; + choosePieceToMove(computerPieces); + } + + /// \imp \ref T1_3 + /// The computer player must be able to move their pieces to a new position + private void move(Figure piece, int newXCoord, int newYCoord) { + Field oldField = piece.getField(); + System.out.println("Computer moving piece at " + oldField.getXCord() + ", " + oldField.getYCord()); + System.out.println("Moving to " + newXCoord + ", " + newYCoord); + Field newField = board[newXCoord][newYCoord]; + if(newField.getFigure() != null) { + playerPieces.remove(newField.getFigure()); + } + oldField.getFigure().removeTexture(); + newField.setFigure(piece); + oldField.removeFigure(); + } + + ///\imp \ref T1_1 \ref T1_2 + ///Determine which piece should be moved by the computer. + ///The computer player will use the following rules to determine which piece to move next: + ///1. It will try to get out of check, if necessary + ///2. It will try to take an opponent's piece, if possible. + ///3. It will try to protect it's own pieces, if possible. + ///4. It will move randomly if it cannot do any of the above. + private void choosePieceToMove(ArrayList
pieces) { + moveablePieces.clear(); + for(Figure piece : pieces) { + if(canMove(piece)) { + moveablePieces.add(piece); + } + } + + if(isInCheck(board)) { + escapeCheck(); + return; + } + + for(Figure piece : moveablePieces) { + if(canTakeOpponentPiece(piece)) { + return; + } + } + + for(Figure piece : moveablePieces) { + if(canEscapeFromBeingTaken(piece)) { + return; + } + } + + randomPiece(); + return; + } + + ///\ref T1_1 The computer player has to be able to determine if the piece is + ///able to move before it can decide which piece to move + private boolean canMove(Figure piece) { + for (int xCoord = 0; xCoord < board.length; xCoord++) { + for(int yCoord = 0; yCoord < board[xCoord].length; yCoord++) { + Field localField = board[xCoord][yCoord]; + if(piece.isMovePossible(localField)) + return true; + } + } + return false; + } + + ///\ref T1_1 \ref T1_2 + ///In order to make the correct move, the computer player + ///must be able to determine whether or not it is currently in check + private boolean isInCheck(Field[][] board) { + for(Figure piece : playerPieces) { + for (int xCoord = 0; xCoord < board.length; xCoord++) { + for(int yCoord = 0; yCoord < board[xCoord].length; yCoord++) { + Field localField = board[xCoord][yCoord]; + if(piece.isMovePossible(localField) && localField.getFigure() instanceof King) { + System.out.println("Computer In check!"); + return true; + } + } + } + } + return false; + } + + ///\ref T1_1 \ref T1_2 + ///In order to choose the correct piece and position to a move, + ///the computer player must first attempt to escape from being in check + private void escapeCheck() { + for(Figure piece : moveablePieces) { + for (int xCoord = 0; xCoord < board.length; xCoord++) { + for(int yCoord = 0; yCoord < board[xCoord].length; yCoord++) { + Field localField = board[xCoord][yCoord]; + if(piece.isMovePossible(localField)) { + Figure temp = localField.getFigure(); + Field oldField = piece.getField(); + if(temp != null) { + playerPieces.remove(temp); + } + oldField.getFigure().removeTexture(); + localField.setFigure(piece); + oldField.removeFigure(); + if(isInCheck(board)) { + if(temp == null) { + localField.getFigure().removeTexture(); + oldField.setFigure(piece); + localField.removeFigure(); + } + else { + localField.setFigure(temp); + oldField.setFigure(piece); + playerPieces.add(temp); + } + } + else { + return; + } + + } + } + } + } + } + + ///\ref T1_1 + ///The computer player has to determine if a piece is able to take one of the + ///player's pieces in order to choose which piece to move + private boolean canTakeOpponentPiece(Figure piece) { + for (int xCoord = 0; xCoord < board.length; xCoord++) { + for(int yCoord = 0; yCoord < board[xCoord].length; yCoord++) { + Field localField = board[xCoord][yCoord]; + if(piece.isMovePossible(localField) && localField.getFigure() != null){ + move(piece, xCoord, yCoord); + return true; + } + } + } + return false; + } + + ///\ref T1_1 The computer player has to determine if a piece is able to escape + ///being taken by the player in order to choose which piece to move + private boolean canEscapeFromBeingTaken(Figure piece) { + for(Figure playerPiece : playerPieces) { + if(playerPiece.isMovePossible(piece.getField())) { + for (int xCoord = 0; xCoord < board.length; xCoord++) { + for(int yCoord = 0; yCoord < board[xCoord].length; yCoord++) { + Field field = board[xCoord][yCoord]; + if(piece.isMovePossible(field)) { + move(piece, xCoord, yCoord); + return true; + } + } } + } + } + return false; + } + + ///\ref T1_1 + ///If none of the other conditions are met, the computer player still has to + ///choose a piece, so it will choose a random piece to move + private void randomPiece() { + Figure piece = moveablePieces.get((int)(Math.random() * moveablePieces.size())); + for (int xCoord = 0; xCoord < board.length; xCoord++) { + for(int yCoord = 0; yCoord < board[xCoord].length; yCoord++) { + Field field = board[xCoord][yCoord]; + if(piece.isMovePossible(field)) { + move(piece, xCoord, yCoord); + return; + } + } + } + } +} From 4105da490c222643e34639241cf3fe76b07c7722 Mon Sep 17 00:00:00 2001 From: Ian Danahy Date: Thu, 15 Apr 2021 19:30:57 -0400 Subject: [PATCH 3/3] Added functionality to play against a computer player. This allows the game to be played without a second person. --- src/main/java/game/Board.java | 750 +++++++++++++++++----------------- 1 file changed, 386 insertions(+), 364 deletions(-) diff --git a/src/main/java/game/Board.java b/src/main/java/game/Board.java index 513a720..2025e3a 100644 --- a/src/main/java/game/Board.java +++ b/src/main/java/game/Board.java @@ -1,364 +1,386 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package game; - -import java.awt.Color; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import javax.swing.*; - -/** - * - * @author gersc - */ -public class Board extends JPanel { - - private Field[][] arrayBoard; - - private ArrayList
destroyedFiguresList; - private ArrayList
whiteFiguresList; - private ArrayList
blackFiguresList; - Field field = null; - Field oldField = null; - int saveYCoord; - int saveXCoord; - boolean isWhitesTurn = true; - InformationBoard infoBoard; - Field collisionField; - - private boolean isSelected = false; - - public Board() throws IOException { - - this.initBoard(); - } - - private void initBoard() { - - this.setSize(800, 800); - - BoardListener boardlistener = new BoardListener(); - arrayBoard = new Field[8][8]; - destroyedFiguresList = new ArrayList<>(); - blackFiguresList = new ArrayList<>(); - whiteFiguresList = new ArrayList<>(); - - this.setLayout(new java.awt.GridLayout(8, 8)); - boolean black = true; - - for (int yCord = 0; yCord < arrayBoard.length; yCord++) { - for (int xCord = 0; xCord < arrayBoard.length; xCord++) { - - Field field = new Field(this, black, xCord, yCord); - - field.addActionListener(boardlistener); - field.setBorder(null); - arrayBoard[xCord][yCord] = field; - - field.setStandartColor(); - - if (yCord == 1) { - Pawn pawn = new Pawn(xCord, yCord, true, field); - field.setFigure(pawn); - } else if (yCord == 0) { - switch (xCord) { - case 0: - case 7: - Rook rook = new Rook(xCord, yCord, true, field); - field.setFigure(rook); - break; - case 1: - case 6: - Knight knight = new Knight(xCord, yCord, true, field); - field.setFigure(knight); - break; - case 2: - case 5: - Bishop bishop = new Bishop(xCord, yCord, true, field); - field.setFigure(bishop); - //Laeufer - break; - case 3: - Queen queen = new Queen(xCord, yCord, true, field); - field.setFigure(queen); - //Queen - break; - default: - King king = new King(xCord, yCord, true, field); - field.setFigure(king); - // King - break; - } - } else if (yCord == 6) { - Pawn pawn = new Pawn(xCord, yCord, false, field); - field.setFigure(pawn); - } else if (yCord == 7) { - switch (xCord) { - case 0: - case 7: - Rook rook = new Rook(xCord, yCord, false, field); - field.setFigure(rook); - break; - case 1: - case 6: - Knight knight = new Knight(xCord, yCord, false, field); - field.setFigure(knight); - break; - case 2: - case 5: - Bishop bishop = new Bishop(xCord, yCord, false, field); - field.setFigure(bishop); - //Laeufer - break; - case 3: - Queen queen = new Queen(xCord, yCord, false, field); - field.setFigure(queen); - //Queen - break; - default: - King king = new King(xCord, yCord, false, field); - field.setFigure(king); - // King - break; - } - } - - this.add(field); - - if (field.getFigure() != null) { - if (field.getFigure().getIsBlack()) { - blackFiguresList.add(field.getFigure()); - } else { - whiteFiguresList.add(field.getFigure()); - } - } - - black = !black; - } - black = !black; - } - - this.printActivePlayer(); - - } - - private class BoardListener implements java.awt.event.ActionListener { - - /* - * Executed when a field is pressed - */ - public void actionPerformed(java.awt.event.ActionEvent event) { - - for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { - for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { - if (event.getSource() == arrayBoard[xCoord][yCoord]) { - field = arrayBoard[xCoord][yCoord]; - saveXCoord = xCoord; - saveYCoord = yCoord; - System.out.println("X: " + xCoord + ", Y: " + yCoord); - break; - } - } - } - - printActivePlayer(); - - /* - * Executed when a pushed friendly unit is on it or the field is empty and no unit is selected - */ - if (field.getFigure() != null && isWhitesTurn != field.getFigure().getIsBlack()) { - this.setCheckedFalse(); - this.removeMarker(); - oldField = field; - isSelected = true; - - if (field.getFigure() instanceof King) { - if (field.getFigure().getIsBlack()) { - this.setCheckedFieldsKing(whiteFiguresList, false); - } else { - this.setCheckedFieldsKing(blackFiguresList, true); - } - } - - /* - * Checks all fields and marks them with the right color - */ - for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { - for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { - Field localField = arrayBoard[xCoord][yCoord]; - if (field.getFigure().isMovePossible(localField)) { - if (localField.getFigure() == null) { - localField.possibleHighlightOn(); - } else if (localField.getFigure().getIsBlack() != field.getFigure().getIsBlack()) { - if (localField.getFigure() instanceof King) { - localField.checkHighlightOn(); - } else { - localField.collisionHighlightOn(); - } - } - } - } - } - oldField.selectionHighlightOn(); - } /* - * Executed when a unit is selected and the pushed field is valid - */ else if (isSelected && oldField.getFigure().isMoveValid(field)) { - this.removeMarker(); - if (field.getFigure() != null) { - destroyedFiguresList.add(field.getFigure()); - } - oldField.getFigure().removeTexture(); - if (oldField.getFigure() instanceof Pawn) { - Pawn localPawn = (Pawn) oldField.getFigure(); - localPawn.setWasMoved(true); - } - - field.setFigure(oldField.getFigure()); - oldField.removeFigure(); - isSelected = false; - - System.out.println(Arrays.toString(destroyedFiguresList.toArray())); - - this.setCheckedFields(whiteFiguresList, false); - this.setCheckedFields(blackFiguresList, true); - if (isWhitesTurn) { - if (!this.checkCheckmated(true)) { - System.out.println("Player White won!"); - System.exit(0); - } - } else { - if (!this.checkCheckmated(false)) { - System.out.println("Player Black won!"); - System.exit(0); - } - } - isWhitesTurn = !isWhitesTurn; - } - } - - /* - * Helpermethod, which removes all markers - */ - private void removeMarker() { - for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { - for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { - Field localField = arrayBoard[xCoord][yCoord]; - - localField.setStandartColor(); - - } - } - } - - public void setCheckedFieldsKing(ArrayList teamList, boolean black) { - ArrayList
localteamList = teamList; - - for (Figure figure : localteamList) { - for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { - for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { - Field field = arrayBoard[xCoord][yCoord]; - if (figure.isMovePossible(field)) { - field.checkHighlightOn(); - if (black) { - field.setCheckedByBlack(true); - } else { - field.setCheckedByWhite(true); - } - } - } - } - } - } - - public void setCheckedFields(ArrayList teamList, boolean black) { - ArrayList
localteamList = teamList; - - for (Figure figure : localteamList) { - for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { - for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { - Field field = arrayBoard[xCoord][yCoord]; - if (figure.isMovePossible(field)) { - if (black) { - field.setCheckedByBlack(true); - } else { - field.setCheckedByWhite(true); - } - } - } - } - } - } - - private void setCheckedFalse() { - for (int y = 0; y < arrayBoard.length; y++) { - for (int x = 0; x < arrayBoard.length; x++) { - Field field = arrayBoard[x][y]; - field.setCheckedByBlack(false); - field.setCheckedByWhite(false); - } - } - } - - private boolean checkCheckmated(boolean blackKing) { - King king = null; - if (blackKing) { - for (Figure figure : blackFiguresList) { - if (figure instanceof King) { - king = (King) figure; - } - } - for (int y = 0; y < arrayBoard.length; y++) { - for (int x = 0; x < arrayBoard.length; x++) { - Field field = arrayBoard[x][y]; - if (king.isMovePossible(field)) { - System.out.println("black can move"); - return true; - } - } - } - - if (king.getField().isCheckedByWhite()) { - return false; - } - } else { - for (Figure figure : whiteFiguresList) { - if (figure instanceof King) { - king = (King) figure; - } - } - for (int y = 0; y < arrayBoard.length; y++) { - for (int x = 0; x < arrayBoard.length; x++) { - Field field = arrayBoard[x][y]; - if (king.isMovePossible(field)) { - System.out.println("white can move"); - return true; - } - } - } - - if (king.getField().isCheckedByBlack()) { - return false; - } - } - - return true; - } - } - - public Field[][] getArrayChessBoard() { - return arrayBoard; - } - - private void printActivePlayer() { - if (isWhitesTurn) { - System.out.println("It's White's turn!"); - } else { - System.out.println("It's Black's turn!"); - } - } - -} +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package game; + +import java.awt.Color; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import javax.swing.*; + +/** + * + * @author gersc + */ +public class Board extends JPanel { + + private Field[][] arrayBoard; + + private ArrayList
destroyedFiguresList; + private ArrayList
whiteFiguresList; + private ArrayList
blackFiguresList; + Field field = null; + Field oldField = null; + int saveYCoord; + int saveXCoord; + boolean isWhitesTurn = true; + boolean onePlayer; + InformationBoard infoBoard; + Field collisionField; + Opponent opponent = null; + + private boolean isSelected = false; + + public Board(boolean onePlayer) throws IOException { + + this.onePlayer = onePlayer; + this.initBoard(); + } + + private void initBoard() { + + this.setSize(800, 800); + + BoardListener boardlistener = new BoardListener(); + arrayBoard = new Field[8][8]; + destroyedFiguresList = new ArrayList<>(); + blackFiguresList = new ArrayList<>(); + whiteFiguresList = new ArrayList<>(); + + if(onePlayer) { + opponent = new Opponent(arrayBoard); + } + + this.setLayout(new java.awt.GridLayout(8, 8)); + boolean black = true; + + for (int yCord = 0; yCord < arrayBoard.length; yCord++) { + for (int xCord = 0; xCord < arrayBoard.length; xCord++) { + + Field field = new Field(this, black, xCord, yCord); + + field.addActionListener(boardlistener); + field.setBorder(null); + arrayBoard[xCord][yCord] = field; + + field.setStandartColor(); + + if (yCord == 1) { + Pawn pawn = new Pawn(xCord, yCord, true, field); + field.setFigure(pawn); + } else if (yCord == 0) { + switch (xCord) { + case 0: + case 7: + Rook rook = new Rook(xCord, yCord, true, field); + field.setFigure(rook); + break; + case 1: + case 6: + Knight knight = new Knight(xCord, yCord, true, field); + field.setFigure(knight); + break; + case 2: + case 5: + Bishop bishop = new Bishop(xCord, yCord, true, field); + field.setFigure(bishop); + //Laeufer + break; + case 3: + Queen queen = new Queen(xCord, yCord, true, field); + field.setFigure(queen); + //Queen + break; + default: + King king = new King(xCord, yCord, true, field); + field.setFigure(king); + // King + break; + } + } else if (yCord == 6) { + Pawn pawn = new Pawn(xCord, yCord, false, field); + field.setFigure(pawn); + } else if (yCord == 7) { + switch (xCord) { + case 0: + case 7: + Rook rook = new Rook(xCord, yCord, false, field); + field.setFigure(rook); + break; + case 1: + case 6: + Knight knight = new Knight(xCord, yCord, false, field); + field.setFigure(knight); + break; + case 2: + case 5: + Bishop bishop = new Bishop(xCord, yCord, false, field); + field.setFigure(bishop); + //Laeufer + break; + case 3: + Queen queen = new Queen(xCord, yCord, false, field); + field.setFigure(queen); + //Queen + break; + default: + King king = new King(xCord, yCord, false, field); + field.setFigure(king); + // King + break; + } + } + + this.add(field); + + if (field.getFigure() != null) { + if (field.getFigure().getIsBlack()) { + blackFiguresList.add(field.getFigure()); + } else { + whiteFiguresList.add(field.getFigure()); + } + } + + black = !black; + } + black = !black; + } + + this.printActivePlayer(); + + } + + private class BoardListener implements java.awt.event.ActionListener { + + /* + * Executed when a field is pressed + */ + public void actionPerformed(java.awt.event.ActionEvent event) { + + for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { + for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { + if (event.getSource() == arrayBoard[xCoord][yCoord]) { + field = arrayBoard[xCoord][yCoord]; + saveXCoord = xCoord; + saveYCoord = yCoord; + System.out.println("X: " + xCoord + ", Y: " + yCoord); + break; + } + } + } + + printActivePlayer(); + + /* + * Executed when a pushed friendly unit is on it or the field is empty and no unit is selected + */ + if (field.getFigure() != null && isWhitesTurn != field.getFigure().getIsBlack()) { + this.setCheckedFalse(); + this.removeMarker(); + oldField = field; + isSelected = true; + + if (field.getFigure() instanceof King) { + if (field.getFigure().getIsBlack()) { + this.setCheckedFieldsKing(whiteFiguresList, false); + } else { + this.setCheckedFieldsKing(blackFiguresList, true); + } + } + + /* + * Checks all fields and marks them with the right color + */ + for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { + for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { + Field localField = arrayBoard[xCoord][yCoord]; + if (field.getFigure().isMovePossible(localField)) { + if (localField.getFigure() == null) { + localField.possibleHighlightOn(); + } else if (localField.getFigure().getIsBlack() != field.getFigure().getIsBlack()) { + if (localField.getFigure() instanceof King) { + localField.checkHighlightOn(); + } else { + localField.collisionHighlightOn(); + } + } + } + } + } + oldField.selectionHighlightOn(); + } /* + * Executed when a unit is selected and the pushed field is valid + */ else if (isSelected && oldField.getFigure().isMoveValid(field)) { + this.removeMarker(); + if (field.getFigure() != null) { + destroyedFiguresList.add(field.getFigure()); + if(field.getFigure().isBlack) { + blackFiguresList.remove(field.getFigure()); + } + else { + whiteFiguresList.remove(field.getFigure()); + } + } + oldField.getFigure().removeTexture(); + if (oldField.getFigure() instanceof Pawn) { + Pawn localPawn = (Pawn) oldField.getFigure(); + localPawn.setWasMoved(true); + } + + field.setFigure(oldField.getFigure()); + oldField.removeFigure(); + isSelected = false; + + System.out.println(Arrays.toString(destroyedFiguresList.toArray())); + + this.setCheckedFields(whiteFiguresList, false); + this.setCheckedFields(blackFiguresList, true); + if (isWhitesTurn) { + if (!this.checkCheckmated(true)) { + System.out.println("Player White won!"); + System.exit(0); + } + } else { + if (!this.checkCheckmated(false)) { + System.out.println("Player Black won!"); + System.exit(0); + } + } + + //if playing against computer, have them takeTurn + if(onePlayer) { + opponent.takeTurn(blackFiguresList, whiteFiguresList); + } + //otherwise, switch which player's turn it is + else { + isWhitesTurn = !isWhitesTurn; + } + + } + } + + /* + * Helpermethod, which removes all markers + */ + private void removeMarker() { + for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { + for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { + Field localField = arrayBoard[xCoord][yCoord]; + + localField.setStandartColor(); + + } + } + } + + public void setCheckedFieldsKing(ArrayList teamList, boolean black) { + ArrayList
localteamList = teamList; + + for (Figure figure : localteamList) { + for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { + for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { + Field field = arrayBoard[xCoord][yCoord]; + if (figure.isMovePossible(field)) { + field.checkHighlightOn(); + if (black) { + field.setCheckedByBlack(true); + } else { + field.setCheckedByWhite(true); + } + } + } + } + } + } + + public void setCheckedFields(ArrayList teamList, boolean black) { + ArrayList
localteamList = teamList; + + for (Figure figure : localteamList) { + for (int yCoord = 0; yCoord < arrayBoard.length; yCoord++) { + for (int xCoord = 0; xCoord < arrayBoard.length; xCoord++) { + Field field = arrayBoard[xCoord][yCoord]; + if (figure.isMovePossible(field)) { + if (black) { + field.setCheckedByBlack(true); + } else { + field.setCheckedByWhite(true); + } + } + } + } + } + } + + private void setCheckedFalse() { + for (int y = 0; y < arrayBoard.length; y++) { + for (int x = 0; x < arrayBoard.length; x++) { + Field field = arrayBoard[x][y]; + field.setCheckedByBlack(false); + field.setCheckedByWhite(false); + } + } + } + + private boolean checkCheckmated(boolean blackKing) { + King king = null; + if (blackKing) { + for (Figure figure : blackFiguresList) { + if (figure instanceof King) { + king = (King) figure; + } + } + for (int y = 0; y < arrayBoard.length; y++) { + for (int x = 0; x < arrayBoard.length; x++) { + Field field = arrayBoard[x][y]; + if (king.isMovePossible(field)) { + System.out.println("black can move"); + return true; + } + } + } + + if (king.getField().isCheckedByWhite()) { + return false; + } + } else { + for (Figure figure : whiteFiguresList) { + if (figure instanceof King) { + king = (King) figure; + } + } + for (int y = 0; y < arrayBoard.length; y++) { + for (int x = 0; x < arrayBoard.length; x++) { + Field field = arrayBoard[x][y]; + if (king.isMovePossible(field)) { + System.out.println("white can move"); + return true; + } + } + } + + if (king.getField().isCheckedByBlack()) { + return false; + } + } + + return true; + } + } + + public Field[][] getArrayChessBoard() { + return arrayBoard; + } + + private void printActivePlayer() { + if (isWhitesTurn) { + System.out.println("It's White's turn!"); + } else { + System.out.println("It's Black's turn!"); + } + } + +}