Commit 91f7f983 authored by Omid Sayfun's avatar Omid Sayfun

Final Check

parent 9e9b637a
Manifest-Version: 1.0
Main-Class: Main
......@@ -2,18 +2,15 @@ import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import lab.game.*;
import java.io.IOException;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
public class Main{
public static void main(String[] args) throws IOException {
......@@ -26,8 +23,61 @@ public class Main{
}
}
class Splash{
class AppFrame extends JFrame implements WindowListener{
public AppFrame(){
super();
this.addWindowListener(this);
}
public AppFrame(String name){
super(name);
this.addWindowListener(this);
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
class Splash{
/**
* Run the game
* @author Omiid
* @throws IOException
*/
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]);
......@@ -36,37 +86,42 @@ class Splash{
ServerSocket server = new ServerSocket(5000);
System.out.println("Server Created");
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
AppFrame frame = new AppFrame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
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.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
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.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
frame.dispose();
server.close();
Chess newChess = new Chess("W", socket);
newChess.run();
}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);
boolean flag = true;
Socket socket = null;
while(flag){
String s = (String)JOptionPane.showInputDialog(
null,
"Enter Server IP:",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"x.x.x.x");
socket = new Socket(s, 5000);
if( socket.isConnected() ){
flag = false;
}
}
System.out.println("Connected to server");
Chess newChess = new Chess("B", socket);
......@@ -142,6 +197,7 @@ class newJButton extends JButton{
/**
* Main Chess Class
* @author Omiid
*/
class Chess implements MouseListener{
private Socket socket = null;
......@@ -152,7 +208,7 @@ class Chess implements MouseListener{
private String color = "W";
private boolean isSelected = false;
private newJButton inMove = null;
private JFrame frame;
private AppFrame frame;
private ArrayList<newJButton> btns = new ArrayList<newJButton>();
private ArrayList<newJButton> whiteLost = new ArrayList<>();
private ArrayList<newJButton> blackLost = new ArrayList<>();
......@@ -171,9 +227,11 @@ class Chess implements MouseListener{
}
/**
* Initial Run
* @author Omiid
* @throws IOException
*/
public void run() throws IOException {
JFrame frame = new JFrame("Chess Frame");
AppFrame frame = new AppFrame("Chess Frame");
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
// Biggest Panel
JPanel fullPanel = new JPanel(new BorderLayout());
......@@ -398,7 +456,7 @@ class Chess implements MouseListener{
frame.add(fullPanel);
// Show Frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
......@@ -413,12 +471,10 @@ class Chess implements MouseListener{
if( line.equals("Over") ){
break;
}
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;
}
}
......@@ -426,7 +482,6 @@ class Chess implements MouseListener{
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;
}
}
......@@ -489,6 +544,7 @@ class Chess implements MouseListener{
/**
* Mouse Clicking behaviour
* @param e The event Happened
* @author Omiid
*/
@Override
public void mouseClicked(MouseEvent e) {
......@@ -568,6 +624,10 @@ class Chess implements MouseListener{
}
}
/**
* Repaint all the button to the current state
* @author Omiid
*/
public void repaint(){
for(newJButton btn : this.btns) {
if( btn.getPiece() != null ){
......@@ -617,6 +677,11 @@ class Chess implements MouseListener{
}
}
/**
* Check if game is over
* @author Omiid
* @throws IOException
*/
public void checkMate() throws IOException {
if( this.board.checkMate() != null ){
......@@ -631,22 +696,33 @@ class Chess implements MouseListener{
String prompt = "";
if( this.board.checkMate().equals("W") ){
prompt = prompt.concat("Black Player Won!");
prompt = prompt.concat("<html><body><center>Black Player Won!M</center>");
}else{
prompt = prompt.concat("White Player Won!");
}
prompt = prompt.concat(" \nDo you want to start a new game?");
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
int result = JOptionPane.showConfirmDialog(this.frame, prompt, "Finished", JOptionPane.YES_NO_OPTION);
if( result == JOptionPane.YES_OPTION ){
Main.rerun();
}else{
System.exit(0);
prompt = prompt.concat("<html><body><center>White Player Won!</center>");
}
// 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 ){
//
// this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
// Main.rerun();
// }else{
// System.exit(0);
// }
JOptionPane optionPane = new JOptionPane(new JLabel(prompt,JLabel.CENTER));
JDialog dialog = optionPane.createDialog("Finished");
dialog.setModal(true);
dialog.setVisible(true);
System.exit(0);
}
}
/**
* Kill a white piece
* @param p The piece
* @author Omiid
* @return true if killed
*/
public boolean killWhite(Piece p){
for(newJButton btn : this.whiteLost){
if( btn.getPiece() == null ){
......@@ -658,6 +734,12 @@ class Chess implements MouseListener{
return false;
}
/**
* Kill a black piece
* @param p The piece
* @author Omiid
* @return true if killed
*/
public boolean killBlack(Piece p){
for(newJButton btn : this.blackLost){
if( btn.getPiece() == null ){
......
......@@ -7,6 +7,13 @@ public class Bishop extends Piece{
super(x, y, color, name);
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) != 0 && Math.abs(this.y - y) != 0 && Math.abs((float)(this.x - x) / (this.y - y)) == 1 ){
......@@ -15,6 +22,14 @@ public class Bishop extends Piece{
return false;
}
/**
* Check if can move to (X, Y) : Used for queen class
* @author Omiid
* @param p the piece to move
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( Math.abs(p.x - x) != 0 && Math.abs(p.y - y) != 0 && Math.abs((float)(p.x - x) / (p.y - y)) == 1 ){
......@@ -23,6 +38,16 @@ public class Bishop extends Piece{
return false;
}
/**
* Check if way is free to go
*
* @param p the piece to check
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
if( p.x - x == 0 ){
......@@ -45,6 +70,14 @@ public class Bishop extends Piece{
return true;
}
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - this.x) / Math.abs(x - this.x);
int yShift = (y - this.y) / Math.abs(y - this.y);
......
This diff is collapsed.
......@@ -6,6 +6,13 @@ public class King extends Piece{
super(x, y, color, name);
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) < 2 && Math.abs(this.y - y) < 2 ){
......@@ -14,6 +21,14 @@ public class King extends Piece{
return false;
}
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
......
......@@ -6,6 +6,13 @@ public class Knight extends Piece{
super(x, y, color, name);
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public boolean canMove(char x, int y){
int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
......@@ -18,6 +25,14 @@ public class Knight extends Piece{
return false;
}
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
return true;
}
......
......@@ -17,6 +17,13 @@ public class Pawn extends Piece{
return this.firstMove;
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 ){
......@@ -37,6 +44,13 @@ public class Pawn extends Piece{
return false;
}
/**
* Check if doing a cross move
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if doing a cross move
*/
@Override
public boolean crossMove(char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
......@@ -46,6 +60,14 @@ public class Pawn extends Piece{
return false;
}
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
......
......@@ -47,6 +47,14 @@ public abstract class Piece{
return this.color;
}
/**
* Check if (x, y) is taken
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The X-Axis
* @author Omiid
* @return true if it is taken
*/
public static Piece checkTaken(ArrayList<Piece> pieces, char x, int y){
for(Piece p : pieces){
if( p.x == x && p.y == y ){
......@@ -58,11 +66,34 @@ public abstract class Piece{
return null;
}
/**
* Check if doing a cross move
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if doing a cross move
*/
public boolean crossMove(char x, int y){
return false;
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if can move
*/
public abstract boolean canMove(char x, int y);
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
}
......@@ -6,6 +6,13 @@ public class Queen extends Piece{
super(x, y, color, name);
}
/**
* Check if can move to (X, Y)
* @author Omiid
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
*/
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){
......@@ -14,6 +21,14 @@ public class Queen extends Piece{
return false;
}
/**
* Check if way is free to go
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Rook.checkWay(this, pieces, x, y) || Bishop.checkWay(this, pieces, x, y) ){
......
......@@ -28,6 +28,7 @@ public class Rook extends Piece{
/**
* Check if can move to (X, Y) : Used for queen class
* @author Omiid
* @param p the piece to move
* @param x The X-Axis
* @param y The Y-Axis
* @return true if can move
......@@ -45,6 +46,7 @@ public class Rook extends Piece{
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
......@@ -75,6 +77,16 @@ public class Rook extends Piece{
}
}
/**
* Check if way is free to go
*
* @param p the piece to check
* @param pieces ArrayList of all pieces
* @param x The X-Axis
* @param y The Y-Axis
* @author Omiid
* @return true if is free to go
*/
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
......
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