Commit 86b34bce authored by 9611046's avatar 9611046

first moves and icon of turn completed

parent db33b6b9
...@@ -3,6 +3,7 @@ package game; ...@@ -3,6 +3,7 @@ package game;
import pieces.*; import pieces.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
...@@ -23,9 +24,14 @@ public class Main extends JFrame implements MouseListener{ ...@@ -23,9 +24,14 @@ public class Main extends JFrame implements MouseListener{
private static Main Mainboard; private static Main Mainboard;
// private JButton[][] chessBoardSquares ; // private JButton[][] chessBoardSquares ;
private Square [][] chessBoardSquares; private Square [][] chessBoardSquares;
private ArrayList<JButton> outWhitePieces;
private ArrayList<JButton> outBlackPieces;
private boolean select=false; private boolean select=false;
private Square lastSquare; private Square lastSquare;
private Piece lastPiece; private Piece lastPiece;
private ArrayList<Square> possibleMoves ;
private String whosTurn = "w";
private boolean isMoved=false;
private JFrame myFrame; private JFrame myFrame;
private static Rook wr01,wr02,br01,br02; private static Rook wr01,wr02,br01,br02;
private static Knight wk01,wk02,bk01,bk02; private static Knight wk01,wk02,bk01,bk02;
...@@ -33,8 +39,9 @@ public class Main extends JFrame implements MouseListener{ ...@@ -33,8 +39,9 @@ public class Main extends JFrame implements MouseListener{
private static Pawn wp[],bp[]; private static Pawn wp[],bp[];
private static Queen wq,bq; private static Queen wq,bq;
private static King wk,bk; private static King wk,bk;
private JButton turn;
public static void main(String[] args) { public static void main(String[] args) {
// Mainboard.setVisible(true); // Mainboard.setVisible(true);
wr01=new Rook("WR01", "src\\game\\White_Rook.png","w"); wr01=new Rook("WR01", "src\\game\\White_Rook.png","w");
...@@ -63,32 +70,49 @@ public static void main(String[] args) { ...@@ -63,32 +70,49 @@ public static void main(String[] args) {
} }
Mainboard = new Main(); Mainboard = new Main();
} }
private void changeTurn(){
if(whosTurn.equals("w")) whosTurn="b";
else whosTurn="w";
}
private Main() { private Main() {
chessBoardSquares = new Square[8][8]; chessBoardSquares = new Square[8][8];
outBlackPieces = new ArrayList<>(16);
outWhitePieces = new ArrayList<>(16);
myFrame = new JFrame("Chess"); myFrame = new JFrame("Chess");
myFrame.setLayout(new BorderLayout(100, 100)); myFrame.setLayout(new BorderLayout(100, 100));
JPanel centerpanel = new JPanel(); JPanel centerpanel = new JPanel();
JPanel westPanel = new JPanel(); JPanel westPanel = new JPanel();
westPanel.setBackground(Color.BLACK); westPanel.setBackground(Color.BLACK);
westPanel.setPreferredSize(new Dimension(400, 30)); westPanel.setPreferredSize(new Dimension(400, 200));
westPanel.setLayout(new BorderLayout(3, 1)); westPanel.setLayout(new BorderLayout(3, 1));
JPanel northOfWest = new JPanel(); JPanel northOfWest = new JPanel();
northOfWest.setPreferredSize(new Dimension(400, 100)); northOfWest.setPreferredSize(new Dimension(400, 300));
northOfWest.setBackground(new Color(51, 0, 0)); northOfWest.setBackground(new Color(51, 0, 0));
Border emptyBorder = BorderFactory.createEmptyBorder();
JPanel centerOfWest = new JPanel(); JPanel centerOfWest = new JPanel();
centerOfWest.setPreferredSize(new Dimension(400, 100)); centerOfWest.setPreferredSize(new Dimension(400, 100));
centerOfWest.setBackground(new Color(153, 102, 0)); centerOfWest.setBackground(new Color(153, 102, 0));
turn = new JButton("Let's start! ");
turn.setBorder(emptyBorder);
turn.setBackground(new Color(153, 102, 0));
turn.setFont(new Font("Arial", Font.PLAIN, 40));
turn.setForeground(Color.black);
turn.setPreferredSize(new Dimension(300,50));
centerOfWest.add(turn);
JPanel southOfWest = new JPanel(); JPanel southOfWest = new JPanel();
southOfWest.setPreferredSize(new Dimension(400, 100)); southOfWest.setPreferredSize(new Dimension(400, 300));
southOfWest.setBackground(new Color(51, 0, 0)); southOfWest.setBackground(new Color(51, 0, 0));
westPanel.add(northOfWest, BorderLayout.NORTH); westPanel.add(northOfWest, BorderLayout.NORTH);
westPanel.add(centerOfWest, BorderLayout.CENTER); westPanel.add(centerOfWest, BorderLayout.CENTER);
westPanel.add(southOfWest, BorderLayout.SOUTH); westPanel.add(southOfWest, BorderLayout.SOUTH);
...@@ -118,6 +142,21 @@ private Main() { ...@@ -118,6 +142,21 @@ private Main() {
} }
} }
} }
for (int i = 0; i <16 ; i++) {
outBlackPieces.add(new JButton());
outWhitePieces.add(new JButton());
}
for(int i=0;i<16;i++){
//outWhitePieces.get(i)=new JButton();
outWhitePieces.get(i).setBackground(new Color(51, 0, 0));
// outWhitePieces.get(i).setBorder(emptyBorder);
northOfWest.add(outWhitePieces.get(i));
// outBlackPieces[i]=new JButton();
outBlackPieces.get(i).setBackground(new Color(51, 0, 0));
// outBlackPieces[i].setBorder(emptyBorder);
southOfWest.add(outBlackPieces.get(i));
}
// Piece piece; // Piece piece;
...@@ -232,6 +271,7 @@ private Main() { ...@@ -232,6 +271,7 @@ private Main() {
// //
// } // }
myFrame.setVisible(true); myFrame.setVisible(true);
myFrame.setResizable(true); myFrame.setResizable(true);
myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH); myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
...@@ -241,8 +281,7 @@ private Main() { ...@@ -241,8 +281,7 @@ private Main() {
if (!select) { if (!select) {
((Square) (e.getSource())).selectSquare(); ((Square) (e.getSource())).selectSquare();
} } else {
else {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
chessBoardSquares[i][j].cancleSelection(); chessBoardSquares[i][j].cancleSelection();
...@@ -250,42 +289,96 @@ private Main() { ...@@ -250,42 +289,96 @@ private Main() {
} }
((Square) (e.getSource())).selectSquare(); ((Square) (e.getSource())).selectSquare();
} }
select=true; select = true;
// Square square1 = null;
// Piece piece1 = null;
if(((Square) (e.getSource())).getPiece()!=null) {
lastSquare = ((Square) (e.getSource()));
lastPiece = ((Square) (e.getSource())).getPiece();
}
//selecting a square and showing it's possible squares
if (((Square) (e.getSource())).getPiece() != null && ((Square) (e.getSource())).getPiece().getPieceColor().equals(whosTurn) ) {
if(((Square) (e.getSource())).getPiece()!=null){ lastSquare = ((Square) (e.getSource()));
lastPiece = ((Square) (e.getSource())).getPiece();
ArrayList<Square> possibleMoves=new ArrayList<>();
for(int i=0;i<8;i++){ for (int i = 0; i < 8; i++) {
for(int j=0;j<8;j++){ for (int j = 0; j < 8; j++) {
// //
if(chessBoardSquares[i][j].isSelected()){ if (chessBoardSquares[i][j].isSelected()) {
Piece piece = chessBoardSquares[i][j].getPiece(); Piece piece = chessBoardSquares[i][j].getPiece();
possibleMoves = piece.move(chessBoardSquares,i,j); possibleMoves = piece.move(chessBoardSquares, i, j);
}
}
} }
}
}
for(Square ps:possibleMoves){ for (Square ps : possibleMoves) {
ps.makePossible(); ps.makePossible();
} }
// System.out.println(piece1+"/"+square1); }
if(((Square) (e.getSource())).isPossibleSquare()&&((Square) (e.getSource())).isSelected()){
// if (lastSquare != null)
((Square) (e.getSource())).setPiece(lastPiece);
// if (lastSquare != null) {
lastSquare.removePiece();
}
} //moving a piece to a possible square
else if(((Square) (e.getSource())).getPiece()==null) {
if (((Square) (e.getSource())).isPossibleSquare() && ((Square) (e.getSource())).isSelected()) {
// if (lastSquare != null)
((Square) (e.getSource())).setPiece(lastPiece);
// if (lastSquare != null) {
lastSquare.removePiece();
isMoved=true;
// chessBoardSquares[1][j].setPiece(bp[j]);
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if (chessBoardSquares[i][j]==lastSquare){
chessBoardSquares[i][j].removePiece();
}
}
}
}
}
else if(!((Square) (e.getSource())).getPiece().getPieceColor().equals(whosTurn)){
if(((Square) (e.getSource())).isPossibleSquare() && ((Square) (e.getSource())).isSelected()){
((Square) (e.getSource())).removePiece();
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if (chessBoardSquares[i][j]== (Square) (e.getSource())){
// if(((Square) (e.getSource())).getPiece().getPieceColor().equals("w")){
// JButton button = new JButton();
// JLabel imageIcon = new JLabel(new ImageIcon(((Square) (e.getSource())).getPiece().getImage()));
// button.add(imageIcon);
//
// outWhitePieces.add(button);
// }
chessBoardSquares[i][j].removePiece();
}
}
}
((Square) (e.getSource())).setPiece(lastPiece);
lastSquare.removePiece();
isMoved=true;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if (chessBoardSquares[i][j]== lastSquare){
chessBoardSquares[i][j].removePiece();
}
}
}
}
}
if(isMoved) {
changeTurn();
possibleMoves.clear();
for (int i = 0; i <8 ; i++) {
for (int j = 0; j <8 ; j++) {
chessBoardSquares[i][j].canclePossibility();
}
}
isMoved=false;
}
turn.setText("It's your turn! ");
turn.setForeground(Color.GRAY);
if(whosTurn.equals("w"))turn.setBackground(Color.white);
else turn.setBackground(Color.BLACK);
} }
......
...@@ -29,7 +29,16 @@ public class Square extends JButton { ...@@ -29,7 +29,16 @@ public class Square extends JButton {
} }
public Square(int x, int y,Piece piece){
public JLabel getImage() {
return image;
}
public void setImage(JLabel image) {
this.image = image;
}
public Square(int x, int y, Piece piece){
this.x=x; this.x=x;
this.y = y; this.y = y;
this.piece=piece; this.piece=piece;
...@@ -46,7 +55,8 @@ public class Square extends JButton { ...@@ -46,7 +55,8 @@ public class Square extends JButton {
// ImageIcon img=new ImageIcon(this.getClass().getResource(piece.getImage())); // ImageIcon img=new ImageIcon(this.getClass().getResource(piece.getImage()));
// image=new JLabel(img); // image=new JLabel(img);
// JLabel imgLabel = new JLabel(new ImageIcon(piece.getImage())); // JLabel imgLabel = new JLabel(new ImageIcon(piece.getImage()));
this.add(new JLabel(new ImageIcon(piece.getImage()))); image= new JLabel(new ImageIcon(piece.getImage()));
this.add(image);
// this.add(image); // this.add(image);
} }
...@@ -59,6 +69,10 @@ public class Square extends JButton { ...@@ -59,6 +69,10 @@ public class Square extends JButton {
this.isSelected=true; this.isSelected=true;
} }
public boolean isSelected(){
return isSelected;
}
/** /**
* deselects a square by deleting its red border line * deselects a square by deleting its red border line
*/ */
...@@ -75,12 +89,20 @@ public class Square extends JButton { ...@@ -75,12 +89,20 @@ public class Square extends JButton {
this.possibleSquare = true; this.possibleSquare = true;
} }
protected void canclePossibility(){
this.possibleSquare=false;
}
/** /**
* removes a piece of a square * removes a piece of a square
*/ */
public void removePiece(){ public void removePiece(){
this.piece = null; this.piece = null;
this.remove(image); this.remove(image);
// this.remove(this.piece);
// this.remove(image);
// this.setImage(null);
// this.remove(new JLabel(new ImageIcon(null));
} }
/** /**
...@@ -91,6 +113,8 @@ public class Square extends JButton { ...@@ -91,6 +113,8 @@ public class Square extends JButton {
return hasPiece; return hasPiece;
} }
/** /**
* @return true if square is possible for moving a piece * @return true if square is possible for moving a piece
*/ */
......
...@@ -10,9 +10,10 @@ public class Bishop extends Piece { ...@@ -10,9 +10,10 @@ public class Bishop extends Piece {
setImage(imagePath); setImage(imagePath);
setPieceColor(color); setPieceColor(color);
} }
@Override
public ArrayList<Square> move(Square[][] boardSquares, int x, int y) { public ArrayList<Square> move(Square[][] boardSquares, int x, int y) {
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns. //The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList<Square> possibleSquares=new ArrayList<>();
possibleSquares.clear(); possibleSquares.clear();
......
...@@ -30,8 +30,11 @@ public class King extends Piece { ...@@ -30,8 +30,11 @@ public class King extends Piece {
{ {
return y; return y;
} }
@Override
public ArrayList<Square> move(Square[][] squares, int x, int y){ public ArrayList<Square> move(Square[][] squares, int x, int y){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns. //The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList<Square> possibleSquares=new ArrayList<>();
possibleSquares.clear(); possibleSquares.clear();
int posx[]={x,x,x+1,x+1,x+1,x-1,x-1,x-1}; int posx[]={x,x,x+1,x+1,x+1,x-1,x-1,x-1};
int posy[]={y-1,y+1,y-1,y,y+1,y-1,y,y+1}; int posy[]={y-1,y+1,y-1,y,y+1,y-1,y,y+1};
...@@ -215,5 +218,8 @@ public class King extends Piece { ...@@ -215,5 +218,8 @@ public class King extends Piece {
} }
public boolean isCheckMate(Square [][] squares){
return false;
}
} }
...@@ -10,8 +10,10 @@ public class Knight extends Piece { ...@@ -10,8 +10,10 @@ public class Knight extends Piece {
setImage(imagePath); setImage(imagePath);
setPieceColor(color); setPieceColor(color);
} }
@Override
public ArrayList<Square> move(Square [][] boardSquares, int x, int y) { public ArrayList<Square> move(Square [][] boardSquares, int x, int y) {
possibleSquares.clear(); ArrayList<Square> possibleSquares=new ArrayList<>();
//possibleSquares.clear();
int posx[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2}; int posx[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2};
int posy[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1}; int posy[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
......
...@@ -6,14 +6,18 @@ import java.util.ArrayList; ...@@ -6,14 +6,18 @@ import java.util.ArrayList;
public class Pawn extends Piece { public class Pawn extends Piece {
public Pawn(String ID,String imagePath,String color){ public Pawn(String ID,String imagePath,String color){
setPieceId(ID); setPieceId(ID);
setImage( imagePath); setImage( imagePath);
setPieceColor(color); setPieceColor(color);
} }
@Override
public ArrayList<Square> move(Square [][] boardSquares, int x, int y){ public ArrayList<Square> move(Square [][] boardSquares, int x, int y){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns. //The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
possibleSquares.clear(); // possibleSquares.clear();
ArrayList<Square> possibleSquares=new ArrayList<>();
if(getPieceColor().equals("w")){ if(getPieceColor().equals("w")){
if(x==0){ if(x==0){
...@@ -50,4 +54,9 @@ public class Pawn extends Piece { ...@@ -50,4 +54,9 @@ public class Pawn extends Piece {
return possibleSquares; return possibleSquares;
} }
// @Override
// public ArrayList<Square> move() {
// return null;
// }
} }
...@@ -17,7 +17,7 @@ private String imagePath; ...@@ -17,7 +17,7 @@ private String imagePath;
//possibleSquares is protected in Piece class therefore subclasses have access to it //possibleSquares is protected in Piece class therefore subclasses have access to it
//for all subclasses used one arrayList and before using it in each class content will be cleared //for all subclasses used one arrayList and before using it in each class content will be cleared
protected ArrayList<Square> possibleSquares = new ArrayList<>(); private ArrayList<Square> possibleSquares = new ArrayList<>();
...@@ -44,4 +44,11 @@ protected ArrayList<Square> possibleSquares = new ArrayList<>(); ...@@ -44,4 +44,11 @@ protected ArrayList<Square> possibleSquares = new ArrayList<>();
public String getImage() { public String getImage() {
return imagePath; return imagePath;
} }
public abstract ArrayList<Square> move(Square[][] boardSquares, int x, int y);
// public abstract ArrayList<Square> move();
} }
...@@ -11,8 +11,10 @@ public class Queen extends Piece { ...@@ -11,8 +11,10 @@ public class Queen extends Piece {
setImage( imagePath); setImage( imagePath);
setPieceColor(color); setPieceColor(color);
} }
@Override
public ArrayList<Square> move(Square [][] boardSquares, int x, int y) { public ArrayList<Square> move(Square [][] boardSquares, int x, int y) {
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns. //The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList<Square> possibleSquares=new ArrayList<>();
possibleSquares.clear(); possibleSquares.clear();
int tempx = x - 1; int tempx = x - 1;
while (tempx >= 0) { while (tempx >= 0) {
......
...@@ -10,8 +10,10 @@ public class Rook extends Piece { ...@@ -10,8 +10,10 @@ public class Rook extends Piece {
setImage( imagePath); setImage( imagePath);
setPieceColor(color); setPieceColor(color);
} }
@Override
public ArrayList<Square> move(Square [][] boardSquares, int x, int y){ public ArrayList<Square> move(Square [][] boardSquares, int x, int y){
//The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns. //The java.util.ArrayList.clear() method removes all of the elements from this list.The list will be empty after this call returns.
ArrayList<Square> possibleSquares=new ArrayList<>();
possibleSquares.clear(); possibleSquares.clear();
int tempx=x-1; int tempx=x-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