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 ;
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;
public Cell(Cell c) {
if (c == null) return;
this.row = c.getRow();
this.col = c.col;
this.empty = c.empty;
}
public Cell(int row, int col, boolean bool) {
this.row = row;
this.col = col;
this.empty = bool;
}
public int getRow() {
return row ;
return row;
}
public int getCol() {
return col ;
return col;
}
public boolean isEmpty() {
return empty ;
return empty;
}
public void setRow (int row) {
this.row = row ;
public void setRow(int row) {
this.row = row;
}
public void setCol (int col) {
this.col = col ;
public void setCol(int col) {
this.col = col;
}
public void setEmpty(boolean empty) {
this.empty = empty ;
this.empty = empty;
}
}
}
enum PieceColor {
enum PieceColor {
BLACK, WHITE
}
......@@ -2,6 +2,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* ChessBoardGUI is a class that show's the graphical form of the game.
......@@ -9,7 +10,12 @@ import java.util.ArrayList;
*@version 1.0
*@since 2019-5-7
*/
public class ChessBoardGUI extends JPanel {
static boolean isServer = false ;
private PrintWriter out;
public JButton[][] button = new JButton[8][8];
private JPanel whiteRemovedPiecePanel;
private JPanel blackRemovedPiecePanel;
......@@ -27,13 +33,15 @@ public class ChessBoardGUI extends JPanel {
private boolean[][] moveBoard = null;
public ArrayList<Piece> deletedList = new ArrayList<>();
private JFrame parentPanel;
public ChessBoardGUI(JPanel whiteRemovedPiecePanel,JPanel blackRemovedPiecePanel,JPanel turnPanel,JFrame parentPanel){
private boolean isFreezGame = false;
public ChessBoardGUI(JPanel whiteRemovedPiecePanel,JPanel blackRemovedPiecePanel,JPanel turnPanel,JFrame parentPanel,PrintWriter out){
super();
this.whiteRemovedPiecePanel= whiteRemovedPiecePanel;
this.blackRemovedPiecePanel = blackRemovedPiecePanel;
this.turnPanel = turnPanel;
this.parentPanel = parentPanel;
parentPanel.setTitle("White Turn...");
this.out = out;
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600);
initlizeGame();
......@@ -51,23 +59,16 @@ public class ChessBoardGUI extends JPanel {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println(finalJ +","+ finalI);
if(isFreezGame) return;
if(isServer && wORb%2==1) return;
if(!isServer && wORb%2==0) return;
source = new Cell(dest);
dest = new Cell(finalI,finalJ);
if(moveBoard!=null){
if(moveBoard[finalI][finalJ] && source!=null && dest!=null){
movePiece(source,dest);
// System.out.println("Moves");
buttonUpdate();
updateDeletedPanel();
//----------------
int condition = checkCondition(board,pw,pb);
if(condition==2){
JOptionPane.showMessageDialog(turnPanel,"Condition White");
}
else if(condition==1){
JOptionPane.showMessageDialog(turnPanel,"Condition Black");
}
sendMovmentToOtherApp(source,dest);
updatesAfterMove();
//----------------
}
}
......@@ -76,15 +77,15 @@ public class ChessBoardGUI extends JPanel {
updateGUIBoard(moveBoard);
}
if(wORb%2==0){
turnLabel.setText("WHITE TURN!");
turnLabel.setText("White Turn...");
}else {
turnLabel.setText("BLACK TURN");
turnLabel.setText("Black Turn...");
}
}
});
add(button[i][j]);
if((i+j)%2==0)
button[i][j].setBackground(Color.LIGHT_GRAY);
button[i][j].setBackground(Color.CYAN);
else {
button[i][j].setBackground(Color.WHITE);
}
......@@ -92,15 +93,32 @@ public class ChessBoardGUI extends JPanel {
}
// setVisible(true);
}
public void updatesAfterMove(){
buttonRepaint();
updateDeletedPanel();
//----------------
int condition = checkmateCondition(board,pw,pb);
if(condition==2){
JOptionPane.showMessageDialog(turnPanel,"White Win");
System.out.println("White Wiinnn");
isFreezGame=true;
}
else if(condition==1){
JOptionPane.showMessageDialog(turnPanel,"Black Win");
System.out.println("Black Win");
isFreezGame=true;
}
}
private void inilizeRemovedPiecePanel(){
for (int i = 0 ; i < 8 ;i++){
for (int j = 0 ; j < 2 ;j++) {
removedPieces[i][j] = new JButton();
removedPieces[i][j].setBackground(Color.PINK);
removedPieces[i][j].setBackground(Color.YELLOW);
// added.setMinimumSize(new Dimension(50,50));
whiteRemovedPiecePanel.add( removedPieces[i][j]);
removedPieces[i][3-j] = new JButton();
removedPieces[i][3-j].setBackground(Color.PINK);
removedPieces[i][3-j].setBackground(Color.YELLOW);
blackRemovedPiecePanel.add( removedPieces[i][3-j]);
}
}
......@@ -124,7 +142,19 @@ public class ChessBoardGUI extends JPanel {
}
}
}
private void buttonUpdate(){
/**
* it sends the Coordinates of the moves of the pieces in form of 4 numbers
* to be changed in the other
* @param source the coordinates that we start from
* @param dest the coordinates that is destination
*/
private void sendMovmentToOtherApp(Cell source, Cell dest){
out.println(source.getRow()+"\t"+source.getCol()+"\t"+dest.getRow()+"\t"+dest.getCol());
out.flush();
}
private void buttonRepaint(){
Piece[][] pieceBoard = getUpdatedBoard();
for (int i = 0 ;i<8 ;i++){
for (int j = 0 ; j < 8 ;j++){
......@@ -134,7 +164,7 @@ public class ChessBoardGUI extends JPanel {
button[i][j].setIcon(new ImageIcon());
}
if((i+j)%2==0)
button[i][j].setBackground(Color.LIGHT_GRAY);
button[i][j].setBackground(Color.CYAN);
else {
button[i][j].setBackground(Color.WHITE);
}
......@@ -150,7 +180,7 @@ public class ChessBoardGUI extends JPanel {
// System.out.println("Reach Thereeee...");
} else {
if((i+j)%2==0)
button[i][j].setBackground(Color.LIGHT_GRAY);
button[i][j].setBackground(Color.CYAN);
else {
button[i][j].setBackground(Color.WHITE);
}
......@@ -158,6 +188,85 @@ public class ChessBoardGUI extends JPanel {
}
}
}
private int checkmateCondition(Cell board[][], Piece pw[], Piece pb[]) {
if (checkCondition(board, pw, pb, 1) == 1) {
for (Piece p:pw) {
for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) {
if (p.getCell().getRow()==i && p.getCell().getCol()==j) continue ;
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
if (p.isValidMove(board[i][j], board, pw, pb)) {
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 1) {
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(false) ;
}
return 0 ;
}
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(false) ;
}
}
}
}
}
System.out.println("Black player win!") ;
return 1;
}
else if (checkCondition(board, pw, pb, 1) == 2) {
for (Piece p:pb) {
for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) {
if (p.getCell().getRow()==i && p.getCell().getCol()==j) continue ;
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
if (p.isValidMove(board[i][j], board, pw, pb)) {
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 2) {
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(false) ;
}
return 0 ;
}
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(false) ;
}
}
}
}
}
System.out.println("White player win!") ;
return 2;
}
else return 0 ;
}
public void initlizeGame(){
white = new Player(PieceColor.WHITE) ;
black = new Player(PieceColor.BLACK) ;
......@@ -215,7 +324,30 @@ public class ChessBoardGUI extends JPanel {
for (Piece p : pw) {
if (!p.isDeleted() && p.getCell().getRow() == source.getRow() &&
p.getCell().getCol() == source.getCol()) {
if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb)) {
int sw ;
if (checkCondition(board, pw, pb, 1)==1) {
sw=1 ;
int i=dest.getRow();
int j=dest.getCol();
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 1) sw=0 ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(false) ;
}
}
else sw=0 ;
if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb)&& sw==0) {
dest = board[dest.getRow()][dest.getCol()];
wORb++;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true);
......@@ -239,20 +371,43 @@ public class ChessBoardGUI extends JPanel {
}else {
for (Piece p:pb) {
if (!p.isDeleted() && p.getCell().getRow()==source.getRow() &&
p.getCell().getCol() == source.getCol())
if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb) ) {
dest = board[dest.getRow()][dest.getCol()];
wORb++ ;
p.getCell().getCol() == source.getCol()) {
int sw ;
if (checkCondition(board, pw, pb, 1)==2) {
sw=1 ;
int i=dest.getRow();
int j=dest.getCol();
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
p.setCell(dest) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 2) sw=0 ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(false) ;
}
}
else sw=0 ;
if (p.isValidMove(board[dest.getRow()][dest.getCol()], board, pw, pb) && sw==0) {
dest = board[dest.getRow()][dest.getCol()];
wORb++;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true);
p.setCell(dest);
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false);
if (p instanceof Pawn) {
Pawn myPawn = (Pawn) p;
myPawn.setOnce(false) ;
myPawn.setOnce(false);
}
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==dest.getRow()
&& whitePiece.getCell().getCol()==dest.getCol()) {
for (Piece whitePiece : pw) {
if (whitePiece.getCell().getRow() == dest.getRow()
&& whitePiece.getCell().getCol() == dest.getCol()) {
whitePiece.setDeleted(true);
deletedList.add(whitePiece);
}
......@@ -261,6 +416,7 @@ public class ChessBoardGUI extends JPanel {
}
}
}
}
//--------------
return false;
......@@ -279,36 +435,37 @@ public class ChessBoardGUI extends JPanel {
}
return pieceboard;
}
private int checkCondition(Cell board[][], Piece pw[], Piece pb[]) {
private int checkCondition(Cell board[][], Piece pw[], Piece pb[], int flag) {
for (Piece p:pw) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pb) {
if (p1.isDeleted()) continue ;
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1){
System.out.println("Check condition for White player!!");
return 1;
if(sw1==1) {
if (flag==0) System.out.println("Check condition for White player!!") ;
return 1 ;
}
}
}
for (Piece p:pb) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pw) {
if (p1.isDeleted()) continue ;
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1) {
System.out.println("Check condition for Balck player!!") ;
return 2;
if (flag==0) System.out.println("Check condition for Black player!!") ;
return 2 ;
}
}
}
return 0;
return 0 ;
}
}
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
......
......@@ -7,6 +7,7 @@ import java.util.Scanner;
*/
public class Main{
private static char boardChar[][] = new char [8][8] ;
private static void printBoard() {
......@@ -55,29 +56,116 @@ public class Main{
}
}
private static void checkCondition(Cell board[][], Piece pw[], Piece pb[]) {
private static int checkCondition(Cell board[][], Piece pw[], Piece pb[], int flag) {
for (Piece p:pw) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pb) {
if (p1.isDeleted()) continue ;
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1) System.out.println("Check condition for White player!!") ;
if(sw1==1) {
if (flag==0) System.out.println("Check condition for White player!!") ;
return 1 ;
}
}
}
for (Piece p:pb) {
if (p instanceof King) {
int sw1=0 ;
for (Piece p1:pw) {
if (p1.isDeleted()) continue ;
if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ;
}
}
if(sw1==1) System.out.println("Check condition for Balck player!!") ;
if(sw1==1) {
if (flag==0) System.out.println("Check condition for Black player!!") ;
return 2 ;
}
}
}
return 0 ;
}
private static int checkmateCondition(Cell board[][], Piece pw[], Piece pb[]) {
if (checkCondition(board, pw, pb, 1) == 1) {
for (Piece p:pw) {
for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) {
if (p.getCell().getRow()==i && p.getCell().getCol()==j) continue ;
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
if (p.isValidMove(board[i][j], board, pw, pb)) {
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 1) {
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(false) ;
}
return 0 ;
}
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(false) ;
}
}
}
}
}
System.out.println("Black player win!") ;
}
else if (checkCondition(board, pw, pb, 1) == 2) {
for (Piece p:pb) {
for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) {
if (p.getCell().getRow()==i && p.getCell().getCol()==j) continue ;
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
if (p.isValidMove(board[i][j], board, pw, pb)) {
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 2) {
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(false) ;
}
return 0 ;
}
board[tmp.getRow()][tmp.getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(false) ;
}
}
}
}
}
System.out.println("White player win!") ;
}
else return 0 ;
return 1 ;
}
public static void main(String[] args) {
......@@ -140,8 +228,32 @@ public class Main{
if (wORb%2==0) {
for (Piece p:pw) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') {
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb)) {
p.getCell().getCol()==s.charAt(1)-'1') {
int sw ;
if (checkCondition(board, pw, pb, 1)==1) {
sw=1 ;
int i=s.charAt(3)-'a' ;
int j=s.charAt(4)-'1' ;
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 1) sw=0 ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece blackPiece:pb) {
if (blackPiece.getCell().getRow()==i && blackPiece.getCell().getCol()==j)
blackPiece.setDeleted(false) ;
}
}
else sw=0 ;
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb) && sw==0) {
wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
......@@ -166,16 +278,41 @@ public class Main{
}
if (wORb%2==0) System.out.println("please enter a valid move!") ;
else {
checkCondition(board, pw, pb) ;
checkCondition(board, pw, pb, 0) ;
updateBoard(pw, pb) ;
printBoard() ;
if (checkmateCondition(board, pw, pb)==1) System.exit(0) ;
}
}
else {
for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0')
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb) ) {
p.getCell().getCol()==s.charAt(1)-'1') {
int sw ;
if (checkCondition(board, pw, pb, 1)==2) {
sw=1 ;
int i=s.charAt(3)-'a' ;
int j=s.charAt(4)-'1' ;
Cell tmp = new Cell(p.getCell().getRow(), p.getCell().getCol(), false) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
board[i][j].setEmpty(false) ;
p.setCell(board[i][j]) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(true) ;
}
if (checkCondition(board, pw, pb, 1) != 2) sw=0 ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
board[i][j].setEmpty(true) ;
p.setCell(tmp) ;
for (Piece whitePiece:pw) {
if (whitePiece.getCell().getRow()==i && whitePiece.getCell().getCol()==j)
whitePiece.setDeleted(false) ;
}
}
else sw=0 ;
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb) && sw==0) {
wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
......@@ -197,11 +334,13 @@ public class Main{
}
}
}
}
if (wORb%2==1) System.out.println("please enter a valid move!") ;
else {
checkCondition(board, pw, pb) ;
checkCondition(board, pw, pb, 0) ;
updateBoard(pw, pb) ;
printBoard() ;
if (checkmateCondition(board, pw, pb)==1) System.exit(0) ;
}
}
}
......@@ -213,3 +352,5 @@ public class Main{
}
/**
* 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,7 +17,7 @@ public class Player {
}
public void setPieces(Piece[] pieces) {
this.pieces = pieces ;
this.pieces = pieces;
}
public void setPieceColor(PieceColor pieceColor) {
......@@ -27,31 +27,30 @@ public class Player {
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) ;
}
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) ;
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);
}
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) ;
} 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);
}
}
}
\ 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,6 +11,22 @@ public class Rook extends Piece {
super(cell, pieceColor) ;
}
/**
* 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 ;
......@@ -61,5 +77,4 @@ public class Rook extends Piece {
}
else 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