Commit 37d719c1 authored by Omid Sayfun's avatar Omid Sayfun

Final Looks Checked

parent 2a18e034
Pipeline #617 canceled with stages
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.*;
import lab.game.*;
......@@ -13,6 +15,10 @@ public class Main{
Chess newChess = new Chess();
newChess.run();
}
public static void rerun(){
Chess newChess = new Chess();
newChess.run();
}
}
/**
......@@ -87,6 +93,7 @@ class Chess implements MouseListener{
private String color = "W";
private boolean isSelected = false;
private newJButton inMove = null;
private JFrame frame;
private ArrayList<newJButton> btns = new ArrayList<newJButton>();
private ArrayList<newJButton> whiteLost = new ArrayList<>();
private ArrayList<newJButton> blackLost = new ArrayList<>();
......@@ -178,7 +185,7 @@ class Chess implements MouseListener{
if( (i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(4,51,106));
btn.setBackground(new Color(46, 83,106));
}
boardPanel.add(btn);
}
......@@ -187,34 +194,40 @@ class Chess implements MouseListener{
JPanel leftDivider = new JPanel(new GridLayout(3, 1));
// Top Left
JPanel topLeft = new JPanel(new GridLayout(2, 8));
topLeft.setBorder(new EmptyBorder(50, 10, 50, 0));
for(char i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
newJButton btn = new newJButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon);
btn.setPreferredSize(new Dimension(70, 70));
btn.setFocusable(false);
btn.setBackground(new Color(0,255,0));
btn.setBackground(new Color(46, 83,106));
topLeft.add(btn);
whiteLost.add(btn);
}
}
// Middle Left
JPanel middleLeft = new JPanel();
middleLeft.setBorder(new EmptyBorder(100, 0, 0, 0));
JLabel playing = new JLabel("White Player is playing");
playing.setFont(new Font("Segoe UI", Font.BOLD, 32));
caption = playing;
middleLeft.add(playing);
leftDivider.add(middleLeft);
// Bottom Left
JPanel bottomLeft = new JPanel(new GridLayout(2, 8));
bottomLeft.setBorder(new EmptyBorder(50, 10, 50, 0));
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
newJButton btn = new newJButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setPreferredSize(new Dimension(70, 70));
btn.setIcon(icon);
btn.setFocusable(false);
btn.setBackground(new Color(0,255,0));
btn.setBackground(new Color(219,217,164));
bottomLeft.add(btn);
blackLost.add(btn);
}
......@@ -232,6 +245,8 @@ class Chess implements MouseListener{
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
this.frame = frame;
}
/**
......@@ -306,14 +321,14 @@ class Chess implements MouseListener{
if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(4,51,106));
btn.setBackground(new Color(46, 83,106));
}
this.isSelected = false;
this.inMove = null;
}
for(newJButton btn : this.whiteLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
Image img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
......@@ -353,6 +368,23 @@ class Chess implements MouseListener{
// this.isSelected = false;
// this.inMove = null;
}
if( this.board.checkMate() != null ){
String prompt = "";
if( this.board.checkMate().equals("W") ){
prompt = prompt.concat("Black Player Won!");
}else{
prompt = prompt.concat("White Player Won!");
}
prompt = prompt.concat(" \nDo you want to start a new game?");
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){
Main.rerun();
}
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
}
}
}
......
......@@ -5,613 +5,615 @@ import java.util.*;
* Main board used for logic
*/
public class Board{
private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces;
private ArrayList<Piece> allPieces;
/**
* Board Constructor for logic
*/
public Board(){
this.whitePieces = new ArrayList<Piece>();
this.blackPieces = new ArrayList<Piece>();
}
/**
* Board constructor for ui
* @author Omiid
* @param whitePieces ArrayList of all white pieces
* @param blackPieces ArrayList of all black pieces
*/
public Board(ArrayList<Piece> whitePieces, ArrayList<Piece> blackPieces){
this.whitePieces = whitePieces;
this.blackPieces = blackPieces;
this.allPieces = new ArrayList<Piece>();
for( Piece p : this.whitePieces ){
this.allPieces.add(p);
private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces;
private ArrayList<Piece> allPieces;
/**
* Board Constructor for logic
*/
public Board(){
this.whitePieces = new ArrayList<Piece>();
this.blackPieces = new ArrayList<Piece>();
}
for( Piece p : this.blackPieces ){
this.allPieces.add(p);
}
}
public ArrayList<Piece> getPieces(){
return this.allPieces;
}
/**
* Initial run of logic
*/
public void initPieces(){
// White Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 2, true, "WP");
this.whitePieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 1, true, "WR");
this.whitePieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 1, true, "WN");
this.whitePieces.add(newKnight);
/**
* Board constructor for ui
* @author Omiid
* @param whitePieces ArrayList of all white pieces
* @param blackPieces ArrayList of all black pieces
*/
public Board(ArrayList<Piece> whitePieces, ArrayList<Piece> blackPieces){
this.whitePieces = whitePieces;
this.blackPieces = blackPieces;
this.allPieces = new ArrayList<Piece>();
for( Piece p : this.whitePieces ){
this.allPieces.add(p);
}
for( Piece p : this.blackPieces ){
this.allPieces.add(p);
}
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 1, true, "WB");
this.whitePieces.add(newBishop);
public ArrayList<Piece> getPieces(){
return this.allPieces;
}
// Queen
Queen whiteQueen = new Queen('D', 1, true,"WQ");
this.whitePieces.add(whiteQueen);
// King
King whiteKing = new King('E', 1, true, "WK");
this.whitePieces.add(whiteKing);
// Black Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 7, false, "BP");
this.blackPieces.add(newPawn);
/**
* Initial run of logic
*/
public void initPieces(){
// White Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 2, true, "WP");
this.whitePieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 1, true, "WR");
this.whitePieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 1, true, "WN");
this.whitePieces.add(newKnight);
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 1, true, "WB");
this.whitePieces.add(newBishop);
}
// Queen
Queen whiteQueen = new Queen('D', 1, true,"WQ");
this.whitePieces.add(whiteQueen);
// King
King whiteKing = new King('E', 1, true, "WK");
this.whitePieces.add(whiteKing);
// Black Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 7, false, "BP");
this.blackPieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 8, false, "BR");
this.blackPieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 8, false, "BN");
this.blackPieces.add(newKnight);
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 8, false, "BB");
this.blackPieces.add(newBishop);
}
// Queen
Queen blackQueen = new Queen('D', 8, false, "BQ");
this.blackPieces.add(blackQueen);
// King
King blackKing = new King('E', 8, false, "BK");
this.blackPieces.add(blackKing);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 8, false, "BR");
this.blackPieces.add(newRook);
/**
* Check if any white player is present at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return true if is present
*/
public boolean isTakenByWhite(int y, char x){
boolean flag = false;
for( Piece p : whitePieces ){
if( p.getY() == y && p.getX() == x ){
flag = true;
break;
}
}
return flag;
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 8, false, "BN");
this.blackPieces.add(newKnight);
/**
* Check if any black player is present at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return true if is present
*/
public boolean isTakenByBlack(int y, char x){
boolean flag = false;
for( Piece p : blackPieces ){
if( p.getY() == y && p.getX() == x ){
flag = true;
break;
}
}
return flag;
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 8, false, "BB");
this.blackPieces.add(newBishop);
/**
* Check if piece available at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return true if is present
*/
public boolean isTaken(int y, char x){
boolean flag = false;
flag = isTakenByWhite(y, x);
if( !flag )
flag = isTakenByBlack(y, x);
return flag;
}
// Queen
Queen blackQueen = new Queen('D', 8, false, "BQ");
this.blackPieces.add(blackQueen);
// King
King blackKing = new King('E', 8, false, "BK");
this.blackPieces.add(blackKing);
}
/**
* Check if any white player is present at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return true if is present
*/
public boolean isTakenByWhite(int y, char x){
boolean flag = false;
for( Piece p : whitePieces ){
if( p.getY() == y && p.getX() == x ){
flag = true;
break;
}
/**
* Find white piece at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return piece available at (X, Y)
*/
public Piece takenByWhite(int y, char x){
Piece newPiece = null;
for( Piece p : whitePieces ){
if( p.getY() == y && p.getX() == x ){
newPiece = p;
break;
}
}
return newPiece;
}
return flag;
}
/**
* Check if any black player is present at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return true if is present
*/
public boolean isTakenByBlack(int y, char x){
boolean flag = false;
for( Piece p : blackPieces ){
if( p.getY() == y && p.getX() == x ){
flag = true;
break;
}
/**
* Find black piece at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return piece available at (X, Y)
*/
public Piece takenByBlack(int y, char x){
Piece newPiece = null;
for( Piece p : blackPieces ){
if( p.getY() == y && p.getX() == x ){
newPiece = p;
break;
}
}
return newPiece;
}
return flag;
}
/**
* Check if piece available at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return true if is present
*/
public boolean isTaken(int y, char x){
boolean flag = false;
flag = isTakenByWhite(y, x);
if( !flag )
flag = isTakenByBlack(y, x);
return flag;
}
/**
* Find white piece at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return piece available at (X, Y)
*/
public Piece takenByWhite(int y, char x){
Piece newPiece = null;
for( Piece p : whitePieces ){
if( p.getY() == y && p.getX() == x ){
newPiece = p;
break;
}
/**
* Find any piece at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return piece available at (X, Y)
*/
public Piece takenBy(int y, char x){
Piece newPiece = null;
newPiece = takenByWhite(y, x);
if( newPiece == null )
newPiece = takenByBlack(y, x);
return newPiece;
}
return newPiece;
}
/**
* Find black piece at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return piece available at (X, Y)
*/
public Piece takenByBlack(int y, char x){
Piece newPiece = null;
for( Piece p : blackPieces ){
if( p.getY() == y && p.getX() == x ){
newPiece = p;
break;
}
/**
* Print Logic Help
*/
public void printHelp(){
System.out.println("***** Board Help *****");
System.out.println("WK: White King \t|\tBK: Black King");
System.out.println("WQ: White Queen \t|\tBQ: Black Queen");
System.out.println("WB: White Bishop\t|\tBB: Black Bishop");
System.out.println("WN: White Knight\t|\tBN: Black Knight");
System.out.println("WR: White Rook \t|\tBR: Black Rook");
System.out.println("WP: White Pawn \t|\tBP: Black Pawn");
System.out.println("Acceptable Command: 'D2 D4'(Move From 2D to 4D)");
System.out.println("Press any key to start the game...");
}
return newPiece;
}
/**
* Find any piece at (X, Y)
* @author Omiid
* @param y The Y-Axis
* @param x The X-Axis
* @return piece available at (X, Y)
*/
public Piece takenBy(int y, char x){
Piece newPiece = null;
newPiece = takenByWhite(y, x);
if( newPiece == null )
newPiece = takenByBlack(y, x);
return newPiece;
}
/**
* Print Logic Help
*/
public void printHelp(){
System.out.println("***** Board Help *****");
System.out.println("WK: White King \t|\tBK: Black King");
System.out.println("WQ: White Queen \t|\tBQ: Black Queen");
System.out.println("WB: White Bishop\t|\tBB: Black Bishop");
System.out.println("WN: White Knight\t|\tBN: Black Knight");
System.out.println("WR: White Rook \t|\tBR: Black Rook");
System.out.println("WP: White Pawn \t|\tBP: Black Pawn");
System.out.println("Acceptable Command: 'D2 D4'(Move From 2D to 4D)");
System.out.println("Press any key to start the game...");
}
/**
* Prints full logic board
* @author Omiid
* @param player Current Player that is playing (W/ B)
*/
public void printBoard(String player){
if( player.equals("W") ){
for(int j = 9; j > 0; j--){
for(char i = 'A'; i < 'J'; i++){
if( j == 9 ){
if( i - 'A' > 0 ){
System.out.print((char)(i - 1) + " ");
}else{
System.out.print(" ");
}
}else{
if( i == 'A' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i - 1);
if( isTaken(j, mapedI) ){
System.out.print(takenBy(j, mapedI).getName() + " ");
}else{
System.out.print("- ");
}
}
}
}
System.out.println();
}
}else{
for(int j = 0; j < 9; j++){
for(char i = 'I'; i >= 'A'; i--){
if( j == 0 ){
if( i == 'I' ){
System.out.print(" ");
}else{
System.out.print((char)(i) + " ");
/**
* Prints full logic board
* @author Omiid
* @param player Current Player that is playing (W/ B)
*/
public void printBoard(String player){
if( player.equals("W") ){
for(int j = 9; j > 0; j--){
for(char i = 'A'; i < 'J'; i++){
if( j == 9 ){
if( i - 'A' > 0 ){
System.out.print((char)(i - 1) + " ");
}else{
System.out.print(" ");
}
}else{
if( i == 'A' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i - 1);
if( isTaken(j, mapedI) ){
System.out.print(takenBy(j, mapedI).getName() + " ");
}else{
System.out.print("- ");
}
}
}
}
System.out.println();
}
}else{
if( i == 'I' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i);
if( isTaken(j, mapedI) ){
System.out.print(takenBy(j, mapedI).getName() + " ");
}else{
System.out.print("- ");
}
}else{
for(int j = 0; j < 9; j++){
for(char i = 'I'; i >= 'A'; i--){
if( j == 0 ){
if( i == 'I' ){
System.out.print(" ");
}else{
System.out.print((char)(i) + " ");
}
}else{
if( i == 'I' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i);
if( isTaken(j, mapedI) ){
System.out.print(takenBy(j, mapedI).getName() + " ");
}else{
System.out.print("- ");
}
}
}
}
System.out.println();
}
}
}
System.out.println();
}
}
}
/**
* Move Piece in the board
* @author Omiid
* @param from String of current place
* @param to String of going place
* @param color String of current player
* @return true if move is valid
*/
public boolean move(String from, String to, String color){// Name Format: 1D
char[] fromArray = from.toCharArray();
char[] toArray = to.toCharArray();
if( fromArray.length == 2 && toArray.length == 2 ){// Name is consisted of 2 chars
if( fromArray[0] >= '1' && fromArray[0] <= '8' && fromArray[1] >= 'A' && fromArray[1] <= 'H' ){// Check from bound
if( toArray[0] >= '1' && toArray[0] <= '8' && toArray[1] >= 'A' && toArray[1] <= 'H' ){// Check to bound
if( canGo(fromArray, toArray, color) ){
ArrayList<Piece> enemy = null;
Piece found = null;
Piece attack = null;
if( color.equals("W") ){
enemy = this.blackPieces;
found = takenByWhite(fromArray[0] - '0', fromArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
}else{
enemy = this.whitePieces;
found = takenByBlack(fromArray[0] - '0', fromArray[1]);
attack = takenByWhite(toArray[0] - '0', toArray[1]);
}
if( attack != null ){
enemy.remove(attack);
}
if( found instanceof Pawn && ((Pawn) found).getFirstMove() == true ){
((Pawn) found).setFirstMove(false);
/**
* Move Piece in the board
* @author Omiid
* @param from String of current place
* @param to String of going place
* @param color String of current player
* @return true if move is valid
*/
public boolean move(String from, String to, String color){// Name Format: 1D
char[] fromArray = from.toCharArray();
char[] toArray = to.toCharArray();
if( fromArray.length == 2 && toArray.length == 2 ){// Name is consisted of 2 chars
if( fromArray[0] >= '1' && fromArray[0] <= '8' && fromArray[1] >= 'A' && fromArray[1] <= 'H' ){// Check from bound
if( toArray[0] >= '1' && toArray[0] <= '8' && toArray[1] >= 'A' && toArray[1] <= 'H' ){// Check to bound
if( canGo(fromArray, toArray, color) ){
ArrayList<Piece> enemy = null;
ArrayList<Piece> selector = null;
Piece found = null;
Piece attack = null;
if( color.equals("W") ){
enemy = this.blackPieces;
selector = this.whitePieces;
found = takenByWhite(fromArray[0] - '0', fromArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
}else{
enemy = this.whitePieces;
selector = this.blackPieces;
found = takenByBlack(fromArray[0] - '0', fromArray[1]);
attack = takenByWhite(toArray[0] - '0', toArray[1]);
}
if( attack != null ){
enemy.remove(attack);
this.allPieces.remove(attack);
}
if( found instanceof Pawn && ((Pawn) found).getFirstMove() == true ){
((Pawn) found).setFirstMove(false);
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true;
}else{
return false;
}
}else{
return false;
return false;
}
}else{
return false;
}
}else{
return false;
}
}
public boolean virtualCheck(ArrayList<Piece> enemy, char x, int y){
for(Piece p : enemy){
if( p.canMove(x, y) ){ // Check if move is valid
public boolean virtualCheck(ArrayList<Piece> enemy, char x, int y){
for(Piece p : enemy){
if( p.canMove(x, y) ){ // Check if move is valid
if( p.checkWay(this.allPieces, x, y) ){
if( p.checkWay(this.allPieces, x, y) ){
return true;
return true;
}
}
}
}
}
return false;
}
/**
* Check if playing player's king is checked
* @author Omiid
* @param base ArrayList of playing player
* @param enemy ArrayList of waiting player
* @return true if is checked
*/
public boolean kingCheck(ArrayList<Piece> base, ArrayList<Piece> enemy){
// find king
int kingY = 0;
char kingX = 'A';
for(Piece p : base){
if( p instanceof King){
kingY = p.y;
kingX = p.x;
}
return false;
}
if( virtualCheck(enemy, kingX, kingY) ){
return true;
}
return false;
}
/**
* Check if playing player's king is mate
* @author Omiid
* @param base ArrayList of playing player
* @param enemy ArrayList of waiting player
* @return true if is mated
*/
private boolean kingMate(ArrayList<Piece> base, ArrayList<Piece> enemy){
int kingY = 0;
char kingX = 'A';
for(Piece p : base){
if( p instanceof King){
kingY = p.y;
kingX = p.x;
}
}
int[] X = {1, 1, 1, 0, -1, -1, -1, 0};
int[] Y = {1, 0, -1, -1, -1, 0, 1, 1};
for(int i = 0; i < 8; i++){
char tempX = (char)(kingX + X[i]);
int tempY = kingY + Y[i];
boolean flag = false;
for(Piece p : enemy){
if( p.canMove(tempX, tempY) ){
if( p.checkWay(this.allPieces, tempX, tempY) ){
flag = true;
}
/**
* Check if playing player's king is checked
* @author Omiid
* @param base ArrayList of playing player
* @param enemy ArrayList of waiting player
* @return true if is checked
*/
public boolean kingCheck(ArrayList<Piece> base, ArrayList<Piece> enemy){
// find king
int kingY = 0;
char kingX = 'A';
for(Piece p : base){
if( p instanceof King){
kingY = p.y;
kingX = p.x;
}
}
}
if( !flag ){
if( virtualCheck(enemy, kingX, kingY) ){
return true;
}
return false;
}
}
return true;
}
/**
* Check which player is checkmated
* @author Omiid
* @return W for white, B for Black
*/
public String checkMate(){
ArrayList<Piece> all = new ArrayList<Piece>();
for(Piece p : this.blackPieces){
all.add(p);
}
for(Piece p : this.whitePieces){
all.add(p);
}
if( kingMate(this.whitePieces, this.blackPieces) ){
return "B";
}else if ( kingMate(this.whitePieces, this.blackPieces) ){
/**
* Check if playing player's king is mate
* @author Omiid
* @param base ArrayList of playing player
* @param enemy ArrayList of waiting player
* @return true if is mated
*/
private boolean kingMate(ArrayList<Piece> base, ArrayList<Piece> enemy){
int kingY = 0;
char kingX = 'A';
King king = null;
String color = null;
for(Piece p : base){
if( p instanceof King){
kingY = p.y;
kingX = p.x;
king = (King)p;
color = Character.toString(p.getName().charAt(0));
}
}
int[] X = {1, 1, 1, 0, -1, -1, -1, 0};
int[] Y = {1, 0, -1, -1, -1, 0, 1, 1};
int counter = 0;
for(int i = 0; i < 8; i++){
char tempX = (char)(kingX + X[i]);
int tempY = kingY + Y[i];
if( canGo(king, tempX, tempY, color) ){
counter++;
}
}
if( counter == 0 && virtualCheck(enemy, kingX, kingY) ){
return "W";
}else{
return null;
}
}
/**
* Check if piece can go to (X, Y)
* @author Omiid
* @param p Piece to move
* @param x The X-Axis
* @param y The Y-Axis
* @param color The Playing player(W/ B)
* @return true if can go
*/
public boolean canGo(Piece p, char x, int y, String color){
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = p;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
toBusy = isTakenByWhite(y, x);
attack = takenByBlack(y, x);
booleanColor = true;
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
toBusy = isTakenByBlack(y, x);
attack = takenByWhite(y, x);
return true;
}else{
return false;
}
}
if( found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return false;
}else{
if( found.canMove(x, y) ){ // Check if move is valid
if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){
/**
* Check which player is checkmated
* @author Omiid
* @return W for white, B for Black
*/
public String checkMate(){
if( kingMate(this.blackPieces, this.whitePieces) ){
return false;
}else if( (found instanceof Pawn) && attack != null && found.getX() - attack.getX() == 0 ){
return "B";
}else if ( kingMate(this.whitePieces, this.blackPieces) ){
return false;
}else if( (found instanceof King) && virtualCheck(enemy, x, y)){
return "W";
}else{
return null;
}
}
return false;
/**
* Check if piece can go to (X, Y)
* @author Omiid
* @param p Piece to move
* @param x The X-Axis
* @param y The Y-Axis
* @param color The Playing player(W/ B)
* @return true if can go
*/
public boolean canGo(Piece p, char x, int y, String color){
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = p;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
toBusy = isTakenByWhite(y, x);
attack = takenByBlack(y, x);
booleanColor = true;
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
toBusy = isTakenByBlack(y, x);
attack = takenByWhite(y, x);
}
if( x >= 'A' && x <= 'H' && y >= 1 && y <= 8 ){
if( found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return false;
}else{
if( found.canMove(x, y) ){ // Check if move is valid
if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){
return false;
}else if( (found instanceof Pawn) && attack != null && found.getX() - attack.getX() == 0 ){
return false;
}else if( (found instanceof King) && virtualCheck(enemy, x, y)){
return false;
}else{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
}else{
return true;
return false;
}
}else{
return false;
}
}else{
return false;
return false;
}
}
}else{
return false;
}
}
public boolean canGo(char[] fromArray, char []toArray, String color){
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = null;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
found = takenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
booleanColor = true;
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
found = takenByBlack(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
attack = takenByWhite(toArray[0] - '0', toArray[1]);
}
if( found != null && found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return false;
}else{
if( found.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
if( found.checkWay(this.allPieces, toArray[1], toArray[0] - '0') ){
if( (found instanceof Pawn) && found.crossMove(toArray[1], toArray[0] - '0') && attack == null ){
return false;
}else if( (found instanceof Pawn) && toArray[1] - fromArray[1] == 0 && attack != null){
public boolean canGo(char[] fromArray, char []toArray, String color){
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = null;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
found = takenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
booleanColor = true;
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
found = takenByBlack(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
attack = takenByWhite(toArray[0] - '0', toArray[1]);
}
if( found != null && found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
return false;
}else if( (found instanceof King) && virtualCheck(enemy, toArray[1], toArray[0] - '0')){
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return false;
return false;
}else{
return true;
if( found.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
if( found.checkWay(this.allPieces, toArray[1], toArray[0] - '0') ){
if( (found instanceof Pawn) && found.crossMove(toArray[1], toArray[0] - '0') && attack == null ){
return false;
}else if( (found instanceof Pawn) && toArray[1] - fromArray[1] == 0 && attack != null){
return false;
}else if( (found instanceof King) && virtualCheck(enemy, toArray[1], toArray[0] - '0')){
return false;
}else{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
}else{
return false;
}
}else{
return false;
return false;
}
}
}else{
return false;
}
}
/**
* Check if piece can attack at (X, Y)
* @author Omiid
* @param p Piece for attack
* @param x The X-Axis
* @param y The Y-Axis
* @param color The Playing player(W/ B)
* @return true if can go
*/
public boolean canAttack(Piece p, char x, int y, String color){
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = p;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
toBusy = isTakenByWhite(y, x);
attack = takenByBlack(y, x);
booleanColor = true;
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
toBusy = isTakenByBlack(y, x);
attack = takenByWhite(y, x);
}
if( found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return false;
}else{
if( found.canMove(x, y) ){ // Check if move is valid
if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){
/**
* Check if piece can attack at (X, Y)
* @author Omiid
* @param p Piece for attack
* @param x The X-Axis
* @param y The Y-Axis
* @param color The Playing player(W/ B)
* @return true if can go
*/
public boolean canAttack(Piece p, char x, int y, String color){
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = p;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
if( color.equals("W") ){
selector = this.whitePieces;
enemy = this.blackPieces;
toBusy = isTakenByWhite(y, x);
attack = takenByBlack(y, x);
booleanColor = true;
}else{
selector = this.blackPieces;
enemy = this.whitePieces;
toBusy = isTakenByBlack(y, x);
attack = takenByWhite(y, x);
}
if( found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
return false;
}else{
if( attack != null ){
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return true;
}else{
return false;
}
}else{
if( found.canMove(x, y) ){ // Check if move is valid
if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){
return false;
}else{
if( attack != null ){
return true;
}else{
return false;
}
}
}else{
return false;
}
}else{
return false;
}
}
}else{
return false;
}
}else{
return false;
return false;
}
}
}else{
return false;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment