Commit 3c6b4cfc authored by Omid Sayfun's avatar Omid Sayfun

Initital Commit

parents
*.iml
.idea
*.class
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.*;
import lab.game.*;
import java.io.IOException;
public class Main{
public static void main(String[] args){
Chess newChess = new Chess();
newChess.run();
}
}
class newJButton extends JButton{
private Piece piece;
private char X;
private int Y;
public newJButton(char X, int Y){
this.piece = null;
this.X = X;
this.Y = Y;
this.setIcon(null);
}
public newJButton(Piece p, char X, int Y){
this.piece = p;
this.X = X;
this.Y = Y;
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + p.getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
this.setIcon(new ImageIcon(img));
}
public Piece getPiece(){
return this.piece;
}
public char getNewX(){
return this.X;
}
public int getNewY(){
return this.Y;
}
}
class Chess implements MouseListener{
private Board board = null;
private String color = "B";
private boolean isSelected = false;
private newJButton inMove = null;
private ArrayList<newJButton> btns = new ArrayList<newJButton>();
public void run(){
JFrame frame = new JFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
// Biggest Panel
JPanel fullPanel = new JPanel(new BorderLayout());
// Board Panel
JPanel boardPanel = new JPanel(new GridLayout(8,8));
ArrayList<Piece> whitePieces = new ArrayList<Piece>();
ArrayList<Piece> blackPieces = new ArrayList<Piece>();
for(int i = 1; i < 9; i++){
for(char j = 'A'; j < 'I'; j++){
newJButton btn = null;
if( i == 8 ){
if( j == 'A' || j == 'H' ){
Rook newRook = new Rook(j, i, true, "WR");
whitePieces.add(newRook);
btn = new newJButton(newRook, j, i);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, true, "WN");
whitePieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, true, "WB");
whitePieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
}else if( j == 'D' ){
Queen whiteQueen = new Queen(j, i, true,"WQ");
whitePieces.add(whiteQueen);
btn = new newJButton(whiteQueen, j, i);
}else if( j == 'E' ){
King whiteKing = new King(j, i, true, "WK");
whitePieces.add(whiteKing);
btn = new newJButton(whiteKing, j, i);
}
}else if( i == 7 ){
Pawn newPawn = new Pawn(j, i, true, "WP");
whitePieces.add(newPawn);
btn = new newJButton(newPawn, j, i);
}else if( i == 1 ){
if( j == 'A' || j == 'H' ){
Rook newRook = new Rook(j, i, false, "BR");
blackPieces.add(newRook);
btn = new newJButton(newRook, j, i);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, false, "BN");
blackPieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, false, "BB");
blackPieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
}else if( j == 'D' ){
Queen blackQueen = new Queen(j, i, false,"BQ");
blackPieces.add(blackQueen);
btn = new newJButton(blackQueen, j, i);
}else if( j == 'E' ){
King blackKing = new King(j, i, false, "BQ");
blackPieces.add(blackKing);
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);
}else{
btn = new newJButton(j, i);
}
this.board = new Board(whitePieces, blackPieces);
this.btns.add(btn);
btn.addMouseListener(this);
btn.setFocusable(false);
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));
}
boardPanel.add(btn);
}
}
// Left Divider Panel
JPanel leftDivider = new JPanel(new GridLayout(3, 1));
// Top Left
JPanel topLeft = new JPanel(new GridLayout(2, 8));
for(char i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
JButton btn = new JButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon);
btn.setBackground(Color.WHITE);
topLeft.add(btn);
}
}
// Middle Left
JPanel middleLeft = new JPanel();
JLabel playing = new JLabel("White Player is in control");
middleLeft.add(playing);
leftDivider.add(middleLeft);
// Bottom Left
JPanel bottomLeft = new JPanel(new GridLayout(2, 8));
for(int i = 0; i < 2; i++){
for(int j = 0; j < 8; j++){
JButton btn = new JButton();
ImageIcon icon = new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB));
btn.setIcon(icon);
btn.setBackground(Color.BLACK);
bottomLeft.add(btn);
}
}
// Add Panels
leftDivider.add(topLeft);
leftDivider.add(middleLeft);
leftDivider.add(bottomLeft);
fullPanel.add(boardPanel, BorderLayout.EAST);
fullPanel.add(leftDivider, BorderLayout.WEST);
frame.add(fullPanel);
// Show Frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
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) ){
btn.setBackground(new Color(255, 0, 0));
}else{
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/dot.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img, "dot"));
}
this.isSelected = true;
this.inMove = source;
}
}
}else{
for(newJButton btn : this.btns) {
// if( ((ImageIcon)btn.getIcon()) != null ){
// System.out.println(((ImageIcon)btn.getIcon()).getDescription());
// }
if( btn.getPiece() != null ){
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(null);
}
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));
}
this.isSelected = false;
this.inMove = null;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
package lab.game;
import javax.xml.stream.events.StartDocument;
import java.util.*;
public class Bishop extends Piece{
public Bishop(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) != 0 && Math.abs(this.y - y) != 0 && Math.abs((float)(this.x - x) / (this.y - y)) == 1 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( Math.abs(p.x - x) != 0 && Math.abs(p.y - y) != 0 && Math.abs((float)(p.x - x) / (p.y - y)) == 1 ){
return true;
}
return false;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
if( p.x - x == 0 ){
return false;
}
if( p.y - y == 0 ){
return false;
}
int xShift = (x - p.x) / Math.abs(x - p.x);
int yShift = (y - p.y) / Math.abs(y - p.y);
int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false;
}
i++;
}
return true;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - this.x) / Math.abs(x - this.x);
int 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) ){
return false;
}
i++;
}
return true;
}
}
package lab.game;
import java.util.*;
public class Board{
private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces;
private ArrayList<Piece> allPieces;
public Board(){
this.whitePieces = new ArrayList<Piece>();
this.blackPieces = new ArrayList<Piece>();
}
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);
}
}
public ArrayList<Piece> getPieces(){
return this.allPieces;
}
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);
}
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;
}
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;
}
public boolean isTaken(int y, char x){
boolean flag = false;
flag = isTakenByWhite(y, x);
if( !flag )
flag = isTakenByBlack(y, x);
return flag;
}
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;
}
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;
}
public Piece takenBy(int y, char x){
Piece newPiece = null;
newPiece = takenByWhite(y, x);
if( newPiece == null )
newPiece = takenByBlack(y, x);
return newPiece;
}
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...");
}
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{
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();
}
}
}
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
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( found.getY() == fromArray[0] - '0' && found.getX() == fromArray[1] ){ // Find Piece in select
if( kingCheck(this.allPieces, 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( attack != null ){
enemy.remove(attack);
}
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;
}
}else{
return false;
}
}else{
return false;
}
}
public boolean kingCheck(ArrayList<Piece> all, 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;
}
}
for(Piece p : enemy){
if( p.canMove(kingX, kingY) ){ // Check if move is valid
if( p.checkWay(all, kingX, kingY) ){
System.out.println(p.getName());
return true;
}
}
}
return false;
}
private boolean kingMate(ArrayList<Piece> all, 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(all, tempX, tempY) ){
flag = true;
}
}
}
if( !flag ){
return false;
}
}
return true;
}
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(all, this.whitePieces, this.blackPieces) ){
return "B";
}else if ( kingMate(all, this.whitePieces, this.blackPieces) ){
return "W";
}else{
return null;
}
}
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(this.allPieces, 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{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
}else{
return false;
}
}
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(this.allPieces, 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( attack != null ){
return true;
}else{
return false;
}
}
}else{
return false;
}
}else{
return false;
}
}
}else{
return false;
}
}
}
package lab.game;
import java.util.*;
public class King extends Piece{
public King(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) < 2 && Math.abs(this.y - y) < 2 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
}
package lab.game;
import java.util.*;
public class Knight extends Piece{
public Knight(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){
int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
for (int i = 0; i < 8; i++) {
if( y == this.y + Y[i] && x == (char)(this.x + X[i]) ){
return true;
}
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
}
package lab.game;
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;
}
public void setFirstMove(boolean flag){
this.firstMove = flag;
}
public boolean getFirstMove(){
return this.firstMove;
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 ){
if( Math.abs(this.y - y) == 1 || (this.firstMove && Math.abs(this.y - y) == 2) ){
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;
}
}else{
return false;
}
}else if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
@Override
public boolean crossMove(char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}else if( Math.abs(this.y - y) == 1 ){
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) ){
return false;
}
return true;
}
}
}
package lab.game;
import java.util.*;
public abstract class Piece{
protected int y;
protected char x;
protected boolean color;
private String name;
public Piece(char x, int y, boolean color, String name){
setX(x);
setY(y);
setColor(color);
setName(name);
}
public void setY(int y){
this.y = y;
}
public void setX(char x){
this.x = x;
}
public void setName(String name){
this.name = name;
}
public void setColor(boolean color){
this.color = color;
}
public int getY(){
return this.y;
}
public char getX(){
return this.x;
}
public String getName(){
return this.name;
}
public boolean getColor(){
return this.color;
}
public static boolean checkTaken(ArrayList<Piece> pieces, char x, int y){
for(Piece p : pieces){
if( p.x == x && p.y == y ){
return true;
}
}
return false;
}
public boolean crossMove(char x, int y){
return false;
}
public abstract boolean canMove(char x, int y);
public abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
}
package lab.game;
import java.util.*;
public class Queen extends Piece{
public Queen(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Rook.checkWay(this, pieces, x, y) || Bishop.checkWay(this, pieces, x, y) ){
return true;
}
return false;
}
}
package lab.game;
import java.util.*;
public class Rook extends Piece{
public Rook(char x, int y, boolean color, String name){
super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 || this.y - y == 0 ){
return true;
}
return false;
}
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 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( this.x - x != 0 ){
xShift = (x - this.x) / Math.abs(x - this.x);
}
if( this.y - y != 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) ){
return false;
}
i++;
}
return true;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( p.x - x != 0 || p.y - y != 0 ){
if( p.x - x != 0 ){
xShift = (x - p.x) / Math.abs(x - p.x);
}
if( p.y - y != 0 ){
yShift = (y - p.y) / Math.abs(y - p.y);
}
int i = 1;
while( x != (char)(p.x + i * xShift) || y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false;
}
i++;
}
return true;
}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