Commit 66d91632 authored by hamed's avatar hamed

Fix King Kish bug & some improvment of code...

parent cb2c3846
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Main; package Main;
import Pieces.*; import Pieces.*;
...@@ -6,6 +11,8 @@ import java.util.ArrayList; ...@@ -6,6 +11,8 @@ import java.util.ArrayList;
import java.util.Queue; import java.util.Queue;
public class Board { public class Board {
private King whiteKing;
private King blackKing;
private ArrayList<Piece> whiteRemovedList = new ArrayList<>(); private ArrayList<Piece> whiteRemovedList = new ArrayList<>();
private ArrayList<Piece> blackRemovedList = new ArrayList<>(); private ArrayList<Piece> blackRemovedList = new ArrayList<>();
private Piece board[][] = new Piece[8][8]; private Piece board[][] = new Piece[8][8];
...@@ -17,7 +24,8 @@ public class Board { ...@@ -17,7 +24,8 @@ public class Board {
board[6][0] = new Knight(6,0,false,this); board[6][0] = new Knight(6,0,false,this);
board[2][0] = new Bishop(2,0,false,this); board[2][0] = new Bishop(2,0,false,this);
board[5][0] = new Bishop(5,0,false,this); board[5][0] = new Bishop(5,0,false,this);
board[3][0] = new King(3,0,false,this); blackKing = new King(3,0,false,this);
board[3][0] = blackKing;
board[4][0] = new Queen(4,0,false,this); board[4][0] = new Queen(4,0,false,this);
...@@ -33,7 +41,8 @@ public class Board { ...@@ -33,7 +41,8 @@ public class Board {
board[2][7] = new Bishop(2,7,true,this); board[2][7] = new Bishop(2,7,true,this);
board[5][7] = new Bishop(5,7,true,this); board[5][7] = new Bishop(5,7,true,this);
board[3][7] = new Queen(3,7,true,this); board[3][7] = new Queen(3,7,true,this);
board[4][7] = new King(4,7,true,this); whiteKing = new King(4,7,true,this);
board[4][7] =whiteKing;
} }
public ArrayList<Piece> getBlackRemovedList(){ public ArrayList<Piece> getBlackRemovedList(){
...@@ -42,6 +51,9 @@ public class Board { ...@@ -42,6 +51,9 @@ public class Board {
public ArrayList<Piece> getWhiteRemovedList(){ public ArrayList<Piece> getWhiteRemovedList(){
return whiteRemovedList; return whiteRemovedList;
} }
public boolean isWhiteMove(){
return whiteMove;
}
public boolean getMove(int x , int y , int destX , int destY){ public boolean getMove(int x , int y , int destX , int destY){
if(board[x][y]==null) return false; if(board[x][y]==null) return false;
// System.out.println(x+","+y+"->"+destX+","+destY); // System.out.println(x+","+y+"->"+destX+","+destY);
...@@ -95,6 +107,41 @@ public class Board { ...@@ -95,6 +107,41 @@ public class Board {
return ret; return ret;
} }
} }
public boolean KishCheck(boolean isWhite){
for(int i = 0 ; i < 8 ; i++){
for (int j = 0 ; j < 8 ; j++){
Piece p = board[i][j];
if(p instanceof King && p.isWhite())
whiteKing = (King)p;
else if(p instanceof King && p.isBlack())
blackKing = (King)p;
}
}
if(isWhite){
for (int i = 0 ; i < 8 ;i++){
for(int j = 0 ; j < 8 ; j++){
if(board[i][j] != null && board[i][j].isBlack()){
int xx = whiteKing.getX();
int yy = whiteKing.getY();
if(board[i][j].isValidMove(xx,yy)) return true;
}
}
}
}
else {
for (int i = 0 ; i < 8 ;i++){
for(int j = 0 ; j < 8 ; j++){
if(board[i][j] != null && board[i][j].isWhite()){
int xx = blackKing.getX();
int yy = blackKing.getY();
if(board[i][j].isValidMove(xx,yy)) return true;
}
}
}
}
return false;
}
public Piece[][] getBoard() { public Piece[][] getBoard() {
return board; return board;
} }
......
package Main;
public class Cell implements Comparable<Cell>{
int x;
int y;
public Cell(int x,int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(Cell o) {
if(o.x == x){
return o.y - y;
}
else {
return o.x-x;
}
}
}
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Main; package Main;
import Pieces.*; import Pieces.*;
...@@ -141,6 +146,19 @@ public class ChessFrame extends JFrame implements MouseListener, MouseMotionList ...@@ -141,6 +146,19 @@ public class ChessFrame extends JFrame implements MouseListener, MouseMotionList
} }
} }
} }
//---------------
g.setFont(new Font("Arial", Font.BOLD, 18));
if(b.KishCheck(true)){
g.drawString("Kish of White King ...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
}else if(b.KishCheck(false)){
g.drawString("Kish of Black King ...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
}else if(b.isWhiteMove()) {
g.drawString("White Turn...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
}else if(!b.isWhiteMove()) {
g.drawString("Black Turn...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
}
} }
@Override @Override
......
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Main; package Main;
import java.util.ArrayList; import java.util.ArrayList;
......
package Main;
enum PiecesName {
KING,
KNIGHT,
PAWN,
BISHOP,
QUEEN,
ROOK
}
\ No newline at end of file
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
...@@ -10,15 +15,6 @@ public class Bishop extends Piece { ...@@ -10,15 +15,6 @@ public class Bishop extends Piece {
super(x, y, isWhite, board); super(x, y, isWhite, board);
} }
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx, dy)) return false;
int deltaX = Math.abs(dx-getX());
int deltaY = Math.abs(dy-getY());
if( deltaX==deltaY) return true;
return false;
}
@Override @Override
public boolean isValidMove(int dx, int dy) { public boolean isValidMove(int dx, int dy) {
// if(!isCanMove(dx, dy)) return false; // if(!isCanMove(dx, dy)) return false;
......
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
import Main.Cell;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
public class King extends Piece { public class King extends Piece {
...@@ -13,17 +16,6 @@ public class King extends Piece { ...@@ -13,17 +16,6 @@ public class King extends Piece {
super(x, y, isWhite, board); super(x, y, isWhite, board);
} }
@Override
public boolean isCanMove(int dx, int dy) {
if (isOutside(dx, dy)) return false;
int deltaX = Math.abs(dx-getX());
int deltaY = Math.abs(dy-getY());
if( deltaX==deltaY && deltaX ==1) return true;
if(deltaX ==1 && deltaY ==0) return true;
if(deltaX==0 && deltaY ==1) return true;
return false;
}
@Override @Override
public boolean isValidMove(int dx, int dy) { public boolean isValidMove(int dx, int dy) {
// if(!isCanMove(dx, dy)) return false; // if(!isCanMove(dx, dy)) return false;
...@@ -36,32 +28,30 @@ public class King extends Piece { ...@@ -36,32 +28,30 @@ public class King extends Piece {
for (int m[] : moves){ for (int m[] : moves){
if(m[0]==dx && m[1] == dy){ if(m[0]==dx && m[1] == dy){
return true; return true;
// break;
} }
} }
return false; return false;
} }
public HashSet<Cell> getThreatedPlaces(){
HashSet<Cell> retSet = new HashSet<>(); private boolean isAttacked(int x , int y){
for (int i = 0 ; i < 8 ; i++){ for (int i = 0 ; i < 8 ; i++){
for(int j =0 ; j < 8 ; j++){ for (int j = 0 ; j < 8 ; j++){
Piece p = getBoard().getBoard()[i][j]; Piece p = getBoard().getBoard()[i][j];
if(p!=null && !(p instanceof King) &&((p.isWhite()&& isBlack()) ||(p.isBlack() && isWhite()))){ if(p!=null && !(p instanceof King)){
ArrayList<int[]> moves = p.moves(); if(p.isWhite() && isBlack()){
for(int m[] : moves){ if(p.isValidMove(x,y)) return true;
Cell c = new Cell(m[0],m[1]); }else if(p.isBlack() && isWhite()){
retSet.add(c); if(p.isValidMove(x,y)) return true;
} }
} }
} }
} }
return retSet; return false;
} }
@Override @Override
public ArrayList<int[]> moves() { public ArrayList<int[]> moves() {
ArrayList<int[]> move = new ArrayList<>(); ArrayList<int[]> move = new ArrayList<>();
HashSet<Cell> threats = getThreatedPlaces();
int x = getX(); int x = getX();
int y = getY(); int y = getY();
int posx[]={x,x,x+1,x+1,x+1,x-1,x-1,x-1}; int posx[]={x,x,x+1,x+1,x+1,x-1,x-1,x-1};
...@@ -70,10 +60,10 @@ public class King extends Piece { ...@@ -70,10 +60,10 @@ public class King extends Piece {
if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)){ if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)){
if(getBoard().isEmpty(posx[i],posy[i]) || (getBoard().getColor(posx[i],posy[i])==2 && this.isWhite()) ||(getBoard().getColor(posx[i],posy[i])==1 && isBlack()) ) if(getBoard().isEmpty(posx[i],posy[i]) || (getBoard().getColor(posx[i],posy[i])==2 && this.isWhite()) ||(getBoard().getColor(posx[i],posy[i])==1 && isBlack()) )
{ {
Cell cmp = new Cell(posx[i],posy[i]); if(!isAttacked(posx[i],posy[i])){
if(!threats.contains(cmp)) {
move.add(new int[]{posx[i], posy[i]}); move.add(new int[]{posx[i], posy[i]});
} }
} }
} }
} }
......
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
...@@ -11,16 +16,6 @@ public class Knight extends Piece { ...@@ -11,16 +16,6 @@ public class Knight extends Piece {
super(x, y, isWhite, board); super(x, y, isWhite, board);
} }
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx,dy)) return false;
if(dx != getX() - 1 && dx != getX() + 1 && dx != getX() + 2 && dx != getX() - 2)
return false;
if(dy != getY() - 2 && dy != getY() + 2 && dy != getY() - 1 && dy != getY() + 1)
return false;
return true;
}
@Override @Override
public boolean isValidMove(int dx, int dy) { public boolean isValidMove(int dx, int dy) {
ArrayList<int[]> moves = moves(); ArrayList<int[]> moves = moves();
......
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
...@@ -11,11 +16,6 @@ public class Pawn extends Piece { ...@@ -11,11 +16,6 @@ public class Pawn extends Piece {
super(x, y, isWhite, board); super(x, y, isWhite, board);
} }
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx,dy)) return false;
return false;
}
@Override @Override
public boolean isValidMove(int dx, int dy) { public boolean isValidMove(int dx, int dy) {
...@@ -47,10 +47,10 @@ public class Pawn extends Piece { ...@@ -47,10 +47,10 @@ public class Pawn extends Piece {
moves.add(new int[]{x,4}); moves.add(new int[]{x,4});
} }
} }
if((x>0)&& ((getBoard().getColor(x-1,y-1)==2 && isWhite() ||getBoard().getColor(x-1,y-1)==1 && isBlack()))) if((x>0)&& ((getBoard().getColor(x-1,y-1)==2 && isWhite()) || (getBoard().getColor(x-1,y-1)==1 && isBlack())))
moves.add(new int[]{x-1,y-1}); moves.add(new int[]{x-1,y-1});
if((x<7)&&((getBoard().getColor(x+1,y-1)==2 && isWhite() ||getBoard().getColor(x+1,y-1)==1 && isBlack()))) if((x<7)&&((getBoard().getColor(x+1,y-1)==2 && isWhite()) || (getBoard().getColor(x+1,y-1)==1 && isBlack())))
moves.add(new int[]{x-1,y+1}); moves.add(new int[]{x+1,y-1});
} }
else else
{ {
...@@ -65,9 +65,9 @@ public class Pawn extends Piece { ...@@ -65,9 +65,9 @@ public class Pawn extends Piece {
moves.add(new int[]{x,3}); moves.add(new int[]{x,3});
} }
} }
if((x>0)&& ((getBoard().getColor(x-1,y+1)==2 && isWhite() ||getBoard().getColor(x-1,y+1)==1 && isBlack()))) if((x>0)&& ((getBoard().getColor(x-1,y+1)==2 && isWhite()) ||(getBoard().getColor(x-1,y+1)==1 && isBlack())))
moves.add(new int[]{x+1,y-1}); moves.add(new int[]{x-1,y+1});
if((x<7)&&((getBoard().getColor(x+1,y+1)==2 && isWhite() ||getBoard().getColor(x+1,y+1)==1 && isBlack()))) if((x<7)&&((getBoard().getColor(x+1,y+1)==2 && isWhite()) ||(getBoard().getColor(x+1,y+1)==1 && isBlack())))
moves.add(new int[]{x+1,y+1}); moves.add(new int[]{x+1,y+1});
} }
return moves; return moves;
......
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
...@@ -15,12 +20,24 @@ public abstract class Piece implements Cloneable { ...@@ -15,12 +20,24 @@ public abstract class Piece implements Cloneable {
this.isWhite = isWhite; this.isWhite = isWhite;
this.board = board; this.board = board;
} }
/**
* This is the method for Color of Piece
* @return isWhite.
*/
public boolean isWhite(){ public boolean isWhite(){
return isWhite; return isWhite;
} }
/**
* This is the method for Color of Piece
* @return isBlack.
*/
public boolean isBlack(){ public boolean isBlack(){
return !isWhite; return !isWhite;
} }
/**
* This is the method for x-axis of Piece
* @return x-axis.
*/
public int getX() { public int getX() {
return x; return x;
} }
...@@ -29,20 +46,48 @@ public abstract class Piece implements Cloneable { ...@@ -29,20 +46,48 @@ public abstract class Piece implements Cloneable {
return board; return board;
} }
/**
* This is the method for set x-axis of Piece
* @param x x-axis
* @return void.
*/
public void setX(int x) { public void setX(int x) {
this.x = x; this.x = x;
} }
/**
* This is the method for y-axis of Piece
* @return y-axis.
*/
public int getY() { public int getY() {
return y; return y;
} }
/**
* This is the method for set y-axis of Piece
* @param y y-axis
* @return void.
*/
public void setY(int y) { public void setY(int y) {
this.y = y; this.y = y;
} }
abstract public boolean isCanMove(int dx,int dy); /**
* This is the method for validate move of piece
* @param dx destination x
* @param dy destination y
* @return is a Valid move.
*/
abstract public boolean isValidMove(int dx,int dy); abstract public boolean isValidMove(int dx,int dy);
/**
* This is the method for list of valid move of piece
* @return list of valid moves of piece.
*/
abstract public ArrayList<int[]> moves(); abstract public ArrayList<int[]> moves();
/**
* This is the method for check outside of piece
* @param dx destination x.
* @param dy destination y.
* @return is destination outside?.
*/
public boolean isOutside(int dx, int dy){ public boolean isOutside(int dx, int dy){
if (dx < 0 || dx > 8 || dy < 0 || dy > 8) if (dx < 0 || dx > 8 || dy < 0 || dy > 8)
return false; return false;
...@@ -57,5 +102,4 @@ public abstract class Piece implements Cloneable { ...@@ -57,5 +102,4 @@ public abstract class Piece implements Cloneable {
} }
return null; return null;
} }
// public void dra
} }
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
...@@ -11,18 +16,6 @@ public class Queen extends Piece { ...@@ -11,18 +16,6 @@ public class Queen extends Piece {
super(x, y, isWhite, board); super(x, y, isWhite, board);
} }
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx, dy)) return false;
int deltaX = Math.abs(dx-getX());
int deltaY = Math.abs(dy-getY());
if( deltaX==deltaY) return true;
if(dx ==getX())
return true;
if(dy == getY())
return true;
return false;
}
@Override @Override
public boolean isValidMove(int dx, int dy) { public boolean isValidMove(int dx, int dy) {
......
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Pieces; package Pieces;
import Main.Board; import Main.Board;
...@@ -9,17 +14,6 @@ public class Rook extends Piece { ...@@ -9,17 +14,6 @@ public class Rook extends Piece {
public Rook(int x, int y, boolean isWhite, Board board) { public Rook(int x, int y, boolean isWhite, Board board) {
super(x, y, isWhite, board); super(x, y, isWhite, board);
} }
@Override
public boolean isCanMove(int dx, int dy) {
if(isOutside(dx, dy)) return false;
if(dx ==getX())
return true;
if(dy == getY())
return true;
return false;
}
@Override @Override
public boolean isValidMove(int dx, int dy) { public boolean isValidMove(int dx, int dy) {
// if(!isCanMove(dx, dy))return false; // if(!isCanMove(dx, dy))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