diff --git a/Chess/Black_Bishop.png b/Chess/bin/chess/Black_Bishop.png similarity index 100% rename from Chess/Black_Bishop.png rename to Chess/bin/chess/Black_Bishop.png diff --git a/Chess/Black_King.png b/Chess/bin/chess/Black_King.png similarity index 100% rename from Chess/Black_King.png rename to Chess/bin/chess/Black_King.png diff --git a/Chess/Black_Knight.png b/Chess/bin/chess/Black_Knight.png similarity index 100% rename from Chess/Black_Knight.png rename to Chess/bin/chess/Black_Knight.png diff --git a/Chess/Black_Pawn.png b/Chess/bin/chess/Black_Pawn.png similarity index 100% rename from Chess/Black_Pawn.png rename to Chess/bin/chess/Black_Pawn.png diff --git a/Chess/Black_Queen.png b/Chess/bin/chess/Black_Queen.png similarity index 100% rename from Chess/Black_Queen.png rename to Chess/bin/chess/Black_Queen.png diff --git a/Chess/Black_Rook.png b/Chess/bin/chess/Black_Rook.png similarity index 100% rename from Chess/Black_Rook.png rename to Chess/bin/chess/Black_Rook.png diff --git a/Chess/bin/chess/Cell.class b/Chess/bin/chess/Cell.class index 9654777..a5f7d7b 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 new file mode 100644 index 0000000..8fff795 Binary files /dev/null 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 new file mode 100644 index 0000000..efd188f Binary files /dev/null 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 new file mode 100644 index 0000000..bcb4f75 Binary files /dev/null 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 new file mode 100644 index 0000000..78a5a7d Binary files /dev/null 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 new file mode 100644 index 0000000..ebf3fdc Binary files /dev/null and b/Chess/bin/chess/Main$TimeChange.class differ diff --git a/Chess/bin/chess/Main.class b/Chess/bin/chess/Main.class index 2977fb2..a2e2043 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 new file mode 100644 index 0000000..34ec6d8 Binary files /dev/null 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 0026895..a8ec2a5 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 72ac279..2411210 100644 Binary files a/Chess/bin/chess/Time.class and b/Chess/bin/chess/Time.class differ diff --git a/Chess/White_Bishop.png b/Chess/bin/chess/White_Bishop.png similarity index 100% rename from Chess/White_Bishop.png rename to Chess/bin/chess/White_Bishop.png diff --git a/Chess/White_King.png b/Chess/bin/chess/White_King.png similarity index 100% rename from Chess/White_King.png rename to Chess/bin/chess/White_King.png diff --git a/Chess/White_Knight.png b/Chess/bin/chess/White_Knight.png similarity index 100% rename from Chess/White_Knight.png rename to Chess/bin/chess/White_Knight.png diff --git a/Chess/White_Pawn.png b/Chess/bin/chess/White_Pawn.png similarity index 100% rename from Chess/White_Pawn.png rename to Chess/bin/chess/White_Pawn.png diff --git a/Chess/White_Queen.png b/Chess/bin/chess/White_Queen.png similarity index 100% rename from Chess/White_Queen.png rename to Chess/bin/chess/White_Queen.png diff --git a/Chess/White_Rook.png b/Chess/bin/chess/White_Rook.png similarity index 100% rename from Chess/White_Rook.png rename to Chess/bin/chess/White_Rook.png diff --git a/Chess/bin/chess/clash.jpg b/Chess/bin/chess/clash.jpg new file mode 100644 index 0000000..2d61506 Binary files /dev/null and b/Chess/bin/chess/clash.jpg differ diff --git a/Chess/bin/chess/icon.png b/Chess/bin/chess/icon.png new file mode 100644 index 0000000..2ae8583 Binary files /dev/null and b/Chess/bin/chess/icon.png differ diff --git a/Chess/bin/pieces/Bishop.class b/Chess/bin/pieces/Bishop.class index f87bf37..786ae5f 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..5f41534 100644 Binary files a/Chess/bin/pieces/King.class and b/Chess/bin/pieces/King.class differ diff --git a/Chess/bin/pieces/Pawn.class b/Chess/bin/pieces/Pawn.class index a7eca22..7f4dfb9 100644 Binary files a/Chess/bin/pieces/Pawn.class and b/Chess/bin/pieces/Pawn.class differ diff --git a/Chess/bin/statistics/Player.class b/Chess/bin/statistics/Player.class deleted file mode 100644 index c7fa5e3..0000000 Binary files a/Chess/bin/statistics/Player.class and /dev/null differ diff --git a/Chess/src/chess/Black_Bishop.png b/Chess/src/chess/Black_Bishop.png new file mode 100644 index 0000000..453cb32 Binary files /dev/null and b/Chess/src/chess/Black_Bishop.png differ diff --git a/Chess/src/chess/Black_King.png b/Chess/src/chess/Black_King.png new file mode 100644 index 0000000..225f869 Binary files /dev/null and b/Chess/src/chess/Black_King.png differ diff --git a/Chess/src/chess/Black_Knight.png b/Chess/src/chess/Black_Knight.png new file mode 100644 index 0000000..8e3d04e Binary files /dev/null and b/Chess/src/chess/Black_Knight.png differ diff --git a/Chess/src/chess/Black_Pawn.png b/Chess/src/chess/Black_Pawn.png new file mode 100644 index 0000000..c432d38 Binary files /dev/null and b/Chess/src/chess/Black_Pawn.png differ diff --git a/Chess/src/chess/Black_Queen.png b/Chess/src/chess/Black_Queen.png new file mode 100644 index 0000000..0d94a1c Binary files /dev/null and b/Chess/src/chess/Black_Queen.png differ diff --git a/Chess/src/chess/Black_Rook.png b/Chess/src/chess/Black_Rook.png new file mode 100644 index 0000000..b9748e8 Binary files /dev/null and b/Chess/src/chess/Black_Rook.png differ diff --git a/Chess/src/chess/Cell.java b/Chess/src/chess/Cell.java index 531110d..9a1ccc6 100644 --- a/Chess/src/chess/Cell.java +++ b/Chess/src/chess/Cell.java @@ -60,23 +60,15 @@ public Cell(Cell cell) throws CloneNotSupportedException public void setPiece(Piece p) //Function to inflate a cell with a piece { piece=p; - ImageIcon img=new javax.swing.ImageIcon(p.getPath()); + 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 { - if (piece instanceof King) - { piece=null; this.remove(content); - } - else - { - piece=null; - this.remove(content); - } } diff --git a/Chess/src/chess/Main.java b/Chess/src/chess/Main.java index 53bbeb0..b9fb856 100644 --- a/Chess/src/chess/Main.java +++ b/Chess/src/chess/Main.java @@ -1,11 +1,17 @@ package chess; +import javax.imageio.ImageIO; import javax.swing.*; +import javax.swing.border.TitledBorder; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import pieces.*; -import statistics.Player; import java.awt.*; import java.awt.event.*; +import java.awt.image.BufferedImage; +import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.ListIterator; /** @@ -21,12 +27,13 @@ * */ -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=1000; + 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; @@ -34,19 +41,36 @@ public class Main extends JFrame implements MouseListener{ private static Queen wq,bq; private static King wk,bk; private Cell c,previous; - private Player WhitePlayer; - private Player BlackPlayer; 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 controlPanel,score; + 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; + private JLabel label,mov; + private static JLabel CHNC; private Time timer; public static Main Mainboard; - private static ArrayList playerlist; - + 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; + static String move="White"; + private Player tempPlayer; + private JScrollPane wscroll,bscroll; + private String[] WNames={},BNames={}; + private JSlider timeSlider=new JSlider(); + private BufferedImage image; + private Button start,wselect,bselect,WNewPlayer,BNewPlayer; + public static int timeRemaining=60; public static void main(String[] args){ //variable initialization @@ -80,16 +104,9 @@ public static void main(String[] args){ Mainboard.setResizable(false); } - //Constructor - private Main() - { + public void createBoard(){ Cell cell; - Piece P; - - //Fetching Player Information - playerlist = WhitePlayer.fetch_players(); - - //Defining the Contents of Board (Left Side of Main GUI) + pieces.Piece P; boardState=new Cell[8][8]; for(int i=0;i<8;i++) for(int j=0;j<8;j++) @@ -136,45 +153,148 @@ else if(i==6) board.add(cell); boardState[i][j]=cell; } - board.setBorder(BorderFactory.createLoweredBevelBorder()); - board.setMinimumSize(new Dimension(700,700)); - //Defining Properties of Control Panel(Right Side of the Main GUI) - score=new JPanel(); - score.setBorder(BorderFactory.createTitledBorder("Scores:")); - score.setLayout(new GridLayout(2,2)); - score.add(new JLabel("Player 1:")); - score.add(new JLabel("Player 2:")); - label = new JLabel("Time Starts now", JLabel.CENTER); - timer = new Time(label); - timer.start(); - - controlPanel=new JPanel(); - controlPanel.setLayout(new BorderLayout()); - controlPanel.add(score,BorderLayout.NORTH); - controlPanel.add(label,BorderLayout.CENTER); - controlPanel.add(new JButton("Back"),BorderLayout.SOUTH); - controlPanel.setMinimumSize(new Dimension(200,700)); + } + + + //Constructor + private Main() + { + createBoard(); + ImageIcon img = new ImageIcon(this.getClass().getResource("icon.png")); + this.setIconImage(img.getImage()); + + //Time Slider Details + timeSlider.setMinimum(1); + timeSlider.setMaximum(15); + timeSlider.setValue(1); + timeSlider.setMajorTickSpacing(2); + timeSlider.setPaintLabels(true); + timeSlider.setPaintTicks(true); + timeSlider.addChangeListener(new TimeChange()); - //Defining the Main Content Pane - Container content=getContentPane(); + //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()) + Bnames.add(bitr.next().name()); + WNames=Wnames.toArray(WNames); + BNames=Bnames.toArray(BNames); + + + board.setBorder(BorderFactory.createLoweredBevelBorder()); + + content=getContentPane(); setSize(Width,Height); setTitle("Chess"); - split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,board, controlPanel); content.setBackground(Color.black); - content.setLayout(new BorderLayout()); - content.add(split); - setDefaultCloseOperation(EXIT_ON_CLOSE); - setAlwaysOnTop(true); + controlPanel=new JPanel(); + + 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); + wcombopanel.setLayout(new FlowLayout()); + bcombopanel.setLayout(new FlowLayout()); + 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.addActionListener(new Handler(0)); + BNewPlayer.addActionListener(new Handler(1)); + wcombopanel.add(wscroll); + wcombopanel.add(wselect); + wcombopanel.add(WNewPlayer); + bcombopanel.add(bscroll); + bcombopanel.add(bselect); + bcombopanel.add(BNewPlayer); + 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); + controlPanel.add(WhitePlayer); + controlPanel.add(BlackPlayer); + + showPlayer=new JPanel(new FlowLayout()); + showPlayer.add(timeSlider); + 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)); + label = new JLabel("Time Starts now", JLabel.CENTER); + label.setFont(new Font("SERIF", Font.BOLD, 30)); + timer=new Time(label); + 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"); + } + + 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); + setDefaultCloseOperation(EXIT_ON_CLOSE); } - // A function to change the chance ffrom 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()) { - System.out.println("Game End!!"); + chance^=1; + gameend(); } if(destinationlist.isEmpty()==false) cleandestinations(destinationlist); @@ -182,8 +302,18 @@ public void changechance() previous.deselect(); previous=null; chance^=1; - timer.reset(); - timer.start(); + if(!end) + { + timer.reset(); + timer.start(); + showPlayer.remove(CHNC); + if(Main.move=="White") + Main.move="Black"; + else + Main.move="White"; + CHNC.setText(Main.move); + showPlayer.add(CHNC); + } } //A function to retrieve the Black King or White King @@ -323,9 +453,49 @@ public boolean checkmate(int color) } + @SuppressWarnings("deprecation") + private void gameend() + { + if(chance==0) + { White.updateGamesWon(); + White.Update_Player(); + winner=White.name(); + } + else + { + Black.updateGamesWon(); + Black.Update_Player(); + winner=Black.name(); + } + previous.removePiece(); + JOptionPane.showMessageDialog(board,"Checkmate!!!\n"+winner+" wins"); + timer.stop(); + 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); + White=null; + Black=null; + board.removeAll(); + createBoard(); + WNewPlayer.enable(); + BNewPlayer.enable(); + wselect.enable(); + bselect.enable(); + end=true; + } + //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 + @Override public void mouseClicked(MouseEvent arg0){ // TODO Auto-generated method stub c=(Cell)arg0.getSource(); @@ -376,7 +546,7 @@ else if(c.getpiece()==null||previous.getpiece().getcolor()!=c.getpiece().getcolo boardState[getKing(chance^1).getx()][getKing(chance^1).gety()].setcheck(); if (checkmate(getKing(chance^1).getcolor())) { - System.out.println("Game End!!"); + gameend(); } } if(getKing(chance).isindanger(boardState)==false) @@ -387,6 +557,11 @@ else if(c.getpiece()==null||previous.getpiece().getcolor()!=c.getpiece().getcolo ((King)c.getpiece()).sety(c.y); } changechance(); + if(!end) + { + timer.reset(); + timer.start(); + } } if(previous!=null) { @@ -440,4 +615,170 @@ public void mousePressed(MouseEvent arg0) { public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } + + + 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); + chance=0; + 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); + label.setText("Time Starts Now"); + displayTime.add(label); + timer.reset(); + timer.start(); + end=false; + } + } + + class TimeChange implements ChangeListener + { + @Override + public void stateChanged(ChangeEvent arg0) + { + timeRemaining=timeSlider.getValue()*60; + } + } + + + class SelectHandler implements ActionListener + { + private int color; + + 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 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;} + } + + 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; + } + 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/statistics/Player.java b/Chess/src/chess/Player.java similarity index 51% rename from Chess/src/statistics/Player.java rename to Chess/src/chess/Player.java index 2ad0770..9f6ecb8 100644 --- a/Chess/src/statistics/Player.java +++ b/Chess/src/chess/Player.java @@ -1,5 +1,6 @@ -package statistics; +package chess; +import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -22,16 +23,15 @@ public class Player implements Serializable{ private static final long serialVersionUID = 1L; - private String firstname; - private String lastname; + private String name; private Integer gamesplayed; private Integer gameswon; //Constructor - Player(String fname,String lname) + public Player(String name) { - this.firstname = new String(fname); - this.lastname = new String(lname); + this.name = name.trim(); + //this.lname = lname.trim(); gamesplayed = new Integer(0); gameswon = new Integer(0); } @@ -39,7 +39,7 @@ public class Player implements Serializable{ //Name Getter public String name() { - return new String(firstname + " " +lastname); + return name; } //Returns the number of games played @@ -60,16 +60,40 @@ public Integer winpercent() return new Integer((gameswon*100)/gamesplayed); } - public ArrayList fetch_players() //Function to fetch the list of the players + //Increments the number of games played + public void updateGamesPlayed() { + gamesplayed++; + } + + //Increments the number of games won + public void updateGamesWon() + { + gameswon++; + } + + + public static ArrayList fetch_players() //Function to fetch the list of the players + { + Player tempplayer; ObjectInputStream input = null; ArrayList players = new ArrayList(); try { - input = new ObjectInputStream(new FileInputStream("chessgamedata.dat")); - while(input.available()>0) - players.add((Player)input.readObject()); - input.close(); + File infile = new File(System.getProperty("user.dir")+ File.separator + "chessgamedata.dat"); + input = new ObjectInputStream(new FileInputStream(infile)); + try + { + while(true) + { + tempplayer = (Player) input.readObject(); + players.add(tempplayer); + } + } + catch(EOFException e) + { + input.close(); + } } catch (FileNotFoundException e) { @@ -86,40 +110,69 @@ public ArrayList fetch_players() //Function to fetch the list of { e.printStackTrace(); JOptionPane.showMessageDialog(null, "Game Data File Corrupted !! Click Ok to Continue Builing New File"); + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); } return players; } - private 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 = new File("gamefiles/chessgamedata.dat"); - File outputfile = new File("gamefiles/tempfile"); + 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(inputfile.exists()==false) - inputfile.createNewFile(); if(outputfile.exists()==false) outputfile.createNewFile(); - input = new ObjectInputStream(new FileInputStream(inputfile)); - output = new ObjectOutputStream(new FileOutputStream(outputfile)); - playerdonotexist=true; - while(input.available()>0) + if(inputfile.exists()==false) { - temp_player = (Player)input.readObject(); - if (temp_player.name().equals(name())) - { + output = new ObjectOutputStream(new java.io.FileOutputStream(outputfile,true)); output.writeObject(this); - playerdonotexist = false; + } + 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; + } + else + output.writeObject(temp_player); } - else - output.writeObject(temp_player); + } + catch(EOFException e){ + input.close(); + } + if(playerdonotexist) + output.writeObject(this); } - 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) + System.out.println("File Renameing Unsuccessful"); } catch (FileNotFoundException e) { @@ -135,10 +188,9 @@ private void Update_Player() //Function to update the statistics of a e.printStackTrace(); JOptionPane.showMessageDialog(null, "Game Data File Corrupted !! Click Ok to Continue Builing New File"); } - finally + catch (Exception e) { - try {input.close();output.close();} - catch (IOException e) {e.printStackTrace();} + } } } diff --git a/Chess/src/chess/Time.java b/Chess/src/chess/Time.java index 1f359d3..5a55876 100644 --- a/Chess/src/chess/Time.java +++ b/Chess/src/chess/Time.java @@ -13,16 +13,17 @@ * It uses a Timer Class * */ + public class Time { private JLabel label; Timer countdownTimer; - int timeRemaining = 61; - - public Time(JLabel passedLabel) { + int Timerem; + public Time(JLabel passedLabel) + { countdownTimer = new Timer(1000, new CountdownTimerListener()); this.label = passedLabel; - + Timerem=Main.timeRemaining; } //A function that starts the timer @@ -30,31 +31,37 @@ public void start() { countdownTimer.start(); } + public void stop() + { + countdownTimer.stop(); + } //A function that resets the timer public void reset() { - timeRemaining=61; + Timerem=Main.timeRemaining; } //A function that is called after every second. It updates the timer and takes other necessary actions class CountdownTimerListener implements ActionListener { - @Override - public void actionPerformed(ActionEvent arg0) { - // TODO Auto-generated method stub - int min,sec; - if (--timeRemaining > 0) - { - min=timeRemaining/60; - sec=timeRemaining%60; - label.setText(String.valueOf(min)+":"+(sec>=10?String.valueOf(sec):"0"+String.valueOf(sec))); - } - else - { - label.setText("Time's up!"); - Main.Mainboard.changechance(); - } - } - } - } \ No newline at end of file + 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/chess/White_Bishop.png b/Chess/src/chess/White_Bishop.png new file mode 100644 index 0000000..26dae01 Binary files /dev/null and b/Chess/src/chess/White_Bishop.png differ diff --git a/Chess/src/chess/White_King.png b/Chess/src/chess/White_King.png new file mode 100644 index 0000000..d734164 Binary files /dev/null and b/Chess/src/chess/White_King.png differ diff --git a/Chess/src/chess/White_Knight.png b/Chess/src/chess/White_Knight.png new file mode 100644 index 0000000..2d716b1 Binary files /dev/null and b/Chess/src/chess/White_Knight.png differ diff --git a/Chess/src/chess/White_Pawn.png b/Chess/src/chess/White_Pawn.png new file mode 100644 index 0000000..e98fae2 Binary files /dev/null and b/Chess/src/chess/White_Pawn.png differ diff --git a/Chess/src/chess/White_Queen.png b/Chess/src/chess/White_Queen.png new file mode 100644 index 0000000..a4fe68c Binary files /dev/null and b/Chess/src/chess/White_Queen.png differ diff --git a/Chess/src/chess/White_Rook.png b/Chess/src/chess/White_Rook.png new file mode 100644 index 0000000..a805de4 Binary files /dev/null and b/Chess/src/chess/White_Rook.png differ diff --git a/Chess/src/chess/clash.jpg b/Chess/src/chess/clash.jpg new file mode 100644 index 0000000..2d61506 Binary files /dev/null and b/Chess/src/chess/clash.jpg differ diff --git a/Chess/src/chess/icon.png b/Chess/src/chess/icon.png new file mode 100644 index 0000000..2ae8583 Binary files /dev/null and b/Chess/src/chess/icon.png differ diff --git a/Chess/src/pieces/Pawn.java b/Chess/src/pieces/Pawn.java index 3dc5c9f..02feb04 100644 --- a/Chess/src/pieces/Pawn.java +++ b/Chess/src/pieces/Pawn.java @@ -10,7 +10,7 @@ */ public class Pawn extends Piece{ - //COnstructors + //Constructors public Pawn(String i,String p,int c) { setId(i); diff --git a/Executable Files/Chess.jar b/Executable Files/Chess.jar new file mode 100644 index 0000000..b4d96ce Binary files /dev/null and b/Executable Files/Chess.jar differ diff --git a/Executable Files/chessgamedata.dat b/Executable Files/chessgamedata.dat new file mode 100644 index 0000000..b763a01 Binary files /dev/null and b/Executable Files/chessgamedata.dat differ diff --git a/LICENSE b/LICENSE index f795f32..82fde85 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013 Ashish Kedia +Copyright (c) 2013 Adarsh Mohata Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/Project Documentation.docx b/Project Documentation.docx index 0f94bb5..5c5c03d 100644 Binary files a/Project Documentation.docx and b/Project Documentation.docx differ