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