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());
......
This diff is collapsed.
......@@ -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