org/homelinux/largo/games/board/checkersboard/CheckersBoard.java
changeset 0 e0dbaef72362
child 7 93fe1f21e0d8
equal deleted inserted replaced
-1:000000000000 0:e0dbaef72362
       
     1 /**
       
     2  *   $Id: CheckersBoard.java 152 2008-04-27 02:04:31Z mbroeker $
       
     3  *  $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/games/board/checkersboard/CheckersBoard.java $
       
     4  */
       
     5 
       
     6 package org.homelinux.largo.games.board.checkersboard;
       
     7 
       
     8 import java.awt.Image;
       
     9 
       
    10 import org.homelinux.largo.games.board.Board;
       
    11 import org.homelinux.largo.games.board.History;
       
    12 import org.homelinux.largo.games.board.Piece;
       
    13 import org.homelinux.largo.utils.ImgComponent;
       
    14 
       
    15 public class CheckersBoard extends Board {
       
    16 	static final long serialVersionUID = 250408;
       
    17 
       
    18 	static final int PIECE = 1;
       
    19 	static final int QUEEN = 2;
       
    20 
       
    21 	final Piece white_super_sun =
       
    22 		new Piece(new ImgComponent("images/white_super_sun.png").getImage(), "white", QUEEN, 50);
       
    23 
       
    24 	final Piece black_super_sun =
       
    25 		new Piece(new ImgComponent("images/black_super_sun.png").getImage(), "black", QUEEN, 50);
       
    26 
       
    27 	void initBoard () {
       
    28 		for (int i=1;i<8;i+=2)
       
    29 			add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE);
       
    30 		for (int i=8;i<15;i+=2)
       
    31 			add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE);
       
    32 		for (int i=17;i<=23;i+=2)
       
    33 			add (new ImgComponent ("images/black_sun.png").getImage (), i, "black", PIECE);
       
    34 
       
    35 		for (int i=40;i<47;i+=2)
       
    36 			add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE);
       
    37 		for (int i=49;i<=55;i+=2)
       
    38 			add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE);
       
    39 		for (int i=56;i<63;i+=2)
       
    40 			add (new ImgComponent ("images/white_sun.png").getImage (), i, "white", PIECE);
       
    41 	}
       
    42 
       
    43 	/**
       
    44 	 * The init method initializes a complete checkers board with figures and a history.
       
    45 	 */
       
    46 	public void init() {
       
    47 		super.init();
       
    48 		initBoard();
       
    49 		board_pieces = getPieces();
       
    50 		repaint();
       
    51 	}
       
    52 
       
    53 	public CheckersBoard (int w, int h) {
       
    54 		super(w, h);
       
    55 	}
       
    56 
       
    57 	void add (Image img, int i, String c, int t) {
       
    58 		int v = 0;
       
    59 
       
    60 		switch(t) {
       
    61 		case PIECE:
       
    62 			v=10;
       
    63 		}
       
    64 
       
    65 		setPiece(i, new Piece(img, c, t, v));
       
    66 	}
       
    67 
       
    68 	/**
       
    69 	 * Checks, wether this move is possible or not.
       
    70 	 */
       
    71 	public boolean validMove (int t, int o) {
       
    72 		int steps;
       
    73 		int rows;
       
    74 		int dx;
       
    75 
       
    76 		if ( t == o )
       
    77 			return false;
       
    78 
       
    79 		if (isSamePiece(t, o))
       
    80 			return false;
       
    81 
       
    82 		if (getType(o) == EMPTY)
       
    83 			return false;
       
    84 
       
    85 		/*
       
    86 		 *	00 01 02 03 04 05 06 07
       
    87 		 *	08 09 10 11 12 13 14 15
       
    88 		 *	16 17 18 19 20 21 22 23
       
    89 		 *	24 25 26 27 28 29 30 31
       
    90 		 *	32 33 34 35 36 37 38 39
       
    91 		 *	40 41 42 43 44 45 46 47
       
    92 		 *	48 49 50 51 52 53 54 55
       
    93 		 *	56 57 58 59 60 61 62 63
       
    94 		 */
       
    95 
       
    96 		steps = Math.abs(t-o);
       
    97 		rows = Math.abs(t/8 - o/8);
       
    98 
       
    99 		switch( getType(o) ) {
       
   100 		case PIECE:
       
   101 			if (steps % 7 == 0 ) {
       
   102 				if (steps == 7 && rows == 1 && isEmpty(t)) {
       
   103 					if ( isBlack(o) && (t>o) )
       
   104 						return true;
       
   105 					if ( isWhite(o) && (t<o) )
       
   106 						return true;
       
   107 				}
       
   108 
       
   109 				if (steps == 14 && rows == 2  && isEmpty(t)) {
       
   110 					if ( isBlack(o) && (t>o) && isEnemy(o+7, o) )
       
   111 						return true;
       
   112 					if ( isWhite(o) && (t<o) && isEnemy(o-7, o) )
       
   113 						return true;
       
   114 				}
       
   115 			}
       
   116 
       
   117 			if (steps % 9 == 0 ) {
       
   118 				if (steps == 9 && rows == 1 && isEmpty(t)) {
       
   119 					if ( isBlack(o) && (t>o) )
       
   120 						return true;
       
   121 					if ( isWhite(o) && (t<o) )
       
   122 						return true;
       
   123 				}
       
   124 
       
   125 				if (steps == 18 && rows == 2  && isEmpty(t)) {
       
   126 					if ( isBlack(o) && (t>o) && isEnemy(o+9, o) )
       
   127 						return true;
       
   128 					if ( isWhite(o) && (t<o) && isEnemy(o-9, o) )
       
   129 						return true;
       
   130 				}
       
   131 			}
       
   132 			break;
       
   133 		case QUEEN:
       
   134 			if (steps % 7 == 0 && isEmpty(t) ) {
       
   135 				if (steps == 7 && rows == 1)
       
   136 					return true;
       
   137 				if (steps == 14 && rows == 2)
       
   138 					return true;
       
   139 				if (steps == 21 && rows == 3)
       
   140 					return true;
       
   141 				if (steps == 28 && rows == 4)
       
   142 					return true;
       
   143 				if (steps == 35 && rows == 5)
       
   144 					return true;
       
   145 				if (steps == 42 && rows == 6)
       
   146 					return true;
       
   147 				if (steps == 49 && rows == 7)
       
   148 					return true;
       
   149 			}
       
   150 
       
   151 			if (steps % 9 == 0 && isEmpty(t) ) {
       
   152 				if (steps == 9 && rows == 1)
       
   153 					return true;
       
   154 				if (steps == 18 && rows == 2)
       
   155 					return true;
       
   156 				if (steps == 27 && rows == 3)
       
   157 					return true;
       
   158 				if (steps == 36 && rows == 4)
       
   159 					return true;
       
   160 				if (steps == 45 && rows == 5)
       
   161 					return true;
       
   162 				if (steps == 54 && rows == 6)
       
   163 					return true;
       
   164 				if (steps == 63 && rows == 7)
       
   165 					return true;
       
   166 			}
       
   167 			break;
       
   168 		}
       
   169 		return false;
       
   170 	}
       
   171 
       
   172 	/**
       
   173 	 * pop: undo the push operation.
       
   174 	 */
       
   175 	public void undo() {
       
   176 		History h1 = null;
       
   177 		History h2 = null;
       
   178 		int size = stack.size();
       
   179 		int pos;
       
   180 
       
   181 		if ( size < 2 )
       
   182 			return;
       
   183 
       
   184 		h1 = stack.elementAt(size-1);
       
   185 		h2 = stack.elementAt(size-2);
       
   186 
       
   187 		setPiece(h1.pos(), h1.piece());
       
   188 		setPiece(h2.pos(), h2.piece());
       
   189 
       
   190 		if ( getType(h2.pos()) != EMPTY ) {
       
   191 			pos = 2*h2.pos()-h1.pos();
       
   192 			if ( pos >= 0 && pos <64)
       
   193 				setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   194 		}
       
   195 
       
   196 		stack.setSize(size-2);
       
   197 
       
   198 		/* Reset the current player */
       
   199 		setBlacksTurn(h2.turn());
       
   200 	}
       
   201 
       
   202 	/**
       
   203 	 * simulates a "valid" move or returns false.
       
   204 	 * all changes are relative to pieces and will not be painted.
       
   205 	 */
       
   206 	public boolean simulateMove(int t, int o) {
       
   207 		int steps = Math.abs(t-o);
       
   208 		int rows = Math.abs(t/8 - o/8);
       
   209 		int pos;
       
   210 
       
   211 		if ( !validMove(t, o) ) {
       
   212 			return false;
       
   213 		}
       
   214 
       
   215 		if ( rows == 2 && getType(o) == PIECE) {
       
   216 			pos =  (t-o)/2 + o;
       
   217 			push(getPiece(pos), pos, getPiece(o), o);
       
   218 			setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   219 		} else
       
   220 			push(getPiece(t), t, getPiece(o), o);
       
   221 
       
   222 		setPiece(t, new Piece(getPiece(o)));
       
   223 		setPiece(o, new Piece(null, null, EMPTY, 0));
       
   224 
       
   225 		return true;
       
   226 	}
       
   227 
       
   228 	/**
       
   229 	 * performs a "valid" move or returns false.
       
   230 	 * all changes are relative to board_pieces and will be painted.
       
   231 	 */
       
   232 	public boolean doMove(int t, int o) {
       
   233 		int steps = Math.abs(t-o);
       
   234 		int rows = Math.abs(t/8-o/8);
       
   235 		int pos;
       
   236 
       
   237 		if ( !validMove(t, o) ) {
       
   238 			return false;
       
   239 		}
       
   240 
       
   241 		if ( rows == 2 && getType(o) == PIECE) {
       
   242 			pos =  (t-o)/2 + o;
       
   243 			push(getPiece(pos), pos, getPiece(o), o);
       
   244 			setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   245 		} else	if ( rows >= 2 && getType(o) == QUEEN ) {
       
   246 			if (steps % 7 == 0 ) {
       
   247 				if ( t > o )
       
   248 					for ( pos=o+7;pos <= t-7;pos+=7 ) {
       
   249 						if ( isEnemy(pos, o) ) {
       
   250 							push(getPiece(pos), pos, getPiece(o), o);
       
   251 							setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   252 						}
       
   253 					}
       
   254 				else {
       
   255 					for ( pos=o-7;pos >= t+7;pos-=7 ) {
       
   256 						if ( isEnemy(pos, o) ) {
       
   257 							push(getPiece(pos), pos, getPiece(o), o);
       
   258 							setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   259 						}
       
   260 					}
       
   261 				}
       
   262 			}
       
   263 
       
   264 			if (steps % 9 == 0 ) {
       
   265 				if ( t > o )
       
   266 					for ( pos=o+9;pos <= t-9;pos+=9 ) {
       
   267 						if ( isEnemy(pos, o) ) {
       
   268 							push(getPiece(pos), pos, getPiece(o), o);
       
   269 							setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   270 						}
       
   271 					}
       
   272 				else {
       
   273 					for ( pos=o-9;pos >= t+9;pos-=9 ) {
       
   274 						if ( isEnemy(pos, o) ) {
       
   275 							push(getPiece(pos), pos, getPiece(o), o);
       
   276 							setPiece(pos, new Piece(null, null, EMPTY, 0));
       
   277 						}
       
   278 					}
       
   279 				}
       
   280 			}
       
   281 		}
       
   282 
       
   283 		if ( t <= 7 && getType(o) == PIECE)
       
   284 			setPiece(t, new Piece(white_super_sun));
       
   285 		else if (t >=56 && getType(o) == PIECE)
       
   286 			setPiece(t, new Piece(black_super_sun));
       
   287 		else
       
   288 			setPiece(t, new Piece(getPiece(o)));
       
   289 
       
   290 		setPiece(o, new Piece(null, null, EMPTY, 0));
       
   291 
       
   292 		board_pieces = getPieces();
       
   293 		repaint();
       
   294 
       
   295 		moveNr = stack.size();
       
   296 
       
   297 		return true;
       
   298 	}
       
   299 }