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
*@version 1.0
*@since 2019-5-7
......@@ -12,6 +13,15 @@ public class Bishop extends Piece {
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
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
......
/**
* Cell class show's the cells in a board for pieces.
*@author kimiadorani
*@version 1.0
*@since 2019-5-7
* This class shows the cell's of the board
*/
class Cell {
private int row, col ;
private boolean empty ;
public Cell (int row, int col) {
this.row = row ;
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;
}
private int row, col;
private boolean empty;
public int getRow() {
return row ;
}
public Cell(int row, int col) {
this.row = row;
this.col = col;
this.empty = true;
}
public int getCol() {
return col ;
}
public Cell(Cell c) {
if (c == null) return;
this.row = c.getRow();
this.col = c.col;
this.empty = c.empty;
}
public boolean isEmpty() {
return empty ;
}
public Cell(int row, int col, boolean bool) {
this.row = row;
this.col = col;
this.empty = bool;
}
public void setRow (int row) {
this.row = row ;
}
public int getRow() {
return row;
}
public void setCol (int col) {
this.col = col ;
}
public int getCol() {
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 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
*@version 1.0
*@since 2019-5-7
*/
public class ChessGameGUI extends JFrame {
private String URL ="";
private PrintWriter out;
private Scanner input;
private ChessBoardGUI gameBoard;
public static void main(String args[]){
new ChessGameGUI();
}
public ChessGameGUI(){
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();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1300,610);
......@@ -31,7 +52,7 @@ public class ChessGameGUI extends JFrame {
blackRemovePanel.setSize(200,600);
//-----------------------------------------
JPanel gameBoard = new ChessBoardGUI(whiteRemovePanel,blackRemovePanel,turnPanel,this);
gameBoard = new ChessBoardGUI(whiteRemovePanel,blackRemovePanel,turnPanel,this,out);
gameBoard.setSize(800,600);
parent.add(whiteRemovePanel);
parent.add(turnPanel);
......@@ -40,4 +61,72 @@ public class ChessGameGUI extends JFrame {
add(parent);
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
*@version 1.0
*@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
*@version 1.0
*@since 2019-5-7
......@@ -44,20 +45,19 @@ public class Pawn extends Piece{
}
if (once) {
//once = false ;
//System.out.println("row: " + this.getCell().getRow()) ;
//System.out.println("col: " + this.getCell().getCol()) ;
if ((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2)
&& c.isEmpty()){
if(getCell().getRow() < c.getRow() && getPieceColor()==PieceColor.WHITE) {
return true;
}else if(getCell().getRow() > c.getRow() && getPieceColor()==PieceColor.BLACK){
return true;
}else {
return false;
}
}
if ( ((this.getCell().getCol() == c.getCol()) &&
(Math.abs(this.getCell().getRow()-c.getRow())==1 ||
( Math.abs(this.getCell().getRow()-c.getRow())==2 &&
((super.getPieceColor()==PieceColor.WHITE && board[c.getRow()-1][c.getCol()].isEmpty()) ||
(super.getPieceColor()==PieceColor.BLACK && board[c.getRow()+1][c.getCol()].isEmpty()) ) )
)
&& c.isEmpty())
||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()==PieceColor.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()==PieceColor.WHITE && sw==1)) )
)
return true ;
else return false ;
}
else {
......
......@@ -9,6 +9,7 @@ import java.io.File;
*/
public abstract class Piece {
private Cell cell ;
private PieceColor pieceColor;
private boolean deleted ;
......
......@@ -5,11 +5,11 @@
*@since 2019-5-7
*/
public class Player {
private Piece[] pieces = new Piece[16] ;
private Piece[] pieces = new Piece[16];
private PieceColor pieceColor;
public Piece[] getPieces() {
return pieces ;
return pieces;
}
public PieceColor getPieceColor() {
......@@ -17,41 +17,40 @@ public class Player {
}
public void setPieces(Piece[] pieces) {
this.pieces = pieces ;
this.pieces = pieces;
}
public void setPieceColor(PieceColor pieceColor) {
this.pieceColor = pieceColor;
}
public Player(PieceColor pieceColor) {
public Player(PieceColor pieceColor) {
this.pieceColor = pieceColor;
if (pieceColor == PieceColor.WHITE) {
for(int i=0 ; i<8 ; i++) {
pieces[i] = new Pawn(new Cell(1,i), pieceColor) ;
for (int i = 0; i < 8; i++) {
pieces[i] = new Pawn(new Cell(1, i), pieceColor);
}
pieces[8] = new Bishop(new Cell(0,2), pieceColor) ;
pieces[9] = new Bishop(new Cell(0,5), pieceColor) ;
pieces[10] = new Knight(new Cell(0,1), pieceColor) ;
pieces[11] = new Knight(new Cell(0,6), pieceColor) ;
pieces[12] = new Rook(new Cell(0,0), pieceColor) ;
pieces[13] = new Rook(new Cell(0,7), pieceColor) ;
pieces[14] = new King(new Cell(0,4), pieceColor) ;
pieces[15] = new Queen(new Cell(0,3), pieceColor) ;
pieces[8] = new Bishop(new Cell(0, 2), pieceColor);
pieces[9] = new Bishop(new Cell(0, 5), pieceColor);
pieces[10] = new Knight(new Cell(0, 1), pieceColor);
pieces[11] = new Knight(new Cell(0, 6), pieceColor);
pieces[12] = new Rook(new Cell(0, 0), pieceColor);
pieces[13] = new Rook(new Cell(0, 7), pieceColor);
pieces[14] = new King(new Cell(0, 4), pieceColor);
pieces[15] = new Queen(new Cell(0, 3), pieceColor);
}
else {
for(int i=0 ; i<8 ; i++){
pieces[i] = new Pawn(new Cell(6,i), pieceColor) ;
} else {
for (int i = 0; i < 8; i++) {
pieces[i] = new Pawn(new Cell(6, i), pieceColor);
}
pieces[8] = new Bishop(new Cell(7,2), pieceColor) ;
pieces[9] = new Bishop(new Cell(7,5), pieceColor) ;
pieces[10] = new Knight(new Cell(7,1), pieceColor) ;
pieces[11] = new Knight(new Cell(7,6), pieceColor) ;
pieces[12] = new Rook(new Cell(7,0), pieceColor) ;
pieces[13] = new Rook(new Cell(7,7), pieceColor) ;
pieces[14] = new King(new Cell(7,4), pieceColor) ;
pieces[15] = new Queen(new Cell(7,3), pieceColor) ;
pieces[8] = new Bishop(new Cell(7, 2), pieceColor);
pieces[9] = new Bishop(new Cell(7, 5), pieceColor);
pieces[10] = new Knight(new Cell(7, 1), pieceColor);
pieces[11] = new Knight(new Cell(7, 6), pieceColor);
pieces[12] = new Rook(new Cell(7, 0), pieceColor);
pieces[13] = new Rook(new Cell(7, 7), pieceColor);
pieces[14] = new King(new Cell(7, 4), 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
*@version 1.0
*@since 2019-5-7
......@@ -11,6 +13,18 @@ public class Queen extends Piece {
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
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
......
......@@ -11,55 +11,70 @@ public class Rook extends Piece {
super(cell, pieceColor) ;
}
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
}
else {
for (Piece p:pb) {
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())
) && (c.isEmpty() || (!c.isEmpty() && sw==1))
) {
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 ;
/**
* isValidMove is the method which has been overrided from the piece class
* here we check if the move rook wants to make is possible or not!
* @param c the cel rook is in it
* @param board
* @param pw the white player piece
* @param pb the black player piece
* @return true if the moves were possible and false if they were not
*/
/**
* shows the moves of the rook , it can move straight
* @author kimiadorani
* @version 1.0
*/
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
int sw=0 ;
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
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()) {
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 ;
else {
for (Piece p:pb) {
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()) {
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())
) && (c.isEmpty() || (!c.isEmpty() && sw==1))
) {
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()) {
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().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()) {
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