Checkers game

  1. /*
  2.  * SelectPanel.java
  3.  *
  4.  * Created on 3 December 2004, 13:42
  5.  * @author  k380h11
  6.  */
  7. package checkers;
  8.  
  9. import javax.swing.*;
  10. import javax.swing.border.*;
  11. import javax.swing.table.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.util.*;
  15. import jess.*;
  16. /**
  17.  * This dialog is a kind of jess (facts) commands ameliorated. In fact, when this dialogs is opens, two jess queries are sent to the database and the results are displayed in the two tables. The first table represents all the movement calculated by Jess. The second shows the actual and probable future positions of the pieces.
  18.  * Five inputs are present to enquire for specific movement in order to reduce the list of reply. Unfortunately, this option was foreseen, but has not been implemented. However, the widget are leaved for next amelioration of the program.
  19.  * They con be used by implementeing the falowing code for example:
  20.  * <pre>
  21.  * Checker.r.reset ();
  22.  * Checker.r.executeCommand("(assert (deepness "+ (String) levelInput.getText () + " " + (String) colorSelect.getSelectedItem () +"))");
  23.  * Checker.r.executeCommand("(focus MOVEMENT)");
  24.  * Checker.r.executeCommand("(run)");
  25.  * </pre>
  26.  * @author k31380h11
  27.  */
  28.  
  29.  
  30. public class SelectDialog extends JDialog {
  31.    
  32.         /**
  33.          * This method set the conditions for the game's graphical user interface.
  34.          */
  35.     public SelectDialog () {
  36.         super ();
  37.         String title = "Movement";
  38.         String laf = UIManager.getSystemLookAndFeelClassName ();
  39.         try {
  40.             UIManager.setLookAndFeel (laf);
  41.         } catch (UnsupportedLookAndFeelException exc) {
  42.             System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
  43.         } catch (Exception exc) {
  44.             System.err.println ("Error loading " + laf + ": " + exc);
  45.         }
  46.        
  47.         addWindowListener (new WindowAdapter () {
  48.             public void windowClosing (WindowEvent evt) {
  49.                 hide ();
  50.             }
  51.         });
  52.         setResizable (true);
  53.         setTitle (title);
  54.         setLocation (200, 200);
  55.         JLabel titleLabel = new JLabel ();
  56.         titleLabel.setFont (new Font ("MS Sans Serif", 1, 18));
  57.         titleLabel.setHorizontalAlignment (SwingConstants.CENTER);
  58.         titleLabel.setText (title);
  59.        
  60.         SelectPanel selectPanel = new SelectPanel ();
  61.        
  62.         getContentPane ().add (titleLabel, BorderLayout.NORTH);
  63.         getContentPane ().add (selectPanel, BorderLayout.CENTER);
  64.         pack ();
  65.     }
  66.    
  67.     /**
  68.      * main panel
  69.      */    
  70.     public class SelectPanel extends JPanel {
  71.        
  72.         /** Creates new form SelectPanel */
  73.         public SelectPanel () {
  74.             pieceSelect = new JComboBox ();
  75.             actionSelect = new JComboBox ();
  76.             colorSelect = new JComboBox ();
  77.             pointInput = new JTextField ();
  78.             pieceLabel = new JLabel ();
  79.             colorLabel = new JLabel();
  80.             actionLabel = new JLabel ();
  81.             pointLabel = new JLabel ();
  82.             levelLabel = new JLabel ();
  83.             levelInput = new JTextField ();
  84.             searchButton = new JButton ();
  85.             jScrollPane = new JScrollPane ();
  86.             movementTable = new JTable ();
  87.             jScrollPane2 = new JScrollPane ();
  88.             PieceTable = new JTable ();
  89.             add (pieceSelect);
  90.             add (actionSelect);
  91.             add(colorSelect);
  92.             add (pointInput);
  93.            
  94.             pieceLabel.setText ("Piece");
  95.             add (pieceLabel);
  96.            
  97.             colorLabel.setText("Color");
  98.             add(colorLabel);
  99.            
  100.             actionLabel.setText ("Action");
  101.             add (actionLabel);
  102.            
  103.             pointLabel.setText ("Point");
  104.             add (pointLabel);
  105.            
  106.             levelLabel.setText ("Level");
  107.             add (levelLabel);
  108.            
  109.             add (levelInput);
  110.            
  111.             searchButton.setText ("Search");
  112.             searchButton.addActionListener (new ActionListener () {
  113.                 public void actionPerformed (ActionEvent evt) {
  114.                     searchButtonActionPerformed (evt);
  115.                 }
  116.             });
  117.             add (searchButton);
  118.            
  119.             jScrollPane.setBorder (new SoftBevelBorder (BevelBorder.RAISED));
  120.             movementTable.setRowSelectionAllowed (false);
  121.             jScrollPane.setViewportView (movementTable);
  122.             add (jScrollPane);
  123.            
  124.             jScrollPane2.setBorder (new SoftBevelBorder (BevelBorder.RAISED));
  125.             PieceTable.setRowSelectionAllowed (false);
  126.             jScrollPane2.setViewportView (PieceTable);
  127.             add (jScrollPane2);
  128.             init ();
  129.         }
  130.        
  131.         /**
  132.          * This method is used to set up the window for the searchButton by collectin information from Jess.
  133.          * The information that it need arer the facts of the piece at all levels.
  134.          *@throws an Jessexception if the java program was unable to get the information from Jess.
  135.          */
  136.  
  137.         private void searchButtonActionPerformed (ActionEvent evt) {
  138.             ArrayList resultMovement = new ArrayList ();
  139.             ArrayList resultPiece = new ArrayList ();
  140.             try {
  141.                 Checker.r.executeCommand ("(defquery search (MOVEMENT::movement (point ?point) (level ?level) (action ?action) (numberOfAction ?numberAction) (next ?next)))");
  142.                 Checker.r.store ("RESULT", Checker.r.runQuery ("search", new ValueVector ()));
  143.                 Checker.r.executeCommand ("(store RESULT (run-query search))");
  144.                 Iterator it = (Iterator) Checker.r.fetch ("RESULT").externalAddressValue (null);
  145.                
  146.                 while (it.hasNext ()) {
  147.                     Token token = (Token) it.next ();
  148.                     resultMovement.add (token.fact (1));
  149.                 }        
  150.                 Checker.r.executeCommand ("(defquery searchPiece (MAIN::Piece (PRS ?next) (OBJECT ?piece) (number ?number) (color ?color) (level ?level) (virtual ?virtual) (king ?king)))");
  151.                 Checker.r.store ("RESULT2", Checker.r.runQuery ("searchPiece",new ValueVector ()));
  152.                 Checker.r.executeCommand ("(store RESULT2 (run-query searchPiece))");
  153.                 it = (Iterator) Checker.r.fetch ("RESULT2").externalAddressValue (null);
  154.                 while (it.hasNext ()) {
  155.                     Token token = (Token) it.next ();
  156.                     resultPiece.add (token.fact (1));
  157.                 }
  158.             } catch (JessException e) {System.err.println (e);}
  159.            
  160.             if (resultMovement != null) {
  161.                 int resultSize = resultMovement.size ();
  162.                 String listData[][] = new String[resultSize][9];
  163.                 String title[] = {"Number", "Color", "Square", "Point","NOA", "Div", "Action", "Level", "Next"};
  164.                
  165.                 for (int i = 0; i<resultSize ; i++) {
  166.                     try {
  167.                         Fact fact = (Fact) resultMovement.get (i);
  168.                         Piece piece = (Piece) fact.getSlotValue ("piece").externalAddressValue (null);
  169.                         Grid.Square curent = piece.getSquare ();
  170.                         int number = piece.getNumber ();
  171.                         boolean color = piece.getColor ();
  172.                         Grid.Square next = (Grid.Square) fact.getSlotValue ("next").externalAddressValue (null);
  173.                         double pts = Double.parseDouble(fact.getSlotValue ("point").toString ());
  174.                         String Action = fact.getSlotValue ("action").toString ();
  175.                         String level = fact.getSlotValue ("level").toString ();
  176.                         double numberOfAction = Double.parseDouble(fact.getSlotValue ("numberOfAction").toString ());
  177.                         listData[i][0] = Integer.toString (number);
  178.                         listData[i][1] = color ? "white" : "red";
  179.                         listData[i][2] = curent.toString ();
  180.                         listData[i][3] = Double.toString(pts);
  181.                         listData[i][4] = Double.toString(numberOfAction);
  182.                         listData[i][5] = Double.toString(pts / numberOfAction);
  183.                         listData[i][6] = Action;
  184.                         listData[i][7] = level;
  185.                         listData[i][8] = next.toString ();
  186.                     } catch (JessException e) {System.err.println (e);}
  187.                 }
  188.                 movementTable.setModel (new DefaultTableModel (listData,title));
  189.             }
  190.             if (resultPiece != null) {
  191.                 int resultSize = resultPiece.size ();
  192.                 String listData[][] = new String[resultSize][6];
  193.                 String title[] = {"square", "number", "color", "level", "virtual", "king"};
  194.                
  195.                 for (int i = 0; i<resultSize ; i++) {
  196.                     try {
  197.                         Fact fact = (Fact) resultPiece.get (i);                      
  198.                         listData[i][0] = fact.getSlotValue ("square").externalAddressValue (null).toString ();
  199.                         listData[i][1] = fact.getSlotValue ("number").toString ();
  200.                         listData[i][2] = fact.getSlotValue ("color").toString ();
  201.                         listData[i][3] = fact.getSlotValue ("level").toString ();
  202.                         listData[i][4] = fact.getSlotValue ("virtual").toString ();
  203.                         listData[i][5] = fact.getSlotValue ("king").toString ();
  204.                     } catch (JessException e) {System.err.println (e);}
  205.                 }
  206.                 PieceTable.setModel (new DefaultTableModel (listData,title));
  207.             }
  208.         }
  209.         /**
  210.          * This method set bounds for the graphical user interface.
  211.          * This is done give parameter where to start and end i pixels
  212.          * and causes this container to lay out its components.
  213.          */    
  214.  
  215.         public void doLayout () {
  216.             pieceSelect.setBounds (20, 40, 90, 20);
  217.             actionSelect.setBounds (210, 40, 80, 20);
  218.             colorSelect.setBounds(130, 40, 60, 20);
  219.             pointInput.setBounds (310, 40, 40, 20);
  220.             pieceLabel.setBounds (20, 10, 40, 20);
  221.             colorLabel.setBounds(130, 10, 40, 20);
  222.             actionLabel.setBounds (210, 10, 40, 20);
  223.             pointLabel.setBounds (310, 10, 40, 20);
  224.             levelLabel.setBounds (360, 10, 40, 20);
  225.             levelInput.setBounds (360, 40, 40, 20);
  226.             searchButton.setBounds (420, 40, 70, 20);
  227.             jScrollPane.setBounds (20, 90, 500, 120);
  228.             movementTable.setBounds (20, 90, 500, 120);
  229.             jScrollPane2.setBounds (20, 240, 500, 120);
  230.             PieceTable.setBounds (20, 240, 500, 120);
  231.         }
  232.  
  233.         /**
  234.          * This method is used to set minimum size of the graphical user interface window .
  235.          * @return the dimension height, width.
  236.          */    
  237.         public Dimension getMinimumSize () {
  238.             return new Dimension (550,380);
  239.         }
  240.    
  241.         /**
  242.          * This method is used to set preferred size of the graphical user interface window .
  243.          * @return the dimension height, width.
  244.          */
  245.         public Dimension getPreferredSize () {
  246.             return new Dimension (550,380);
  247.         }
  248.         /**
  249.          * This method is used  initilize the graphical user interface window .
  250.          * the method calls other methods to initilize board  .
  251.          */    
  252.         public void init () {
  253.             actionSelect.addItem ("");
  254.             actionSelect.addItem ("eat");
  255.             actionSelect.addItem ("move");      
  256.             colorSelect.addItem (Boolean.toString(Checker.getInstance().getTurn()).toUpperCase ());
  257.             colorSelect.addItem (Boolean.toString(!Checker.getInstance().getTurn()).toUpperCase ());
  258.             levelInput.setText("0");
  259.             pieceSelect.addItem (null);
  260.             searchButtonActionPerformed (null);
  261.         }
  262.        
  263.         private JLabel actionLabel;
  264.         private JComboBox actionSelect;
  265.         private JTable movementTable;
  266.         private JTextField pointInput;
  267.         private JLabel pointLabel;
  268.         private JLabel pieceLabel;
  269.         private JComboBox pieceSelect;
  270.         private JScrollPane jScrollPane;
  271.         private JScrollPane jScrollPane2;
  272.         private JTable PieceTable;
  273.         private JComboBox colorSelect;
  274.         private JLabel colorLabel;
  275.         private JTextField levelInput;
  276.         private JLabel levelLabel;
  277.         private JButton searchButton;
  278.        
  279.     }
  280. }

contact - link to this site