Commit 4f26b735 authored by kimia's avatar kimia

network done

parent ebc68736
/** /**
* Bishop class show the Bishop's moves that moves diagonally, and can move as far as a player wants it to unless another piece blocks it. * Bishop class show the Bishop's moves that moves diagonally, and can move
* as far as a player wants it to unless another piece blocks it.
*@author kimiadorani *@author kimiadorani
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
...@@ -12,6 +13,15 @@ public class Bishop extends Piece { ...@@ -12,6 +13,15 @@ public class Bishop extends Piece {
super(cell, pieceColor) ; super(cell, pieceColor) ;
} }
/**
* this method check that if the move for Bishop is possible or not !
* @param c
* @param board
* @param pw
* @param pb
* @return
*/
@Override @Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ; if (super.isDeleted()) return false ;
......
/** /**
* Cell class show's the cells in a board for pieces. * This class shows the cell's of the board
*@author kimiadorani
*@version 1.0
*@since 2019-5-7
*/ */
class Cell { class Cell {
private int row, col ;
private boolean empty ;
public Cell (int row, int col) { private int row, col;
this.row = row ; private boolean empty;
this.col = col ;
this.empty = true ;
}
public Cell(Cell c){
if(c == null) return;
this.row = c.getRow();
this.col = c.col;
this.empty = c.empty;
}
public int getRow() { public Cell(int row, int col) {
return row ; this.row = row;
} this.col = col;
this.empty = true;
}
public int getCol() { public Cell(Cell c) {
return col ; if (c == null) return;
} this.row = c.getRow();
this.col = c.col;
this.empty = c.empty;
}
public boolean isEmpty() { public Cell(int row, int col, boolean bool) {
return empty ; this.row = row;
} this.col = col;
this.empty = bool;
}
public void setRow (int row) { public int getRow() {
this.row = row ; return row;
} }
public void setCol (int col) { public int getCol() {
this.col = col ; return col;
} }
public boolean isEmpty() {
return empty;
}
public void setRow(int row) {
this.row = row;
}
public void setCol(int col) {
this.col = col;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public void setEmpty(boolean empty) {
this.empty = empty ;
} }
} enum PieceColor {
BLACK, WHITE
enum PieceColor {
BLACK, WHITE
} }
This diff is collapsed.
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/** /**
* ChessGameGUI is a class where we set the size and positions of the frames * ChessGameGUI is a class where we set the size and positi of the frames
*@author kimiadorani *@author kimiadorani
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
*/ */
public class ChessGameGUI extends JFrame { public class ChessGameGUI extends JFrame {
private String URL ="";
private PrintWriter out;
private Scanner input;
private ChessBoardGUI gameBoard;
public static void main(String args[]){ public static void main(String args[]){
new ChessGameGUI(); new ChessGameGUI();
} }
public ChessGameGUI(){ public ChessGameGUI(){
super(); super();
URL = JOptionPane.showInputDialog("Insert Server Address (Leave Blank if want be Server)");
if(URL.isEmpty()){
server();
ChessBoardGUI.isServer = true;
setTitle("Server: Player 1(White)");
}
else {
client(URL);
setTitle("Client: Player 2(Black)");
}
JPanel parent = new JPanel(); JPanel parent = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1300,610); setSize(1300,610);
...@@ -31,7 +52,7 @@ public class ChessGameGUI extends JFrame { ...@@ -31,7 +52,7 @@ public class ChessGameGUI extends JFrame {
blackRemovePanel.setSize(200,600); blackRemovePanel.setSize(200,600);
//----------------------------------------- //-----------------------------------------
JPanel gameBoard = new ChessBoardGUI(whiteRemovePanel,blackRemovePanel,turnPanel,this); gameBoard = new ChessBoardGUI(whiteRemovePanel,blackRemovePanel,turnPanel,this,out);
gameBoard.setSize(800,600); gameBoard.setSize(800,600);
parent.add(whiteRemovePanel); parent.add(whiteRemovePanel);
parent.add(turnPanel); parent.add(turnPanel);
...@@ -40,4 +61,72 @@ public class ChessGameGUI extends JFrame { ...@@ -40,4 +61,72 @@ public class ChessGameGUI extends JFrame {
add(parent); add(parent);
setVisible(true); setVisible(true);
} }
/**
* creating a socket and waits for a client to connect with 1234 port as soon as
* a client connected to the socket it gets an inputStream and outputStream
*/
private void server(){
try {
ServerSocket server = new ServerSocket(1234);
Socket socket = server.accept();
System.out.println("Server Socket Completed");
input = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream());
Thread thread = new Thread() {
@Override
public void run() {
while(input.hasNextLine()){
String str = input.nextLine();
System.out.println("Client Message:"+str);
String splt[] = str.split("\t");
Cell source = new Cell(Integer.parseInt(splt[0]),Integer.parseInt(splt[1]));
Cell dest = new Cell(Integer.parseInt(splt[2]),Integer.parseInt(splt[3]));
if(gameBoard.movePiece(source,dest)) {
gameBoard.updatesAfterMove();
}
}
}
};
thread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* try to connect to the socket and gets an inputStream and outputStream
* @param address address is an input we give to the system which is the address of the server
*/
private void client(String address){
try {
Socket socket = new Socket(address,1234);
System.out.println("Client Socket Completed");
out = new PrintWriter(socket.getOutputStream());
input = new Scanner(socket.getInputStream());
out.flush();
Thread thread = new Thread() {
@Override
public void run() {
while(input.hasNextLine()){
String str = input.nextLine();
System.out.println("Server Message: "+str);
String splt[] = str.split("\t");
Cell source = new Cell(Integer.parseInt(splt[0]),Integer.parseInt(splt[1]));
Cell dest = new Cell(Integer.parseInt(splt[2]),Integer.parseInt(splt[3]));
if(gameBoard.movePiece(source,dest)) {
gameBoard.updatesAfterMove();
}
}
}
};
thread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
/** /**
* King class show the king's move that can move only one to any direction .(king ia the most important piece in chess) * King class show the king's move that can move only one to any direction .
* (king ia the most important piece in chess)
*@author kimiadorani *@author kimiadorani
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
......
This diff is collapsed.
/** /**
* Pawn class show the pawn's moves that can move forward one or two step in their first move , after that they can only move one space forward , to capture , the pawn moves one step diagonally. * Pawn class show the pawn's moves that can move forward one or two step in their first move ,
* after that they can only move one space forward , to capture , the pawn moves one step diagonally.
*@author kimiadorani *@author kimiadorani
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
...@@ -44,20 +45,19 @@ public class Pawn extends Piece{ ...@@ -44,20 +45,19 @@ public class Pawn extends Piece{
} }
if (once) { if (once) {
//once = false ; if ( ((this.getCell().getCol() == c.getCol()) &&
//System.out.println("row: " + this.getCell().getRow()) ; (Math.abs(this.getCell().getRow()-c.getRow())==1 ||
//System.out.println("col: " + this.getCell().getCol()) ; ( Math.abs(this.getCell().getRow()-c.getRow())==2 &&
if ((this.getCell().getCol() == c.getCol()) && ((super.getPieceColor()==PieceColor.WHITE && board[c.getRow()-1][c.getCol()].isEmpty()) ||
(Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2) (super.getPieceColor()==PieceColor.BLACK && board[c.getRow()+1][c.getCol()].isEmpty()) ) )
&& c.isEmpty()){ )
if(getCell().getRow() < c.getRow() && getPieceColor()==PieceColor.WHITE) { && c.isEmpty())
return true; ||
}else if(getCell().getRow() > c.getRow() && getPieceColor()==PieceColor.BLACK){ (Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
return true; ((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()==PieceColor.BLACK && sw==1) ||
}else { (this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()==PieceColor.WHITE && sw==1)) )
return false; )
} return true ;
}
else return false ; else return false ;
} }
else { else {
......
...@@ -9,6 +9,7 @@ import java.io.File; ...@@ -9,6 +9,7 @@ import java.io.File;
*/ */
public abstract class Piece { public abstract class Piece {
private Cell cell ; private Cell cell ;
private PieceColor pieceColor; private PieceColor pieceColor;
private boolean deleted ; private boolean deleted ;
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
*@since 2019-5-7 *@since 2019-5-7
*/ */
public class Player { public class Player {
private Piece[] pieces = new Piece[16] ; private Piece[] pieces = new Piece[16];
private PieceColor pieceColor; private PieceColor pieceColor;
public Piece[] getPieces() { public Piece[] getPieces() {
return pieces ; return pieces;
} }
public PieceColor getPieceColor() { public PieceColor getPieceColor() {
...@@ -17,41 +17,40 @@ public class Player { ...@@ -17,41 +17,40 @@ public class Player {
} }
public void setPieces(Piece[] pieces) { public void setPieces(Piece[] pieces) {
this.pieces = pieces ; this.pieces = pieces;
} }
public void setPieceColor(PieceColor pieceColor) { public void setPieceColor(PieceColor pieceColor) {
this.pieceColor = pieceColor; this.pieceColor = pieceColor;
} }
public Player(PieceColor pieceColor) { public Player(PieceColor pieceColor) {
this.pieceColor = pieceColor; this.pieceColor = pieceColor;
if (pieceColor == PieceColor.WHITE) { if (pieceColor == PieceColor.WHITE) {
for(int i=0 ; i<8 ; i++) { for (int i = 0; i < 8; i++) {
pieces[i] = new Pawn(new Cell(1,i), pieceColor) ; pieces[i] = new Pawn(new Cell(1, i), pieceColor);
} }
pieces[8] = new Bishop(new Cell(0,2), pieceColor) ; pieces[8] = new Bishop(new Cell(0, 2), pieceColor);
pieces[9] = new Bishop(new Cell(0,5), pieceColor) ; pieces[9] = new Bishop(new Cell(0, 5), pieceColor);
pieces[10] = new Knight(new Cell(0,1), pieceColor) ; pieces[10] = new Knight(new Cell(0, 1), pieceColor);
pieces[11] = new Knight(new Cell(0,6), pieceColor) ; pieces[11] = new Knight(new Cell(0, 6), pieceColor);
pieces[12] = new Rook(new Cell(0,0), pieceColor) ; pieces[12] = new Rook(new Cell(0, 0), pieceColor);
pieces[13] = new Rook(new Cell(0,7), pieceColor) ; pieces[13] = new Rook(new Cell(0, 7), pieceColor);
pieces[14] = new King(new Cell(0,4), pieceColor) ; pieces[14] = new King(new Cell(0, 4), pieceColor);
pieces[15] = new Queen(new Cell(0,3), pieceColor) ; pieces[15] = new Queen(new Cell(0, 3), pieceColor);
} } else {
else { for (int i = 0; i < 8; i++) {
for(int i=0 ; i<8 ; i++){ pieces[i] = new Pawn(new Cell(6, i), pieceColor);
pieces[i] = new Pawn(new Cell(6,i), pieceColor) ;
} }
pieces[8] = new Bishop(new Cell(7,2), pieceColor) ; pieces[8] = new Bishop(new Cell(7, 2), pieceColor);
pieces[9] = new Bishop(new Cell(7,5), pieceColor) ; pieces[9] = new Bishop(new Cell(7, 5), pieceColor);
pieces[10] = new Knight(new Cell(7,1), pieceColor) ; pieces[10] = new Knight(new Cell(7, 1), pieceColor);
pieces[11] = new Knight(new Cell(7,6), pieceColor) ; pieces[11] = new Knight(new Cell(7, 6), pieceColor);
pieces[12] = new Rook(new Cell(7,0), pieceColor) ; pieces[12] = new Rook(new Cell(7, 0), pieceColor);
pieces[13] = new Rook(new Cell(7,7), pieceColor) ; pieces[13] = new Rook(new Cell(7, 7), pieceColor);
pieces[14] = new King(new Cell(7,4), pieceColor) ; pieces[14] = new King(new Cell(7, 4), pieceColor);
pieces[15] = new Queen(new Cell(7,3), pieceColor) ; pieces[15] = new Queen(new Cell(7, 3), pieceColor);
} }
} }
} }
\ No newline at end of file
/** /**
* Queen class show the Queen's moves which is the most powerful piece in the game , It reunites the power of rook and the bishop ( it can go in a straight line horizontally and vertically also it can move diagonally. * Queen class show the Queen's moves which is the most powerful piece in the game ,
* It reunites the power of rook and the bishop
* ( it can go in a straight line horizontally and vertically also it can move diagonally.)
*@author kimiadorani *@author kimiadorani
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
...@@ -11,6 +13,18 @@ public class Queen extends Piece { ...@@ -11,6 +13,18 @@ public class Queen extends Piece {
super(cell, pieceColor) ; super(cell, pieceColor) ;
} }
/**
* isValidMove is the method which has been overrided from the piece class
* this method in this class checks the validity of the moves that queen wants to make
* @param c
* @param board
* @param pw
* @param pb
* @return
*/
@Override @Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ; if (super.isDeleted()) return false ;
......
...@@ -11,55 +11,70 @@ public class Rook extends Piece { ...@@ -11,55 +11,70 @@ public class Rook extends Piece {
super(cell, pieceColor) ; super(cell, pieceColor) ;
} }
@Override /**
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) { * isValidMove is the method which has been overrided from the piece class
if (super.isDeleted()) return false ; * here we check if the move rook wants to make is possible or not!
int sw=0 ; * @param c the cel rook is in it
if (super.getPieceColor() == PieceColor.BLACK) { * @param board
for (Piece p:pw) { * @param pw the white player piece
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ; * @param pb the black player piece
} * @return true if the moves were possible and false if they were not
} */
else { /**
for (Piece p:pb) { * shows the moves of the rook , it can move straight
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ; * @author kimiadorani
} * @version 1.0
}
if ( ( */
(this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow())
) && (c.isEmpty() || (!c.isEmpty() && sw==1)) @Override
) { public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() < c.getRow()) { if (super.isDeleted()) return false ;
for (int i=1 ; i<8 ; ++i) { int sw=0 ;
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ; if (super.getPieceColor() == PieceColor.BLACK) {
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false; for (Piece p:pw) {
if (this.getCell().getRow()+i == c.getRow()) break ; if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
} }
} }
if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() > c.getRow()) { else {
for (int i=-1 ; i>-8 ; --i) { for (Piece p:pb) {
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ; if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
if (this.getCell().getRow()+i == c.getRow()) break ;
} }
} }
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() == c.getRow()) { if ( (
for (int i=1 ; i<8 ; ++i) { (this.getCell().getCol() == c.getCol()) || (this.getCell().getRow() == c.getRow())
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ; ) && (c.isEmpty() || (!c.isEmpty() && sw==1))
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false; ) {
if (this.getCell().getCol()+i == c.getCol()) break ; if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() < c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
if (this.getCell().getRow()+i == c.getRow()) break ;
}
} }
} if (this.getCell().getCol() == c.getCol() && this.getCell().getRow() > c.getRow()) {
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() == c.getRow()) { for (int i=-1 ; i>-8 ; --i) {
for (int i=-1 ; i>-8 ; --i) { if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ; if (!board[this.getCell().getRow()+i][c.getCol()].isEmpty()) return false;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false; if (this.getCell().getRow()+i == c.getRow()) break ;
if (this.getCell().getCol()+i == c.getCol()) break ; }
}
if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=1 ; i<8 ; ++i) {
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
}
if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() == c.getRow()) {
for (int i=-1 ; i>-8 ; --i) {
if (this.getCell().getCol()+i == c.getCol() && sw==1) break ;
if (!board[c.getRow()][this.getCell().getCol()+i].isEmpty()) return false;
if (this.getCell().getCol()+i == c.getCol()) break ;
}
} }
return true ;
} }
return true ; else return false ;
} }
else return false ; }
}
}
\ No newline at end of file
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