Commit 7bc51122 authored by kimia's avatar kimia

GUI

parent 633367f9
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/src.iml" filepath="$PROJECT_DIR$/.idea/src.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
This diff is collapsed.
public class Bishop extends Piece {
public Bishop(Cell cell, Color color) {
super(cell, color) ;
public Bishop(Cell cell, PieceColor pieceColor) {
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.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
......@@ -7,6 +7,12 @@ class Cell {
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() {
return row ;
......@@ -34,6 +40,6 @@ class Cell {
}
enum Color {
enum PieceColor {
BLACK, WHITE
}
This diff is collapsed.
import javax.swing.*;
import java.awt.*;
public class ChessGameGUI extends JFrame {
public static void main(String args[]){
new ChessGameGUI();
}
public ChessGameGUI(){
super();
JPanel parent = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1300,610);
parent.setSize(1300,610);
setResizable(false);
parent.setLayout(new BoxLayout(parent,BoxLayout.X_AXIS));
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(8,4));
p1.setSize(500,600);
JPanel gameBoard = new ChessBoardGUI(p1);
gameBoard.setSize(800,600);
parent.add(p1);
parent.add(gameBoard);
add(parent);
setVisible(true);
}
}
public class King extends Piece {
public King(Cell cell, Color color) {
super(cell, color) ;
public King(Cell cell, PieceColor pieceColor) {
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.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
......@@ -8,15 +8,15 @@
public class Knight extends Piece {
public Knight(Cell cell, Color color) {
super(cell, color) ;
public Knight(Cell cell, PieceColor pieceColor) {
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.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
import java.util.Scanner;
public class Main{
/**
/**
*prints board and control the moves of the pieces
*@author kimiadorani
*@version 1.0
*@since 2019-5-7
*/
private static char boardChar[][] = new char [8][8] ;
private static char boardChar[][] = new char [8][8] ;
private static void printBoard() {
for (int i=1 ; i<9 ; ++i) {
......@@ -82,8 +83,8 @@ private static char boardChar[][] = new char [8][8] ;
public static void main(String[] args) {
Scanner in = new Scanner(System.in) ;
Player white = new Player(Color.WHITE) ;
Player black = new Player(Color.BLACK) ;
Player white = new Player(PieceColor.WHITE) ;
Player black = new Player(PieceColor.BLACK) ;
Piece pw[] = white.getPieces() ;
Piece pb[] = black.getPieces() ;
Cell board[][] = new Cell[8][8] ;
......@@ -141,7 +142,7 @@ private static char boardChar[][] = new char [8][8] ;
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)) {
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb)) {
wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
......@@ -175,7 +176,7 @@ private static char boardChar[][] = new char [8][8] ;
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) ) {
if (p.isValidMove(board[s.charAt(3)-'a'][s.charAt(4)-'1'], board, pw, pb) ) {
wORb++ ;
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
......@@ -212,4 +213,3 @@ private static char boardChar[][] = new char [8][8] ;
}
}
\ No newline at end of file
public class Pawn extends Piece{
private boolean once, end ;
public Pawn(Cell cell, Color color) {
super(cell, color) ;
public Pawn(Cell cell, PieceColor pieceColor) {
super(cell, pieceColor) ;
this.once = true ;
this.end = false ;
}
......@@ -18,11 +18,11 @@ public class Pawn extends Piece{
@Override
public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
if (super.isDeleted()) return false ;
if (super.getColor()==Color.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getColor()==Color.WHITE && super.getCell().getRow()==7) end = true ;
if (super.getPieceColor()== PieceColor.BLACK && super.getCell().getRow()==0) end = true ;
if (super.getPieceColor()== PieceColor.WHITE && super.getCell().getRow()==7) end = true ;
int sw=0 ;
if (super.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......@@ -56,12 +56,12 @@ public class Pawn extends Piece{
else {
if(
(this.getCell().getCol() == c.getCol() &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE)) )
((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()== PieceColor.BLACK) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()== PieceColor.WHITE)) )
||
(Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
((this.getCell().getRow()-c.getRow()==1 && super.getColor()==Color.BLACK && sw==1) ||
(this.getCell().getRow()-c.getRow()==-1 && super.getColor()==Color.WHITE && sw==1)) )
((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 ;
}
......
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
public abstract class Piece {
private Cell cell ;
private Color color ;
private PieceColor pieceColor;
private boolean deleted ;
public Piece (Cell cell, Color color) {
public Piece (Cell cell, PieceColor pieceColor) {
this.cell = cell ;
this.cell.setEmpty(false) ;
this.color = color ;
this.pieceColor = pieceColor;
this.deleted = false ;
}
......@@ -16,9 +20,62 @@ public abstract class Piece {
public Cell getCell() {
return cell;
}
public Image getPieceImage(){
Image image = null;
try {
if (this instanceof Bishop) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Bishop.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Bishop.png"));
}
} else if (this instanceof King) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-King.png"));
} else {
image = ImageIO.read(new File("./pics/Black-King.png"));
}
} else if (this instanceof Knight) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Knight.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Knight.png"));
}
} else if (this instanceof Pawn) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Pawn.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Pawn.png"));
}
} else if (this instanceof Queen) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Queen.png"));
public Color getColor() {
return color;
} else {
image = ImageIO.read(new File("./pics/Black-Queen.png"));
}
} else if (this instanceof Rook) {
if (getPieceColor().equals(PieceColor.WHITE)) {
image = ImageIO.read(new File("./pics/White-Rook.png"));
} else {
image = ImageIO.read(new File("./pics/Black-Rook.png"));
}
}
}catch (Exception exception){
}
return image;
}
public PieceColor getPieceColor() {
return pieceColor;
}
public boolean isDeleted() {
......@@ -29,8 +86,8 @@ public abstract class Piece {
this.cell = cell;
}
public void setColor(Color color) {
this.color = color;
public void setPieceColor(PieceColor pieceColor) {
this.pieceColor = pieceColor;
}
public void setDeleted(boolean deleted) {
......
public class Player {
private Piece[] pieces = new Piece[16] ;
private Color color ;
private PieceColor pieceColor;
public Piece[] getPieces() {
return pieces ;
}
public Color getColor() {
return color ;
public PieceColor getPieceColor() {
return pieceColor;
}
public void setPieces(Piece[] pieces) {
this.pieces = pieces ;
}
public void setColor(Color color) {
this.color = color ;
public void setPieceColor(PieceColor pieceColor) {
this.pieceColor = pieceColor;
}
public Player(Color color) {
this.color = color ;
if (color == Color.WHITE) {
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), color) ;
}
pieces[8] = new Bishop(new Cell(0,2), color) ;
pieces[9] = new Bishop(new Cell(0,5), color) ;
pieces[10] = new Knight(new Cell(0,1), color) ;
pieces[11] = new Knight(new Cell(0,6), color) ;
pieces[12] = new Rook(new Cell(0,0), color) ;
pieces[13] = new Rook(new Cell(0,7), color) ;
pieces[14] = new King(new Cell(0,4), color) ;
pieces[15] = new Queen(new Cell(0,3), color) ;
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), color) ;
}
pieces[8] = new Bishop(new Cell(7,2), color) ;
pieces[9] = new Bishop(new Cell(7,5), color) ;
pieces[10] = new Knight(new Cell(7,1), color) ;
pieces[11] = new Knight(new Cell(7,6), color) ;
pieces[12] = new Rook(new Cell(7,0), color) ;
pieces[13] = new Rook(new Cell(7,7), color) ;
pieces[14] = new King(new Cell(7,4), color) ;
pieces[15] = new Queen(new Cell(7,3), color) ;
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
......@@ -2,15 +2,15 @@
public class Queen extends Piece {
public Queen(Cell cell, Color color) {
super(cell, color) ;
public Queen(Cell cell, PieceColor pieceColor) {
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.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
......@@ -7,15 +7,15 @@
public class Rook extends Piece {
public Rook(Cell cell, Color color) {
super(cell, color) ;
public Rook(Cell cell, PieceColor pieceColor) {
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.getColor() == Color.BLACK) {
if (super.getPieceColor() == PieceColor.BLACK) {
for (Piece p:pw) {
if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
}
......
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