Commit 91f7f983 authored by Omid Sayfun's avatar Omid Sayfun

Final Check

parent 9e9b637a
Manifest-Version: 1.0
Main-Class: Main
...@@ -2,18 +2,15 @@ import javax.imageio.ImageIO; ...@@ -2,18 +2,15 @@ import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedInputStream; import java.io.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.*; import java.util.*;
import lab.game.*; import lab.game.*;
import java.io.IOException;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
public class Main{ public class Main{
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
...@@ -26,8 +23,61 @@ public class Main{ ...@@ -26,8 +23,61 @@ public class Main{
} }
} }
class Splash{ class AppFrame extends JFrame implements WindowListener{
public AppFrame(){
super();
this.addWindowListener(this);
}
public AppFrame(String name){
super(name);
this.addWindowListener(this);
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
class Splash{
/**
* Run the game
* @author Omiid
* @throws IOException
*/
public void run() throws IOException { public void run() throws IOException {
Object[] options = {"Server", "Client"}; Object[] options = {"Server", "Client"};
int result = JOptionPane.showOptionDialog(null, "Which role do you take?", "Start a Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]); int result = JOptionPane.showOptionDialog(null, "Which role do you take?", "Start a Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
...@@ -36,37 +86,42 @@ class Splash{ ...@@ -36,37 +86,42 @@ class Splash{
ServerSocket server = new ServerSocket(5000); ServerSocket server = new ServerSocket(5000);
System.out.println("Server Created"); System.out.println("Server Created");
JFrame frame = new JFrame(); AppFrame frame = new AppFrame();
JPanel panel = new JPanel(new BorderLayout()); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JLabel label = new JLabel("Waiting for opponent..."); JLabel label = new JLabel("Waiting for opponent...");
label.setBorder(new EmptyBorder(40,40,40,40)); label.setBorder(new EmptyBorder(40,40,40,40));
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
panel.add(label);
frame.add(label); frame.add(label);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack(); frame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2); frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setVisible(true); frame.setVisible(true);
Socket socket = server.accept(); Socket socket = server.accept();
System.out.println("Client Connected"); System.out.println("Client Connected");
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); frame.dispose();
server.close(); server.close();
Chess newChess = new Chess("W", socket); Chess newChess = new Chess("W", socket);
newChess.run(); newChess.run();
}else { // Client }else { // Client
String s = (String)JOptionPane.showInputDialog( boolean flag = true;
null, Socket socket = null;
"Enter Server IP:", while(flag){
"Customized Dialog", String s = (String)JOptionPane.showInputDialog(
JOptionPane.PLAIN_MESSAGE, null,
null, "Enter Server IP:",
null, "Customized Dialog",
"x.x.x.x"); JOptionPane.PLAIN_MESSAGE,
null,
Socket socket = new Socket(s, 5000); null,
"x.x.x.x");
socket = new Socket(s, 5000);
if( socket.isConnected() ){
flag = false;
}
}
System.out.println("Connected to server"); System.out.println("Connected to server");
Chess newChess = new Chess("B", socket); Chess newChess = new Chess("B", socket);
...@@ -142,6 +197,7 @@ class newJButton extends JButton{ ...@@ -142,6 +197,7 @@ class newJButton extends JButton{
/** /**
* Main Chess Class * Main Chess Class
* @author Omiid
*/ */
class Chess implements MouseListener{ class Chess implements MouseListener{
private Socket socket = null; private Socket socket = null;
...@@ -152,7 +208,7 @@ class Chess implements MouseListener{ ...@@ -152,7 +208,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 AppFrame 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<>();
...@@ -171,9 +227,11 @@ class Chess implements MouseListener{ ...@@ -171,9 +227,11 @@ class Chess implements MouseListener{
} }
/** /**
* Initial Run * Initial Run
* @author Omiid
* @throws IOException
*/ */
public void run() throws IOException { public void run() throws IOException {
JFrame frame = new JFrame("Chess Frame"); AppFrame frame = new AppFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH); frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
// Biggest Panel // Biggest Panel
JPanel fullPanel = new JPanel(new BorderLayout()); JPanel fullPanel = new JPanel(new BorderLayout());
...@@ -398,7 +456,7 @@ class Chess implements MouseListener{ ...@@ -398,7 +456,7 @@ class Chess implements MouseListener{
frame.add(fullPanel); frame.add(fullPanel);
// Show Frame // Show Frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.pack(); frame.pack();
frame.setVisible(true); frame.setVisible(true);
...@@ -413,12 +471,10 @@ class Chess implements MouseListener{ ...@@ -413,12 +471,10 @@ class Chess implements MouseListener{
if( line.equals("Over") ){ if( line.equals("Over") ){
break; break;
} }
System.out.println(line);
String[] splited = line.split(" "); String[] splited = line.split(" ");
for(newJButton btn : this.btns){ for(newJButton btn : this.btns){
if( btn.getNewX() == splited[0].charAt(1) && btn.getNewY() == splited[0].charAt(0) - '0' ){ if( btn.getNewX() == splited[0].charAt(1) && btn.getNewY() == splited[0].charAt(0) - '0' ){
System.out.println("Found inmove");
this.inMove = btn; this.inMove = btn;
} }
} }
...@@ -426,7 +482,6 @@ class Chess implements MouseListener{ ...@@ -426,7 +482,6 @@ class Chess implements MouseListener{
for(newJButton btn : this.btns){ for(newJButton btn : this.btns){
if( btn.getNewX() == splited[1].charAt(1) && btn.getNewY() == splited[1].charAt(0) - '0' ){ if( btn.getNewX() == splited[1].charAt(1) && btn.getNewY() == splited[1].charAt(0) - '0' ){
System.out.println("Found source");
source = btn; source = btn;
} }
} }
...@@ -489,6 +544,7 @@ class Chess implements MouseListener{ ...@@ -489,6 +544,7 @@ class Chess implements MouseListener{
/** /**
* Mouse Clicking behaviour * Mouse Clicking behaviour
* @param e The event Happened * @param e The event Happened
* @author Omiid
*/ */
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
...@@ -568,6 +624,10 @@ class Chess implements MouseListener{ ...@@ -568,6 +624,10 @@ class Chess implements MouseListener{
} }
} }
/**
* Repaint all the button to the current state
* @author Omiid
*/
public void repaint(){ public void repaint(){
for(newJButton btn : this.btns) { for(newJButton btn : this.btns) {
if( btn.getPiece() != null ){ if( btn.getPiece() != null ){
...@@ -617,6 +677,11 @@ class Chess implements MouseListener{ ...@@ -617,6 +677,11 @@ class Chess implements MouseListener{
} }
} }
/**
* Check if game is over
* @author Omiid
* @throws IOException
*/
public void checkMate() throws IOException { public void checkMate() throws IOException {
if( this.board.checkMate() != null ){ if( this.board.checkMate() != null ){
...@@ -631,22 +696,33 @@ class Chess implements MouseListener{ ...@@ -631,22 +696,33 @@ class Chess implements MouseListener{
String prompt = ""; String prompt = "";
if( this.board.checkMate().equals("W") ){ if( this.board.checkMate().equals("W") ){
prompt = prompt.concat("Black Player Won!"); prompt = prompt.concat("<html><body><center>Black Player Won!M</center>");
}else{ }else{
prompt = prompt.concat("White Player Won!"); prompt = prompt.concat("<html><body><center>White Player Won!</center>");
}
prompt = prompt.concat(" \nDo you want to start a new game?");
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){
Main.rerun();
}else{
System.exit(0);
} }
// 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 ){
//
// this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
// Main.rerun();
// }else{
// System.exit(0);
// }
JOptionPane optionPane = new JOptionPane(new JLabel(prompt,JLabel.CENTER));
JDialog dialog = optionPane.createDialog("Finished");
dialog.setModal(true);
dialog.setVisible(true);
System.exit(0);
} }
} }
/**
* Kill a white piece
* @param p The piece
* @author Omiid
* @return true if killed
*/
public boolean killWhite(Piece p){ public boolean killWhite(Piece p){
for(newJButton btn : this.whiteLost){ for(newJButton btn : this.whiteLost){
if( btn.getPiece() == null ){ if( btn.getPiece() == null ){
...@@ -658,6 +734,12 @@ class Chess implements MouseListener{ ...@@ -658,6 +734,12 @@ class Chess implements MouseListener{
return false; return false;
} }
/**
* Kill a black piece
* @param p The piece
* @author Omiid
* @return true if killed
*/
public boolean killBlack(Piece p){ public boolean killBlack(Piece p){
for(newJButton btn : this.blackLost){ for(newJButton btn : this.blackLost){
if( btn.getPiece() == null ){ if( btn.getPiece() == null ){
......
...@@ -7,6 +7,13 @@ public class Bishop extends Piece{ ...@@ -7,6 +7,13 @@ public class Bishop extends Piece{
super(x, y, color, 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 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 ){ if( Math.abs(this.x - x) != 0 && Math.abs(this.y - y) != 0 && Math.abs((float)(this.x - x) / (this.y - y)) == 1 ){
...@@ -15,6 +22,14 @@ public class Bishop extends Piece{ ...@@ -15,6 +22,14 @@ public class Bishop extends Piece{
return false; return false;
} }
/**
* Check if can move to (X, Y) : Used for queen class
* @author Omiid
* @param p the piece to move
* @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 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 ){ if( Math.abs(p.x - x) != 0 && Math.abs(p.y - y) != 0 && Math.abs((float)(p.x - x) / (p.y - y)) == 1 ){
...@@ -23,6 +38,16 @@ public class Bishop extends Piece{ ...@@ -23,6 +38,16 @@ public class Bishop extends Piece{
return false; return false;
} }
/**
* Check if way is free to go
*
* @param p the piece to check
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){ public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
if( p.x - x == 0 ){ if( p.x - x == 0 ){
...@@ -45,6 +70,14 @@ public class Bishop extends Piece{ ...@@ -45,6 +70,14 @@ public class Bishop extends Piece{
return true; return true;
} }
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - this.x) / Math.abs(x - this.x); int xShift = (x - this.x) / Math.abs(x - this.x);
int yShift = (y - this.y) / Math.abs(y - this.y); int yShift = (y - this.y) / Math.abs(y - this.y);
......
...@@ -343,7 +343,6 @@ public class Board{ ...@@ -343,7 +343,6 @@ public class Board{
} }
public boolean virtualCheck(ArrayList<Piece> enemy, char x, int y){ public boolean virtualCheck(ArrayList<Piece> enemy, char x, int y){
String temp = Character.toString(enemy.get(0).getName().charAt(0));
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
...@@ -415,6 +414,7 @@ public class Board{ ...@@ -415,6 +414,7 @@ public class Board{
int tempY = kingY + Y[i]; int tempY = kingY + Y[i];
if( canGo(king, tempX, tempY, color) ){ if( canGo(king, tempX, tempY, color) ){
System.out.println(king.getName() + " : " + tempX + tempY);
counter++; counter++;
} }
} }
...@@ -453,9 +453,86 @@ public class Board{ ...@@ -453,9 +453,86 @@ public class Board{
* @return true if can go * @return true if can go
*/ */
public boolean canGo(Piece p, char x, int y, String color){ public boolean canGo(Piece p, char x, int y, String color){
char[] fromArray = {(char)(p.getY() + '0'), p.getX()};
char[] toArray = {(char)(y + '0'), x};
return canGo(fromArray, toArray, 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) ){
//
// if( attack == null ){
//
// return false;
// }else{
// if( y == attack.getY() && x == attack.getX() ){
//
// if( found.color && y < found.getY() ){
//
// return true;
// }else if( !found.color && found.getY() < y ){
//
// return true;
// }else{
// return false;
// }
// }else{
// 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{
// return false;
// }
// }else{
// return false;
// }
}
public boolean canGo(char[] fromArray, char []toArray, String color){
ArrayList<Piece> selector = null; ArrayList<Piece> selector = null;
ArrayList<Piece> enemy = null; ArrayList<Piece> enemy = null;
Piece found = p; Piece found = null;
boolean toBusy = false; boolean toBusy = false;
Piece attack = null; Piece attack = null;
boolean booleanColor = false; boolean booleanColor = false;
...@@ -463,37 +540,39 @@ public class Board{ ...@@ -463,37 +540,39 @@ public class Board{
selector = this.whitePieces; selector = this.whitePieces;
enemy = this.blackPieces; enemy = this.blackPieces;
toBusy = isTakenByWhite(y, x); found = takenByWhite(fromArray[0] - '0', fromArray[1]);
attack = takenByBlack(y, x); toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
attack = takenByBlack(toArray[0] - '0', toArray[1]);
booleanColor = true; booleanColor = true;
}else{ }else{
selector = this.blackPieces; selector = this.blackPieces;
enemy = this.whitePieces; enemy = this.whitePieces;
toBusy = isTakenByBlack(y, x); found = takenByBlack(fromArray[0] - '0', fromArray[1]);
attack = takenByWhite(y, x); toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
attack = takenByWhite(toArray[0] - '0', toArray[1]);
} }
if( x >= 'A' && x <= 'H' && y >= 1 && y <= 8 ){ if( found != null && 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) ){
if( kingCheck(selector, enemy) && !(found instanceof King) ){ return false;
}else{
if( toArray[0] >= '1' && toArray[0] <= '8' && toArray[1] >= 'A' && toArray[1] <= 'H' ){
return false; if( found.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
}else{ if( found.checkWay(this.allPieces, toArray[1], toArray[0] - '0') ){
if( found.canMove(x, y) ){ // Check if move is valid if( (found instanceof Pawn) && found.crossMove(toArray[1], toArray[0] - '0') ){
if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) ){
if( attack == null ){ if( attack == null ){
return false; return false;
}else{ }else{
if( y == attack.getY() && x == attack.getX() ){ if( toArray[0] - '0' == attack.getY() && toArray[1] == attack.getX() ){
if( found.color && y < found.getY() ){ if( found.color && toArray[0] < fromArray[0] ){
return true; return true;
}else if( !found.color && found.getY() < y ){ }else if( !found.color && fromArray[0] < toArray[0] ){
return true; return true;
}else{ }else{
...@@ -503,88 +582,36 @@ public class Board{ ...@@ -503,88 +582,36 @@ public class Board{
return false; return false;
} }
} }
}else if( (found instanceof Pawn) && attack != null && found.getX() - attack.getX() == 0 ){ }else if( (found instanceof Pawn) && toArray[1] - fromArray[1] == 0 && attack != null){
return false;
}else if( (found instanceof King) && virtualCheck(enemy, x, y)){
return false; return false;
}else{ }else if( (found instanceof King) && virtualCheck(enemy, toArray[1], toArray[0] - '0')){
return true;
}
}else{
return false;
}
}else{
return false;
}
}
}else{
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
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') ){
if( attack == null ){
return false; return false;
}else{ }else{
if( toArray[0] - '0' == attack.getY() && toArray[1] == attack.getX() ){ if( attack != null ){
if( found.color && toArray[0] < fromArray[0] ){
return true; enemy.remove(attack);
}else if( !found.color && fromArray[0] < toArray[0] ){ this.allPieces.remove(attack);
}
return true; char tempX = found.getX();
}else{ int tempY = found.getY();
return false; found.setY(toArray[0] - '0');
} found.setX(toArray[1]);
}else{ boolean flag = true;
return false; if( kingCheck(selector, enemy) ){
flag = false;
}
if( attack != null ){
enemy.add(attack);
this.allPieces.add(attack);
} }
found.setX(tempX);
found.setY(tempY);
return flag;
} }
}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{ }else{
return true; return false;
} }
}else{ }else{
return false; return false;
......
...@@ -6,6 +6,13 @@ public class King extends Piece{ ...@@ -6,6 +6,13 @@ public class King extends Piece{
super(x, y, color, 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 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 ){ if( Math.abs(this.x - x) < 2 && Math.abs(this.y - y) < 2 ){
...@@ -14,6 +21,14 @@ public class King extends Piece{ ...@@ -14,6 +21,14 @@ public class King extends Piece{
return false; 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
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true; return true;
} }
......
...@@ -6,6 +6,13 @@ public class Knight extends Piece{ ...@@ -6,6 +6,13 @@ public class Knight extends Piece{
super(x, y, color, 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){ public boolean canMove(char x, int y){
int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 }; int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 }; int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
...@@ -18,6 +25,14 @@ public class Knight extends Piece{ ...@@ -18,6 +25,14 @@ public class Knight extends Piece{
return false; 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
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true; return true;
} }
......
...@@ -17,6 +17,13 @@ public class Pawn extends Piece{ ...@@ -17,6 +17,13 @@ public class Pawn extends Piece{
return this.firstMove; return this.firstMove;
} }
/**
* 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 public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 ){ if( this.x - x == 0 ){
...@@ -37,6 +44,13 @@ public class Pawn extends Piece{ ...@@ -37,6 +44,13 @@ public class Pawn extends Piece{
return false; return false;
} }
/**
* Check if doing a cross move
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if doing a cross move
*/
@Override @Override
public boolean crossMove(char x, int y){ public boolean crossMove(char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){ if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
...@@ -46,6 +60,14 @@ public class Pawn extends Piece{ ...@@ -46,6 +60,14 @@ public class Pawn extends Piece{
return false; 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
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){ if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
......
...@@ -47,6 +47,14 @@ public abstract class Piece{ ...@@ -47,6 +47,14 @@ public abstract class Piece{
return this.color; return this.color;
} }
/**
* Check if (x, y) is taken
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The X-Axis
* @author Omiid
* @return true if it is taken
*/
public static Piece checkTaken(ArrayList<Piece> pieces, char x, int y){ public static Piece checkTaken(ArrayList<Piece> pieces, char x, int y){
for(Piece p : pieces){ for(Piece p : pieces){
if( p.x == x && p.y == y ){ if( p.x == x && p.y == y ){
...@@ -58,11 +66,34 @@ public abstract class Piece{ ...@@ -58,11 +66,34 @@ public abstract class Piece{
return null; return null;
} }
/**
* Check if doing a cross move
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if doing a cross move
*/
public boolean crossMove(char x, int y){ public boolean crossMove(char x, int y){
return false; return false;
} }
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if can move
*/
public abstract boolean canMove(char x, int y); public abstract boolean canMove(char x, int y);
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y); public abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
} }
...@@ -6,6 +6,13 @@ public class Queen extends Piece{ ...@@ -6,6 +6,13 @@ public class Queen extends Piece{
super(x, y, color, 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 public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){ if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){
...@@ -14,6 +21,14 @@ public class Queen extends Piece{ ...@@ -14,6 +21,14 @@ public class Queen extends Piece{
return false; 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
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Rook.checkWay(this, pieces, x, y) || Bishop.checkWay(this, pieces, x, y) ){ if( Rook.checkWay(this, pieces, x, y) || Bishop.checkWay(this, pieces, x, y) ){
......
...@@ -28,6 +28,7 @@ public class Rook extends Piece{ ...@@ -28,6 +28,7 @@ public class Rook extends Piece{
/** /**
* Check if can move to (X, Y) : Used for queen class * Check if can move to (X, Y) : Used for queen class
* @author Omiid * @author Omiid
* @param p the piece to move
* @param x The X-Axis * @param x The X-Axis
* @param y The Y-Axis * @param y The Y-Axis
* @return true if can move * @return true if can move
...@@ -45,6 +46,7 @@ public class Rook extends Piece{ ...@@ -45,6 +46,7 @@ public class Rook extends Piece{
* @param pieces ArrayList of all pieces * @param pieces ArrayList of all pieces
* @param x The X-Axis * @param x The X-Axis
* @param y The Y-Axis * @param y The Y-Axis
* @author Omiid
* @return true if is free to go * @return true if is free to go
*/ */
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
...@@ -75,6 +77,16 @@ public class Rook extends Piece{ ...@@ -75,6 +77,16 @@ public class Rook extends Piece{
} }
} }
/**
* Check if way is free to go
*
* @param p the piece to check
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){ public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = 0; int xShift = 0;
int yShift = 0; int yShift = 0;
......
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