Commit ff795df0 authored by Omid Sayfun's avatar Omid Sayfun

First GUI Interactions added

parent 3c6b4cfc
......@@ -15,11 +15,21 @@ public class Main{
}
}
/**
* A New Button class that has piece attribute
* @author Omiid
*/
class newJButton extends JButton{
private Piece piece;
private char X;
private int Y;
/**
* Make new button
* @param X The X-Axis
* @param Y The Y-Axis
* @author Omiid
*/
public newJButton(char X, int Y){
this.piece = null;
this.X = X;
......@@ -27,6 +37,13 @@ class newJButton extends JButton{
this.setIcon(null);
}
/**
* Make new button
* @param p The piece
* @param X The X-Axis
* @param Y The Y-Axis
* @author Omiid
*/
public newJButton(Piece p, char X, int Y){
this.piece = p;
this.X = X;
......@@ -44,6 +61,10 @@ class newJButton extends JButton{
return this.piece;
}
public void setPiece(Piece p){
this.piece = p;
}
public char getNewX(){
return this.X;
}
......@@ -53,6 +74,9 @@ class newJButton extends JButton{
}
}
/**
* Main Chess Class
*/
class Chess implements MouseListener{
private Board board = null;
private String color = "B";
......@@ -60,6 +84,9 @@ class Chess implements MouseListener{
private newJButton inMove = null;
private ArrayList<newJButton> btns = new ArrayList<newJButton>();
/**
* Initial Run
*/
public void run(){
JFrame frame = new JFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
......@@ -130,10 +157,10 @@ class Chess implements MouseListener{
btn = new newJButton(blackKing, j, i);
}
}else if( i == 2 ){
// Pawn newPawn = new Pawn(j, i, false, "BP");
// blackPieces.add(newPawn);
// btn = new newJButton(newPawn, j, i);
btn = new newJButton(j, i);
Pawn newPawn = new Pawn(j, i, false, "BP");
blackPieces.add(newPawn);
btn = new newJButton(newPawn, j, i);
// btn = new newJButton(j, i);
}else{
btn = new newJButton(j, i);
}
......@@ -195,10 +222,14 @@ class Chess implements MouseListener{
frame.setVisible(true);
}
/**
* Mouse Clicking behaviour
* @param e The event Happened
*/
@Override
public void mouseClicked(MouseEvent e) {
newJButton source = (newJButton)(e.getSource());
if( !isSelected ){
newJButton source = (newJButton)(e.getSource());
for(newJButton btn : this.btns){
if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
if( this.board.canAttack(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
......@@ -218,6 +249,22 @@ class Chess implements MouseListener{
}
}
}else{
if( this.board.canGo(this.inMove.getPiece(), source.getNewX(), source.getNewY(), this.color) ){
if( this.board.canAttack(this.inMove.getPiece(), source.getNewX(), source.getNewY(), this.color) ){
source.setPiece(this.inMove.getPiece());
this.inMove.setPiece(null);
}else{
Piece temp = this.inMove.getPiece();
this.inMove.setPiece(source.getPiece());
source.setPiece(temp);
}
System.out.println(this.board.move( inMove.getNewY() + Character.toString(inMove.getNewX()), source.getNewY() + Character.toString(source.getNewX()), this.color));
if( this.color.equals("W")){
this.color = "B";
}else{
this.color = "W";
}
}
for(newJButton btn : this.btns) {
// if( ((ImageIcon)btn.getIcon()) != null ){
// System.out.println(((ImageIcon)btn.getIcon()).getDescription());
......
package lab.game;
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;
......@@ -27,6 +39,9 @@ public class Board{
return this.allPieces;
}
/**
* Initial run of logic
*/
public void initPieces(){
// White Players
// Pawns
......@@ -84,6 +99,13 @@ public class Board{
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 ){
......@@ -96,6 +118,13 @@ public class Board{
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 ){
......@@ -108,6 +137,13 @@ public class Board{
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);
......@@ -116,6 +152,13 @@ public class Board{
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 ){
......@@ -128,6 +171,13 @@ public class Board{
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 ){
......@@ -140,6 +190,13 @@ public class Board{
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);
......@@ -148,6 +205,9 @@ public class Board{
return newPiece;
}
/**
* Print Logic Help
*/
public void printHelp(){
System.out.println("***** Board Help *****");
System.out.println("WK: White King \t|\tBK: Black King");
......@@ -160,6 +220,11 @@ public class Board{
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") ){
......@@ -217,6 +282,14 @@ public class Board{
}
}
/**
* 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();
......@@ -226,58 +299,31 @@ public class Board{
if( toArray[0] >= '1' && toArray[0] <= '8' && toArray[1] >= 'A' && toArray[1] <= 'H' ){// Check to bound
ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null;
Piece found = null;
boolean toBusy = false;
Piece attack = null;
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]);
}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 && !toBusy ){// Found and to is not busy
if( canGo(fromArray, toArray, color) ){
if( found.getY() == fromArray[0] - '0' && found.getX() == fromArray[1] ){ // Find Piece in select
ArrayList<Piece> enemy = null;
Piece found = null;
Piece attack = null;
if( color.equals("W") ){
if( kingCheck(this.allPieces, selector, enemy) && !(found instanceof King) ){
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 ){
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( attack != null ){
enemy.remove(attack);
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true;
}
}else{
return false;
}
}else{
return false;
}
}
enemy.remove(attack);
}
return false;
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;
}
......@@ -292,7 +338,14 @@ public class Board{
}
}
public boolean kingCheck(ArrayList<Piece> all, ArrayList<Piece> base, ArrayList<Piece> enemy){
/**
* 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';
......@@ -306,7 +359,7 @@ public class Board{
for(Piece p : enemy){
if( p.canMove(kingX, kingY) ){ // Check if move is valid
if( p.checkWay(all, kingX, kingY) ){
if( p.checkWay(this.allPieces, kingX, kingY) ){
System.out.println(p.getName());
return true;
......@@ -316,7 +369,14 @@ public class Board{
return false;
}
private boolean kingMate(ArrayList<Piece> all, ArrayList<Piece> base, ArrayList<Piece> enemy){
/**
* 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){
......@@ -335,7 +395,7 @@ public class Board{
for(Piece p : enemy){
if( p.canMove(tempX, tempY) ){
if( p.checkWay(all, tempX, tempY) ){
if( p.checkWay(this.allPieces, tempX, tempY) ){
flag = true;
}
......@@ -349,6 +409,11 @@ public class Board{
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){
......@@ -357,10 +422,10 @@ public class Board{
for(Piece p : this.whitePieces){
all.add(p);
}
if( kingMate(all, this.whitePieces, this.blackPieces) ){
if( kingMate(this.whitePieces, this.blackPieces) ){
return "B";
}else if ( kingMate(all, this.whitePieces, this.blackPieces) ){
}else if ( kingMate(this.whitePieces, this.blackPieces) ){
return "W";
}else{
......@@ -368,10 +433,19 @@ public class Board{
}
}
/**
* 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;
Piece found = p;
boolean toBusy = false;
Piece attack = null;
boolean booleanColor = false;
......@@ -390,14 +464,12 @@ public class Board{
}
if( found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
if( kingCheck(this.allPieces, selector, enemy) && !(found instanceof King) ){
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;
......@@ -415,7 +487,62 @@ public class Board{
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{
return true;
}
}else{
return false;
}
}else{
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;
......@@ -438,7 +565,7 @@ public class Board{
}
if( found.getColor() == booleanColor && !toBusy ){// Found and to is not busy
if( kingCheck(this.allPieces, selector, enemy) && !(found instanceof King) ){
if( kingCheck(selector, enemy) && !(found instanceof King) ){
return false;
}else{
......
......@@ -3,6 +3,7 @@ import java.util.*;
public class Pawn extends Piece{
private boolean firstMove;
public Pawn(char x, int y, boolean color, String name){
super(x, y, color, name);
this.firstMove = true;
......@@ -23,10 +24,6 @@ public class Pawn extends Piece{
if( (this.color && this.y - y > 0) || (!this.color && y - this.y > 0) ){ // Moving Backward
if( this.firstMove ){
this.firstMove = false;
}
return true;
}else{
return false;
......@@ -57,20 +54,8 @@ public class Pawn extends Piece{
return true;
}else{
int yShift = 0;
if( this.y - y != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( y != this.y + i * yShift ){
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
return false;
}
i++;
}
if( checkTaken(pieces, this.x, this.y + i * yShift) ){
int yShift = (y - this.y) / Math.abs(y - this.y);
if( checkTaken(pieces, this.x, this.y + yShift) || checkTaken(pieces, this.x, this.y + 2 * yShift) ){
return false;
}
......
package lab.game;
import java.util.*;
/**
* Rook Class
*/
public class Rook extends Piece{
public Rook(char x, int y, boolean color, String name){
super(x, y, color, name);
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 || this.y - y == 0 ){
......@@ -14,6 +25,13 @@ public class Rook extends Piece{
return false;
}
/**
* Check if can move to (X, Y) : Used for queen class
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( p.x - x == 0 || p.y - y == 0 ){
......@@ -22,26 +40,39 @@ public class Rook extends Piece{
return false;
}
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( this.x - x != 0 ){
if( this.x - x == 0 || this.y - y == 0 ){
xShift = (x - this.x) / Math.abs(x - this.x);
}
if( this.y - y != 0 ){
if( this.x - x != 0 ){
yShift = (y - this.y) / Math.abs(y - this.y);
}
int i = 1;
while( x != (char)(this.x + i * xShift) || y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
xShift = (this.x - x) / Math.abs(x - this.x);
}
if( this.y - y != 0 ){
yShift = (this.y - y) / Math.abs(y - this.y);
}
int i = 1;
boolean flag = true;
while( this.x != (char)(x + i * xShift) || this.y != y + i * yShift ){
if( checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) ){
return false;
flag = false;
}
i++;
}
i++;
return flag;
}else{
return false;
}
return true;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
......
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