Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- public class Draughts {
- /***
- * Program: Draughts.java
- * Creator: Chris Clarke
- * Created: 05.11.2009
- * Purpose: Draughts (checkers) 2 player game
- * Modified: 25.05.2012 (JFrame), 23.07.2012 (mouseClicked bug fixed)
- ***/
- public static void main (String[] args) {
- MyDraughtsJFrame frame = new MyDraughtsJFrame();
- frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
- frame.setSize(1024, 736);
- frame.setBackground(Color.GRAY);
- frame.setVisible(true);
- }
- }
- class MyDraughtsJFrame extends JFrame implements ActionListener {
- public MyDraughtsJFrame() {
- setTitle("Draughts by Chris Clarke");
- MenuBar mbar = new MenuBar();
- Menu fileMenu = new Menu("File");
- newMI = new MenuItem("New");
- newMI.addActionListener(this);
- fileMenu.add(newMI);
- exitMI = new MenuItem("Exit");
- exitMI.addActionListener(this);
- fileMenu.add(exitMI);
- mbar.add(fileMenu);
- setMenuBar(mbar);
- initialiseArray();
- initialise();
- this.setFocusable(true);
- this.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- mClicked( e.getX(), e.getY());
- }
- public void mousePressed(MouseEvent e) {}
- public void mouseReleased(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- });
- } // end MyDraughtsJFrame()
- public void update(Graphics g) {
- paint(g);
- }
- public void initialiseArray() {
- position=new int[] { SPACE,BLACK,SPACE,BLACK,SPACE,BLACK,SPACE,BLACK,
- BLACK,SPACE,BLACK,SPACE,BLACK,SPACE,BLACK,SPACE,
- SPACE,BLACK,SPACE, BLACK,SPACE,BLACK,SPACE,BLACK,
- SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,
- SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,SPACE,
- WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,
- SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,
- WHITE,SPACE,WHITE,SPACE,WHITE,SPACE,WHITE,SPACE };
- } // end initialiseArray()
- public void initialise() {
- captured=SPACE;
- move=FROM;
- from=0;
- wins=0;
- numBlack=4*3;
- numWhite=4*3;
- colourToPlay=WHITE;
- } // end initialise()
- public void drawACounter(Graphics g, int x, int y, int colour) {
- if (colour==WHITE) {
- g.setColor(Color.BLACK);
- g.drawOval(x, y+Y_OFFSET, 50, 25);
- g.setColor(Color.WHITE);
- for (int i=0; i<10; i++)
- g.fillOval(x,y+Y_OFFSET-i, 50, 25);
- g.setColor(Color.BLACK);
- g.drawOval(x, y+Y_OFFSET-11, 50, 25);
- } else if (colour==BLACK) {
- g.setColor(Color.WHITE);
- g.drawOval(x, y+Y_OFFSET, 50, 25);
- g.setColor(Color.BLACK);
- for (int i=0; i<10; i++)
- g.fillOval(x, y+Y_OFFSET-i, 50, 25);
- g.setColor(Color.WHITE);
- g.drawOval(x, y+Y_OFFSET-11, 50, 25);
- } else if (colour==BLACK_KING) {
- g.setColor(Color.WHITE);
- g.drawOval(x, y+Y_OFFSET, 50, 25);
- g.setColor(Color.BLACK);
- for (int i=0; i<10; i++)
- g.fillOval(x, y+Y_OFFSET-i, 50, 25);
- g.setColor(Color.WHITE);
- g.drawOval(x, y+Y_OFFSET-10, 50, 25);
- g.setColor(Color.BLACK);
- for (int i=0; i<10; i++)
- g.fillOval(x, y+Y_OFFSET-i-10, 50, 25);
- g.setColor(Color.WHITE);
- g.drawOval(x, y+Y_OFFSET-20, 50, 25);
- } else if (colour==WHITE_KING) {
- g.setColor(Color.BLACK);
- g.drawOval(x, y+Y_OFFSET, 50, 25);
- g.setColor(Color.WHITE);
- for (int i=0; i<10; i++)
- g.fillOval(x, y+Y_OFFSET-i, 50, 25);
- g.setColor(Color.BLACK);
- g.drawOval(x, y+Y_OFFSET-10, 50, 25);
- g.setColor(Color.WHITE);
- for (int i=0; i<10; i++)
- g.fillOval(x, y+Y_OFFSET-i-10, 50, 25);
- g.setColor(Color.BLACK);
- g.drawOval(x, y+Y_OFFSET-20, 50, 25);
- }
- } // end drawACounter()
- public void drawCounters(Graphics g) {
- int x = TOP_LEFT+SQUARE_SIZE;
- int y = TOP_LEFT+SQUARE_SIZE;
- for (int row=0; row<8; row++) {
- for (int col=0; col<8; col++) {
- if ((col+row)%2==1) { // odd number
- if (position[row*8+col]==BLACK&&row==7) {
- position[row*8+col]=BLACK_KING;
- numBlack++;
- }
- if (position[row*8+col]==WHITE&&row==0) {
- position[row*8+col]=WHITE_KING;
- numWhite++;
- }
- drawACounter(g,x,y, position[row*8+col]);
- }
- x+=SQUARE_SIZE;
- }
- y+=SQUARE_SIZE;
- x-=SQUARE_SIZE*8;
- }
- } // end drawCounters()
- public void drawBoard(Graphics g) {
- // draw chequered board
- for (int row=0; row<8; row++) {
- for (int col=0; col<8; col++) {
- if ((col+row)%2==0) // even number
- g.setColor(Color.LIGHT_GRAY);
- else
- g.setColor(Color.BLUE);
- g.fillRect(TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*col),
- TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*row),
- SQUARE_SIZE, SQUARE_SIZE);
- }
- }
- g.setColor(Color.LIGHT_GRAY); // draw border
- g.drawRect(TOP_LEFT+SQUARE_SIZE, TOP_LEFT+SQUARE_SIZE, SQUARE_SIZE*8,
- SQUARE_SIZE*8);
- } // end drawBoard()
- public void updateCounters() {
- position[to]=position[from];
- position[from]=SPACE;
- } // end updateCounters()
- public void paint(Graphics g) {
- drawBoard(g);
- drawCounters(g);
- if (wins==WHITE) {
- JOptionPane.showMessageDialog(null, "White Wins!");
- wins=0;
- } else if (wins==BLACK) {
- JOptionPane.showMessageDialog(null, "Black Wins!");
- wins=0;
- }
- } // end paint()
- public void updateScore() {
- if (captured==BLACK) {
- if (--numBlack==0)
- wins=WHITE;
- } else if (captured==BLACK_KING) {
- numBlack-=2;
- if (numBlack==0)
- wins=WHITE;
- } else if (captured==WHITE) {
- if (--numWhite==0)
- wins=BLACK;
- } else if (captured==WHITE_KING) {
- numWhite-=2;
- if (numWhite==0)
- wins=BLACK;
- }
- repaint();
- } // end updateScore()
- public boolean canCapture(int to, int row, int col) {
- // having captured, can player recapture?
- if (colourToPlay==BLACK) {
- if (position[to]==BLACK) {
- if (row<6) {
- if (col<6) {
- if (position[to+16+2]==SPACE
- && position[to+8+1]==WHITE)
- return true;
- }
- if (col>1) {
- if (position[to+16-2]==SPACE
- && position[to+8-1]==WHITE)
- return true;
- }
- }
- }
- if (position[to]==BLACK_KING) {
- if (row<6) {
- if (col<6) {
- if (position[to+16+2]==SPACE
- && position[to+8+1]==WHITE)
- return true;
- }
- if (col>1) {
- if (position[to+16-2]==SPACE
- && position[to+8-1]==WHITE)
- return true;
- }
- }
- if (row>1) {
- if (col<6) {
- if (position[to-16+2]==SPACE
- && position[to-8+1]==WHITE)
- return true;
- }
- if (col>1) {
- if (position[to-16-2]==SPACE
- && position[to-8-1]==WHITE)
- return true;
- }
- }
- }
- } else { // white to play
- if (position[to]==WHITE) {
- if (row>1) {
- if (col<6) {
- if (position[to-16+2]==SPACE
- && position[to-8+1]==BLACK)
- return true;
- }
- if (col>1) {
- if (position[to-16-2]==SPACE
- && position[to-8-1]==BLACK)
- return true;
- }
- }
- }
- if (position[to]==WHITE_KING) {
- if (row>1) {
- if (col<6) {
- if (position[to-16+2]==SPACE
- && position[to-8+1]==BLACK)
- return true;
- }
- if (col>1) {
- if (position[to-16-2]==SPACE
- && position[to-8-1]==BLACK)
- return true;
- }
- }
- if (row<6) {
- if (col<6) {
- if (position[to+16+2]==SPACE
- && position[to+8+1]==BLACK)
- return true;
- }
- if (col>1) {
- if (position[to+16-2]==SPACE
- && position[to+8-1]==BLACK)
- return true;
- }
- }
- }
- }
- return false;
- } // end canCapture()
- public boolean mClicked(int x, int y) {
- for (int row=0; row<8; row++) {
- for (int col=0; col<8; col++) {
- // where on board
- if (x>TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*col)
- &&x<TOP_LEFT+SQUARE_SIZE+SQUARE_SIZE+(SQUARE_SIZE*col)
- &&y>TOP_LEFT+SQUARE_SIZE+(SQUARE_SIZE*row)
- &&y<TOP_LEFT+SQUARE_SIZE+SQUARE_SIZE+(SQUARE_SIZE*row)) {
- if ((col+row)%2==0) // even number=light square
- return false;
- if (move==FROM) {
- from=row*8+col;
- if (colourToPlay==BLACK) {
- if (!(position[from]==BLACK
- ||position[from]==BLACK_KING))
- return false;
- } else {
- if (!(position[from]==WHITE
- ||position[from]==WHITE_KING))
- return false;
- }
- direction=ANY;
- if (position[from]==BLACK) direction=DOWN;
- if (position[from]==WHITE) direction=UP;
- fromRow=row;
- fromCol=col;
- move=TO;
- multiJump=false;
- } else { // move=TO
- to=row*8+col;
- if (to==from) {
- move=FROM;
- return false;
- }
- if (position[to]!=SPACE) return false;
- toRow=row;
- toCol=col;
- ChangeColour=true;
- // cut out the middle man (if jump occurred)
- if (toCol-fromCol==2||fromCol-toCol==2) {
- if (direction==UP&&fromRow-toRow!=2)
- return false;
- if (direction==DOWN&&toRow-fromRow!=2)
- return false;
- int avgRow=(toRow+fromRow)/2;
- int avgCol=(toCol+fromCol)/2;
- captured=position[(avgRow)*8+avgCol];
- if (captured==colourToPlay||
- captured==SPACE)
- return false;
- position[(avgRow)*8+avgCol]=SPACE;
- updateScore();
- updateCounters();
- if (canCapture(to, row, col))
- ChangeColour=false;
- } else {
- if (multiJump) return false;
- if (!(toCol-fromCol==1||fromCol-toCol==1))
- return false;
- if (direction==UP&&fromRow-toRow!=1)
- return false;
- if (direction==DOWN&&toRow-fromRow!=1)
- return false;
- updateCounters();
- }
- move=FROM;
- repaint();
- if (ChangeColour) {
- // change player
- if (colourToPlay==WHITE)
- colourToPlay=BLACK;
- else
- colourToPlay=WHITE;
- } else { // can capture again
- multiJump=true;
- from=to;
- fromRow=row;
- fromCol=col;
- move=TO;
- }
- }
- }
- }
- }
- return true;
- } // end mClicked()
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == exitMI)
- System.exit(0);
- else if (e.getSource() == newMI) {
- initialiseArray();
- initialise();
- repaint();
- }
- }
- private static final int TOP_LEFT = 100;
- private static final int Y_OFFSET = 20;
- private static final int SQUARE_SIZE = 50;
- private static final int SPACE = 0;
- private static final int BLACK = 1;
- private static final int WHITE = 2;
- private static final int BLACK_KING = 3;
- private static final int WHITE_KING = 4;
- private static final int FROM = 0;
- private static final int TO = 1;
- // direction of movement
- private static final int UP = 0;
- private static final int DOWN = 1;
- private static final int ANY = 2;
- private int move=0;
- private int from=0;
- private int to=0;
- private int toRow=0;
- private int fromRow=0;
- private int toCol=0;
- private int fromCol=0;
- private int[] position;
- private int captured;
- private int colourToPlay;
- private int direction;
- private int wins;
- // number of counters on either side
- private int numBlack, numWhite;
- private boolean ChangeColour; // WHEN >1 captures, don't change colour
- private boolean multiJump; // true when >1 jump available
- MenuItem newMI, exitMI;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement