diff --git a/Chess/bin/chess/Cell.class b/Chess/bin/chess/Cell.class index 9b2661d..59823ab 100644 Binary files a/Chess/bin/chess/Cell.class and b/Chess/bin/chess/Cell.class differ diff --git a/Chess/bin/chess/Main$1.class b/Chess/bin/chess/Main$1.class index 79ac6cf..0656f95 100644 Binary files a/Chess/bin/chess/Main$1.class and b/Chess/bin/chess/Main$1.class differ diff --git a/Chess/bin/chess/Main$Handler.class b/Chess/bin/chess/Main$Handler.class index f9ddce9..b622be7 100644 Binary files a/Chess/bin/chess/Main$Handler.class and b/Chess/bin/chess/Main$Handler.class differ diff --git a/Chess/bin/chess/Main$START.class b/Chess/bin/chess/Main$START.class index ec2c174..e574be2 100644 Binary files a/Chess/bin/chess/Main$START.class and b/Chess/bin/chess/Main$START.class differ diff --git a/Chess/bin/chess/Main$SelectHandler.class b/Chess/bin/chess/Main$SelectHandler.class index e4cf24a..97f9800 100644 Binary files a/Chess/bin/chess/Main$SelectHandler.class and b/Chess/bin/chess/Main$SelectHandler.class differ diff --git a/Chess/bin/chess/Main$TimeChange.class b/Chess/bin/chess/Main$TimeChange.class index ef725ab..d3298b8 100644 Binary files a/Chess/bin/chess/Main$TimeChange.class and b/Chess/bin/chess/Main$TimeChange.class differ diff --git a/Chess/bin/chess/Main.class b/Chess/bin/chess/Main.class index b05d3ba..4dce2e1 100644 Binary files a/Chess/bin/chess/Main.class and b/Chess/bin/chess/Main.class differ diff --git a/Chess/bin/chess/Player.class b/Chess/bin/chess/Player.class index ce4ba71..9f11551 100644 Binary files a/Chess/bin/chess/Player.class and b/Chess/bin/chess/Player.class differ diff --git a/Chess/bin/chess/Time$CountdownTimerListener.class b/Chess/bin/chess/Time$CountdownTimerListener.class index d6b4621..0649d7e 100644 Binary files a/Chess/bin/chess/Time$CountdownTimerListener.class and b/Chess/bin/chess/Time$CountdownTimerListener.class differ diff --git a/Chess/bin/chess/Time.class b/Chess/bin/chess/Time.class index 3e4193b..d7f1cb5 100644 Binary files a/Chess/bin/chess/Time.class and b/Chess/bin/chess/Time.class differ diff --git a/Chess/bin/pieces/Bishop.class b/Chess/bin/pieces/Bishop.class index f87bf37..d7f8c6a 100644 Binary files a/Chess/bin/pieces/Bishop.class and b/Chess/bin/pieces/Bishop.class differ diff --git a/Chess/bin/pieces/King.class b/Chess/bin/pieces/King.class index b5af433..cd80219 100644 Binary files a/Chess/bin/pieces/King.class and b/Chess/bin/pieces/King.class differ diff --git a/Chess/bin/pieces/Knight.class b/Chess/bin/pieces/Knight.class index 30f13e1..79479c7 100644 Binary files a/Chess/bin/pieces/Knight.class and b/Chess/bin/pieces/Knight.class differ diff --git a/Chess/bin/pieces/Pawn.class b/Chess/bin/pieces/Pawn.class index a7eca22..e785e1a 100644 Binary files a/Chess/bin/pieces/Pawn.class and b/Chess/bin/pieces/Pawn.class differ diff --git a/Chess/bin/pieces/Piece.class b/Chess/bin/pieces/Piece.class index 76d2a18..baa2f5a 100644 Binary files a/Chess/bin/pieces/Piece.class and b/Chess/bin/pieces/Piece.class differ diff --git a/Chess/bin/pieces/Queen.class b/Chess/bin/pieces/Queen.class index 719dc7d..be5575c 100644 Binary files a/Chess/bin/pieces/Queen.class and b/Chess/bin/pieces/Queen.class differ diff --git a/Chess/bin/pieces/Rook.class b/Chess/bin/pieces/Rook.class index 0dbeddc..bbbbc77 100644 Binary files a/Chess/bin/pieces/Rook.class and b/Chess/bin/pieces/Rook.class differ diff --git a/Chess/src/chess/Cell.java b/Chess/src/chess/Cell.java index 331a927..b927b74 100644 --- a/Chess/src/chess/Cell.java +++ b/Chess/src/chess/Cell.java @@ -6,136 +6,129 @@ import pieces.*; /** - * This is the Cell Class. It is the token class of our GUI. - * There are total of 64 cells that together makes up the Chess Board + * This is the Cell Class. It is the token class of our GUI. There are total of + * 64 cells that together makes up the Chess Board * */ -public class Cell extends JPanel implements Cloneable{ - - //Member Variables +public class Cell extends JPanel implements Cloneable { + + // Member Variables private static final long serialVersionUID = 1L; private boolean ispossibledestination; private JLabel content; private Piece piece; - int x,y; //is public because this is to be accessed by all the other class - private boolean isSelected=false; - private boolean ischeck=false; - - //Constructors - public Cell(int x,int y,Piece p) - { - this.x=x; - this.y=y; - + int x, y; // is public because this is to be accessed by all the other class + private boolean isSelected = false; + private boolean ischeck = false; + + // Constructors + public Cell(int x, int y, Piece p) { + this.x = x; + this.y = y; + setLayout(new BorderLayout()); - - if((x+y)%2==0) - setBackground(new Color(113,198,113)); - - else - setBackground(Color.white); - - if(p!=null) - setPiece(p); + + if ((x + y) % 2 == 0) + setBackground(new Color(113, 198, 113)); + + else + setBackground(Color.white); + + if (p != null) + setPiece(p); } - - //A constructor that takes a cell as argument and returns a new cell will the same data but different reference - public Cell(Cell cell) throws CloneNotSupportedException - { - this.x=cell.x; - this.y=cell.y; + + // A constructor that takes a cell as argument and returns a new cell will the + // same data but different reference + public Cell(Cell cell) throws CloneNotSupportedException { + this.x = cell.x; + this.y = cell.y; setLayout(new BorderLayout()); - if((x+y)%2==0) - setBackground(new Color(113,198,113)); + if ((x + y) % 2 == 0) + setBackground(new Color(113, 198, 113)); else setBackground(Color.white); - if(cell.getpiece()!=null) - { + if (cell.getpiece() != null) { setPiece(cell.getpiece().getcopy()); - } - else - piece=null; + } else + piece = null; } - - public void setPiece(Piece p) //Function to inflate a cell with a piece + + public void setPiece(Piece p) // Function to inflate a cell with a piece { - piece=p; - ImageIcon img=new javax.swing.ImageIcon(this.getClass().getResource(p.getPath())); - content=new JLabel(img); + piece = p; + ImageIcon img = new javax.swing.ImageIcon(this.getClass().getResource(p.getPath())); + content = new JLabel(img); this.add(content); } - - public void removePiece() //Function to remove a piece from the cell + + public void removePiece() // Function to remove a piece from the cell { - if (piece instanceof King) - { - piece=null; + if (piece instanceof King) { + piece = null; this.remove(content); - } - else - { - piece=null; + } else { + piece = null; this.remove(content); } } - - - public Piece getpiece() //Function to access piece of a particular cell + + public Piece getpiece() // Function to access piece of a particular cell { return this.piece; } - - public void select() //Function to mark a cell indicating it's selection + + public void select() // Function to mark a cell indicating it's selection { - this.setBorder(BorderFactory.createLineBorder(Color.red,6)); - this.isSelected=true; + this.setBorder(BorderFactory.createLineBorder(Color.red, 6)); + this.isSelected = true; } - - public boolean isselected() //Function to return if the cell is under selection + + public boolean isselected() // Function to return if the cell is under selection { return this.isSelected; } - - public void deselect() //Function to delselect the cell + + public void deselect() // Function to delselect the cell { this.setBorder(null); - this.isSelected=false; + this.isSelected = false; } - - public void setpossibledestination() //Function to highlight a cell to indicate that it is a possible valid move + + public void setpossibledestination() // Function to highlight a cell to indicate that it is a possible valid move { - this.setBorder(BorderFactory.createLineBorder(Color.blue,4)); - this.ispossibledestination=true; + this.setBorder(BorderFactory.createLineBorder(Color.blue, 4)); + this.ispossibledestination = true; } - - public void removepossibledestination() //Remove the cell from the list of possible moves + + public void removepossibledestination() // Remove the cell from the list of possible moves { this.setBorder(null); - this.ispossibledestination=false; + this.ispossibledestination = false; } - - public boolean ispossibledestination() //Function to check if the cell is a possible destination + + public boolean ispossibledestination() // Function to check if the cell is a possible destination { return this.ispossibledestination; } - - public void setcheck() //Function to highlight the current cell as checked (For King) + + public void setcheck() // Function to highlight the current cell as checked (For King) { this.setBackground(Color.RED); - this.ischeck=true; + this.ischeck = true; } - - public void removecheck() //Function to deselect check + + public void removecheck() // Function to deselect check { this.setBorder(null); - if((x+y)%2==0) - setBackground(new Color(113,198,113)); + if ((x + y) % 2 == 0) + setBackground(new Color(113, 198, 113)); else setBackground(Color.white); - this.ischeck=false; + this.ischeck = false; } - - public boolean ischeck() //Function to check if the current cell is in check + + public boolean ischeck() // Function to check if the current cell is in check { return ischeck; } diff --git a/Chess/src/chess/Main.java b/Chess/src/chess/Main.java index 28a8e47..dd00403 100644 --- a/Chess/src/chess/Main.java +++ b/Chess/src/chess/Main.java @@ -1,4 +1,5 @@ package chess; + import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.TitledBorder; @@ -19,112 +20,109 @@ * */ - /** - * This is the Main Class of our project. - * All GUI Elements are declared, initialized and used in this class itself. - * It is inherited from the JFrame Class of Java's Swing Library. + * This is the Main Class of our project. All GUI Elements are declared, + * initialized and used in this class itself. It is inherited from the JFrame + * Class of Java's Swing Library. * */ -public class Main extends JFrame implements MouseListener -{ +public class Main extends JFrame implements MouseListener { private static final long serialVersionUID = 1L; - - //Variable Declaration - private static final int Height=700; - private static final int Width=1110; - private static Rook wr01,wr02,br01,br02; - private static Knight wk01,wk02,bk01,bk02; - private static Bishop wb01,wb02,bb01,bb02; - private static Pawn wp[],bp[]; - private static Queen wq,bq; - private static King wk,bk; - private Cell c,previous; - private int chance=0; + + // Variable Declaration + private static final int Height = 700; + private static final int Width = 1110; + private static Rook wr01, wr02, br01, br02; + private static Knight wk01, wk02, bk01, bk02; + private static Bishop wb01, wb02, bb01, bb02; + private static Pawn wp[], bp[]; + private static Queen wq, bq; + private static King wk, bk; + private Cell c, previous; + private int chance = 0; private Cell boardState[][]; private ArrayList destinationlist = new ArrayList(); - private Player White=null,Black=null; - private JPanel board=new JPanel(new GridLayout(8,8)); - private JPanel wdetails=new JPanel(new GridLayout(3,3)); - private JPanel bdetails=new JPanel(new GridLayout(3,3)); - private JPanel wcombopanel=new JPanel(); - private JPanel bcombopanel=new JPanel(); - private JPanel controlPanel,WhitePlayer,BlackPlayer,temp,displayTime,showPlayer,time; + private Player White = null, Black = null; + private JPanel board = new JPanel(new GridLayout(8, 8)); + private JPanel wdetails = new JPanel(new GridLayout(3, 3)); + private JPanel bdetails = new JPanel(new GridLayout(3, 3)); + private JPanel wcombopanel = new JPanel(); + private JPanel bcombopanel = new JPanel(); + private JPanel controlPanel, WhitePlayer, BlackPlayer, temp, displayTime, showPlayer, time; private JSplitPane split; - private JLabel label,mov; + private JLabel label, mov; private static JLabel CHNC; private Time timer; public static Main Mainboard; - private boolean selected=false,end=false; + private boolean selected = false, end = false; private Container content; - private ArrayList wplayer,bplayer; - private ArrayList Wnames=new ArrayList(); - private ArrayList Bnames=new ArrayList(); - private JComboBox wcombo,bcombo; - private String wname=null,bname=null,winner=null; + private ArrayList wplayer, bplayer; + private ArrayList Wnames = new ArrayList(); + private ArrayList Bnames = new ArrayList(); + private JComboBox wcombo, bcombo; + private String wname = null, bname = null, winner = null; static String move; private Player tempPlayer; - private JScrollPane wscroll,bscroll; - private String[] WNames={},BNames={}; + private JScrollPane wscroll, bscroll; + private String[] WNames = {}, BNames = {}; private JSlider timeSlider; private BufferedImage image; - private Button start,wselect,bselect,WNewPlayer,BNewPlayer; - public static int timeRemaining=60; - public static void main(String[] args){ - - //variable initialization - wr01=new Rook("WR01","White_Rook.png",0); - wr02=new Rook("WR02","White_Rook.png",0); - br01=new Rook("BR01","Black_Rook.png",1); - br02=new Rook("BR02","Black_Rook.png",1); - wk01=new Knight("WK01","White_Knight.png",0); - wk02=new Knight("WK02","White_Knight.png",0); - bk01=new Knight("BK01","Black_Knight.png",1); - bk02=new Knight("BK02","Black_Knight.png",1); - wb01=new Bishop("WB01","White_Bishop.png",0); - wb02=new Bishop("WB02","White_Bishop.png",0); - bb01=new Bishop("BB01","Black_Bishop.png",1); - bb02=new Bishop("BB02","Black_Bishop.png",1); - wq=new Queen("WQ","White_Queen.png",0); - bq=new Queen("BQ","Black_Queen.png",1); - wk=new King("WK","White_King.png",0,7,3); - bk=new King("BK","Black_King.png",1,0,3); - wp=new Pawn[8]; - bp=new Pawn[8]; - for(int i=0;i<8;i++) - { - wp[i]=new Pawn("WP0"+(i+1),"White_Pawn.png",0); - bp[i]=new Pawn("BP0"+(i+1),"Black_Pawn.png",1); - } - - //Setting up the board - Mainboard = new Main(); - Mainboard.setVisible(true); - Mainboard.setResizable(false); + private Button start, wselect, bselect, WNewPlayer, BNewPlayer; + public static int timeRemaining = 60; + + public static void main(String[] args) { + + // variable initialization + wr01 = new Rook("WR01", "White_Rook.png", 0); + wr02 = new Rook("WR02", "White_Rook.png", 0); + br01 = new Rook("BR01", "Black_Rook.png", 1); + br02 = new Rook("BR02", "Black_Rook.png", 1); + wk01 = new Knight("WK01", "White_Knight.png", 0); + wk02 = new Knight("WK02", "White_Knight.png", 0); + bk01 = new Knight("BK01", "Black_Knight.png", 1); + bk02 = new Knight("BK02", "Black_Knight.png", 1); + wb01 = new Bishop("WB01", "White_Bishop.png", 0); + wb02 = new Bishop("WB02", "White_Bishop.png", 0); + bb01 = new Bishop("BB01", "Black_Bishop.png", 1); + bb02 = new Bishop("BB02", "Black_Bishop.png", 1); + wq = new Queen("WQ", "White_Queen.png", 0); + bq = new Queen("BQ", "Black_Queen.png", 1); + wk = new King("WK", "White_King.png", 0, 7, 3); + bk = new King("BK", "Black_King.png", 1, 0, 3); + wp = new Pawn[8]; + bp = new Pawn[8]; + for (int i = 0; i < 8; i++) { + wp[i] = new Pawn("WP0" + (i + 1), "White_Pawn.png", 0); + bp[i] = new Pawn("BP0" + (i + 1), "Black_Pawn.png", 1); + } + + // Setting up the board + Mainboard = new Main(); + Mainboard.setVisible(true); + Mainboard.setResizable(false); } - - //Constructor - private Main() - { - timeRemaining=60; + + // Constructor + private Main() { + timeRemaining = 60; timeSlider = new JSlider(); - move="White"; - wname=null; - bname=null; - winner=null; - board=new JPanel(new GridLayout(8,8)); - wdetails=new JPanel(new GridLayout(3,3)); - bdetails=new JPanel(new GridLayout(3,3)); - bcombopanel=new JPanel(); - wcombopanel=new JPanel(); - Wnames=new ArrayList(); - Bnames=new ArrayList(); - board.setMinimumSize(new Dimension(800,700)); + move = "White"; + wname = null; + bname = null; + winner = null; + board = new JPanel(new GridLayout(8, 8)); + wdetails = new JPanel(new GridLayout(3, 3)); + bdetails = new JPanel(new GridLayout(3, 3)); + bcombopanel = new JPanel(); + wcombopanel = new JPanel(); + Wnames = new ArrayList(); + Bnames = new ArrayList(); + board.setMinimumSize(new Dimension(800, 700)); ImageIcon img = new ImageIcon(this.getClass().getResource("icon.png")); this.setIconImage(img.getImage()); - - //Time Slider Details + + // Time Slider Details timeSlider.setMinimum(1); timeSlider.setMaximum(15); timeSlider.setValue(1); @@ -132,56 +130,58 @@ private Main() timeSlider.setPaintLabels(true); timeSlider.setPaintTicks(true); timeSlider.addChangeListener(new TimeChange()); - - - //Fetching Details of all Players - wplayer= Player.fetch_players(); - Iterator witr=wplayer.iterator(); - while(witr.hasNext()) + + // Fetching Details of all Players + wplayer = Player.fetch_players(); + Iterator witr = wplayer.iterator(); + while (witr.hasNext()) Wnames.add(witr.next().name()); - - bplayer= Player.fetch_players(); - Iterator bitr=bplayer.iterator(); - while(bitr.hasNext()) + + bplayer = Player.fetch_players(); + Iterator bitr = bplayer.iterator(); + while (bitr.hasNext()) Bnames.add(bitr.next().name()); - WNames=Wnames.toArray(WNames); - BNames=Bnames.toArray(BNames); - + WNames = Wnames.toArray(WNames); + BNames = Bnames.toArray(BNames); + Cell cell; board.setBorder(BorderFactory.createLoweredBevelBorder()); pieces.Piece P; - content=getContentPane(); - setSize(Width,Height); + content = getContentPane(); + setSize(Width, Height); setTitle("Chess"); content.setBackground(Color.black); - controlPanel=new JPanel(); + controlPanel = new JPanel(); content.setLayout(new BorderLayout()); - controlPanel.setLayout(new GridLayout(3,3)); - controlPanel.setBorder(BorderFactory.createTitledBorder(null, "Statistics", TitledBorder.TOP,TitledBorder.CENTER, new Font("Lucida Calligraphy",Font.PLAIN,20), Color.ORANGE)); - - //Defining the Player Box in Control Panel - WhitePlayer=new JPanel(); - WhitePlayer.setBorder(BorderFactory.createTitledBorder(null, "White Player", TitledBorder.TOP,TitledBorder.CENTER, new Font("times new roman",Font.BOLD,18), Color.RED)); + controlPanel.setLayout(new GridLayout(3, 3)); + controlPanel.setBorder(BorderFactory.createTitledBorder(null, "Statistics", TitledBorder.TOP, + TitledBorder.CENTER, new Font("Lucida Calligraphy", Font.PLAIN, 20), Color.ORANGE)); + + // Defining the Player Box in Control Panel + WhitePlayer = new JPanel(); + WhitePlayer.setBorder(BorderFactory.createTitledBorder(null, "White Player", TitledBorder.TOP, + TitledBorder.CENTER, new Font("times new roman", Font.BOLD, 18), Color.RED)); WhitePlayer.setLayout(new BorderLayout()); - - BlackPlayer=new JPanel(); - BlackPlayer.setBorder(BorderFactory.createTitledBorder(null, "Black Player", TitledBorder.TOP,TitledBorder.CENTER, new Font("times new roman",Font.BOLD,18), Color.BLUE)); - BlackPlayer.setLayout(new BorderLayout()); - - JPanel whitestats=new JPanel(new GridLayout(3,3)); - JPanel blackstats=new JPanel(new GridLayout(3,3)); - wcombo=new JComboBox(WNames); - bcombo=new JComboBox(BNames); - wscroll=new JScrollPane(wcombo); - bscroll=new JScrollPane(bcombo); + + BlackPlayer = new JPanel(); + BlackPlayer.setBorder(BorderFactory.createTitledBorder(null, "Black Player", TitledBorder.TOP, + TitledBorder.CENTER, new Font("times new roman", Font.BOLD, 18), Color.BLUE)); + BlackPlayer.setLayout(new BorderLayout()); + + JPanel whitestats = new JPanel(new GridLayout(3, 3)); + JPanel blackstats = new JPanel(new GridLayout(3, 3)); + wcombo = new JComboBox(WNames); + bcombo = new JComboBox(BNames); + wscroll = new JScrollPane(wcombo); + bscroll = new JScrollPane(bcombo); wcombopanel.setLayout(new FlowLayout()); bcombopanel.setLayout(new FlowLayout()); - wselect=new Button("Select"); - bselect=new Button("Select"); + wselect = new Button("Select"); + bselect = new Button("Select"); wselect.addActionListener(new SelectHandler(0)); bselect.addActionListener(new SelectHandler(1)); - WNewPlayer=new Button("New Player"); - BNewPlayer=new Button("New Player"); + WNewPlayer = new Button("New Player"); + BNewPlayer = new Button("New Player"); WNewPlayer.addActionListener(new Handler(0)); BNewPlayer.addActionListener(new Handler(1)); wcombopanel.add(wscroll); @@ -190,607 +190,580 @@ private Main() bcombopanel.add(bscroll); bcombopanel.add(bselect); bcombopanel.add(BNewPlayer); - WhitePlayer.add(wcombopanel,BorderLayout.NORTH); - BlackPlayer.add(bcombopanel,BorderLayout.NORTH); + WhitePlayer.add(wcombopanel, BorderLayout.NORTH); + BlackPlayer.add(bcombopanel, BorderLayout.NORTH); whitestats.add(new JLabel("Name :")); whitestats.add(new JLabel("Played :")); whitestats.add(new JLabel("Won :")); blackstats.add(new JLabel("Name :")); blackstats.add(new JLabel("Played :")); blackstats.add(new JLabel("Won :")); - WhitePlayer.add(whitestats,BorderLayout.WEST); - BlackPlayer.add(blackstats,BorderLayout.WEST); + WhitePlayer.add(whitestats, BorderLayout.WEST); + BlackPlayer.add(blackstats, BorderLayout.WEST); controlPanel.add(WhitePlayer); controlPanel.add(BlackPlayer); - - - //Defining all the Cells - boardState=new Cell[8][8]; - for(int i=0;i<8;i++) - for(int j=0;j<8;j++) - { - P=null; - if(i==0&&j==0) - P=br01; - else if(i==0&&j==7) - P=br02; - else if(i==7&&j==0) - P=wr01; - else if(i==7&&j==7) - P=wr02; - else if(i==0&&j==1) - P=bk01; - else if (i==0&&j==6) - P=bk02; - else if(i==7&&j==1) - P=wk01; - else if (i==7&&j==6) - P=wk02; - else if(i==0&&j==2) - P=bb01; - else if (i==0&&j==5) - P=bb02; - else if(i==7&&j==2) - P=wb01; - else if(i==7&&j==5) - P=wb02; - else if(i==0&&j==3) - P=bk; - else if(i==0&&j==4) - P=bq; - else if(i==7&&j==3) - P=wk; - else if(i==7&&j==4) - P=wq; - else if(i==1) - P=bp[j]; - else if(i==6) - P=wp[j]; - cell=new Cell(i,j,P); + + // Defining all the Cells + boardState = new Cell[8][8]; + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) { + P = null; + if (i == 0 && j == 0) + P = br01; + else if (i == 0 && j == 7) + P = br02; + else if (i == 7 && j == 0) + P = wr01; + else if (i == 7 && j == 7) + P = wr02; + else if (i == 0 && j == 1) + P = bk01; + else if (i == 0 && j == 6) + P = bk02; + else if (i == 7 && j == 1) + P = wk01; + else if (i == 7 && j == 6) + P = wk02; + else if (i == 0 && j == 2) + P = bb01; + else if (i == 0 && j == 5) + P = bb02; + else if (i == 7 && j == 2) + P = wb01; + else if (i == 7 && j == 5) + P = wb02; + else if (i == 0 && j == 3) + P = bk; + else if (i == 0 && j == 4) + P = bq; + else if (i == 7 && j == 3) + P = wk; + else if (i == 7 && j == 4) + P = wq; + else if (i == 1) + P = bp[j]; + else if (i == 6) + P = wp[j]; + cell = new Cell(i, j, P); cell.addMouseListener(this); board.add(cell); - boardState[i][j]=cell; + boardState[i][j] = cell; } - showPlayer=new JPanel(new FlowLayout()); + showPlayer = new JPanel(new FlowLayout()); showPlayer.add(timeSlider); - JLabel setTime=new JLabel("Set Timer(in mins):"); - start=new Button("Start"); + JLabel setTime = new JLabel("Set Timer(in mins):"); + start = new Button("Start"); start.setBackground(Color.black); start.setForeground(Color.white); - start.addActionListener(new START()); - start.setPreferredSize(new Dimension(120,40)); - setTime.setFont(new Font("Arial",Font.BOLD,16)); + start.addActionListener(new START()); + start.setPreferredSize(new Dimension(120, 40)); + setTime.setFont(new Font("Arial", Font.BOLD, 16)); label = new JLabel("Time Starts now", JLabel.CENTER); - label.setFont(new Font("SERIF", Font.BOLD, 30)); - displayTime=new JPanel(new FlowLayout()); - time=new JPanel(new GridLayout(3,3)); - time.add(setTime); - time.add(showPlayer); - displayTime.add(start); - time.add(displayTime); - controlPanel.add(time); - board.setMinimumSize(new Dimension(800,700)); - - //The Left Layout When Game is inactive - temp=new JPanel(){ + label.setFont(new Font("SERIF", Font.BOLD, 30)); + displayTime = new JPanel(new FlowLayout()); + time = new JPanel(new GridLayout(3, 3)); + time.add(setTime); + time.add(showPlayer); + displayTime.add(start); + time.add(displayTime); + controlPanel.add(time); + board.setMinimumSize(new Dimension(800, 700)); + + // The Left Layout When Game is inactive + temp = new JPanel() { private static final long serialVersionUID = 1L; - + @Override - public void paintComponent(Graphics g) { - try { - image = ImageIO.read(this.getClass().getResource("clash.jpg")); - } catch (IOException ex) { - System.out.println("not found"); - } - + public void paintComponent(Graphics g) { + try { + image = ImageIO.read(this.getClass().getResource("clash.jpg")); + } catch (IOException ex) { + System.out.println("not found"); + } + g.drawImage(image, 0, 0, null); - } - }; - - temp.setMinimumSize(new Dimension(800,700)); - controlPanel.setMinimumSize(new Dimension(285,700)); - split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,temp, controlPanel); - - content.add(split); + } + }; + + temp.setMinimumSize(new Dimension(800, 700)); + controlPanel.setMinimumSize(new Dimension(285, 700)); + split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, temp, controlPanel); + + content.add(split); setDefaultCloseOperation(EXIT_ON_CLOSE); - } - - // A function to change the chance from White Player to Black Player or vice verse + } + + // A function to change the chance from White Player to Black Player or vice + // verse // It is made public because it is to be accessed in the Time Class - public void changechance() - { - if (boardState[getKing(chance).getx()][getKing(chance).gety()].ischeck()) - { - chance^=1; + public void changechance() { + if (boardState[getKing(chance).getx()][getKing(chance).gety()].ischeck()) { + chance ^= 1; gameend(); } - if(destinationlist.isEmpty()==false) + if (destinationlist.isEmpty() == false) cleandestinations(destinationlist); - if(previous!=null) + if (previous != null) previous.deselect(); - previous=null; - chance^=1; - if(!end && timer!=null) - { + previous = null; + chance ^= 1; + if (!end && timer != null) { timer.reset(); timer.start(); showPlayer.remove(CHNC); - if(Main.move=="White") - Main.move="Black"; + if (Main.move == "White") + Main.move = "Black"; else - Main.move="White"; + Main.move = "White"; CHNC.setText(Main.move); showPlayer.add(CHNC); } } - - //A function to retrieve the Black King or White King - private King getKing(int color) - { - if (color==0) + + // A function to retrieve the Black King or White King + private King getKing(int color) { + if (color == 0) return wk; else return bk; } - - //A function to clean the highlights of possible destination cells - private void cleandestinations(ArrayList destlist) //Function to clear the last move's destinations - { - ListIterator it = destlist.listIterator(); - while(it.hasNext()) - it.next().removepossibledestination(); - } - - //A function that indicates the possible moves by highlighting the Cells - private void highlightdestinations(ArrayList destlist) - { - ListIterator it = destlist.listIterator(); - while(it.hasNext()) - it.next().setpossibledestination(); - } - - - //Function to check if the king will be in danger if the given move is made - private boolean willkingbeindanger(Cell fromcell,Cell tocell) - { - Cell newboardstate[][] = new Cell[8][8]; - for(int i=0;i<8;i++) - for(int j=0;j<8;j++) - { try { newboardstate[i][j] = new Cell(boardState[i][j]);} catch (CloneNotSupportedException e){e.printStackTrace(); System.out.println("There is a problem with cloning !!"); }} - - if(newboardstate[tocell.x][tocell.y].getpiece()!=null) + + // A function to clean the highlights of possible destination cells + private void cleandestinations(ArrayList destlist) // Function to clear the last move's destinations + { + ListIterator it = destlist.listIterator(); + while (it.hasNext()) + it.next().removepossibledestination(); + } + + // A function that indicates the possible moves by highlighting the Cells + private void highlightdestinations(ArrayList destlist) { + ListIterator it = destlist.listIterator(); + while (it.hasNext()) + it.next().setpossibledestination(); + } + + // Function to check if the king will be in danger if the given move is made + private boolean willkingbeindanger(Cell fromcell, Cell tocell) { + Cell newboardstate[][] = new Cell[8][8]; + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) { + try { + newboardstate[i][j] = new Cell(boardState[i][j]); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + System.out.println("There is a problem with cloning !!"); + } + } + + if (newboardstate[tocell.x][tocell.y].getpiece() != null) newboardstate[tocell.x][tocell.y].removePiece(); - + newboardstate[tocell.x][tocell.y].setPiece(newboardstate[fromcell.x][fromcell.y].getpiece()); - if(newboardstate[tocell.x][tocell.y].getpiece() instanceof King) - { - ((King)(newboardstate[tocell.x][tocell.y].getpiece())).setx(tocell.x); - ((King)(newboardstate[tocell.x][tocell.y].getpiece())).sety(tocell.y); + if (newboardstate[tocell.x][tocell.y].getpiece() instanceof King) { + ((King) (newboardstate[tocell.x][tocell.y].getpiece())).setx(tocell.x); + ((King) (newboardstate[tocell.x][tocell.y].getpiece())).sety(tocell.y); } newboardstate[fromcell.x][fromcell.y].removePiece(); - if (((King)(newboardstate[getKing(chance).getx()][getKing(chance).gety()].getpiece())).isindanger(newboardstate)==true) + if (((King) (newboardstate[getKing(chance).getx()][getKing(chance).gety()].getpiece())) + .isindanger(newboardstate) == true) return true; else return false; - } - - //A function to eliminate the possible moves that will put the King in danger - private ArrayList filterdestination (ArrayList destlist, Cell fromcell) - { - ArrayList newlist = new ArrayList(); - Cell newboardstate[][] = new Cell[8][8]; - ListIterator it = destlist.listIterator(); - int x,y; - while (it.hasNext()) - { - for(int i=0;i<8;i++) - for(int j=0;j<8;j++) - { try { newboardstate[i][j] = new Cell(boardState[i][j]);} catch (CloneNotSupportedException e){e.printStackTrace();}} - - Cell tempc = it.next(); - if(newboardstate[tempc.x][tempc.y].getpiece()!=null) - newboardstate[tempc.x][tempc.y].removePiece(); - newboardstate[tempc.x][tempc.y].setPiece(newboardstate[fromcell.x][fromcell.y].getpiece()); - x=getKing(chance).getx(); - y=getKing(chance).gety(); - if(newboardstate[fromcell.x][fromcell.y].getpiece() instanceof King) - { - ((King)(newboardstate[tempc.x][tempc.y].getpiece())).setx(tempc.x); - ((King)(newboardstate[tempc.x][tempc.y].getpiece())).sety(tempc.y); - x=tempc.x; - y=tempc.y; - } - newboardstate[fromcell.x][fromcell.y].removePiece(); - if ((((King)(newboardstate[x][y].getpiece())).isindanger(newboardstate)==false)) - newlist.add(tempc); - } - return newlist; - } - - //A Function to filter the possible moves when the king of the current player is under Check - private ArrayList incheckfilter (ArrayList destlist, Cell fromcell, int color) - { - ArrayList newlist = new ArrayList(); - Cell newboardstate[][] = new Cell[8][8]; - ListIterator it = destlist.listIterator(); - int x,y; - while (it.hasNext()) - { - for(int i=0;i<8;i++) - for(int j=0;j<8;j++) - { try { newboardstate[i][j] = new Cell(boardState[i][j]);} catch (CloneNotSupportedException e){e.printStackTrace();}} - Cell tempc = it.next(); - if(newboardstate[tempc.x][tempc.y].getpiece()!=null) - newboardstate[tempc.x][tempc.y].removePiece(); - newboardstate[tempc.x][tempc.y].setPiece(newboardstate[fromcell.x][fromcell.y].getpiece()); - x=getKing(color).getx(); - y=getKing(color).gety(); - if(newboardstate[tempc.x][tempc.y].getpiece() instanceof King) - { - ((King)(newboardstate[tempc.x][tempc.y].getpiece())).setx(tempc.x); - ((King)(newboardstate[tempc.x][tempc.y].getpiece())).sety(tempc.y); - x=tempc.x; - y=tempc.y; - } - newboardstate[fromcell.x][fromcell.y].removePiece(); - if ((((King)(newboardstate[x][y].getpiece())).isindanger(newboardstate)==false)) - newlist.add(tempc); - } - return newlist; - } - - //A function to check if the King is check-mate. The Game Ends if this function returns true. - public boolean checkmate(int color) - { - ArrayList dlist = new ArrayList(); - for(int i=0;i<8;i++) - { - for(int j=0;j<8;j++) - { - if (boardState[i][j].getpiece()!=null && boardState[i][j].getpiece().getcolor()==color) - { - dlist.clear(); - dlist=boardState[i][j].getpiece().move(boardState, i, j); - dlist=incheckfilter(dlist,boardState[i][j],color); - if(dlist.size()!=0) - return false; - } - } - } - return true; - } - - - @SuppressWarnings("deprecation") - private void gameend() - { - cleandestinations(destinationlist); - displayTime.disable(); - timer.countdownTimer.stop(); - if(previous!=null) - previous.removePiece(); - if(chance==0) - { White.updateGamesWon(); - White.Update_Player(); - winner=White.name(); + } + + // A function to eliminate the possible moves that will put the King in danger + private ArrayList filterdestination(ArrayList destlist, Cell fromcell) { + ArrayList newlist = new ArrayList(); + Cell newboardstate[][] = new Cell[8][8]; + ListIterator it = destlist.listIterator(); + int x, y; + while (it.hasNext()) { + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) { + try { + newboardstate[i][j] = new Cell(boardState[i][j]); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + } + + Cell tempc = it.next(); + if (newboardstate[tempc.x][tempc.y].getpiece() != null) + newboardstate[tempc.x][tempc.y].removePiece(); + newboardstate[tempc.x][tempc.y].setPiece(newboardstate[fromcell.x][fromcell.y].getpiece()); + x = getKing(chance).getx(); + y = getKing(chance).gety(); + if (newboardstate[fromcell.x][fromcell.y].getpiece() instanceof King) { + ((King) (newboardstate[tempc.x][tempc.y].getpiece())).setx(tempc.x); + ((King) (newboardstate[tempc.x][tempc.y].getpiece())).sety(tempc.y); + x = tempc.x; + y = tempc.y; + } + newboardstate[fromcell.x][fromcell.y].removePiece(); + if ((((King) (newboardstate[x][y].getpiece())).isindanger(newboardstate) == false)) + newlist.add(tempc); } - else - { + return newlist; + } + + // A Function to filter the possible moves when the king of the current player + // is under Check + private ArrayList incheckfilter(ArrayList destlist, Cell fromcell, int color) { + ArrayList newlist = new ArrayList(); + Cell newboardstate[][] = new Cell[8][8]; + ListIterator it = destlist.listIterator(); + int x, y; + while (it.hasNext()) { + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) { + try { + newboardstate[i][j] = new Cell(boardState[i][j]); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + } + Cell tempc = it.next(); + if (newboardstate[tempc.x][tempc.y].getpiece() != null) + newboardstate[tempc.x][tempc.y].removePiece(); + newboardstate[tempc.x][tempc.y].setPiece(newboardstate[fromcell.x][fromcell.y].getpiece()); + x = getKing(color).getx(); + y = getKing(color).gety(); + if (newboardstate[tempc.x][tempc.y].getpiece() instanceof King) { + ((King) (newboardstate[tempc.x][tempc.y].getpiece())).setx(tempc.x); + ((King) (newboardstate[tempc.x][tempc.y].getpiece())).sety(tempc.y); + x = tempc.x; + y = tempc.y; + } + newboardstate[fromcell.x][fromcell.y].removePiece(); + if ((((King) (newboardstate[x][y].getpiece())).isindanger(newboardstate) == false)) + newlist.add(tempc); + } + return newlist; + } + + // A function to check if the King is check-mate. The Game Ends if this function + // returns true. + public boolean checkmate(int color) { + ArrayList dlist = new ArrayList(); + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 8; j++) { + if (boardState[i][j].getpiece() != null && boardState[i][j].getpiece().getcolor() == color) { + dlist.clear(); + dlist = boardState[i][j].getpiece().move(boardState, i, j); + dlist = incheckfilter(dlist, boardState[i][j], color); + if (dlist.size() != 0) + return false; + } + } + } + return true; + } + + @SuppressWarnings("deprecation") + private void gameend() { + cleandestinations(destinationlist); + displayTime.disable(); + timer.countdownTimer.stop(); + if (previous != null) + previous.removePiece(); + if (chance == 0) { + White.updateGamesWon(); + White.Update_Player(); + winner = White.name(); + } else { Black.updateGamesWon(); Black.Update_Player(); - winner=Black.name(); + winner = Black.name(); } - JOptionPane.showMessageDialog(board,"Checkmate!!!\n"+winner+" wins"); + JOptionPane.showMessageDialog(board, "Checkmate!!!\n" + winner + " wins"); WhitePlayer.remove(wdetails); BlackPlayer.remove(bdetails); displayTime.remove(label); - + displayTime.add(start); showPlayer.remove(mov); showPlayer.remove(CHNC); showPlayer.revalidate(); showPlayer.add(timeSlider); - + split.remove(board); split.add(temp); WNewPlayer.enable(); BNewPlayer.enable(); wselect.enable(); bselect.enable(); - end=true; + end = true; Mainboard.disable(); Mainboard.dispose(); Mainboard = new Main(); Mainboard.setVisible(true); Mainboard.setResizable(false); - } - - //These are the abstract function of the parent class. Only relevant method here is the On-Click Fuction - //which is called when the user clicks on a particular cell + } + + // These are the abstract function of the parent class. Only relevant method + // here is the On-Click Fuction + // which is called when the user clicks on a particular cell @Override - public void mouseClicked(MouseEvent arg0){ + public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub - c=(Cell)arg0.getSource(); - if (previous==null) - { - if(c.getpiece()!=null) - { - if(c.getpiece().getcolor()!=chance) + c = (Cell) arg0.getSource(); + if (previous == null) { + if (c.getpiece() != null) { + if (c.getpiece().getcolor() != chance) return; c.select(); - previous=c; + previous = c; destinationlist.clear(); - destinationlist=c.getpiece().move(boardState, c.x, c.y); - if(c.getpiece() instanceof King) - destinationlist=filterdestination(destinationlist,c); - else - { - if(boardState[getKing(chance).getx()][getKing(chance).gety()].ischeck()) - destinationlist = new ArrayList(filterdestination(destinationlist,c)); - else if(destinationlist.isEmpty()==false && willkingbeindanger(c,destinationlist.get(0))) + destinationlist = c.getpiece().move(boardState, c.x, c.y); + if (c.getpiece() instanceof King) + destinationlist = filterdestination(destinationlist, c); + else { + if (boardState[getKing(chance).getx()][getKing(chance).gety()].ischeck()) + destinationlist = new ArrayList(filterdestination(destinationlist, c)); + else if (destinationlist.isEmpty() == false && willkingbeindanger(c, destinationlist.get(0))) destinationlist.clear(); } highlightdestinations(destinationlist); } - } - else - { - if(c.x==previous.x && c.y==previous.y) - { + } else { + if (c.x == previous.x && c.y == previous.y) { c.deselect(); cleandestinations(destinationlist); destinationlist.clear(); - previous=null; - } - else if(c.getpiece()==null||previous.getpiece().getcolor()!=c.getpiece().getcolor()) - { - if(c.ispossibledestination()) - { - if(c.getpiece()!=null) + previous = null; + } else if (c.getpiece() == null || previous.getpiece().getcolor() != c.getpiece().getcolor()) { + if (c.ispossibledestination()) { + if (c.getpiece() != null) c.removePiece(); c.setPiece(previous.getpiece()); if (previous.ischeck()) previous.removecheck(); previous.removePiece(); - if(getKing(chance^1).isindanger(boardState)) - { - boardState[getKing(chance^1).getx()][getKing(chance^1).gety()].setcheck(); - if (checkmate(getKing(chance^1).getcolor())) - { + if (getKing(chance ^ 1).isindanger(boardState)) { + boardState[getKing(chance ^ 1).getx()][getKing(chance ^ 1).gety()].setcheck(); + if (checkmate(getKing(chance ^ 1).getcolor())) { previous.deselect(); - if(previous.getpiece()!=null) + if (previous.getpiece() != null) previous.removePiece(); gameend(); } } - if(getKing(chance).isindanger(boardState)==false) + if (getKing(chance).isindanger(boardState) == false) boardState[getKing(chance).getx()][getKing(chance).gety()].removecheck(); - if(c.getpiece() instanceof King) - { - ((King)c.getpiece()).setx(c.x); - ((King)c.getpiece()).sety(c.y); + if (c.getpiece() instanceof King) { + ((King) c.getpiece()).setx(c.x); + ((King) c.getpiece()).sety(c.y); } changechance(); - if(!end) - { + if (!end) { timer.reset(); timer.start(); } } - if(previous!=null) - { + if (previous != null) { previous.deselect(); - previous=null; + previous = null; } cleandestinations(destinationlist); destinationlist.clear(); - } - else if(previous.getpiece().getcolor()==c.getpiece().getcolor()) - { + } else if (previous.getpiece().getcolor() == c.getpiece().getcolor()) { previous.deselect(); cleandestinations(destinationlist); destinationlist.clear(); c.select(); - previous=c; - destinationlist=c.getpiece().move(boardState, c.x, c.y); - if(c.getpiece() instanceof King) - destinationlist=filterdestination(destinationlist,c); - else - { - if(boardState[getKing(chance).getx()][getKing(chance).gety()].ischeck()) - destinationlist = new ArrayList(filterdestination(destinationlist,c)); - else if(destinationlist.isEmpty()==false && willkingbeindanger(c,destinationlist.get(0))) + previous = c; + destinationlist = c.getpiece().move(boardState, c.x, c.y); + if (c.getpiece() instanceof King) + destinationlist = filterdestination(destinationlist, c); + else { + if (boardState[getKing(chance).getx()][getKing(chance).gety()].ischeck()) + destinationlist = new ArrayList(filterdestination(destinationlist, c)); + else if (destinationlist.isEmpty() == false && willkingbeindanger(c, destinationlist.get(0))) destinationlist.clear(); } highlightdestinations(destinationlist); } } - if(c.getpiece()!=null && c.getpiece() instanceof King) - { - ((King)c.getpiece()).setx(c.x); - ((King)c.getpiece()).sety(c.y); + if (c.getpiece() != null && c.getpiece() instanceof King) { + ((King) c.getpiece()).setx(c.x); + ((King) c.getpiece()).sety(c.y); } } - - //Other Irrelevant abstract function. Only the Click Event is captured. + + // Other Irrelevant abstract function. Only the Click Event is captured. @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } + @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } + @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } - @Override - public void mouseReleased(MouseEvent arg0) { - // TODO Auto-generated method stub - } - - - class START implements ActionListener - { - @SuppressWarnings("deprecation") @Override - public void actionPerformed(ActionEvent arg0) { + public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub - - if(White==null||Black==null) - {JOptionPane.showMessageDialog(controlPanel, "Fill in the details"); - return;} - White.updateGamesPlayed(); - White.Update_Player(); - Black.updateGamesPlayed(); - Black.Update_Player(); - WNewPlayer.disable(); - BNewPlayer.disable(); - wselect.disable(); - bselect.disable(); - split.remove(temp); - split.add(board); - showPlayer.remove(timeSlider); - mov=new JLabel("Move:"); - mov.setFont(new Font("Comic Sans MS",Font.PLAIN,20)); - mov.setForeground(Color.red); - showPlayer.add(mov); - CHNC=new JLabel(move); - CHNC.setFont(new Font("Comic Sans MS",Font.BOLD,20)); - CHNC.setForeground(Color.blue); - showPlayer.add(CHNC); - displayTime.remove(start); - displayTime.add(label); - timer=new Time(label); - timer.start(); } + + class START implements ActionListener { + + @SuppressWarnings("deprecation") + @Override + public void actionPerformed(ActionEvent arg0) { + // TODO Auto-generated method stub + + if (White == null || Black == null) { + JOptionPane.showMessageDialog(controlPanel, "Fill in the details"); + return; + } + White.updateGamesPlayed(); + White.Update_Player(); + Black.updateGamesPlayed(); + Black.Update_Player(); + WNewPlayer.disable(); + BNewPlayer.disable(); + wselect.disable(); + bselect.disable(); + split.remove(temp); + split.add(board); + showPlayer.remove(timeSlider); + mov = new JLabel("Move:"); + mov.setFont(new Font("Comic Sans MS", Font.PLAIN, 20)); + mov.setForeground(Color.red); + showPlayer.add(mov); + CHNC = new JLabel(move); + CHNC.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); + CHNC.setForeground(Color.blue); + showPlayer.add(CHNC); + displayTime.remove(start); + displayTime.add(label); + timer = new Time(label); + timer.start(); + } } - - class TimeChange implements ChangeListener - { + + class TimeChange implements ChangeListener { @Override - public void stateChanged(ChangeEvent arg0) - { - timeRemaining=timeSlider.getValue()*60; + public void stateChanged(ChangeEvent arg0) { + timeRemaining = timeSlider.getValue() * 60; } } - - - class SelectHandler implements ActionListener - { + + class SelectHandler implements ActionListener { private int color; - - SelectHandler(int i) - { - color=i; + + SelectHandler(int i) { + color = i; } - @Override - public void actionPerformed(ActionEvent arg0) - { - // TODO Auto-generated method stub - tempPlayer=null; - String n=(color==0)?wname:bname; - JComboBox jc=(color==0)?wcombo:bcombo; - JComboBox ojc=(color==0)?bcombo:wcombo; - ArrayList pl=(color==0)?wplayer:bplayer; - //ArrayList otherPlayer=(color==0)?bplayer:wplayer; - ArrayList opl=Player.fetch_players(); - if(opl.isEmpty()) - return; - JPanel det=(color==0)?wdetails:bdetails; - JPanel PL=(color==0)?WhitePlayer:BlackPlayer; - if(selected==true) - det.removeAll(); - n=(String)jc.getSelectedItem(); - Iterator it=pl.iterator(); - Iterator oit=opl.iterator(); - while(it.hasNext()) - { - Player p=it.next(); - if(p.name().equals(n)) - {tempPlayer=p; - break;} + + @Override + public void actionPerformed(ActionEvent arg0) { + // TODO Auto-generated method stub + tempPlayer = null; + String n = (color == 0) ? wname : bname; + JComboBox jc = (color == 0) ? wcombo : bcombo; + JComboBox ojc = (color == 0) ? bcombo : wcombo; + ArrayList pl = (color == 0) ? wplayer : bplayer; + // ArrayList otherPlayer=(color==0)?bplayer:wplayer; + ArrayList opl = Player.fetch_players(); + if (opl.isEmpty()) + return; + JPanel det = (color == 0) ? wdetails : bdetails; + JPanel PL = (color == 0) ? WhitePlayer : BlackPlayer; + if (selected == true) + det.removeAll(); + n = (String) jc.getSelectedItem(); + Iterator it = pl.iterator(); + Iterator oit = opl.iterator(); + while (it.hasNext()) { + Player p = it.next(); + if (p.name().equals(n)) { + tempPlayer = p; + break; } - while(oit.hasNext()) - { - Player p=oit.next(); - if(p.name().equals(n)) - {opl.remove(p); - break;} + } + while (oit.hasNext()) { + Player p = oit.next(); + if (p.name().equals(n)) { + opl.remove(p); + break; } - - if(tempPlayer==null) - return; - if(color==0) - White=tempPlayer; - else - Black=tempPlayer; - bplayer=opl; - ojc.removeAllItems(); - for (Player s:opl) - ojc.addItem(s.name()); - det.add(new JLabel(" "+tempPlayer.name())); - det.add(new JLabel(" "+tempPlayer.gamesplayed())); - det.add(new JLabel(" "+tempPlayer.gameswon())); - - PL.revalidate(); - PL.repaint(); - PL.add(det); - selected=true; } - + + if (tempPlayer == null) + return; + if (color == 0) + White = tempPlayer; + else + Black = tempPlayer; + bplayer = opl; + ojc.removeAllItems(); + for (Player s : opl) + ojc.addItem(s.name()); + det.add(new JLabel(" " + tempPlayer.name())); + det.add(new JLabel(" " + tempPlayer.gamesplayed())); + det.add(new JLabel(" " + tempPlayer.gameswon())); + + PL.revalidate(); + PL.repaint(); + PL.add(det); + selected = true; } - - - - class Handler implements ActionListener{ - private int color; - Handler(int i) - { - color=i; - } - @Override - public void actionPerformed(ActionEvent e) { - // TODO Auto-generated method stub - String n=(color==0)?wname:bname; - JPanel j=(color==0)?WhitePlayer:BlackPlayer; - ArrayList N=Player.fetch_players(); - Iterator it=N.iterator(); - JPanel det=(color==0)?wdetails:bdetails; - n=JOptionPane.showInputDialog(j,"Enter your name"); - - if(n!=null) - { - - while(it.hasNext()) - { - if(it.next().name().equals(n)) - {JOptionPane.showMessageDialog(j,"Player exists"); - return;} - } - - if(n.length()!=0) - {Player tem=new Player(n); - tem.Update_Player(); - if(color==0) - White=tem; - else - Black=tem; - } - else return; + + } + + class Handler implements ActionListener { + private int color; + + Handler(int i) { + color = i; + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + String n = (color == 0) ? wname : bname; + JPanel j = (color == 0) ? WhitePlayer : BlackPlayer; + ArrayList N = Player.fetch_players(); + Iterator it = N.iterator(); + JPanel det = (color == 0) ? wdetails : bdetails; + n = JOptionPane.showInputDialog(j, "Enter your name"); + + if (n != null) { + + while (it.hasNext()) { + if (it.next().name().equals(n)) { + JOptionPane.showMessageDialog(j, "Player exists"); + return; } - else + } + + if (n.length() != 0) { + Player tem = new Player(n); + tem.Update_Player(); + if (color == 0) + White = tem; + else + Black = tem; + } else return; - det.removeAll(); - det.add(new JLabel(" "+n)); - det.add(new JLabel(" 0")); - det.add(new JLabel(" 0")); - j.revalidate(); - j.repaint(); - j.add(det); - selected=true; - } - } + } else + return; + det.removeAll(); + det.add(new JLabel(" " + n)); + det.add(new JLabel(" 0")); + det.add(new JLabel(" 0")); + j.revalidate(); + j.repaint(); + j.add(det); + selected = true; + } + } } \ No newline at end of file diff --git a/Chess/src/chess/Player.java b/Chess/src/chess/Player.java index e6f48c6..59fe5b2 100644 --- a/Chess/src/chess/Player.java +++ b/Chess/src/chess/Player.java @@ -13,101 +13,84 @@ import javax.swing.JOptionPane; - /** - * This is the Player Class - * It provides the functionality of keeping track of all the users - * Objects of this class is updated and written in the Game's Data Files after every Game + * This is the Player Class It provides the functionality of keeping track of + * all the users Objects of this class is updated and written in the Game's Data + * Files after every Game * */ -public class Player implements Serializable{ - +public class Player implements Serializable { + private static final long serialVersionUID = 1L; private String name; private Integer gamesplayed; private Integer gameswon; - - //Constructor - public Player(String name) - { + + // Constructor + public Player(String name) { this.name = name.trim(); - //this.lname = lname.trim(); + // this.lname = lname.trim(); gamesplayed = new Integer(0); gameswon = new Integer(0); } - - //Name Getter - public String name() - { + + // Name Getter + public String name() { return name; } - - //Returns the number of games played - public Integer gamesplayed() - { + + // Returns the number of games played + public Integer gamesplayed() { return gamesplayed; } - - //Returns the number of games won - public Integer gameswon() - { + + // Returns the number of games won + public Integer gameswon() { return gameswon; } - - //Calculates the win percentage of the player - public Integer winpercent() - { - return new Integer((gameswon*100)/gamesplayed); + + // Calculates the win percentage of the player + public Integer winpercent() { + return new Integer((gameswon * 100) / gamesplayed); } - - //Increments the number of games played - public void updateGamesPlayed() - { + + // Increments the number of games played + public void updateGamesPlayed() { gamesplayed++; } - - //Increments the number of games won - public void updateGamesWon() - { + + // Increments the number of games won + public void updateGamesWon() { gameswon++; } - - - public static ArrayList fetch_players() //Function to fetch the list of the players + + public static ArrayList fetch_players() // Function to fetch the list of the players { Player tempplayer; ObjectInputStream input = null; ArrayList players = new ArrayList(); - try - { - File infile = new File(System.getProperty("user.dir")+ File.separator + "chessgamedata.dat"); + try { + File infile = new File(System.getProperty("user.dir") + File.separator + "chessgamedata.dat"); input = new ObjectInputStream(new FileInputStream(infile)); - try - { - while(true) - { + try { + while (true) { tempplayer = (Player) input.readObject(); players.add(tempplayer); } - } - catch(EOFException e) - { + } catch (EOFException e) { input.close(); } - } - catch (FileNotFoundException e) - { + } catch (FileNotFoundException e) { players.clear(); return players; - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); - try {input.close();} catch (IOException e1) {} + try { + input.close(); + } catch (IOException e1) { + } JOptionPane.showMessageDialog(null, "Unable to read the required Game files !!"); - } - catch (ClassNotFoundException e) - { + } catch (ClassNotFoundException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Game Data File Corrupted !! Click Ok to Continue Builing New File"); } catch (Exception e1) { @@ -116,81 +99,62 @@ public static ArrayList fetch_players() //Function to fetch the } return players; } - - public void Update_Player() //Function to update the statistics of a player + + public void Update_Player() // Function to update the statistics of a player { ObjectInputStream input = null; ObjectOutputStream output = null; Player temp_player; - File inputfile=null; - File outputfile=null; - try - { - inputfile = new File(System.getProperty("user.dir")+ File.separator + "chessgamedata.dat"); - outputfile = new File(System.getProperty("user.dir")+ File.separator + "tempfile.dat"); - } catch (SecurityException e) - { + File inputfile = null; + File outputfile = null; + try { + inputfile = new File(System.getProperty("user.dir") + File.separator + "chessgamedata.dat"); + outputfile = new File(System.getProperty("user.dir") + File.separator + "tempfile.dat"); + } catch (SecurityException e) { JOptionPane.showMessageDialog(null, "Read-Write Permission Denied !! Program Cannot Start"); System.exit(0); - } + } boolean playerdonotexist; - try - { - if(outputfile.exists()==false) + try { + if (outputfile.exists() == false) outputfile.createNewFile(); - if(inputfile.exists()==false) - { - output = new ObjectOutputStream(new java.io.FileOutputStream(outputfile,true)); - output.writeObject(this); - } - else - { + if (inputfile.exists() == false) { + output = new ObjectOutputStream(new java.io.FileOutputStream(outputfile, true)); + output.writeObject(this); + } else { input = new ObjectInputStream(new FileInputStream(inputfile)); output = new ObjectOutputStream(new FileOutputStream(outputfile)); - playerdonotexist=true; - try - { - while(true) - { - temp_player = (Player)input.readObject(); - if (temp_player.name().equals(name())) - { - output.writeObject(this); - playerdonotexist = false; + playerdonotexist = true; + try { + while (true) { + temp_player = (Player) input.readObject(); + if (temp_player.name().equals(name())) { + output.writeObject(this); + playerdonotexist = false; + } else + output.writeObject(temp_player); } - else - output.writeObject(temp_player); - } - } - catch(EOFException e){ + } catch (EOFException e) { input.close(); } - if(playerdonotexist) + if (playerdonotexist) output.writeObject(this); } inputfile.delete(); output.close(); - File newf = new File(System.getProperty("user.dir")+ File.separator + "chessgamedata.dat"); - if(outputfile.renameTo(newf)==false) + File newf = new File(System.getProperty("user.dir") + File.separator + "chessgamedata.dat"); + if (outputfile.renameTo(newf) == false) System.out.println("File Renameing Unsuccessful"); - } - catch (FileNotFoundException e) - { + } catch (FileNotFoundException e) { e.printStackTrace(); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Unable to read/write the required Game files !! Press ok to continue"); - } - catch (ClassNotFoundException e) - { + } catch (ClassNotFoundException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Game Data File Corrupted !! Click Ok to Continue Builing New File"); - } - catch (Exception e) - { - + } catch (Exception e) { + } } } diff --git a/Chess/src/chess/Time.java b/Chess/src/chess/Time.java index 257eaba..f8c3055 100644 --- a/Chess/src/chess/Time.java +++ b/Chess/src/chess/Time.java @@ -6,58 +6,50 @@ import javax.swing.JLabel; import javax.swing.Timer; - /** - * This is the Time Class. - * It contains all the required variables and functions related to the timer on the Main GUI - * It uses a Timer Class + * This is the Time Class. It contains all the required variables and functions + * related to the timer on the Main GUI It uses a Timer Class * */ -public class Time -{ - private JLabel label; - Timer countdownTimer; - int Timerem; - public Time(JLabel passedLabel) - { - countdownTimer = new Timer(1000, new CountdownTimerListener()); - this.label = passedLabel; - Timerem=Main.timeRemaining; - } - - //A function that starts the timer - public void start() - { - countdownTimer.start(); - } - - //A function that resets the timer - public void reset() - { - Timerem=Main.timeRemaining; - } - - //A function that is called after every second. It updates the timer and takes other necessary actions - class CountdownTimerListener implements ActionListener - { - public void actionPerformed(ActionEvent e) - { - int min,sec; - if (Timerem > 0) - { - min=Timerem/60; - sec=Timerem%60; - label.setText(String.valueOf(min)+":"+(sec>=10?String.valueOf(sec):"0"+String.valueOf(sec))); - Timerem--; - } - else - { - label.setText("Time's up!"); - reset(); - start(); - Main.Mainboard.changechance(); - } - } - } +public class Time { + private JLabel label; + Timer countdownTimer; + int Timerem; + + public Time(JLabel passedLabel) { + countdownTimer = new Timer(1000, new CountdownTimerListener()); + this.label = passedLabel; + Timerem = Main.timeRemaining; + } + + // A function that starts the timer + public void start() { + countdownTimer.start(); + } + + // A function that resets the timer + public void reset() { + Timerem = Main.timeRemaining; + } + + // A function that is called after every second. It updates the timer and takes + // other necessary actions + class CountdownTimerListener implements ActionListener { + public void actionPerformed(ActionEvent e) { + int min, sec; + if (Timerem > 0) { + min = Timerem / 60; + sec = Timerem % 60; + label.setText( + String.valueOf(min) + ":" + (sec >= 10 ? String.valueOf(sec) : "0" + String.valueOf(sec))); + Timerem--; + } else { + label.setText("Time's up!"); + reset(); + start(); + Main.Mainboard.changechance(); + } + } + } } \ No newline at end of file diff --git a/Chess/src/pieces/Bishop.java b/Chess/src/pieces/Bishop.java index 5b5d7f1..42a022d 100644 --- a/Chess/src/pieces/Bishop.java +++ b/Chess/src/pieces/Bishop.java @@ -4,86 +4,77 @@ import chess.Cell; - /** - * This is the Bishop Class. - * The Move Function defines the basic rules for movement of Bishop on a chess board + * This is the Bishop Class. The Move Function defines the basic rules for + * movement of Bishop on a chess board * * */ -public class Bishop extends Piece{ - - //Constructor - public Bishop(String i,String p,int c) - { +public class Bishop extends Piece { + + // Constructor + public Bishop(String i, String p, int c) { setId(i); setPath(p); setColor(c); } - - //move function defined. It returns a list of all the possible destinations of a Bishop - //The basic principle of Bishop Movement on chess board has been implemented - public ArrayList move(Cell state[][],int x,int y) - { - //Bishop can Move diagonally in all 4 direction (NW,NE,SW,SE) - //This function defines that logic + + // move function defined. It returns a list of all the possible destinations of + // a Bishop + // The basic principle of Bishop Movement on chess board has been implemented + public ArrayList move(Cell state[][], int x, int y) { + // Bishop can Move diagonally in all 4 direction (NW,NE,SW,SE) + // This function defines that logic possiblemoves.clear(); - int tempx=x+1,tempy=y-1; - while(tempx<8&&tempy>=0) - { - if(state[tempx][tempy].getpiece()==null) - { + int tempx = x + 1, tempy = y - 1; + while (tempx < 8 && tempy >= 0) { + if (state[tempx][tempy].getpiece() == null) { possiblemoves.add(state[tempx][tempy]); - } - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + } else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } tempx++; tempy--; } - tempx=x-1;tempy=y+1; - while(tempx>=0&&tempy<8) - { - if(state[tempx][tempy].getpiece()==null) + tempx = x - 1; + tempy = y + 1; + while (tempx >= 0 && tempy < 8) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } tempx--; tempy++; } - tempx=x-1;tempy=y-1; - while(tempx>=0&&tempy>=0) - { - if(state[tempx][tempy].getpiece()==null) + tempx = x - 1; + tempy = y - 1; + while (tempx >= 0 && tempy >= 0) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } tempx--; tempy--; } - tempx=x+1;tempy=y+1; - while(tempx<8&&tempy<8) - { - if(state[tempx][tempy].getpiece()==null) + tempx = x + 1; + tempy = y + 1; + while (tempx < 8 && tempy < 8) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } diff --git a/Chess/src/pieces/King.java b/Chess/src/pieces/King.java index 4e9336a..911e65c 100644 --- a/Chess/src/pieces/King.java +++ b/Chess/src/pieces/King.java @@ -4,226 +4,209 @@ import chess.Cell; -public class King extends Piece{ - - private int x,y; //Extra variables for King class to keep a track of king's position - - //King Constructor - public King(String i,String p,int c,int x,int y) - { +public class King extends Piece { + + private int x, y; // Extra variables for King class to keep a track of king's position + + // King Constructor + public King(String i, String p, int c, int x, int y) { setx(x); sety(y); setId(i); setPath(p); setColor(c); } - - //general value access functions - public void setx(int x) - { - this.x=x; + + // general value access functions + public void setx(int x) { + this.x = x; } - public void sety(int y) - { - this.y=y; + + public void sety(int y) { + this.y = y; } - public int getx() - { + + public int getx() { return x; } - public int gety() - { + + public int gety() { return y; } - //Move Function for King Overridden from Pieces - public ArrayList move(Cell state[][],int x,int y) - { - //King can move only one step. So all the adjacent 8 cells have been considered. + + // Move Function for King Overridden from Pieces + public ArrayList move(Cell state[][], int x, int y) { + // King can move only one step. So all the adjacent 8 cells have been + // considered. possiblemoves.clear(); - 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}; - for(int i=0;i<8;i++) - if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)) - if((state[posx[i]][posy[i]].getpiece()==null||state[posx[i]][posy[i]].getpiece().getcolor()!=this.getcolor())) + 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 }; + for (int i = 0; i < 8; i++) + if ((posx[i] >= 0 && posx[i] < 8 && posy[i] >= 0 && posy[i] < 8)) + if ((state[posx[i]][posy[i]].getpiece() == null + || state[posx[i]][posy[i]].getpiece().getcolor() != this.getcolor())) possiblemoves.add(state[posx[i]][posy[i]]); return possiblemoves; } - - - - //Function to check if king is under threat - //It checks whether there is any piece of opposite color that can attack king for a given board state - public boolean isindanger(Cell state[][]) - { - - //Checking for attack from left,right,up and down - for(int i=x+1;i<8;i++) - { - if(state[i][y].getpiece()==null) - continue; - else if(state[i][y].getpiece().getcolor()==this.getcolor()) - break; - else - { - if ((state[i][y].getpiece() instanceof Rook) || (state[i][y].getpiece() instanceof Queen)) - return true; - else - break; - } - } - for(int i=x-1;i>=0;i--) - { - if(state[i][y].getpiece()==null) - continue; - else if(state[i][y].getpiece().getcolor()==this.getcolor()) - break; - else - { - if ((state[i][y].getpiece() instanceof Rook) || (state[i][y].getpiece() instanceof Queen)) - return true; - else - break; - } - } - for(int i=y+1;i<8;i++) - { - if(state[x][i].getpiece()==null) - continue; - else if(state[x][i].getpiece().getcolor()==this.getcolor()) - break; - else - { - if ((state[x][i].getpiece() instanceof Rook) || (state[x][i].getpiece() instanceof Queen)) - return true; - else - break; - } - } - for(int i=y-1;i>=0;i--) - { - if(state[x][i].getpiece()==null) - continue; - else if(state[x][i].getpiece().getcolor()==this.getcolor()) - break; - else - { - if ((state[x][i].getpiece() instanceof Rook) || (state[x][i].getpiece() instanceof Queen)) - return true; - else - break; - } - } - - //checking for attack from diagonal direction - int tempx=x+1,tempy=y-1; - while(tempx<8&&tempy>=0) - { - if(state[tempx][tempy].getpiece()==null) - { + + // Function to check if king is under threat + // It checks whether there is any piece of opposite color that can attack king + // for a given board state + public boolean isindanger(Cell state[][]) { + + // Checking for attack from left,right,up and down + for (int i = x + 1; i < 8; i++) { + if (state[i][y].getpiece() == null) + continue; + else if (state[i][y].getpiece().getcolor() == this.getcolor()) + break; + else { + if ((state[i][y].getpiece() instanceof Rook) || (state[i][y].getpiece() instanceof Queen)) + return true; + else + break; + } + } + for (int i = x - 1; i >= 0; i--) { + if (state[i][y].getpiece() == null) + continue; + else if (state[i][y].getpiece().getcolor() == this.getcolor()) + break; + else { + if ((state[i][y].getpiece() instanceof Rook) || (state[i][y].getpiece() instanceof Queen)) + return true; + else + break; + } + } + for (int i = y + 1; i < 8; i++) { + if (state[x][i].getpiece() == null) + continue; + else if (state[x][i].getpiece().getcolor() == this.getcolor()) + break; + else { + if ((state[x][i].getpiece() instanceof Rook) || (state[x][i].getpiece() instanceof Queen)) + return true; + else + break; + } + } + for (int i = y - 1; i >= 0; i--) { + if (state[x][i].getpiece() == null) + continue; + else if (state[x][i].getpiece().getcolor() == this.getcolor()) + break; + else { + if ((state[x][i].getpiece() instanceof Rook) || (state[x][i].getpiece() instanceof Queen)) + return true; + else + break; + } + } + + // checking for attack from diagonal direction + int tempx = x + 1, tempy = y - 1; + while (tempx < 8 && tempy >= 0) { + if (state[tempx][tempy].getpiece() == null) { tempx++; tempy--; - } - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + } else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { if (state[tempx][tempy].getpiece() instanceof Bishop || state[tempx][tempy].getpiece() instanceof Queen) - return true; - else - break; + return true; + else + break; } } - tempx=x-1;tempy=y+1; - while(tempx>=0&&tempy<8) - { - if(state[tempx][tempy].getpiece()==null) - { + tempx = x - 1; + tempy = y + 1; + while (tempx >= 0 && tempy < 8) { + if (state[tempx][tempy].getpiece() == null) { tempx--; tempy++; - } - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + } else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { if (state[tempx][tempy].getpiece() instanceof Bishop || state[tempx][tempy].getpiece() instanceof Queen) - return true; - else - break; + return true; + else + break; } } - tempx=x-1;tempy=y-1; - while(tempx>=0&&tempy>=0) - { - if(state[tempx][tempy].getpiece()==null) - { + tempx = x - 1; + tempy = y - 1; + while (tempx >= 0 && tempy >= 0) { + if (state[tempx][tempy].getpiece() == null) { tempx--; tempy--; - } - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + } else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { if (state[tempx][tempy].getpiece() instanceof Bishop || state[tempx][tempy].getpiece() instanceof Queen) - return true; - else - break; + return true; + else + break; } } - tempx=x+1;tempy=y+1; - while(tempx<8&&tempy<8) - { - if(state[tempx][tempy].getpiece()==null) - { + tempx = x + 1; + tempy = y + 1; + while (tempx < 8 && tempy < 8) { + if (state[tempx][tempy].getpiece() == null) { tempx++; tempy++; - } - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + } else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { if (state[tempx][tempy].getpiece() instanceof Bishop || state[tempx][tempy].getpiece() instanceof Queen) - return true; - else - break; + return true; + else + break; } } - - //Checking for attack from the Knight of opposite color - 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}; - for(int i=0;i<8;i++) - if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)) - if(state[posx[i]][posy[i]].getpiece()!=null && state[posx[i]][posy[i]].getpiece().getcolor()!=this.getcolor() && (state[posx[i]][posy[i]].getpiece() instanceof Knight)) - { + + // Checking for attack from the Knight of opposite color + 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 }; + for (int i = 0; i < 8; i++) + if ((posx[i] >= 0 && posx[i] < 8 && posy[i] >= 0 && posy[i] < 8)) + if (state[posx[i]][posy[i]].getpiece() != null + && state[posx[i]][posy[i]].getpiece().getcolor() != this.getcolor() + && (state[posx[i]][posy[i]].getpiece() instanceof Knight)) { return true; } - - - //Checking for attack from the Pawn of opposite color - int pox[]={x+1,x+1,x+1,x,x,x-1,x-1,x-1}; - int poy[]={y-1,y+1,y,y+1,y-1,y+1,y-1,y}; + + // Checking for attack from the Pawn of opposite color + int pox[] = { x + 1, x + 1, x + 1, x, x, x - 1, x - 1, x - 1 }; + int poy[] = { y - 1, y + 1, y, y + 1, y - 1, y + 1, y - 1, y }; { - for(int i=0;i<8;i++) - if((pox[i]>=0&&pox[i]<8&&poy[i]>=0&&poy[i]<8)) - if(state[pox[i]][poy[i]].getpiece()!=null && state[pox[i]][poy[i]].getpiece().getcolor()!=this.getcolor() && (state[pox[i]][poy[i]].getpiece() instanceof King)) - { + for (int i = 0; i < 8; i++) + if ((pox[i] >= 0 && pox[i] < 8 && poy[i] >= 0 && poy[i] < 8)) + if (state[pox[i]][poy[i]].getpiece() != null + && state[pox[i]][poy[i]].getpiece().getcolor() != this.getcolor() + && (state[pox[i]][poy[i]].getpiece() instanceof King)) { return true; } } - if(getcolor()==0) - { - if(x>0&&y>0&&state[x-1][y-1].getpiece()!=null&&state[x-1][y-1].getpiece().getcolor()==1&&(state[x-1][y-1].getpiece() instanceof Pawn)) + if (getcolor() == 0) { + if (x > 0 && y > 0 && state[x - 1][y - 1].getpiece() != null + && state[x - 1][y - 1].getpiece().getcolor() == 1 + && (state[x - 1][y - 1].getpiece() instanceof Pawn)) return true; - if(x>0&&y<7&&state[x-1][y+1].getpiece()!=null&&state[x-1][y+1].getpiece().getcolor()==1&&(state[x-1][y+1].getpiece() instanceof Pawn)) + if (x > 0 && y < 7 && state[x - 1][y + 1].getpiece() != null + && state[x - 1][y + 1].getpiece().getcolor() == 1 + && (state[x - 1][y + 1].getpiece() instanceof Pawn)) return true; - } - else - { - if(x<7&&y>0&&state[x+1][y-1].getpiece()!=null&&state[x+1][y-1].getpiece().getcolor()==0&&(state[x+1][y-1].getpiece() instanceof Pawn)) + } else { + if (x < 7 && y > 0 && state[x + 1][y - 1].getpiece() != null + && state[x + 1][y - 1].getpiece().getcolor() == 0 + && (state[x + 1][y - 1].getpiece() instanceof Pawn)) return true; - if(x<7&&y<7&&state[x+1][y+1].getpiece()!=null&&state[x+1][y+1].getpiece().getcolor()==0&&(state[x+1][y+1].getpiece() instanceof Pawn)) + if (x < 7 && y < 7 && state[x + 1][y + 1].getpiece() != null + && state[x + 1][y + 1].getpiece().getcolor() == 0 + && (state[x + 1][y + 1].getpiece() instanceof Pawn)) return true; } - return false; - } + return false; + } } \ No newline at end of file diff --git a/Chess/src/pieces/Knight.java b/Chess/src/pieces/Knight.java index 4721809..fce8996 100644 --- a/Chess/src/pieces/Knight.java +++ b/Chess/src/pieces/Knight.java @@ -6,31 +6,29 @@ /** * This is the Knight Class inherited from the Piece abstract class - * + * * */ -public class Knight extends Piece{ - - //Constructor - public Knight(String i,String p,int c) - { +public class Knight extends Piece { + + // Constructor + public Knight(String i, String p, int c) { setId(i); setPath(p); setColor(c); } - - //Move Function overridden - //There are at max 8 possible moves for a knight at any point of time. - //Knight moves only 2(1/2) steps - public ArrayList move(Cell state[][],int x,int y) - { + + // Move Function overridden + // There are at max 8 possible moves for a knight at any point of time. + // Knight moves only 2(1/2) steps + public ArrayList move(Cell state[][], int x, int y) { possiblemoves.clear(); - 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}; - for(int i=0;i<8;i++) - if((posx[i]>=0&&posx[i]<8&&posy[i]>=0&&posy[i]<8)) - if((state[posx[i]][posy[i]].getpiece()==null||state[posx[i]][posy[i]].getpiece().getcolor()!=this.getcolor())) - { + 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 }; + for (int i = 0; i < 8; i++) + if ((posx[i] >= 0 && posx[i] < 8 && posy[i] >= 0 && posy[i] < 8)) + if ((state[posx[i]][posy[i]].getpiece() == null + || state[posx[i]][posy[i]].getpiece().getcolor() != this.getcolor())) { possiblemoves.add(state[posx[i]][posy[i]]); } return possiblemoves; diff --git a/Chess/src/pieces/Pawn.java b/Chess/src/pieces/Pawn.java index 3dc5c9f..6348b66 100644 --- a/Chess/src/pieces/Pawn.java +++ b/Chess/src/pieces/Pawn.java @@ -8,59 +8,55 @@ * This is the Pawn Class inherited from the piece * */ -public class Pawn extends Piece{ - - //COnstructors - public Pawn(String i,String p,int c) - { +public class Pawn extends Piece { + + // COnstructors + public Pawn(String i, String p, int c) { setId(i); setPath(p); setColor(c); } - - //Move Function Overridden - public ArrayList move(Cell state[][],int x,int y) - { - //Pawn can move only one step except the first chance when it may move 2 steps - //It can move in a diagonal fashion only for attacking a piece of opposite color - //It cannot move backward or move forward to attact a piece - + + // Move Function Overridden + public ArrayList move(Cell state[][], int x, int y) { + // Pawn can move only one step except the first chance when it may move 2 steps + // It can move in a diagonal fashion only for attacking a piece of opposite + // color + // It cannot move backward or move forward to attact a piece + possiblemoves.clear(); - if(getcolor()==0) - { - if(x==0) + if (getcolor() == 0) { + if (x == 0) return possiblemoves; - if(state[x-1][y].getpiece()==null) - { - possiblemoves.add(state[x-1][y]); - if(x==6) - { - if(state[4][y].getpiece()==null) + if (state[x - 1][y].getpiece() == null) { + possiblemoves.add(state[x - 1][y]); + if (x == 6) { + if (state[4][y].getpiece() == null) possiblemoves.add(state[4][y]); } } - if((y>0)&&(state[x-1][y-1].getpiece()!=null)&&(state[x-1][y-1].getpiece().getcolor()!=this.getcolor())) - possiblemoves.add(state[x-1][y-1]); - if((y<7)&&(state[x-1][y+1].getpiece()!=null)&&(state[x-1][y+1].getpiece().getcolor()!=this.getcolor())) - possiblemoves.add(state[x-1][y+1]); - } - else - { - if(x==8) + if ((y > 0) && (state[x - 1][y - 1].getpiece() != null) + && (state[x - 1][y - 1].getpiece().getcolor() != this.getcolor())) + possiblemoves.add(state[x - 1][y - 1]); + if ((y < 7) && (state[x - 1][y + 1].getpiece() != null) + && (state[x - 1][y + 1].getpiece().getcolor() != this.getcolor())) + possiblemoves.add(state[x - 1][y + 1]); + } else { + if (x == 8) return possiblemoves; - if(state[x+1][y].getpiece()==null) - { - possiblemoves.add(state[x+1][y]); - if(x==1) - { - if(state[3][y].getpiece()==null) + if (state[x + 1][y].getpiece() == null) { + possiblemoves.add(state[x + 1][y]); + if (x == 1) { + if (state[3][y].getpiece() == null) possiblemoves.add(state[3][y]); } } - if((y>0)&&(state[x+1][y-1].getpiece()!=null)&&(state[x+1][y-1].getpiece().getcolor()!=this.getcolor())) - possiblemoves.add(state[x+1][y-1]); - if((y<7)&&(state[x+1][y+1].getpiece()!=null)&&(state[x+1][y+1].getpiece().getcolor()!=this.getcolor())) - possiblemoves.add(state[x+1][y+1]); + if ((y > 0) && (state[x + 1][y - 1].getpiece() != null) + && (state[x + 1][y - 1].getpiece().getcolor() != this.getcolor())) + possiblemoves.add(state[x + 1][y - 1]); + if ((y < 7) && (state[x + 1][y + 1].getpiece() != null) + && (state[x + 1][y + 1].getpiece().getcolor() != this.getcolor())) + possiblemoves.add(state[x + 1][y + 1]); } return possiblemoves; } diff --git a/Chess/src/pieces/Piece.java b/Chess/src/pieces/Piece.java index 7e2f4d2..3860136 100644 --- a/Chess/src/pieces/Piece.java +++ b/Chess/src/pieces/Piece.java @@ -4,61 +4,56 @@ import chess.Cell; - /** - * This is the Piece Class. It is an abstract class from which all the actual pieces are inherited. - * It defines all the function common to all the pieces - * The move() function an abstract function that has to be overridden in all the inherited class - * It implements Cloneable interface as a copy of the piece is required very often + * This is the Piece Class. It is an abstract class from which all the actual + * pieces are inherited. It defines all the function common to all the pieces + * The move() function an abstract function that has to be overridden in all the + * inherited class It implements Cloneable interface as a copy of the piece is + * required very often */ -public abstract class Piece implements Cloneable{ +public abstract class Piece implements Cloneable { - //Member Variables + // Member Variables private int color; - private String id=null; + private String id = null; private String path; - protected ArrayList possiblemoves = new ArrayList(); //Protected (access from child classes) - public abstract ArrayList move(Cell pos[][],int x,int y); //Abstract Function. Must be overridden - - //Id Setter - public void setId(String id) - { - this.id=id; + protected ArrayList possiblemoves = new ArrayList(); // Protected (access from child classes) + + public abstract ArrayList move(Cell pos[][], int x, int y); // Abstract Function. Must be overridden + + // Id Setter + public void setId(String id) { + this.id = id; } - - //Path Setter - public void setPath(String path) - { - this.path=path; + + // Path Setter + public void setPath(String path) { + this.path = path; } - - //Color Setter - public void setColor(int c) - { - this.color=c; + + // Color Setter + public void setColor(int c) { + this.color = c; } - - //Path getter - public String getPath() - { + + // Path getter + public String getPath() { return path; } - - //Id getter - public String getId() - { + + // Id getter + public String getId() { return id; } - - //Color Getter - public int getcolor() - { + + // Color Getter + public int getcolor() { return this.color; } - - //Function to return the a "shallow" copy of the object. The copy has exact same variable value but different reference - public Piece getcopy() throws CloneNotSupportedException - { + + // Function to return the a "shallow" copy of the object. The copy has exact + // same variable value but different reference + public Piece getcopy() throws CloneNotSupportedException { return (Piece) this.clone(); } } \ No newline at end of file diff --git a/Chess/src/pieces/Queen.java b/Chess/src/pieces/Queen.java index 28a5821..cee668c 100644 --- a/Chess/src/pieces/Queen.java +++ b/Chess/src/pieces/Queen.java @@ -8,141 +8,126 @@ * This is the Queen Class inherited from the abstract Piece class * */ -public class Queen extends Piece{ - - //Constructors - public Queen(String i,String p,int c) - { +public class Queen extends Piece { + + // Constructors + public Queen(String i, String p, int c) { setId(i); setPath(p); setColor(c); } - - //Move Function Defined - public ArrayList move(Cell state[][],int x,int y) - { - //Queen has most number of possible moves - //Queen can move any number of steps in all 8 direction - //The possible moves of queen is a combination of Rook and Bishop + + // Move Function Defined + public ArrayList move(Cell state[][], int x, int y) { + // Queen has most number of possible moves + // Queen can move any number of steps in all 8 direction + // The possible moves of queen is a combination of Rook and Bishop possiblemoves.clear(); - - //Checking possible moves in vertical direction - int tempx=x-1; - while(tempx>=0) - { - if(state[tempx][y].getpiece()==null) + + // Checking possible moves in vertical direction + int tempx = x - 1; + while (tempx >= 0) { + if (state[tempx][y].getpiece() == null) possiblemoves.add(state[tempx][y]); - else if(state[tempx][y].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][y].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][y]); break; } tempx--; } - - tempx=x+1; - while(tempx<8) - { - if(state[tempx][y].getpiece()==null) + + tempx = x + 1; + while (tempx < 8) { + if (state[tempx][y].getpiece() == null) possiblemoves.add(state[tempx][y]); - else if(state[tempx][y].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][y].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][y]); break; } tempx++; } - - - //Checking possible moves in horizontal Direction - int tempy=y-1; - while(tempy>=0) - { - if(state[x][tempy].getpiece()==null) + + // Checking possible moves in horizontal Direction + int tempy = y - 1; + while (tempy >= 0) { + if (state[x][tempy].getpiece() == null) possiblemoves.add(state[x][tempy]); - else if(state[x][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[x][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[x][tempy]); break; } tempy--; } - tempy=y+1; - while(tempy<8) - { - if(state[x][tempy].getpiece()==null) + tempy = y + 1; + while (tempy < 8) { + if (state[x][tempy].getpiece() == null) possiblemoves.add(state[x][tempy]); - else if(state[x][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[x][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[x][tempy]); break; } tempy++; } - - //Checking for possible moves in diagonal direction - tempx=x+1;tempy=y-1; - while(tempx<8&&tempy>=0) - { - if(state[tempx][tempy].getpiece()==null) + + // Checking for possible moves in diagonal direction + tempx = x + 1; + tempy = y - 1; + while (tempx < 8 && tempy >= 0) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } tempx++; tempy--; } - tempx=x-1;tempy=y+1; - while(tempx>=0&&tempy<8) - { - if(state[tempx][tempy].getpiece()==null) + tempx = x - 1; + tempy = y + 1; + while (tempx >= 0 && tempy < 8) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } tempx--; tempy++; } - tempx=x-1;tempy=y-1; - while(tempx>=0&&tempy>=0) - { - if(state[tempx][tempy].getpiece()==null) + tempx = x - 1; + tempy = y - 1; + while (tempx >= 0 && tempy >= 0) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } tempx--; tempy--; } - tempx=x+1;tempy=y+1; - while(tempx<8&&tempy<8) - { - if(state[tempx][tempy].getpiece()==null) + tempx = x + 1; + tempy = y + 1; + while (tempx < 8 && tempy < 8) { + if (state[tempx][tempy].getpiece() == null) possiblemoves.add(state[tempx][tempy]); - else if(state[tempx][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][tempy]); break; } diff --git a/Chess/src/pieces/Rook.java b/Chess/src/pieces/Rook.java index 1abad27..01431b0 100644 --- a/Chess/src/pieces/Rook.java +++ b/Chess/src/pieces/Rook.java @@ -8,72 +8,62 @@ * This is the Rook class inherited from abstract Piece class * */ -public class Rook extends Piece{ - - //Constructor - public Rook(String i,String p,int c) - { +public class Rook extends Piece { + + // Constructor + public Rook(String i, String p, int c) { setId(i); setPath(p); setColor(c); } - - //Move function defined - public ArrayList move(Cell state[][],int x,int y) - { - //Rook can move only horizontally or vertically + + // Move function defined + public ArrayList move(Cell state[][], int x, int y) { + // Rook can move only horizontally or vertically possiblemoves.clear(); - int tempx=x-1; - while(tempx>=0) - { - if(state[tempx][y].getpiece()==null) + int tempx = x - 1; + while (tempx >= 0) { + if (state[tempx][y].getpiece() == null) possiblemoves.add(state[tempx][y]); - else if(state[tempx][y].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][y].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][y]); break; } tempx--; } - tempx=x+1; - while(tempx<8) - { - if(state[tempx][y].getpiece()==null) + tempx = x + 1; + while (tempx < 8) { + if (state[tempx][y].getpiece() == null) possiblemoves.add(state[tempx][y]); - else if(state[tempx][y].getpiece().getcolor()==this.getcolor()) + else if (state[tempx][y].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[tempx][y]); break; } tempx++; } - int tempy=y-1; - while(tempy>=0) - { - if(state[x][tempy].getpiece()==null) + int tempy = y - 1; + while (tempy >= 0) { + if (state[x][tempy].getpiece() == null) possiblemoves.add(state[x][tempy]); - else if(state[x][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[x][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[x][tempy]); break; } tempy--; } - tempy=y+1; - while(tempy<8) - { - if(state[x][tempy].getpiece()==null) + tempy = y + 1; + while (tempy < 8) { + if (state[x][tempy].getpiece() == null) possiblemoves.add(state[x][tempy]); - else if(state[x][tempy].getpiece().getcolor()==this.getcolor()) + else if (state[x][tempy].getpiece().getcolor() == this.getcolor()) break; - else - { + else { possiblemoves.add(state[x][tempy]); break; }