Checkers game

  1. package checkers;
  2.  
  3. import jess.*;
  4.  
  5. import java.util.LinkedList;
  6. import java.util.ListIterator;
  7. import java.util.StringTokenizer;
  8. import java.io.InputStreamReader;
  9. import java.io.BufferedReader;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import javax.swing.*;
  14. import java.awt.*;
  15. import java.awt.event.*;
  16.  
  17.         /**
  18.          * This class is the entry point of the program (main () method) and stands for the game class. The class constructs the main dialogs and handle the user choice and the displays some feedback information inside a message box.
  19.          */
  20. public class Checker extends JDialog {
  21.     static Rete r = new Rete();
  22.     boolean isOver = false;
  23.     private FootPanel footPanel;
  24.     boolean turn;
  25.     static Player player[] = new Player[2];
  26.     static private Checker checker = new Checker();
  27.    
  28.         /**
  29.          * This method is calling the methods in the other class to initilize the game and it runs the main module of Jess.
  30.          */
  31.     public Checker() {
  32.         super();
  33.         String title = "Checker";
  34.         String laf = UIManager.getSystemLookAndFeelClassName();
  35.         try {
  36.             UIManager.setLookAndFeel(laf);
  37.         } catch (UnsupportedLookAndFeelException exc) {
  38.             System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
  39.         } catch (Exception exc) {
  40.             System.err.println("Error loading " + laf + ": " + exc);
  41.         }
  42.        
  43.         addWindowListener(new WindowAdapter() {
  44.             public void windowClosing(WindowEvent evt) {
  45.                 System.exit(0);
  46.             }
  47.         });
  48.         setResizable(false);
  49.         setTitle(title);
  50.        
  51.         JLabel titleLabel = new JLabel();
  52.         titleLabel.setFont(new Font("MS Sans Serif", 1, 18));
  53.         titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
  54.         titleLabel.setText(title);
  55.         footPanel = new FootPanel();
  56.         getContentPane().add(titleLabel, BorderLayout.NORTH);
  57.         getContentPane().add(Grid.getInstance(), BorderLayout.CENTER);
  58.         getContentPane().add(footPanel, BorderLayout.SOUTH);
  59.         pack();
  60.         try {
  61.             r.executeCommand("(batch main.clp)");
  62.         } catch (JessException e) {System.err.println(e);}
  63.     }
  64.  
  65.      /**
  66.       * This method is used get the instance of the Checker's object.
  67.       * @return the information about the checker.
  68.       */
  69.  
  70.     static public Checker getInstance() {
  71.         return checker;
  72.     }
  73.        
  74.         /**
  75.          * This is the Main Method used to start and initilize the game
  76.          * It ask for the game options to be played (Human vs Human, Jess vs Jess...
  77.          * @param argv not used
  78.          */    
  79.  
  80.     static public void main(String[] argv) {  
  81.         {Grid grid;}
  82.         Object[] possibleValues = { "Human vs Human", "Human vs Jess", "Jess vs Jess" };
  83.         String selectedValue = (String) JOptionPane.showInputDialog(null,
  84.         "Choose a mode", "Input",
  85.         JOptionPane.INFORMATION_MESSAGE, null,
  86.         possibleValues, possibleValues[0]);
  87.         if (selectedValue == null)
  88.             System.exit(0);
  89.        
  90.         if (selectedValue.equals("Human vs Human")) {
  91.             player[0] = new Human ("Human", Piece.WHITE);  
  92.             player[1] = new Human("Human", Piece.BLACK);
  93.         }
  94.         else if (selectedValue.equals("Human vs Jess")) {
  95.             player[0] = new Human ("Human", Piece.WHITE);  
  96.             player[1] = new Jess("Jess", Piece.BLACK);
  97.         }
  98.         else if (selectedValue.equals("Jess vs Jess")) {
  99.             player[0] = new Jess ("Jess", Piece.WHITE);  
  100.             player[1] = new Jess("Jess", Piece.BLACK);
  101.         }
  102.         else {alert ("Please, choose a mode");}
  103.         player[0].setNext(player[1]);
  104.         player[1].setNext(player[0]);
  105.        
  106.         checker.show();
  107.        
  108.         checker.init();
  109.        
  110.         player[0].hasTurn();
  111.     }
  112.      /**
  113.       * This method is to print out the exception if any error should happen in the program.
  114.       * Prints out the error.
  115.       * @param exception The exception containing the message to be printed
  116.       */
  117.  
  118.     static public void alert(Exception exception) {
  119.         JOptionPane.showMessageDialog(null, exception.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
  120.         exception.printStackTrace();
  121.     }
  122.     /**
  123.      * This method is used to print out the information about the error
  124.      * Prints out the information about the error.
  125.      * @param info Informasion to be printed out
  126.      */
  127.     static public void alert(String info) {
  128.         JOptionPane.showMessageDialog(null, info, "Information", JOptionPane.INFORMATION_MESSAGE);
  129.     }
  130.    
  131.         /**
  132.          * This Method is used print out a message in the textbox of graphical user interface.
  133.          * @param msg message to be append to the textArea
  134.          */
  135.  
  136.     public void displayMessage(String msg) {
  137.         footPanel.resultTextArea.append(msg + "\n");
  138.     }
  139.  
  140.         /**
  141.          * This method get information about the board and initilize a new board with this information
  142.          */
  143.    
  144.     public void init() {
  145.         Board board = Board.getInstance();
  146.         board.init();
  147.     }
  148.    
  149.         /**
  150.          * This method is invoked when a player win the game.
  151.          * @param color The color of the winer
  152.          */
  153.  
  154.     public void win(boolean color) {
  155.         if (player[0].color == color)
  156.             alert(player[0] + " win the game.");
  157.         else
  158.             alert(player[1] + " win the game.");
  159.         isOver = true;
  160.     }
  161.         /**
  162.          * This method is used to find out if the game is over
  163.          * @return true if the game is over;
  164.          */    
  165.  
  166.     public boolean isOver() {
  167.         return isOver;
  168.     }
  169.         /**
  170.          * This method is used to tell who's turn it is.
  171.          * @return true if it is white else false if it is black
  172.          */
  173.  
  174.     public boolean getTurn(){
  175.         return turn;
  176.     }
  177.    
  178.         /**
  179.          * This Class is extendsion of JPanel which is used for the graphial part
  180.          * This class setup the graphical user interface.
  181.          */
  182.  
  183.  
  184.     private class FootPanel extends JPanel {
  185.        
  186.         /** Creates new form footPanel */
  187.         public FootPanel() {
  188.             actionLabel = new JLabel();
  189.             jScrollPane1 = new JScrollPane();
  190.             resultTextArea = new JTextArea();
  191.            
  192.             actionLabel.setText("Movement");
  193.             add(actionLabel);
  194.            
  195.             resultTextArea.setEditable(false);
  196.             jScrollPane1.setViewportView(resultTextArea);
  197.             add(jScrollPane1);
  198.            
  199.             playBtn = new JButton();
  200.             playBtn.setText("Play");
  201.             playBtn.addActionListener(new ActionListener() {
  202.                 public void actionPerformed(ActionEvent evt) {
  203.                     if (turn)
  204.                         player[1].hasTurn();
  205.                     else
  206.                         player[0].hasTurn();
  207.                 }
  208.             });
  209.             add(playBtn);
  210.            
  211.             movBtn = new JButton();
  212.             movBtn.setText("Move");
  213.             movBtn.addActionListener(new ActionListener() {
  214.                 public void actionPerformed(ActionEvent evt) {
  215.                     SelectDialog selectDialog = new SelectDialog();
  216.                     selectDialog.show();
  217.                 }
  218.             });
  219.             add(movBtn);
  220.         }
  221.        
  222.         /**
  223.          * Causes this container to lay out its components.
  224.          */
  225.         public void doLayout() {
  226.             actionLabel.setBounds(30, 10, 80, 20);
  227.             jScrollPane1.setBounds(20, 40, 250, 90);
  228.             playBtn.setBounds(60, 140, 80, 30);
  229.             movBtn.setBounds(150, 140, 80, 30);
  230.         }
  231.        
  232.         /**
  233.          * @return an instance of Dimension that represents the minimum size of this container.
  234.          */
  235.         public Dimension getMinimumSize() {
  236.             return new Dimension(300,180);
  237.         }
  238.        
  239.         /**
  240.          * @return an instance of Dimension that represents the preferred size of this container.
  241.          */
  242.         public Dimension getPreferredSize() {
  243.             return new Dimension(300,180);
  244.         }
  245.        
  246.         private JLabel actionLabel;
  247.         private JScrollPane jScrollPane1;
  248.         private JTextArea resultTextArea;
  249.         private JButton playBtn;
  250.         private JButton movBtn;
  251.     }
  252. }

contact - link to this site