Advertisement
brilliant_moves

Draughts.java

May 25th, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 11.27 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6.  
  7. public class Draughts {
  8.  
  9.     /***
  10.     *   Program:    Draughts.java
  11.     *   Creator:    Chris Clarke
  12.     *   Created:    05.11.2009
  13.     *   Purpose:    Draughts (checkers) 2 player game
  14.     *   Modified:   25.05.2012 (JFrame), 23.07.2012 (mouseClicked bug fixed)
  15.     ***/
  16.  
  17.     public static void main (String[] args) {
  18.         MyDraughtsJFrame frame = new MyDraughtsJFrame();
  19.         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
  20.         frame.setSize(1024, 736);
  21.         frame.setBackground(Color.GRAY);
  22.         frame.setVisible(true);
  23.     }
  24. }
  25.    
  26. class MyDraughtsJFrame extends JFrame implements ActionListener {
  27.     public MyDraughtsJFrame() {
  28.         setTitle("Draughts by Chris Clarke");
  29.         MenuBar mbar = new MenuBar();
  30.         Menu fileMenu = new Menu("File");
  31.         newMI = new MenuItem("New");
  32.         newMI.addActionListener(this);
  33.         fileMenu.add(newMI);
  34.         exitMI = new MenuItem("Exit");
  35.         exitMI.addActionListener(this);
  36.         fileMenu.add(exitMI);
  37.         mbar.add(fileMenu);
  38.         setMenuBar(mbar);
  39.         initialiseArray();
  40.         initialise();
  41.         this.setFocusable(true);
  42.             this.addMouseListener(new MouseAdapter() {
  43.                     public void mouseClicked(MouseEvent e) {
  44.                         mClicked( e.getX(), e.getY());
  45.             }
  46.  
  47.                     public void mousePressed(MouseEvent e) {}
  48.  
  49.                     public void mouseReleased(MouseEvent e) {}
  50.  
  51.                     public void mouseEntered(MouseEvent e) {}
  52.  
  53.                     public void mouseExited(MouseEvent e) {}
  54.  
  55.         });
  56.  
  57.     } // end MyDraughtsJFrame()
  58.  
  59.     public void update(Graphics g) {
  60.         paint(g);
  61.     }
  62.  
  63.     public void initialiseArray() {
  64.         position=new int[] { SPACE,BLACK,SPACE,BLACK,SPACE,BLACK,SPACE,BLACK,
  65.          BLACK,SPACE,BLACK,SPACE,BLACK,SPACE,BLACK,SPACE,
  66.          SPACE,BLACK,SPACE, BLACK,SPACE,BLACK,SPACE,BLACK,
  67.          SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,
  68.          SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,
  69.          WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,
  70.          SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,
  71.          WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,SPACE };
  72.     } // end initialiseArray()
  73.  
  74.     public void initialise() {
  75.         captured=SPACE;
  76.         move=FROM;
  77.         from=0;
  78.         wins=0;
  79.         numBlack=4*3;
  80.         numWhite=4*3;
  81.         colourToPlay=WHITE;
  82.     } // end initialise()
  83.  
  84.     public void drawACounter(Graphics g, int x, int y, int colour) {
  85.         if (colour==WHITE) {
  86.             g.setColor(Color.BLACK);
  87.             g.drawOval(x, y+Y_OFFSET, 50, 25);
  88.             g.setColor(Color.WHITE);
  89.             for (int i=0; i<10; i++)
  90.                 g.fillOval(x,y+Y_OFFSET-i, 50, 25);
  91.             g.setColor(Color.BLACK);
  92.             g.drawOval(x, y+Y_OFFSET-11, 50, 25);
  93.         } else if (colour==BLACK) {
  94.             g.setColor(Color.WHITE);
  95.             g.drawOval(x, y+Y_OFFSET, 50, 25);
  96.             g.setColor(Color.BLACK);
  97.             for (int i=0; i<10; i++)
  98.                 g.fillOval(x, y+Y_OFFSET-i, 50, 25);
  99.             g.setColor(Color.WHITE);
  100.             g.drawOval(x, y+Y_OFFSET-11, 50, 25);
  101.         } else if (colour==BLACK_KING) {
  102.             g.setColor(Color.WHITE);
  103.             g.drawOval(x, y+Y_OFFSET, 50, 25);
  104.             g.setColor(Color.BLACK);
  105.             for (int i=0; i<10; i++)
  106.                 g.fillOval(x, y+Y_OFFSET-i, 50, 25);
  107.             g.setColor(Color.WHITE);
  108.             g.drawOval(x, y+Y_OFFSET-10, 50, 25);
  109.             g.setColor(Color.BLACK);
  110.             for (int i=0; i<10; i++)
  111.                 g.fillOval(x, y+Y_OFFSET-i-10, 50, 25);
  112.             g.setColor(Color.WHITE);
  113.             g.drawOval(x, y+Y_OFFSET-20, 50, 25);
  114.         } else if (colour==WHITE_KING) {
  115.             g.setColor(Color.BLACK);
  116.             g.drawOval(x, y+Y_OFFSET, 50, 25);
  117.             g.setColor(Color.WHITE);
  118.             for (int i=0; i<10; i++)
  119.                 g.fillOval(x, y+Y_OFFSET-i, 50, 25);
  120.             g.setColor(Color.BLACK);
  121.             g.drawOval(x, y+Y_OFFSET-10, 50, 25);
  122.             g.setColor(Color.WHITE);
  123.             for (int i=0; i<10; i++)
  124.                 g.fillOval(x, y+Y_OFFSET-i-10, 50, 25);
  125.             g.setColor(Color.BLACK);
  126.             g.drawOval(x, y+Y_OFFSET-20, 50, 25);
  127.         }
  128.     } // end drawACounter()
  129.  
  130.     public void drawCounters(Graphics g) {
  131.         int x = TOP_LEFT+SQUARE_SIZE;
  132.         int y = TOP_LEFT+SQUARE_SIZE;
  133.  
  134.         for (int row=0; row<8; row++) {
  135.             for (int col=0; col<8; col++) {
  136.                 if ((col+row)%2==1) { // odd number
  137.                     if (position[row*8+col]==BLACK&&row==7) {
  138.                         position[row*8+col]=BLACK_KING;
  139.                         numBlack++;
  140.                     }
  141.                     if (position[row*8+col]==WHITE&&row==0) {
  142.                         position[row*8+col]=WHITE_KING;
  143.                         numWhite++;
  144.                     }
  145.                     drawACounter(g,x,y, position[row*8+col]);
  146.                 }
  147.                 x+=SQUARE_SIZE;
  148.             }
  149.             y+=SQUARE_SIZE;
  150.             x-=SQUARE_SIZE*8;
  151.         }
  152.     } // end drawCounters()
  153.  
  154.     public void drawBoard(Graphics g) {
  155.         // draw chequered board
  156.         for (int row=0; row<8; row++) {
  157.             for (int col=0; col<8; col++) {
  158.                 if ((col+row)%2==0) // even number
  159.                     g.setColor(Color.LIGHT_GRAY);
  160.                 else
  161.                     g.setColor(Color.BLUE);
  162.                 g.fillRect(TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*col),
  163.                  TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*row),
  164.                  SQUARE_SIZE, SQUARE_SIZE);
  165.             }
  166.         }
  167.         g.setColor(Color.LIGHT_GRAY); // draw border
  168.         g.drawRect(TOP_LEFT+SQUARE_SIZE, TOP_LEFT+SQUARE_SIZE, SQUARE_SIZE*8,
  169.          SQUARE_SIZE*8);
  170.     } // end drawBoard()
  171.  
  172.     public void updateCounters() {
  173.         position[to]=position[from];
  174.         position[from]=SPACE;
  175.     } // end updateCounters()
  176.  
  177.     public void paint(Graphics g) {
  178.         drawBoard(g);
  179.         drawCounters(g);
  180.         if (wins==WHITE) {
  181.             JOptionPane.showMessageDialog(null, "White Wins!");
  182.             wins=0;
  183.         } else if (wins==BLACK) {
  184.             JOptionPane.showMessageDialog(null, "Black Wins!");
  185.             wins=0;
  186.         }
  187.     } // end paint()
  188.  
  189.     public void updateScore() {
  190.         if (captured==BLACK) {
  191.             if (--numBlack==0)
  192.                 wins=WHITE;
  193.         } else if (captured==BLACK_KING) {
  194.             numBlack-=2;
  195.             if (numBlack==0)
  196.                 wins=WHITE;
  197.         } else if (captured==WHITE) {
  198.             if (--numWhite==0)
  199.                 wins=BLACK;
  200.         } else if (captured==WHITE_KING) {
  201.             numWhite-=2;
  202.             if (numWhite==0)
  203.                 wins=BLACK;
  204.         }
  205.         repaint();
  206.     } // end updateScore()
  207.  
  208.     public boolean canCapture(int to, int row, int col) {
  209.     // having captured, can player recapture?
  210.         if (colourToPlay==BLACK) {
  211.             if (position[to]==BLACK) {
  212.                 if (row<6) {
  213.                     if (col<6) {
  214.                         if (position[to+16+2]==SPACE
  215.                          && position[to+8+1]==WHITE)
  216.                             return true;
  217.                     }
  218.                     if (col>1) {
  219.                         if (position[to+16-2]==SPACE
  220.                          && position[to+8-1]==WHITE)
  221.                             return true;
  222.                     }
  223.                 }
  224.             }
  225.             if (position[to]==BLACK_KING) {
  226.                 if (row<6) {
  227.                     if (col<6) {
  228.                         if (position[to+16+2]==SPACE
  229.                          && position[to+8+1]==WHITE)
  230.                             return true;
  231.                     }
  232.                     if (col>1) {
  233.                         if (position[to+16-2]==SPACE
  234.                          && position[to+8-1]==WHITE)
  235.                             return true;
  236.                     }
  237.                 }
  238.                 if (row>1) {
  239.                     if (col<6) {
  240.                         if (position[to-16+2]==SPACE
  241.                          && position[to-8+1]==WHITE)
  242.                             return true;
  243.                     }
  244.                     if (col>1) {
  245.                         if (position[to-16-2]==SPACE
  246.                          && position[to-8-1]==WHITE)
  247.                             return true;
  248.                     }
  249.                 }
  250.             }
  251.         } else {    // white to play
  252.             if (position[to]==WHITE) {
  253.                 if (row>1) {
  254.                     if (col<6) {
  255.                         if (position[to-16+2]==SPACE
  256.                          && position[to-8+1]==BLACK)
  257.                             return true;
  258.                     }
  259.                     if (col>1) {
  260.                         if (position[to-16-2]==SPACE
  261.                          && position[to-8-1]==BLACK)
  262.                             return true;
  263.                     }
  264.                 }
  265.             }
  266.             if (position[to]==WHITE_KING) {
  267.                 if (row>1) {
  268.                     if (col<6) {
  269.                         if (position[to-16+2]==SPACE
  270.                          && position[to-8+1]==BLACK)
  271.                             return true;
  272.                     }
  273.                     if (col>1) {
  274.                         if (position[to-16-2]==SPACE
  275.                          && position[to-8-1]==BLACK)
  276.                             return true;
  277.                     }
  278.                 }
  279.                 if (row<6) {
  280.                     if (col<6) {
  281.                         if (position[to+16+2]==SPACE
  282.                          && position[to+8+1]==BLACK)
  283.                             return true;
  284.                     }
  285.                     if (col>1) {
  286.                         if (position[to+16-2]==SPACE
  287.                          && position[to+8-1]==BLACK)
  288.                             return true;
  289.                     }
  290.                 }
  291.             }
  292.         }
  293.         return false;
  294.     } // end canCapture()
  295.  
  296.     public boolean mClicked(int x, int y) {
  297.         for (int row=0; row<8; row++) {
  298.             for (int col=0; col<8; col++) {
  299.                 // where on board
  300.                 if (x>TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*col)
  301.                 &&x<TOP_LEFT+SQUARE_SIZE+SQUARE_SIZE+(SQUARE_SIZE*col)
  302.                  &&y>TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*row)
  303.                 &&y<TOP_LEFT+SQUARE_SIZE+SQUARE_SIZE+(SQUARE_SIZE*row)) {
  304.                     if ((col+row)%2==0) // even number=light square
  305.                         return false;
  306.                     if (move==FROM) {
  307.                         from=row*8+col;
  308.                         if (colourToPlay==BLACK) {
  309.                             if (!(position[from]==BLACK
  310.                              ||position[from]==BLACK_KING))
  311.                                 return false;
  312.                         } else {
  313.                             if (!(position[from]==WHITE
  314.                              ||position[from]==WHITE_KING))
  315.                                 return false;
  316.                         }
  317.                         direction=ANY;
  318.                         if (position[from]==BLACK) direction=DOWN;
  319.                         if (position[from]==WHITE) direction=UP;
  320.                         fromRow=row;
  321.                         fromCol=col;
  322.                         move=TO;
  323.                         multiJump=false;
  324.                     } else {    // move=TO
  325.                         to=row*8+col;
  326.                         if (to==from) {
  327.                             move=FROM;
  328.                             return false;
  329.                         }
  330.                         if (position[to]!=SPACE) return false;
  331.                         toRow=row;
  332.                         toCol=col;
  333.                         ChangeColour=true;
  334.                         // cut out the middle man (if jump occurred)
  335.                         if (toCol-fromCol==2||fromCol-toCol==2) {
  336.                             if (direction==UP&&fromRow-toRow!=2)
  337.                                 return false;
  338.                             if (direction==DOWN&&toRow-fromRow!=2)
  339.                                 return false;
  340.                             int avgRow=(toRow+fromRow)/2;
  341.                             int avgCol=(toCol+fromCol)/2;
  342.                             captured=position[(avgRow)*8+avgCol];
  343.                             if (captured==colourToPlay||
  344.                              captured==SPACE)
  345.                                 return false;
  346.                             position[(avgRow)*8+avgCol]=SPACE;
  347.                             updateScore();
  348.                             updateCounters();
  349.                             if (canCapture(to, row, col))
  350.                                 ChangeColour=false;
  351.                         } else {
  352.                             if (multiJump) return false;
  353.                             if (!(toCol-fromCol==1||fromCol-toCol==1))
  354.                                 return false;
  355.                             if (direction==UP&&fromRow-toRow!=1)
  356.                                 return false;
  357.                             if (direction==DOWN&&toRow-fromRow!=1)
  358.                                 return false;
  359.                             updateCounters();
  360.                         }
  361.                         move=FROM;
  362.                         repaint();
  363.                         if (ChangeColour) {
  364.                             // change player
  365.                             if (colourToPlay==WHITE)
  366.                                 colourToPlay=BLACK;
  367.                             else
  368.                                 colourToPlay=WHITE;
  369.                         } else { // can capture again
  370.                             multiJump=true;
  371.                             from=to;
  372.                             fromRow=row;
  373.                             fromCol=col;
  374.                             move=TO;
  375.                         }
  376.                     }
  377.                 }
  378.             }
  379.         }
  380.         return true;
  381.     } // end mClicked()
  382.  
  383.     public void actionPerformed(ActionEvent e) {
  384.         if (e.getSource() == exitMI)
  385.             System.exit(0);
  386.         else if (e.getSource() == newMI) {
  387.             initialiseArray();
  388.             initialise();
  389.             repaint();
  390.         }
  391.     }  
  392.  
  393.     private static final int TOP_LEFT = 100;
  394.     private static final int Y_OFFSET = 20;
  395.     private static final int SQUARE_SIZE = 50;
  396.    
  397.     private static final int SPACE = 0;
  398.     private static final int BLACK = 1;
  399.     private static final int WHITE = 2;
  400.     private static final int BLACK_KING = 3;
  401.     private static final int WHITE_KING = 4;
  402.  
  403.     private static final int FROM = 0;
  404.     private static final int TO = 1;
  405.  
  406.     // direction of movement
  407.     private static final int UP = 0;
  408.     private static final int DOWN = 1;
  409.     private static final int ANY = 2;
  410.  
  411.     private int     move=0;
  412.     private int     from=0;
  413.     private int     to=0;
  414.     private int     toRow=0;
  415.     private int     fromRow=0;
  416.     private int     toCol=0;
  417.     private int     fromCol=0;
  418.     private int[]       position;
  419.     private int     captured;
  420.     private int     colourToPlay;
  421.     private int     direction;
  422.     private int     wins;
  423.                 // number of counters on either side
  424.     private int     numBlack, numWhite;
  425.     private boolean     ChangeColour;   // WHEN >1 captures, don't change colour
  426.     private boolean     multiJump;  // true when >1 jump available
  427.     MenuItem        newMI, exitMI;
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement