Commit 8b09ab57 authored by Omid Sayfun's avatar Omid Sayfun

Socket added

parent 67380b70
......@@ -6,21 +6,74 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import lab.game.*;
import java.io.IOException;
public class Main{
public static void main(String[] args){
Chess newChess = new Chess();
newChess.run();
public static void main(String[] args) throws IOException {
Splash newSplash = new Splash();
newSplash.run();
}
public static void rerun(){
Chess newChess = new Chess();
newChess.run();
public static void rerun() throws IOException {
Splash newSplash = new Splash();
newSplash.run();
}
}
class Splash{
public void run() throws IOException {
Object[] options = {"Server", "Client"};
int result = JOptionPane.showOptionDialog(null, "Which role do you take?", "Start a Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if( result == JOptionPane.YES_OPTION ){ // Server
ServerSocket server = new ServerSocket(5000);
System.out.println("Server Created");
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Waiting for opponent...");
label.setBorder(new EmptyBorder(40,40,40,40));
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
panel.add(label);
frame.add(label);
frame.pack();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setVisible(true);
Socket socket = server.accept();
System.out.println("Client Connected");
frame.dispose();
Chess newChess = new Chess("W", socket);
newChess.run();
socket.close();
}else { // Client
String s = (String)JOptionPane.showInputDialog(
null,
"Enter Server IP:",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"x.x.x.x");
Socket socket = new Socket(s, 5000);
System.out.println("Connected to server");
Chess newChess = new Chess("B", socket);
newChess.run();
socket.close();
}
}
}
/**
* A New Button class that has piece attribute
* @author Omiid
......@@ -29,9 +82,11 @@ class newJButton extends JButton{
private Piece piece;
private char X;
private int Y;
private Boolean color;
public newJButton(){
this.piece = null;
this.color = null;
this.setIcon(null);
}
......@@ -55,10 +110,11 @@ class newJButton extends JButton{
* @param Y The Y-Axis
* @author Omiid
*/
public newJButton(Piece p, char X, int Y){
public newJButton(Piece p, char X, int Y, boolean color){
this.piece = p;
this.X = X;
this.Y = Y;
this.color = color;
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + p.getName().toLowerCase() + ".png"));
......@@ -72,6 +128,7 @@ class newJButton extends JButton{
return this.piece;
}
public void setPiece(Piece p){
this.piece = p;
}
......@@ -83,13 +140,21 @@ class newJButton extends JButton{
public int getNewY(){
return this.Y;
}
public boolean getColor(){
return this.color;
}
}
/**
* Main Chess Class
*/
class Chess implements MouseListener{
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
private Board board = null;
private boolean me;
private String color = "W";
private boolean isSelected = false;
private newJButton inMove = null;
......@@ -98,10 +163,22 @@ class Chess implements MouseListener{
private ArrayList<newJButton> whiteLost = new ArrayList<>();
private ArrayList<newJButton> blackLost = new ArrayList<>();
private JLabel caption;
public Chess(String me, Socket socket) throws IOException {
if( me.equals("W") ){
this.me = true;
}else{
this.me = false;
}
this.socket = socket;
this.input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
this.output = new DataOutputStream(this.socket.getOutputStream());
}
/**
* Initial Run
*/
public void run(){
public void run() throws IOException {
JFrame frame = new JFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
// Biggest Panel
......@@ -119,61 +196,61 @@ class Chess implements MouseListener{
Rook newRook = new Rook(j, i, true, "WR");
whitePieces.add(newRook);
btn = new newJButton(newRook, j, i);
btn = new newJButton(newRook, j, i, true);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, true, "WN");
whitePieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
btn = new newJButton(newKnight, j, i, true);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, true, "WB");
whitePieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
btn = new newJButton(newBishop, j, i, true);
}else if( j == 'D' ){
Queen whiteQueen = new Queen(j, i, true,"WQ");
whitePieces.add(whiteQueen);
btn = new newJButton(whiteQueen, j, i);
btn = new newJButton(whiteQueen, j, i, true);
}else if( j == 'E' ){
King whiteKing = new King(j, i, true, "WK");
whitePieces.add(whiteKing);
btn = new newJButton(whiteKing, j, i);
btn = new newJButton(whiteKing, j, i, true);
}
}else if( i == 7 ){
Pawn newPawn = new Pawn(j, i, true, "WP");
whitePieces.add(newPawn);
btn = new newJButton(newPawn, j, i);
btn = new newJButton(newPawn, j, i, true);
}else if( i == 1 ){
if( j == 'A' || j == 'H' ){
Rook newRook = new Rook(j, i, false, "BR");
blackPieces.add(newRook);
btn = new newJButton(newRook, j, i);
btn = new newJButton(newRook, j, i, false);
}else if( j == 'B' || j == 'G' ){
Knight newKnight = new Knight(j, i, false, "BN");
blackPieces.add(newKnight);
btn = new newJButton(newKnight, j, i);
btn = new newJButton(newKnight, j, i, false);
}else if( j == 'C' || j == 'F' ){
Bishop newBishop = new Bishop(j, i, false, "BB");
blackPieces.add(newBishop);
btn = new newJButton(newBishop, j, i);
btn = new newJButton(newBishop, j, i, false);
}else if( j == 'D' ){
Queen blackQueen = new Queen(j, i, false,"BQ");
blackPieces.add(blackQueen);
btn = new newJButton(blackQueen, j, i);
btn = new newJButton(blackQueen, j, i, false);
}else if( j == 'E' ){
King blackKing = new King(j, i, false, "BQ");
blackPieces.add(blackKing);
btn = new newJButton(blackKing, j, i);
btn = new newJButton(blackKing, j, i, false);
}
}else if( i == 2 ){
Pawn newPawn = new Pawn(j, i, false, "BP");
blackPieces.add(newPawn);
btn = new newJButton(newPawn, j, i);
btn = new newJButton(newPawn, j, i, false);
// btn = new newJButton(j, i);
}else{
btn = new newJButton(j, i);
......@@ -247,7 +324,82 @@ class Chess implements MouseListener{
frame.setVisible(true);
this.frame = frame;
}
String line = "";
while (!line.equals("Over"))
{
try
{
line = this.input.readUTF();
System.out.println(line);
String[] splited = line.split(" ");
for(newJButton btn : this.btns){
if( btn.getNewX() == splited[0].charAt(1) && btn.getNewY() == splited[0].charAt(0) - '0' ){
System.out.println("Found inmove");
this.inMove = btn;
}
}
newJButton source = null;
for(newJButton btn : this.btns){
if( btn.getNewX() == splited[1].charAt(1) && btn.getNewY() == splited[1].charAt(0) - '0' ){
System.out.println("Found source");
source = btn;
}
}
if( this.board.canGo(this.inMove.getPiece(), source.getNewX(), source.getNewY(), this.color) ){
if( this.board.canAttack(this.inMove.getPiece(), source.getNewX(), source.getNewY(), this.color) ){
Piece temp = source.getPiece();
source.setPiece(this.inMove.getPiece());
this.inMove.setPiece(null);
if( this.color.equals("W") ){
killBlack(temp);
}else{
killWhite(temp);
}
}else{
Piece temp = this.inMove.getPiece();
this.inMove.setPiece(source.getPiece());
source.setPiece(temp);
}
System.out.println(this.board.move( inMove.getNewY() + Character.toString(inMove.getNewX()), source.getNewY() + Character.toString(source.getNewX()), this.color));
if( this.color.equals("W")){
caption.setText("Black Player is playing");
this.color = "B";
}else{
caption.setText("White Player is playing");
this.color = "W";
}
this.isSelected = false;
this.inMove = null;
}
repaint();
try {
checkMate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
catch(IOException i)
{
System.out.println(i);
}
}
try {
this.output.writeUTF("Over");
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Closing connection");
this.input.close();
this.output.close();
}
/**
* Mouse Clicking behaviour
......@@ -256,23 +408,33 @@ class Chess implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
newJButton source = (newJButton)(e.getSource());
if( !isSelected ){
for(newJButton btn : this.btns){
if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
if( this.board.canAttack(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
if( !this.isSelected ){
boolean flag = false;
if( this.me && this.color.equals("W") ){
btn.setBackground(new Color(255, 0, 0));
}else{
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/dot.png"));
} catch (IOException e1) {
e1.printStackTrace();
flag = true;
}else if( !this.me && this.color.equals("B") ){
flag = true;
}
if( source.getColor() == this.me && flag ){
for(newJButton btn : this.btns){
if( source != btn && source.getPiece() != null && this.board.canGo(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
if( this.board.canAttack(source.getPiece(), btn.getNewX(), btn.getNewY(), this.color) ){
btn.setBackground(new Color(255, 0, 0));
}else{
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/dot.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img, "dot"));
}
btn.setIcon(new ImageIcon(img, "dot"));
this.isSelected = true;
this.inMove = source;
}
this.isSelected = true;
this.inMove = source;
}
}
}else{
......@@ -293,7 +455,14 @@ class Chess implements MouseListener{
this.inMove.setPiece(source.getPiece());
source.setPiece(temp);
}
System.out.println(this.board.move( inMove.getNewY() + Character.toString(inMove.getNewX()), source.getNewY() + Character.toString(source.getNewX()), this.color));
if( this.board.move( inMove.getNewY() + Character.toString(inMove.getNewX()), source.getNewY() + Character.toString(source.getNewX()), this.color) ){
try {
this.output.writeUTF(inMove.getNewY() + Character.toString(inMove.getNewX()) + " " + source.getNewY() + source.getNewX());
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(true);
}
if( this.color.equals("W")){
caption.setText("Black Player is playing");
......@@ -302,89 +471,89 @@ class Chess implements MouseListener{
caption.setText("White Player is playing");
this.color = "W";
}
}
for(newJButton btn : this.btns) {
// if( ((ImageIcon)btn.getIcon()) != null ){
// System.out.println(((ImageIcon)btn.getIcon()).getDescription());
// }
if( btn.getPiece() != null ){
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(null);
}
if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(46, 83,106));
}
this.isSelected = false;
this.inMove = null;
}
for(newJButton btn : this.whiteLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)));
repaint();
try {
checkMate();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void repaint(){
for(newJButton btn : this.btns) {
if( btn.getPiece() != null ){
Image img = null;
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
// if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
// btn.setBackground(new Color(219,217,164));
// }else{
// btn.setBackground(new Color(4,51,106));
// }
// this.isSelected = false;
// this.inMove = null;
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(null);
}
for(newJButton btn : this.blackLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)));
if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
btn.setBackground(new Color(219,217,164));
}else{
btn.setBackground(new Color(46, 83,106));
}
}
for(newJButton btn : this.whiteLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
// if( (btn.getNewY() % 2 == 1 && btn.getNewX() % 2 == 1) || (btn.getNewY() % 2 == 0 && btn.getNewX() % 2 == 0) ){
// btn.setBackground(new Color(219,217,164));
// }else{
// btn.setBackground(new Color(4,51,106));
// }
// this.isSelected = false;
// this.inMove = null;
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)));
}
if( this.board.checkMate() != null ){
}
for(newJButton btn : this.blackLost) {
if( btn.getPiece() != null ){
Image img = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
try {
img = ImageIO.read(Chess.class.getResource("resources/images/" + btn.getPiece().getName().toLowerCase() + ".png"));
} catch (IOException e1) {
e1.printStackTrace();
}
btn.setIcon(new ImageIcon(img));
}else{
btn.setIcon(new ImageIcon(
new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB)));
}
}
}
String prompt = "";
if( this.board.checkMate().equals("W") ){
public void checkMate() throws IOException {
if( this.board.checkMate() != null ){
prompt = prompt.concat("Black Player Won!");
}else{
prompt = prompt.concat("White Player Won!");
}
prompt = prompt.concat(" \nDo you want to start a new game?");
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){
try {
this.output.writeUTF("Over");
} catch (IOException e1) {
e1.printStackTrace();
}
String prompt = "";
if( this.board.checkMate().equals("W") ){
Main.rerun();
}
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
prompt = prompt.concat("Black Player Won!");
}else{
prompt = prompt.concat("White Player Won!");
}
prompt = prompt.concat(" \nDo you want to start a new game?");
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){
Main.rerun();
}
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
}
}
......
......@@ -36,7 +36,7 @@ public class Bishop extends Piece{
int yShift = (y - p.y) / Math.abs(y - p.y);
int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) != null && !(checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) instanceof King) ){
return false;
}
......@@ -50,7 +50,7 @@ public class Bishop extends Piece{
int yShift = (y - this.y) / Math.abs(y - this.y);
int i = 1;
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) != null && !(checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) instanceof King) ){
return false;
}
......
......@@ -343,12 +343,18 @@ public class Board{
}
public boolean virtualCheck(ArrayList<Piece> enemy, char x, int y){
String temp = Character.toString(enemy.get(0).getName().charAt(0));
for(Piece p : enemy){
if( p.canMove(x, y) ){ // Check if move is valid
if( p.checkWay(this.allPieces, x, y) ){
return true;
if( p instanceof Pawn && p.getX() - x == 0 ){
return false;
}else{
return true;
}
}
}
}
......@@ -630,7 +636,31 @@ public class Board{
if( found.checkWay(this.allPieces, x, y) ){
if( (found instanceof Pawn) && found.crossMove(x, y) && attack == null ){
if( (found instanceof Pawn) && found.crossMove(x, y) ){
if( attack == null ){
return false;
}else{
if( y == attack.getY() && x == attack.getX() ){
if( found.color && y < found.getY() ){
return true;
}else if( !found.color && found.getY() < y ){
return true;
}else{
return false;
}
}else{
return false;
}
}
}else if( (found instanceof Pawn) && attack != null && found.getX() - attack.getX() == 0 ){
return false;
}else if( (found instanceof King) && virtualCheck(enemy, x, y)){
return false;
}else{
......
......@@ -55,7 +55,7 @@ public class Pawn extends Piece{
return true;
}else{
int yShift = (y - this.y) / Math.abs(y - this.y);
if( checkTaken(pieces, this.x, this.y + yShift) || checkTaken(pieces, this.x, this.y + 2 * yShift) ){
if( checkTaken(pieces, this.x, this.y + yShift) != null || checkTaken(pieces, this.x, this.y + 2 * yShift) != null ){
return false;
}
......
......@@ -47,14 +47,15 @@ public abstract class Piece{
return this.color;
}
public static boolean checkTaken(ArrayList<Piece> pieces, char x, int y){
public static Piece checkTaken(ArrayList<Piece> pieces, char x, int y){
for(Piece p : pieces){
if( p.x == x && p.y == y ){
return true;
return p;
}
}
return false;
return null;
}
public boolean crossMove(char x, int y){
......
......@@ -63,7 +63,7 @@ public class Rook extends Piece{
int i = 1;
boolean flag = true;
while( this.x != (char)(x + i * xShift) || this.y != y + i * yShift ){
if( checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) ){
if( checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) != null && !(checkTaken(pieces, (char)(x + i * xShift), y + i * yShift) instanceof King) ){
flag = false;
}
......@@ -90,7 +90,7 @@ public class Rook extends Piece{
}
int i = 1;
while( x != (char)(p.x + i * xShift) || y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) != null && !(checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) instanceof King) ){
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