Commit 9e9b637a authored by Omid Sayfun's avatar Omid Sayfun

Minor Bugs fixed

parent 8b09ab57
...@@ -43,18 +43,19 @@ class Splash{ ...@@ -43,18 +43,19 @@ class Splash{
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
panel.add(label); panel.add(label);
frame.add(label); frame.add(label);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack(); frame.pack();
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.dispose(); frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
server.close();
Chess newChess = new Chess("W", socket); Chess newChess = new Chess("W", socket);
newChess.run(); newChess.run();
socket.close();
}else { // Client }else { // Client
String s = (String)JOptionPane.showInputDialog( String s = (String)JOptionPane.showInputDialog(
null, null,
...@@ -70,7 +71,6 @@ class Splash{ ...@@ -70,7 +71,6 @@ class Splash{
Chess newChess = new Chess("B", socket); Chess newChess = new Chess("B", socket);
newChess.run(); newChess.run();
socket.close();
} }
} }
} }
...@@ -82,11 +82,9 @@ class newJButton extends JButton{ ...@@ -82,11 +82,9 @@ 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);
} }
...@@ -110,11 +108,10 @@ class newJButton extends JButton{ ...@@ -110,11 +108,10 @@ 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, boolean color){ public newJButton(Piece p, char X, int Y){
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"));
...@@ -141,9 +138,6 @@ class newJButton extends JButton{ ...@@ -141,9 +138,6 @@ class newJButton extends JButton{
return this.Y; return this.Y;
} }
public boolean getColor(){
return this.color;
}
} }
/** /**
...@@ -187,86 +181,171 @@ class Chess implements MouseListener{ ...@@ -187,86 +181,171 @@ class Chess implements MouseListener{
JPanel boardPanel = new JPanel(new GridLayout(8,8)); JPanel boardPanel = new JPanel(new GridLayout(8,8));
ArrayList<Piece> whitePieces = new ArrayList<Piece>(); ArrayList<Piece> whitePieces = new ArrayList<Piece>();
ArrayList<Piece> blackPieces = new ArrayList<Piece>(); ArrayList<Piece> blackPieces = new ArrayList<Piece>();
for(int i = 1; i < 9; i++){ if( this.me ){
for(char j = 'A'; j < 'I'; j++){
newJButton btn = null; for(int i = 1; i < 9; i++){
for(char j = 'A'; j < 'I'; j++){
if( i == 8 ){ newJButton btn = null;
if( j == 'A' || j == 'H' ){
if( i == 8 ){
Rook newRook = new Rook(j, i, true, "WR"); if( j == 'A' || j == 'H' ){
whitePieces.add(newRook);
btn = new newJButton(newRook, j, i, true); Rook newRook = new Rook(j, i, true, "WR");
}else if( j == 'B' || j == 'G' ){ whitePieces.add(newRook);
btn = new newJButton(newRook, j, i);
Knight newKnight = new Knight(j, i, true, "WN"); }else if( j == 'B' || j == 'G' ){
whitePieces.add(newKnight);
btn = new newJButton(newKnight, j, i, true); Knight newKnight = new Knight(j, i, true, "WN");
}else if( j == 'C' || j == 'F' ){ whitePieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
Bishop newBishop = new Bishop(j, i, true, "WB"); }else if( j == 'C' || j == 'F' ){
whitePieces.add(newBishop);
btn = new newJButton(newBishop, j, i, true); Bishop newBishop = new Bishop(j, i, true, "WB");
}else if( j == 'D' ){ whitePieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
Queen whiteQueen = new Queen(j, i, true,"WQ"); }else if( j == 'D' ){
whitePieces.add(whiteQueen);
btn = new newJButton(whiteQueen, j, i, true); Queen whiteQueen = new Queen(j, i, true,"WQ");
}else if( j == 'E' ){ whitePieces.add(whiteQueen);
King whiteKing = new King(j, i, true, "WK"); btn = new newJButton(whiteQueen, j, i);
whitePieces.add(whiteKing); }else if( j == 'E' ){
btn = new newJButton(whiteKing, j, i, true); 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);
} }
}else if( i == 7 ){ this.board = new Board(whitePieces, blackPieces);
Pawn newPawn = new Pawn(j, i, true, "WP"); this.btns.add(btn);
whitePieces.add(newPawn); btn.addMouseListener(this);
btn = new newJButton(newPawn, j, i, true); btn.setFocusable(false);
}else if( i == 1 ){ if( (i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ){
if( j == 'A' || j == 'H' ){ btn.setBackground(new Color(219,217,164));
}else{
Rook newRook = new Rook(j, i, false, "BR"); btn.setBackground(new Color(46, 83,106));
blackPieces.add(newRook);
btn = new newJButton(newRook, j, i, false);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, false, "BN");
blackPieces.add(newKnight);
btn = new newJButton(newKnight, j, i, false);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, false, "BB");
blackPieces.add(newBishop);
btn = new newJButton(newBishop, j, i, false);
}else if( j == 'D' ){
Queen blackQueen = new Queen(j, i, false,"BQ");
blackPieces.add(blackQueen);
btn = new newJButton(blackQueen, j, i, false);
}else if( j == 'E' ){
King blackKing = new King(j, i, false, "BQ");
blackPieces.add(blackKing);
btn = new newJButton(blackKing, j, i, false);
} }
}else if( i == 2 ){ boardPanel.add(btn);
Pawn newPawn = new Pawn(j, i, false, "BP");
blackPieces.add(newPawn);
btn = new newJButton(newPawn, j, i, false);
// btn = new newJButton(j, i);
}else{
btn = new newJButton(j, i);
} }
this.board = new Board(whitePieces, blackPieces); }
this.btns.add(btn); }else{
btn.addMouseListener(this); for(int i = 8; i >= 1; i--){
btn.setFocusable(false); for(char j = 'H'; j >= 'A'; j--){
if( (i % 2 == 1 && j % 2 == 1) || (i % 2 == 0 && j % 2 == 0) ){ newJButton btn = null;
btn.setBackground(new Color(219,217,164));
}else{ if( i == 8 ){
btn.setBackground(new Color(46, 83,106)); 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(46, 83,106));
}
boardPanel.add(btn);
} }
boardPanel.add(btn);
} }
} }
// Left Divider Panel // Left Divider Panel
JPanel leftDivider = new JPanel(new GridLayout(3, 1)); JPanel leftDivider = new JPanel(new GridLayout(3, 1));
// Top Left // Top Left
...@@ -326,12 +405,14 @@ class Chess implements MouseListener{ ...@@ -326,12 +405,14 @@ class Chess implements MouseListener{
this.frame = frame; this.frame = frame;
String line = ""; String line = "";
while (!line.equals("Over")) while (true)
{ {
try try
{ {
line = this.input.readUTF(); line = this.input.readUTF();
if( line.equals("Over") ){
break;
}
System.out.println(line); System.out.println(line);
String[] splited = line.split(" "); String[] splited = line.split(" ");
for(newJButton btn : this.btns){ for(newJButton btn : this.btns){
...@@ -390,13 +471,17 @@ class Chess implements MouseListener{ ...@@ -390,13 +471,17 @@ class Chess implements MouseListener{
System.out.println(i); System.out.println(i);
} }
} }
try { if( this.output != null ){
this.output.writeUTF("Over");
} catch (IOException e1) { try {
e1.printStackTrace(); this.output.writeUTF("Over");
} catch (IOException e1) {
e1.printStackTrace();
}
} }
System.out.println("Closing connection"); System.out.println("Closing connection");
this.socket.close();
this.input.close(); this.input.close();
this.output.close(); this.output.close();
} }
...@@ -416,7 +501,7 @@ class Chess implements MouseListener{ ...@@ -416,7 +501,7 @@ class Chess implements MouseListener{
}else if( !this.me && this.color.equals("B") ){ }else if( !this.me && this.color.equals("B") ){
flag = true; flag = true;
} }
if( source.getColor() == this.me && flag ){ if( source.getPiece() != null && source.getPiece().getColor() == this.me && flag ){
for(newJButton btn : this.btns){ for(newJButton btn : this.btns){
if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){ if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
...@@ -471,9 +556,9 @@ class Chess implements MouseListener{ ...@@ -471,9 +556,9 @@ class Chess implements MouseListener{
caption.setText("White Player is playing"); caption.setText("White Player is playing");
this.color = "W"; this.color = "W";
} }
this.isSelected = false;
this.inMove = null;
} }
this.isSelected = false;
this.inMove = null;
repaint(); repaint();
try { try {
checkMate(); checkMate();
...@@ -535,10 +620,13 @@ class Chess implements MouseListener{ ...@@ -535,10 +620,13 @@ class Chess implements MouseListener{
public void checkMate() throws IOException { public void checkMate() throws IOException {
if( this.board.checkMate() != null ){ if( this.board.checkMate() != null ){
try { if ( !this.socket.isClosed() ){
this.output.writeUTF("Over");
} catch (IOException e1) { try {
e1.printStackTrace(); this.output.writeUTF("Over");
} catch (IOException e1) {
e1.printStackTrace();
}
} }
String prompt = ""; String prompt = "";
if( this.board.checkMate().equals("W") ){ if( this.board.checkMate().equals("W") ){
...@@ -548,12 +636,14 @@ class Chess implements MouseListener{ ...@@ -548,12 +636,14 @@ class Chess implements MouseListener{
prompt = prompt.concat("White Player Won!"); prompt = prompt.concat("White Player Won!");
} }
prompt = prompt.concat(" \nDo you want to start a new game?"); 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); int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){ if( result == JOptionPane.YES_OPTION ){
Main.rerun(); Main.rerun();
}else{
System.exit(0);
} }
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
} }
} }
......
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