Commit 8b09ab57 authored by Omid Sayfun's avatar Omid Sayfun

Socket added

parent 67380b70
...@@ -6,21 +6,74 @@ import java.awt.event.MouseEvent; ...@@ -6,21 +6,74 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*; import java.util.*;
import lab.game.*; import lab.game.*;
import java.io.IOException; import java.io.IOException;
public class Main{ public class Main{
public static void main(String[] args){ public static void main(String[] args) throws IOException {
Chess newChess = new Chess(); Splash newSplash = new Splash();
newChess.run(); newSplash.run();
} }
public static void rerun(){ public static void rerun() throws IOException {
Chess newChess = new Chess(); Splash newSplash = new Splash();
newChess.run(); newSplash.run();
} }
} }
class Splash{
public void run() throws IOException {
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]);
if( result == JOptionPane.YES_OPTION ){ // Server
ServerSocket server = new ServerSocket(5000);
System.out.println("Server Created");
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Waiting for opponent...");
label.setBorder(new EmptyBorder(40,40,40,40));
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
panel.add(label);
frame.add(label);
frame.pack();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setVisible(true);
Socket socket = server.accept();
System.out.println("Client Connected");
frame.dispose();
Chess newChess = new Chess("W", socket);
newChess.run();
socket.close();
}else { // Client
String s = (String)JOptionPane.showInputDialog(
null,
"Enter Server IP:",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"x.x.x.x");
Socket socket = new Socket(s, 5000);
System.out.println("Connected to server");
Chess newChess = new Chess("B", socket);
newChess.run();
socket.close();
}
}
}
/** /**
* A New Button class that has piece attribute * A New Button class that has piece attribute
* @author Omiid * @author Omiid
...@@ -29,9 +82,11 @@ class newJButton extends JButton{ ...@@ -29,9 +82,11 @@ class newJButton extends JButton{
private Piece piece; private Piece piece;
private char X; private char X;
private int Y; private int Y;
private Boolean color;
public newJButton(){ public newJButton(){
this.piece = null; this.piece = null;
this.color = null;
this.setIcon(null); this.setIcon(null);
} }
...@@ -55,10 +110,11 @@ class newJButton extends JButton{ ...@@ -55,10 +110,11 @@ class newJButton extends JButton{
* @param Y The Y-Axis * @param Y The Y-Axis
* @author Omiid * @author Omiid
*/ */
public newJButton(Piece p, char X, int Y){ public newJButton(Piece p, char X, int Y, boolean color){
this.piece = p; this.piece = p;
this.X = X; this.X = X;
this.Y = Y; this.Y = Y;
this.color = color;
Image img = null; Image img = null;
try { try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + p.getName().toLowerCase() + ".png")); img = ImageIO.read(Chess.class.getResource("resources/images/" + p.getName().toLowerCase() + ".png"));
...@@ -72,6 +128,7 @@ class newJButton extends JButton{ ...@@ -72,6 +128,7 @@ class newJButton extends JButton{
return this.piece; return this.piece;
} }
public void setPiece(Piece p){ public void setPiece(Piece p){
this.piece = p; this.piece = p;
} }
...@@ -83,13 +140,21 @@ class newJButton extends JButton{ ...@@ -83,13 +140,21 @@ class newJButton extends JButton{
public int getNewY(){ public int getNewY(){
return this.Y; return this.Y;
} }
public boolean getColor(){
return this.color;
}
} }
/** /**
* Main Chess Class * Main Chess Class
*/ */
class Chess implements MouseListener{ class Chess implements MouseListener{
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
private Board board = null; private Board board = null;
private boolean me;
private String color = "W"; private String color = "W";
private boolean isSelected = false; private boolean isSelected = false;
private newJButton inMove = null; private newJButton inMove = null;
...@@ -98,10 +163,22 @@ class Chess implements MouseListener{ ...@@ -98,10 +163,22 @@ class Chess implements MouseListener{
private ArrayList<newJButton> whiteLost = new ArrayList<>(); private ArrayList<newJButton> whiteLost = new ArrayList<>();
private ArrayList<newJButton> blackLost = new ArrayList<>(); private ArrayList<newJButton> blackLost = new ArrayList<>();
private JLabel caption; private JLabel caption;
public Chess(String me, Socket socket) throws IOException {
if( me.equals("W") ){
this.me = true;
}else{
this.me = false;
}
this.socket = socket;
this.input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
this.output = new DataOutputStream(this.socket.getOutputStream());
}
/** /**
* Initial Run * Initial Run
*/ */
public void run(){ public void run() throws IOException {
JFrame frame = new JFrame("Chess Frame"); JFrame frame = new JFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH); frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
// Biggest Panel // Biggest Panel
...@@ -119,61 +196,61 @@ class Chess implements MouseListener{ ...@@ -119,61 +196,61 @@ class Chess implements MouseListener{
Rook newRook = new Rook(j, i, true, "WR"); Rook newRook = new Rook(j, i, true, "WR");
whitePieces.add(newRook); whitePieces.add(newRook);
btn = new newJButton(newRook, j, i); btn = new newJButton(newRook, j, i, true);
}else if( j == 'B' || j == 'G' ){ }else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, true, "WN"); Knight newKnight = new Knight(j, i, true, "WN");
whitePieces.add(newKnight); whitePieces.add(newKnight);
btn = new newJButton(newKnight, j, i); btn = new newJButton(newKnight, j, i, true);
}else if( j == 'C' || j == 'F' ){ }else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, true, "WB"); Bishop newBishop = new Bishop(j, i, true, "WB");
whitePieces.add(newBishop); whitePieces.add(newBishop);
btn = new newJButton(newBishop, j, i); btn = new newJButton(newBishop, j, i, true);
}else if( j == 'D' ){ }else if( j == 'D' ){
Queen whiteQueen = new Queen(j, i, true,"WQ"); Queen whiteQueen = new Queen(j, i, true,"WQ");
whitePieces.add(whiteQueen); whitePieces.add(whiteQueen);
btn = new newJButton(whiteQueen, j, i); btn = new newJButton(whiteQueen, j, i, true);
}else if( j == 'E' ){ }else if( j == 'E' ){
King whiteKing = new King(j, i, true, "WK"); King whiteKing = new King(j, i, true, "WK");
whitePieces.add(whiteKing); whitePieces.add(whiteKing);
btn = new newJButton(whiteKing, j, i); btn = new newJButton(whiteKing, j, i, true);
} }
}else if( i == 7 ){ }else if( i == 7 ){
Pawn newPawn = new Pawn(j, i, true, "WP"); Pawn newPawn = new Pawn(j, i, true, "WP");
whitePieces.add(newPawn); whitePieces.add(newPawn);
btn = new newJButton(newPawn, j, i); btn = new newJButton(newPawn, j, i, true);
}else if( i == 1 ){ }else if( i == 1 ){
if( j == 'A' || j == 'H' ){ if( j == 'A' || j == 'H' ){
Rook newRook = new Rook(j, i, false, "BR"); Rook newRook = new Rook(j, i, false, "BR");
blackPieces.add(newRook); blackPieces.add(newRook);
btn = new newJButton(newRook, j, i); btn = new newJButton(newRook, j, i, false);
}else if( j == 'B' || j == 'G' ){ }else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, false, "BN"); Knight newKnight = new Knight(j, i, false, "BN");
blackPieces.add(newKnight); blackPieces.add(newKnight);
btn = new newJButton(newKnight, j, i); btn = new newJButton(newKnight, j, i, false);
}else if( j == 'C' || j == 'F' ){ }else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, false, "BB"); Bishop newBishop = new Bishop(j, i, false, "BB");
blackPieces.add(newBishop); blackPieces.add(newBishop);
btn = new newJButton(newBishop, j, i); btn = new newJButton(newBishop, j, i, false);
}else if( j == 'D' ){ }else if( j == 'D' ){
Queen blackQueen = new Queen(j, i, false,"BQ"); Queen blackQueen = new Queen(j, i, false,"BQ");
blackPieces.add(blackQueen); blackPieces.add(blackQueen);
btn = new newJButton(blackQueen, j, i); btn = new newJButton(blackQueen, j, i, false);
}else if( j == 'E' ){ }else if( j == 'E' ){
King blackKing = new King(j, i, false, "BQ"); King blackKing = new King(j, i, false, "BQ");
blackPieces.add(blackKing); blackPieces.add(blackKing);
btn = new newJButton(blackKing, j, i); btn = new newJButton(blackKing, j, i, false);
} }
}else if( i == 2 ){ }else if( i == 2 ){
Pawn newPawn = new Pawn(j, i, false, "BP"); Pawn newPawn = new Pawn(j, i, false, "BP");
blackPieces.add(newPawn); blackPieces.add(newPawn);
btn = new newJButton(newPawn, j, i); btn = new newJButton(newPawn, j, i, false);
// btn = new newJButton(j, i); // btn = new newJButton(j, i);
}else{ }else{
btn = new newJButton(j, i); btn = new newJButton(j, i);
...@@ -247,7 +324,82 @@ class Chess implements MouseListener{ ...@@ -247,7 +324,82 @@ class Chess implements MouseListener{
frame.setVisible(true); frame.setVisible(true);
this.frame = frame; this.frame = frame;
}
String line = "";
while (!line.equals("Over"))
{
try
{
line = this.input.readUTF();
System.out.println(line);
String[] splited = line.split(" ");
for(newJButton btn : this.btns){
if( btn.getNewX() == splited[0].charAt(1) && btn.getNewY() == splited[0].charAt(0) - '0' ){
System.out.println("Found inmove");
this.inMove = btn;
}
}
newJButton source = null;
for(newJButton btn : this.btns){
if( btn.getNewX() == splited[1].charAt(1) && btn.getNewY() == splited[1].charAt(0) - '0' ){
System.out.println("Found source");
source = btn;
}
}
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) ){
Piece temp = source.getPiece();
source.setPiece(this.inMove.getPiece());
this.inMove.setPiece(null);
if( this.color.equals("W") ){
killBlack(temp);
}else{
killWhite(temp);
}
}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")){
caption.setText("Black Player is playing");
this.color = "B";
}else{
caption.setText("White Player is playing");
this.color = "W";
}
this.isSelected = false;
this.inMove = null;
}
repaint();
try {
checkMate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
catch(IOException i)
{
System.out.println(i);
}
}
try {
this.output.writeUTF("Over");
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Closing connection");
this.input.close();
this.output.close();
}
/** /**
* Mouse Clicking behaviour * Mouse Clicking behaviour
...@@ -256,23 +408,33 @@ class Chess implements MouseListener{ ...@@ -256,23 +408,33 @@ class Chess implements MouseListener{
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
newJButton source = (newJButton)(e.getSource()); newJButton source = (newJButton)(e.getSource());
if( !isSelected ){ if( !this.isSelected ){
for(newJButton btn : this.btns){ boolean flag = false;
if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){ if( this.me && this.color.equals("W") ){
if( this.board.canAttack(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
btn.setBackground(new Color(255, 0, 0)); flag = true;
}else{ }else if( !this.me && this.color.equals("B") ){
Image img = null; flag = true;
try { }
img = ImageIO.read(Chess.class.getResource("resources/images/dot.png")); if( source.getColor() == this.me && flag ){
} catch (IOException e1) {
e1.printStackTrace(); 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"));
} }
btn.setIcon(new ImageIcon(img, "dot")); this.isSelected = true;
this.inMove = source;
} }
this.isSelected = true;
this.inMove = source;
} }
} }
}else{ }else{
...@@ -293,7 +455,14 @@ class Chess implements MouseListener{ ...@@ -293,7 +455,14 @@ class Chess implements MouseListener{
this.inMove.setPiece(source.getPiece()); this.inMove.setPiece(source.getPiece());
source.setPiece(temp); 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.board.move( inMove.getNewY() + Character.toString(inMove.getNewX()), source.getNewY() + Character.toString(source.getNewX()), this.color) ){
try {
this.output.writeUTF(inMove.getNewY() + Character.toString(inMove.getNewX()) + " " + source.getNewY() + source.getNewX());
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(true);
}
if( this.color.equals("W")){ if( this.color.equals("W")){
caption.setText("Black Player is playing"); caption.setText("Black Player is playing");
...@@ -302,89 +471,89 @@ class Chess implements MouseListener{ ...@@ -302,89 +471,89 @@ class Chess implements MouseListener{
caption.setText("White Player is playing"); caption.setText("White Player is playing");
this.color = "W"; this.color = "W";
} }
}
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(46, 83,106));
}
this.isSelected = false; this.isSelected = false;
this.inMove = null; this.inMove = null;
} }
for(newJButton btn : this.whiteLost) { repaint();
if( btn.getPiece() != null ){ try {
Image img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB); checkMate();
try { } catch (IOException e1) {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png")); e1.printStackTrace();
} catch (IOException e1) { }
e1.printStackTrace(); }
} }
btn.setIcon(new ImageIcon(img));
}else{ public void repaint(){
btn.setIcon(new ImageIcon( for(newJButton btn : this.btns) {
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB))); 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();
} }
// if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){ btn.setIcon(new ImageIcon(img));
// btn.setBackground(new Color(219,217,164)); }else{
// }else{ btn.setIcon(null);
// btn.setBackground(new Color(4,51,106));
// }
// this.isSelected = false;
// this.inMove = null;
} }
for(newJButton btn : this.blackLost) { if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
if( btn.getPiece() != null ){ btn.setBackground(new Color(219,217,164));
Image img = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB); }else{
try { btn.setBackground(new Color(46, 83,106));
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png")); }
} catch (IOException e1) { }
e1.printStackTrace(); for(newJButton btn : this.whiteLost) {
} if( btn.getPiece() != null ){
btn.setIcon(new ImageIcon(img)); Image img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
}else{ try {
btn.setIcon(new ImageIcon( img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB))); } catch (IOException e1) {
e1.printStackTrace();
} }
// if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){ btn.setIcon(new ImageIcon(img));
// btn.setBackground(new Color(219,217,164)); }else{
// }else{ btn.setIcon(new ImageIcon(
// btn.setBackground(new Color(4,51,106)); new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)));
// }
// this.isSelected = false;
// this.inMove = null;
} }
if( this.board.checkMate() != null ){ }
for(newJButton btn : this.blackLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
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(new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)));
}
}
}
String prompt = ""; public void checkMate() throws IOException {
if( this.board.checkMate().equals("W") ){ if( this.board.checkMate() != null ){
prompt = prompt.concat("Black Player Won!"); try {
}else{ this.output.writeUTF("Over");
prompt = prompt.concat("White Player Won!"); } catch (IOException e1) {
} e1.printStackTrace();
prompt = prompt.concat(" \nDo you want to start a new game?"); }
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION); String prompt = "";
if( result == JOptionPane.YES_OPTION ){ if( this.board.checkMate().equals("W") ){
Main.rerun(); prompt = prompt.concat("Black Player Won!");
} }else{
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING)); prompt = prompt.concat("White Player Won!");
}
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 ){
Main.rerun();
} }
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
} }
} }
......
...@@ -36,7 +36,7 @@ public class Bishop extends Piece{ ...@@ -36,7 +36,7 @@ public class Bishop extends Piece{
int yShift = (y - p.y) / Math.abs(y - p.y); int yShift = (y - p.y) / Math.abs(y - p.y);
int i = 1; int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){ while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){ if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) != null && !(checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) instanceof King) ){
return false; return false;
} }
...@@ -50,7 +50,7 @@ public class Bishop extends Piece{ ...@@ -50,7 +50,7 @@ public class Bishop extends Piece{
int yShift = (y - this.y) / Math.abs(y - this.y); int yShift = (y - this.y) / Math.abs(y - this.y);
int i = 1; int i = 1;
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){ while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){ if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) != null && !(checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) instanceof King) ){
return false; return false;
} }
......
...@@ -343,12 +343,18 @@ public class Board{ ...@@ -343,12 +343,18 @@ 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
if( p.checkWay(this.allPieces, x, y) ){ if( p.checkWay(this.allPieces, x, y) ){
return true; if( p instanceof Pawn && p.getX() - x == 0 ){
return false;
}else{
return true;
}
} }
} }
} }
...@@ -630,7 +636,31 @@ public class Board{ ...@@ -630,7 +636,31 @@ public class Board{
if( found.checkWay(this.allPieces, x, y) ){ if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){ 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; return false;
}else{ }else{
......
...@@ -55,7 +55,7 @@ public class Pawn extends Piece{ ...@@ -55,7 +55,7 @@ public class Pawn extends Piece{
return true; return true;
}else{ }else{
int yShift = (y - this.y) / Math.abs(y - this.y); 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) ){ if( checkTaken(pieces, this.x, this.y + yShift) != null || checkTaken(pieces, this.x, this.y + 2 * yShift) != null ){
return false; return false;
} }
......
...@@ -47,14 +47,15 @@ public abstract class Piece{ ...@@ -47,14 +47,15 @@ public abstract class Piece{
return this.color; return this.color;
} }
public static boolean 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 ){
return true;
return p;
} }
} }
return false; return null;
} }
public boolean crossMove(char x, int y){ public boolean crossMove(char x, int y){
......
...@@ -63,7 +63,7 @@ public class Rook extends Piece{ ...@@ -63,7 +63,7 @@ public class Rook extends Piece{
int i = 1; int i = 1;
boolean flag = true; boolean flag = true;
while( this.x != (char)(x + i * xShift) || this.y != y + i * yShift ){ while( this.x != (char)(x + i * xShift) || this.y != y + i * yShift ){
if( checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) ){ if( checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) != null && !(checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) instanceof King) ){
flag = false; flag = false;
} }
...@@ -90,7 +90,7 @@ public class Rook extends Piece{ ...@@ -90,7 +90,7 @@ public class Rook extends Piece{
} }
int i = 1; int i = 1;
while( x != (char)(p.x + i * xShift) || y != p.y + i * yShift ){ while( x != (char)(p.x + i * xShift) || y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){ if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) != null && !(checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) instanceof King) ){
return false; 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