diff --git a/org/homelinux/largo/games/board/checkersboard/CheckersBoard.java b/org/homelinux/largo/games/board/checkersboard/CheckersBoard.java new file mode 100644 --- /dev/null +++ b/org/homelinux/largo/games/board/checkersboard/CheckersBoard.java @@ -0,0 +1,299 @@ +/** + * $Id: CheckersBoard.java 152 2008-04-27 02:04:31Z mbroeker $ + * $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/games/board/checkersboard/CheckersBoard.java $ + */ + +package org.homelinux.largo.games.board.checkersboard; + +import java.awt.Image; + +import org.homelinux.largo.games.board.Board; +import org.homelinux.largo.games.board.History; +import org.homelinux.largo.games.board.Piece; +import org.homelinux.largo.utils.ImgComponent; + +public class CheckersBoard extends Board { + static final long serialVersionUID = 250408; + + static final int PIECE = 1; + static final int QUEEN = 2; + + final Piece white_super_sun = + new Piece(new ImgComponent("images/white_super_sun.png").getImage(), "white", QUEEN, 50); + + final Piece black_super_sun = + new Piece(new ImgComponent("images/black_super_sun.png").getImage(), "black", QUEEN, 50); + + void initBoard () { + for (int i=1;i<8;i+=2) + add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE); + for (int i=8;i<15;i+=2) + add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE); + for (int i=17;i<=23;i+=2) + add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE); + + for (int i=40;i<47;i+=2) + add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE); + for (int i=49;i<=55;i+=2) + add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE); + for (int i=56;i<63;i+=2) + add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE); + } + + /** + * The init method initializes a complete checkers board with figures and a history. + */ + public void init() { + super.init(); + initBoard(); + board_pieces = getPieces(); + repaint(); + } + + public CheckersBoard (int w, int h) { + super(w, h); + } + + void add (Image img, int i, String c, int t) { + int v = 0; + + switch(t) { + case PIECE: + v=10; + } + + setPiece(i, new Piece(img, c, t, v)); + } + + /** + * Checks, wether this move is possible or not. + */ + public boolean validMove (int t, int o) { + int steps; + int rows; + int dx; + + if ( t == o ) + return false; + + if (isSamePiece(t, o)) + return false; + + if (getType(o) == EMPTY) + return false; + + /* + * 00 01 02 03 04 05 06 07 + * 08 09 10 11 12 13 14 15 + * 16 17 18 19 20 21 22 23 + * 24 25 26 27 28 29 30 31 + * 32 33 34 35 36 37 38 39 + * 40 41 42 43 44 45 46 47 + * 48 49 50 51 52 53 54 55 + * 56 57 58 59 60 61 62 63 + */ + + steps = Math.abs(t-o); + rows = Math.abs(t/8 - o/8); + + switch( getType(o) ) { + case PIECE: + if (steps % 7 == 0 ) { + if (steps == 7 && rows == 1 && isEmpty(t)) { + if ( isBlack(o) && (t>o) ) + return true; + if ( isWhite(o) && (to) && isEnemy(o+7, o) ) + return true; + if ( isWhite(o) && (to) ) + return true; + if ( isWhite(o) && (to) && isEnemy(o+9, o) ) + return true; + if ( isWhite(o) && (t= 0 && pos <64) + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } + + stack.setSize(size-2); + + /* Reset the current player */ + setBlacksTurn(h2.turn()); + } + + /** + * simulates a "valid" move or returns false. + * all changes are relative to pieces and will not be painted. + */ + public boolean simulateMove(int t, int o) { + int steps = Math.abs(t-o); + int rows = Math.abs(t/8 - o/8); + int pos; + + if ( !validMove(t, o) ) { + return false; + } + + if ( rows == 2 && getType(o) == PIECE) { + pos = (t-o)/2 + o; + push(getPiece(pos), pos, getPiece(o), o); + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } else + push(getPiece(t), t, getPiece(o), o); + + setPiece(t, new Piece(getPiece(o))); + setPiece(o, new Piece(null, null, EMPTY, 0)); + + return true; + } + + /** + * performs a "valid" move or returns false. + * all changes are relative to board_pieces and will be painted. + */ + public boolean doMove(int t, int o) { + int steps = Math.abs(t-o); + int rows = Math.abs(t/8-o/8); + int pos; + + if ( !validMove(t, o) ) { + return false; + } + + if ( rows == 2 && getType(o) == PIECE) { + pos = (t-o)/2 + o; + push(getPiece(pos), pos, getPiece(o), o); + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } else if ( rows >= 2 && getType(o) == QUEEN ) { + if (steps % 7 == 0 ) { + if ( t > o ) + for ( pos=o+7;pos <= t-7;pos+=7 ) { + if ( isEnemy(pos, o) ) { + push(getPiece(pos), pos, getPiece(o), o); + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } + } + else { + for ( pos=o-7;pos >= t+7;pos-=7 ) { + if ( isEnemy(pos, o) ) { + push(getPiece(pos), pos, getPiece(o), o); + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } + } + } + } + + if (steps % 9 == 0 ) { + if ( t > o ) + for ( pos=o+9;pos <= t-9;pos+=9 ) { + if ( isEnemy(pos, o) ) { + push(getPiece(pos), pos, getPiece(o), o); + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } + } + else { + for ( pos=o-9;pos >= t+9;pos-=9 ) { + if ( isEnemy(pos, o) ) { + push(getPiece(pos), pos, getPiece(o), o); + setPiece(pos, new Piece(null, null, EMPTY, 0)); + } + } + } + } + } + + if ( t <= 7 && getType(o) == PIECE) + setPiece(t, new Piece(white_super_sun)); + else if (t >=56 && getType(o) == PIECE) + setPiece(t, new Piece(black_super_sun)); + else + setPiece(t, new Piece(getPiece(o))); + + setPiece(o, new Piece(null, null, EMPTY, 0)); + + board_pieces = getPieces(); + repaint(); + + moveNr = stack.size(); + + return true; + } +}