Commit 511efd5f authored by Arghavan's avatar Arghavan

chess finished

parent 28c207d0
/**
*Board and movement of pieces
*@author arghavan soleymanpour
*@version 2.0
*/
package Main;
import Pieces.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
public class ChessFrame extends JFrame implements MouseListener, MouseMotionListener {
private static Board b = new Board();
private static int WIDTH = 750;
private static int WIDTH_2= 500;
private static int HEIGHT = 600;
private static int yBias = 50;
private int startX;
private int startY;
private int endX;
private int endY;
public static void main(String args[]){
new ChessFrame();
}
public ChessFrame(){
super();
addMouseListener(this);
setSize(WIDTH_2+WIDTH+12,yBias+HEIGHT+12);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private Image getPieceImage(Piece p){
BufferedImage ret = null;
try {
if (p.isWhite()) {
if (p instanceof Bishop) {
ret = ImageIO.read(new File("./Chess/Chess_blt60.png"));
} else if (p instanceof King) {
ret = ImageIO.read(new File("./Chess/Chess_klt60.png"));
} else if (p instanceof Knight) {
ret = ImageIO.read(new File("./Chess/Chess_nlt60.png"));
} else if (p instanceof Pawn) {
ret = ImageIO.read(new File("./Chess/Chess_plt60.png"));
} else if (p instanceof Queen) {
ret = ImageIO.read(new File("./Chess/Chess_qlt60.png"));
} else if (p instanceof Rook) {
ret = ImageIO.read(new File("./Chess/Chess_rlt60.png"));
}
} else {
if (p instanceof Bishop) {
ret = ImageIO.read(new File("./Chess/Chess_bdt60.png"));
} else if (p instanceof King) {
ret = ImageIO.read(new File("./Chess/Chess_kdt60.png"));
} else if (p instanceof Knight) {
ret = ImageIO.read(new File("./Chess/Chess_ndt60.png"));
} else if (p instanceof Pawn) {
ret = ImageIO.read(new File("./Chess/Chess_pdt60.png"));
} else if (p instanceof Queen) {
ret = ImageIO.read(new File("./Chess/Chess_qdt60.png"));
} else if (p instanceof Rook) {
ret = ImageIO.read(new File("./Chess/Chess_rdt60.png"));
}
}
}
catch (Exception e){
}
return ret;
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
int dy = HEIGHT/8;
int dx = WIDTH/8;
for(int i = 0 ; i <= 8 ; i++){ // Pain Chess Board
int H=i*dy;
int W = i*dx;
g.drawLine(0, yBias+H, WIDTH, yBias+H);
g.drawLine(0, yBias+H+1, WIDTH, yBias+H+1);
g.drawLine(0, yBias+H+2, WIDTH, yBias+H+2);
//-----------------------------------------------
g.drawLine(W, yBias, W, getHeight());
g.drawLine(W+1, yBias, W+1, getHeight());
g.drawLine(W+2, yBias, W+2, getHeight());
}
g.setColor(Color.GRAY);
for (int i = 0 ; i < 8 ; i++){
for (int j = 0 ; j < 8 ; j++){
if((i+j)%2==0){
g.fillRect(i*dx,yBias+j*dy,dx,dy);
}
if(!b.isEmpty(i,j)){
Image image = getPieceImage(b.getBoard()[i][j]);
// SwingUtilities.
// g.dra
g.drawImage(image,i*dx,yBias+j*dy,dx,dy,this);
}
}
}
//--------------------------- Paint Extra Board...
g.setColor(Color.BLACK);
dx = WIDTH_2/8;
for (int i = 0 ; i <= 8 ;i++){
int W = i*dx+WIDTH;
g.drawLine(W, yBias, W, yBias+dy*2);
g.drawLine(W+1, yBias, W+1, yBias+dy*2);
g.drawLine(W+2, yBias, W+2, yBias+dy*2);
//--------------------------------
g.drawLine(W, HEIGHT+yBias-2*dy, W, getHeight());
g.drawLine(W+1, HEIGHT+yBias-2*dy, W+1, getHeight());
g.drawLine(W+2, HEIGHT+yBias-2*dy, W+2, getHeight());
}
for (int i = 0 ; i < 3 ; i++){
g.drawLine(WIDTH,i*dy+yBias,getWidth(),i*dy+yBias);
g.drawLine(WIDTH,i*dy+yBias+1,getWidth(),i*dy+yBias+1);
g.drawLine(WIDTH,i*dy+yBias+2,getWidth(),i*dy+yBias+2);
g.drawLine(WIDTH,HEIGHT-(i*dy)+yBias,getWidth(),HEIGHT-(i*dy)+yBias);
g.drawLine(WIDTH,HEIGHT-(i*dy-1)+yBias,getWidth(),HEIGHT-(i*dy-1)+yBias);
g.drawLine(WIDTH,HEIGHT-(i*dy-2)+yBias,getWidth()+yBias,HEIGHT-(i*dy-2)+yBias);
}
//----------------------
for(int i = 0 ; i < 8 ; i++){
for(int j = 0 ; j < 2 ;j++){
int ind= i+j*8;
if(ind < b.getBlackRemovedList().size()){
Image image = getPieceImage(b.getBlackRemovedList().get(ind));
g.drawImage(image,WIDTH+i*dx,yBias+j*dy,dx,dy,this);
}
//------------------------------------------
if(ind < b.getWhiteRemovedList().size()){
Image image = getPieceImage(b.getWhiteRemovedList().get(ind));
g.drawImage(image,WIDTH+i*dx,HEIGHT+yBias-(j+1)*dy,dx,dy,this);
}
}
}
//---------------
g.setFont(new Font("Arial", Font.BOLD, 18));
if(b.KishCheck(true)){
g.drawString("Kish of White King ...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
if(b.checkGameOver(true)){
g.drawString("Black Color is Win ...", WIDTH + WIDTH_2 / 3, yBias+yBias+HEIGHT / 2);
}
}else if(b.KishCheck(false)){
g.drawString("Kish of Black King ...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
if(b.checkGameOver(false)){
g.drawString("White Color is Win ...", WIDTH + WIDTH_2 / 3, yBias+yBias+HEIGHT / 2);
}
}else if(b.isWhiteMove()) {
g.drawString("White Turn...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
}else if(!b.isWhiteMove()) {
g.drawString("Black Turn...", WIDTH + WIDTH_2 / 3, yBias+HEIGHT / 2);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// if(SwingUtilities.isLeftMouseButton(e)){
// int x = e.getX();
// int y = e.getY()-yBias;
// int dx = WIDTH/8;
// int dy = HEIGHT/8;
// System.out.println(x/dx+","+y/dy);
// }
}
@Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
int x = e.getX();
int y = e.getY()-yBias;
if(x > WIDTH || y > HEIGHT) return;
int dx = WIDTH/8;
int dy = HEIGHT/8;
startX = x/dx;
startY = y/dy;
System.out.println(x/dx+","+y/dy);
}
}
@Override
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
int x = e.getX();
int y = e.getY()-yBias;
if(x > WIDTH || y > HEIGHT) return;
int dx = WIDTH/8;
int dy = HEIGHT/8;
endX = x/dx;
endY = y/dy;
System.out.println(x/dx+","+y/dy);
b.getMove(startX,startY,endX,endY);
repaint();
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
......@@ -8,10 +8,16 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ChessGUI extends JFrame {
private Board b = new Board();
private Scanner reader;
private PrintWriter writer;
private int defaultPort = 4444;
private JButton buttonBoard[][] = new JButton[8][8];
private JButton blackRemoveButton[] = new JButton[16];
private JButton whiteRemoveButton[] = new JButton[16];
......@@ -22,11 +28,22 @@ public class ChessGUI extends JFrame {
private int endX;
private int endY;
private boolean turn = true;
private boolean isServer = false;
public static void main(String args[]){
new ChessGUI();
}
public ChessGUI(){
super();
String adress = JOptionPane.showInputDialog("Insert Server Adress: (Insert 0 for be a server)");
if(adress.equalsIgnoreCase("0")){
inilizeServer();
isServer = true;
setTitle("Server");
}else {
intilizeClient(adress);
setTitle("Client");
isServer=false;
}
setSize(1200,600);
this.setLayout(new GridLayout(1,2));
JPanel chessBoard = new JPanel();
......@@ -52,6 +69,50 @@ public class ChessGUI extends JFrame {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void inilizeServer(){
try {
ServerSocket server = new ServerSocket(defaultPort);
Socket s = server.accept();
reader = new Scanner(s.getInputStream());
writer = new PrintWriter(s.getOutputStream());
initThread();
} catch (IOException e) {
e.printStackTrace();
}
}
private void initThread(){
Thread t = new Thread() {
@Override
public void run() {
while (reader.hasNextInt()){
int sX = reader.nextInt();
int sY = reader.nextInt();
int eX = reader.nextInt();
int eY = reader.nextInt();
move(sX,sY,eX,eY);
}
}
};
t.start();
}
private void updateOtherClient(int startX , int startY,int endX , int endY){
writer.println(startX);
writer.println(startY);
writer.println(endX);
writer.println(endY);
writer.flush();
}
private void intilizeClient(String addr){
try {
Socket s = new Socket(addr,defaultPort);
reader = new Scanner(s.getInputStream());
writer = new PrintWriter(s.getOutputStream());
initThread();
} catch (IOException e) {
e.printStackTrace();
}
}
private void updateRemovePanel(){
int i = 0;
for (Piece p :b.getWhiteRemovedList()){
......@@ -94,13 +155,32 @@ public class ChessGUI extends JFrame {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(isServer && !turn)
return;
if(!isServer && turn)
return;
startX = endX;
startY = endY;
endX = fI;
endY = fJ;
// System.out.println(startX+","+startY);
if(validMoves !=null && validMoves[endX][endY]){
move(startX,startY,endX,endY);
updateOtherClient(startX,startY,endX,endY);
}
validMoves = getValidMoves( b.getBoard()[endX][endY]);
updateButtons();
}
});
buttonBoard[i][j] = button;
chessBoard.add(button);
}
}
}
private void move(int startX , int startY,int endX , int endY){
b.getMove(startX,startY,endX,endY);
turn = !turn;
if(b.KishCheck(true)){
notificationLabel.setText("White Kish...");
}else if(b.KishCheck(false)){
......@@ -113,20 +193,9 @@ public class ChessGUI extends JFrame {
else if (!turn){
notificationLabel.setText("Black Turn...");
}
turn = !turn;
updateRemovePanel();
// System.out.println("Moves Approved");
}
validMoves = getValidMoves( b.getBoard()[endX][endY]);
updateButtons();
}
});
buttonBoard[i][j] = button;
chessBoard.add(button);
}
}
}
private void updateButtons(){
for (int j = 0 ; j < 8 ;j++){
for (int i = 0 ; i < 8 ; i++){
......
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