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