org/homelinux/largo/games/board/chessboard/ChessBoard.java
changeset 13 f83884cc7d2f
parent 11 1afe167876fb
child 14 f12f77aa13b2
equal deleted inserted replaced
12:d28c1e402d82 13:f83884cc7d2f
    10 import org.homelinux.largo.games.board.Board;
    10 import org.homelinux.largo.games.board.Board;
    11 import org.homelinux.largo.games.board.Piece;
    11 import org.homelinux.largo.games.board.Piece;
    12 import org.homelinux.largo.utils.ImgComponent;
    12 import org.homelinux.largo.utils.ImgComponent;
    13 
    13 
    14 public class ChessBoard extends Board {
    14 public class ChessBoard extends Board {
    15 	static final long serialVersionUID = 140208;
    15     static final long serialVersionUID = 140208;
    16 
    16 
    17 	static final int ROOK = 1;
    17     static final int ROOK = 1;
    18 	static final int KNIGHT = 2;
    18     static final int KNIGHT = 2;
    19 	static final int BISHOP = 3;
    19     static final int BISHOP = 3;
    20 	static final int QUEEN = 4;
    20     static final int QUEEN = 4;
    21 	static final int KING = 5;
    21     static final int KING = 5;
    22 	static final int PAWN = 6;
    22     static final int PAWN = 6;
    23 
    23 
    24 	final Piece white_queen = new Piece(new ImgComponent("images/white_queen.png").getImage(),
    24     final Piece white_queen = new Piece(new ImgComponent("images/white_queen.png").getImage(), "white", QUEEN, 90);
    25 					"white", QUEEN, 90);
    25     final Piece black_queen = new Piece(new ImgComponent("images/black_queen.png").getImage(), "black", QUEEN, 90);
    26 	final Piece black_queen = new Piece(new ImgComponent("images/black_queen.png").getImage(),
    26 
    27 					"black", QUEEN, 90);
    27     boolean white_rochades_small;
    28 
    28     boolean white_rochades_big;
    29 	boolean white_rochades_small;
    29     boolean black_rochades_small;
    30 	boolean white_rochades_big;
    30     boolean black_rochades_big;
    31 	boolean black_rochades_small;
    31 
    32 	boolean black_rochades_big;
    32     void initBoard() {
    33 
    33         add(new ImgComponent("images/black_rook.png").getImage(), 0, "black", ROOK);
    34 	void initBoard () {
    34         add(new ImgComponent("images/black_knight.png").getImage(), 1, "black", KNIGHT);
    35 		add (new ImgComponent ("images/black_rook.png").getImage (), 0, "black", ROOK);
    35         add(new ImgComponent("images/black_bishop.png").getImage(), 2, "black", BISHOP);
    36 		add (new ImgComponent ("images/black_knight.png").getImage (), 1, "black", KNIGHT);
    36         add(new ImgComponent("images/black_queen.png").getImage(), 3, "black", QUEEN);
    37 		add (new ImgComponent ("images/black_bishop.png").getImage (), 2, "black", BISHOP);
    37         add(new ImgComponent("images/black_king.png").getImage(), 4, "black", KING);
    38 		add (new ImgComponent ("images/black_queen.png").getImage (), 3, "black", QUEEN);
    38         add(new ImgComponent("images/black_bishop.png").getImage(), 5, "black", BISHOP);
    39 		add (new ImgComponent ("images/black_king.png").getImage (), 4, "black", KING);
    39         add(new ImgComponent("images/black_knight.png").getImage(), 6, "black", KNIGHT);
    40 		add (new ImgComponent ("images/black_bishop.png").getImage (), 5, "black", BISHOP);
    40         add(new ImgComponent("images/black_rook.png").getImage(), 7, "black", ROOK);
    41 		add (new ImgComponent ("images/black_knight.png").getImage (), 6, "black", KNIGHT);
    41 
    42 		add (new ImgComponent ("images/black_rook.png").getImage (), 7, "black", ROOK);
    42         for (int i = 0; i < 8; i++)
    43 
    43             add(new ImgComponent("images/black_pawn.png").getImage(), 8 + i, "black", PAWN);
    44 		for (int i = 0; i < 8; i++)
    44 
    45 			add (new ImgComponent ("images/black_pawn.png").getImage (), 8 + i, "black", PAWN);
    45         add(new ImgComponent("images/white_rook.png").getImage(), 56, "white", ROOK);
    46 
    46         add(new ImgComponent("images/white_knight.png").getImage(), 57, "white", KNIGHT);
    47 		add (new ImgComponent ("images/white_rook.png").getImage (), 56, "white", ROOK);
    47         add(new ImgComponent("images/white_bishop.png").getImage(), 58, "white", BISHOP);
    48 		add (new ImgComponent ("images/white_knight.png").getImage (), 57, "white", KNIGHT);
    48         add(new ImgComponent("images/white_queen.png").getImage(), 59, "white", QUEEN);
    49 		add (new ImgComponent ("images/white_bishop.png").getImage (), 58, "white", BISHOP);
    49         add(new ImgComponent("images/white_king.png").getImage(), 60, "white", KING);
    50 		add (new ImgComponent ("images/white_queen.png").getImage (), 59, "white", QUEEN);
    50         add(new ImgComponent("images/white_bishop.png").getImage(), 61, "white", BISHOP);
    51 		add (new ImgComponent ("images/white_king.png").getImage (), 60, "white", KING);
    51         add(new ImgComponent("images/white_knight.png").getImage(), 62, "white", KNIGHT);
    52 		add (new ImgComponent ("images/white_bishop.png").getImage (), 61, "white", BISHOP);
    52         add(new ImgComponent("images/white_rook.png").getImage(), 63, "white", ROOK);
    53 		add (new ImgComponent ("images/white_knight.png").getImage (), 62, "white", KNIGHT);
    53 
    54 		add (new ImgComponent ("images/white_rook.png").getImage (), 63, "white", ROOK);
    54         for (int i = 0; i < 8; i++)
    55 
    55             add(new ImgComponent("images/white_pawn.png").getImage(), 55 - i, "white", PAWN);
    56 		for (int i = 0; i < 8; i++)
    56     }
    57 			add (new ImgComponent ("images/white_pawn.png").getImage (), 55 - i, "white", PAWN);
    57 
    58 	}
    58     /**
    59 
    59      * The init method initializes a complete chess board with figures and a
    60 	/**
    60      * history.
    61 	 * The init method initializes a complete chess board with figures and a history.
    61      */
    62 	 */
    62     public void init() {
    63 	public void init() {
    63         super.init();
    64 		super.init();
    64         initBoard();
    65 		initBoard();
    65         board_pieces = getPieces();
    66 		board_pieces = getPieces();
    66         repaint();
    67 		repaint();
    67     }
    68 	}
    68 
    69 
    69     public ChessBoard(int w, int h) {
    70 	public ChessBoard (int w, int h) {
    70         super(w, h);
    71 		super(w, h);
    71     }
    72 	}
    72 
    73 
    73     void add(Image img, int i, String c, int t) {
    74 	void add (Image img, int i, String c, int t) {
    74         int v = 0;
    75 		int v = 0;
    75 
    76 
    76         switch (t) {
    77 		switch (t) {
    77         case ROOK:
    78 		case ROOK:
    78             v = 80;
    79 			v = 80;
    79             break;
    80 			break;
    80         case KNIGHT:
    81 		case KNIGHT:
    81             v = 30;
    82 			v = 30;
    82             break;
    83 			break;
    83         case BISHOP:
    84 		case BISHOP:
    84             v = 30;
    85 			v = 30;
    85             break;
    86 			break;
    86         case QUEEN:
    87 		case QUEEN:
    87             v = 90;
    88 			v = 90;
    88             break;
    89 			break;
    89         case KING:
    90 		case KING:
    90             v = 120;
    91 			v = 120;
    91             break;
    92 			break;
    92         case PAWN:
    93 		case PAWN:
    93             v = 10;
    94 			v = 10;
    94             break;
    95 			break;
    95         }
    96 		}
    96 
    97 
    97         setPiece(i, new Piece(img, c, t, v));
    98 		setPiece(i, new Piece(img, c, t, v));
    98     }
    99 	}
    99 
   100 
   100     /**
   101 	/**
   101      * returns true, when the white king is at pieces[i]
   102 	 * returns true, when the white king is at pieces[i]
   102      */
   103 	 */
   103     public boolean isWhiteKing(int i) {
   104 	public boolean isWhiteKing(int i) {
   104         if (isWhite(i) && getType(i) == KING)
   105 		if ( isWhite(i) && getType(i) == KING)
   105             return true;
   106 			return true;
   106         return false;
   107 		return false;
   107     }
   108 	}
   108 
   109 
   109     /**
   110 	/**
   110      * returns true, when the black king is at pieces[i]
   111 	 * returns true, when the black king is at pieces[i]
   111      */
   112 	 */
   112     public boolean isBlackKing(int i) {
   113 	public boolean isBlackKing(int i) {
   113         if (isBlack(i) && getType(i) == KING)
   114 		if ( isBlack(i) && getType(i) == KING)
   114             return true;
   115 			return true;
   115         return false;
   116 		return false;
   116     }
   117 	}
   117 
   118 
   118     /**
   119 	/**
   119      * Returns the score of the current position Every field controlled by
   120 	 * Returns the score of the current position
   120      * [color] gives one point A King, controlled by [color], gives 10 extra
   121 	 * Every field controlled by [color] gives one point
   121      * points
   122 	 * A King, controlled by [color], gives 10 extra points
   122      */
   123 	 */
   123     public int controls(String color) {
   124 	public int controls(String color) {
   124         int t, o;
   125 		int t, o;
   125         int value = 0;
   126 		int value = 0;
   126 
   127 
   127         for (o = 0; o < 64; o++) {
   128 		for ( o=0;o<64;o++) {
   128             if (color.equals(getColor(o))) {
   129 			if (color.equals(getColor(o))) {
   129                 for (t = 0; t < 64; t++) {
   130 				for (t=0;t<64;t++) {
   130                     if (validMove(t, o)) {
   131 					if(validMove(t, o)) {
   131                         if (getType(t) == KING)
   132 						if ( getType(t) == KING )
   132                             value += 10;
   133 							value+=10;
   133                         value++;
   134 						value++;
   134                     }
   135 					}
   135                 }
   136 				}
   136             }
   137 			}
   137         }
   138 		}
   138         return value;
   139 		return value;
   139     }
   140 	}
   140 
   141 
   141     /**
   142 	/**
   142      * Checks, wether this move is possible or not.
   143 	 * Checks, wether this move is possible or not.
   143      */
   144 	 */
   144     public boolean validMove(int t, int o) {
   145 	public boolean validMove (int t, int o) {
   145         int steps;
   146 		int steps;
   146         int rows;
   147 		int rows;
   147         int dx;
   148 		int dx;
   148 
   149 
   149         /* Must be here */
   150 		/* Must be here */
   150         white_rochades_small = false;
   151 		white_rochades_small = false;
   151         white_rochades_big = false;
   152 		white_rochades_big = false;
   152         black_rochades_small = false;
   153 		black_rochades_small = false;
   153         black_rochades_big = false;
   154 		black_rochades_big = false;
   154 
   155 
   155         if (t == o)
   156 		if ( t == o )
   156             return false;
   157 			return false;
   157 
   158 
   158         if (isSamePiece(t, o))
   159 		if (isSamePiece(t, o))
   159             return false;
   160 			return false;
   160 
   161 
   161         if (getType(o) == EMPTY)
   162 		if (getType(o) == EMPTY)
   162             return false;
   163 			return false;
   163 
   164 
   164         /*
   165 		/*
   165          * 00 01 02 03 04 05 06 07
   166 		 *	00 01 02 03 04 05 06 07
   166          * 08 09 10 11 12 13 14 15
   167 		 *	08 09 10 11 12 13 14 15
   167          * 16 17 18 19 20 21 22 23
   168 		 *	16 17 18 19 20 21 22 23
   168          * 24 25 26 27 28 29 30 31
   169 		 *	24 25 26 27 28 29 30 31
   169          * 32 33 34 35 36 37 38 39
   170 		 *	32 33 34 35 36 37 38 39
   170          * 40 41 42 43 44 45 46 47
   171 		 *	40 41 42 43 44 45 46 47
   171          * 48 49 50 51 52 53 54 55
   172 		 *	48 49 50 51 52 53 54 55
   172          * 56 57 58 59 60 61 62 63
   173 		 *	56 57 58 59 60 61 62 63
   173          */
   174 		 */
   174 
   175 
   175         steps = Math.abs(t - o);
   176 		steps = Math.abs(t-o);
   176         rows = Math.abs(t / 8 - o / 8);
   177 		rows = Math.abs(t/8 - o/8);
   177 
   178 
   178         switch (getType(o)) {
   179 		switch( getType(o) ) {
   179         case ROOK:
   180 		case ROOK:
   180             if (steps < 8 && rows == 0) {
   181 			if (steps < 8 && rows == 0 ) {
   181                 if (t > o)
   182 				if(t>o)
   182                     for (dx = o + 1; dx < t; dx++) {
   183 					for ( dx = o+1; dx < t; dx++ ) {
   183                         if (getColor(dx) != null) {
   184 						if ( getColor(dx) != null ) {
   184                             return false;
   185 							return false;
   185                         }
   186 						}
   186                     }
   187 					}
   187                 else
   188 				else
   188                     for (dx = o - 1; dx > t; dx--) {
   189 					for ( dx = o-1; dx > t; dx-- ) {
   189                         if (getColor(dx) != null) {
   190 						if ( getColor(dx) != null ) {
   190                             return false;
   191 							return false;
   191                         }
   192 						}
   192                     }
   193 					}
   193                 return true;
   194 				return true;
   194             }
   195 			}
   195             if (steps % 8 == 0) {
   196 			if (steps % 8 == 0 ) {
   196                 if (t > o) {
   197 				if ( t>o ) {
   197                     for (dx = o + 8; dx < t; dx += 8) {
   198 					for ( dx = o+8;  dx < t; dx+=8 ) {
   198                         if (getColor(dx) != null)
   199 						if (getColor(dx) != null)
   199                             return false;
   200 							return false;
   200                     }
   201 					}
   201                 } else {
   202 				} else {
   202                     for (dx = o - 8; dx > t; dx -= 8) {
   203 					for ( dx = o-8;  dx > t; dx-=8 ) {
   203                         if (getColor(dx) != null)
   204 						if (getColor(dx) != null)
   204                             return false;
   205 							return false;
   205                     }
   206 					}
   206                 }
   207 				}
   207                 if (steps == 8 && rows == 1)
   208 				if (steps == 8 && rows == 1)
   208                     return true;
   209 					return true;
   209                 if (steps == 16 && rows == 2)
   210 				if (steps == 16 && rows == 2)
   210                     return true;
   211 					return true;
   211                 if (steps == 24 && rows == 3)
   212 				if (steps == 24 && rows == 3)
   212                     return true;
   213 					return true;
   213                 if (steps == 32 && rows == 4)
   214 				if (steps == 32 && rows == 4)
   214                     return true;
   215 					return true;
   215                 if (steps == 40 && rows == 5)
   216 				if (steps == 40 && rows == 5)
   216                     return true;
   217 					return true;
   217                 if (steps == 48 && rows == 6)
   218 				if (steps == 48 && rows == 6)
   218                     return true;
   219 					return true;
   219                 if (steps == 56 && rows == 7)
   220 				if (steps == 56 && rows == 7)
   220                     return true;
   221 					return true;
   221             }
   222 			}
   222             break;
   223 			break;
   223         case KNIGHT:
   224 		case KNIGHT:
   224             /* works fine */
   225 			/* works fine */
   225             if (steps == 6 && rows == 1)
   226 			if (steps == 6 && rows == 1)
   226                 return true;
   227 				return true;
   227             if (steps == 10 && rows == 1)
   228 			if (steps == 10 && rows == 1)
   228                 return true;
   229 				return true;
   229             if (steps == 15 && rows == 2)
   230 			if (steps == 15 && rows == 2)
   230                 return true;
   231 				return true;
   231             if (steps == 17 && rows == 2)
   232 			if (steps == 17 && rows == 2)
   232                 return true;
   233 				return true;
   233             break;
   234 			break;
   234         case BISHOP:
   235 		case BISHOP:
   235             if (steps % 7 == 0) {
   236 			if (steps % 7 == 0 ) {
   236                 if (t > o) {
   237 				if ( t>o ) {
   237                     for (dx = o + 7; dx < t; dx += 7) {
   238 					for ( dx = o+7;  dx < t; dx+=7 ) {
   238                         if (getColor(dx) != null)
   239 						if (getColor(dx) != null)
   239                             return false;
   240 							return false;
   240                     }
   241 					}
   241                 } else {
   242 				} else {
   242                     for (dx = o - 7; dx > t; dx -= 7) {
   243 					for ( dx = o-7;  dx > t; dx-=7 ) {
   243                         if (getColor(dx) != null)
   244 						if (getColor(dx) != null)
   244                             return false;
   245 							return false;
   245                     }
   246 					}
   246                 }
   247 				}
   247                 if (steps == 7 && rows == 1)
   248 				if (steps == 7 && rows == 1)
   248                     return true;
   249 					return true;
   249                 if (steps == 14 && rows == 2)
   250 				if (steps == 14 && rows == 2)
   250                     return true;
   251 					return true;
   251                 if (steps == 21 && rows == 3)
   252 				if (steps == 21 && rows == 3)
   252                     return true;
   253 					return true;
   253                 if (steps == 28 && rows == 4)
   254 				if (steps == 28 && rows == 4)
   254                     return true;
   255 					return true;
   255                 if (steps == 35 && rows == 5)
   256 				if (steps == 35 && rows == 5)
   256                     return true;
   257 					return true;
   257                 if (steps == 42 && rows == 6)
   258 				if (steps == 42 && rows == 6)
   258                     return true;
   259 					return true;
   259                 if (steps == 49 && rows == 7)
   260 				if (steps == 49 && rows == 7)
   260                     return true;
   261 					return true;
   261             }
   262 			}
   262             if (steps % 9 == 0) {
   263 			if (steps % 9 == 0 ) {
   263                 if (t > o) {
   264 				if ( t>o ) {
   264                     for (dx = o + 9; dx < t; dx += 9) {
   265 					for ( dx = o+9;  dx < t; dx+=9 ) {
   265                         if (getColor(dx) != null)
   266 						if (getColor(dx) != null)
   266                             return false;
   267 							return false;
   267                     }
   268 					}
   268                 } else {
   269 				} else {
   269                     for (dx = o - 9; dx > t; dx -= 9) {
   270 					for ( dx = o-9; dx > t; dx-=9 ) {
   270                         if (getColor(dx) != null)
   271 						if (getColor(dx) != null)
   271                             return false;
   272 							return false;
   272                     }
   273 					}
   273                 }
   274 				}
   274                 if (steps == 9 && rows == 1)
   275 				if (steps == 9 && rows == 1)
   275                     return true;
   276 					return true;
   276                 if (steps == 18 && rows == 2)
   277 				if (steps == 18 && rows == 2)
   277                     return true;
   278 					return true;
   278                 if (steps == 27 && rows == 3)
   279 				if (steps == 27 && rows == 3)
   279                     return true;
   280 					return true;
   280                 if (steps == 36 && rows == 4)
   281 				if (steps == 36 && rows == 4)
   281                     return true;
   282 					return true;
   282                 if (steps == 45 && rows == 5)
   283 				if (steps == 45 && rows == 5)
   283                     return true;
   284 					return true;
   284                 if (steps == 54 && rows == 6)
   285 				if (steps == 54 && rows == 6)
   285                     return true;
   286 					return true;
   286                 if (steps == 63 && rows == 7)
   287 				if (steps == 63 && rows == 7)
   287                     return true;
   288 					return true;
   288             }
   289 			}
   289             break;
   290 			break;
   290 
   291 
   291         case QUEEN:
   292 		case QUEEN:
   292             if (steps < 8 && rows == 0) {
   293 			if (steps < 8 && rows == 0 ) {
   293                 if (t > o)
   294 				if(t>o)
   294                     for (dx = o + 1; dx < t; dx++) {
   295 					for ( dx = o+1; dx < t; dx++ ) {
   295                         if (getColor(dx) != null)
   296 						if ( getColor(dx) != null )
   296                             return false;
   297 							return false;
   297                     }
   298 					}
   298                 else
   299 				else
   299                     for (dx = o - 1; dx > t; dx--) {
   300 					for ( dx = o-1; dx > t; dx-- ) {
   300                         if (getColor(dx) != null)
   301 						if ( getColor(dx) != null )
   301                             return false;
   302 							return false;
   302                     }
   303 					}
   303                 return true;
   304 				return true;
   304             }
   305 			}
   305 
   306 
   306             if (steps % 8 == 0) {
   307 			if (steps % 8 == 0 ) {
   307                 if (t > o) {
   308 				if ( t>o ) {
   308                     for (dx = o + 8; dx < t; dx += 8) {
   309 					for ( dx = o+8;  dx < t; dx+=8 ) {
   309                         if (getColor(dx) != null)
   310 						if (getColor(dx) != null)
   310                             return false;
   311 							return false;
   311                     }
   312 					}
   312                 } else {
   313 				} else {
   313                     for (dx = o - 8; dx > t; dx -= 8) {
   314 					for ( dx = o-8;  dx > t; dx-=8 ) {
   314                         if (getColor(dx) != null)
   315 						if (getColor(dx) != null)
   315                             return false;
   316 							return false;
   316                     }
   317 					}
   317                 }
   318 				}
   318 
   319 
   319                 if (steps == 8 && rows == 1)
   320 				if (steps == 8 && rows == 1)
   320                     return true;
   321 					return true;
   321                 if (steps == 16 && rows == 2)
   322 				if (steps == 16 && rows == 2)
   322                     return true;
   323 					return true;
   323                 if (steps == 24 && rows == 3)
   324 				if (steps == 24 && rows == 3)
   324                     return true;
   325 					return true;
   325                 if (steps == 32 && rows == 4)
   326 				if (steps == 32 && rows == 4)
   326                     return true;
   327 					return true;
   327                 if (steps == 40 && rows == 5)
   328 				if (steps == 40 && rows == 5)
   328                     return true;
   329 					return true;
   329                 if (steps == 48 && rows == 6)
   330 				if (steps == 48 && rows == 6)
   330                     return true;
   331 					return true;
   331                 if (steps == 56 && rows == 7)
   332 				if (steps == 56 && rows == 7)
   332                     return true;
   333 					return true;
   333             }
   334 			}
   334             if (steps % 7 == 0) {
   335 			if (steps % 7 == 0 ) {
   335                 if (t > o) {
   336 				if ( t>o ) {
   336                     for (dx = o + 7; dx < t; dx += 7) {
   337 					for ( dx = o+7;  dx < t; dx+=7 ) {
   337                         if (getColor(dx) != null)
   338 						if (getColor(dx) != null)
   338                             return false;
   339 							return false;
   339                     }
   340 					}
   340                 } else {
   341 				} else {
   341                     for (dx = o - 7; dx > t; dx -= 7) {
   342 					for ( dx = o-7;  dx > t; dx-=7 ) {
   342                         if (getColor(dx) != null)
   343 						if (getColor(dx) != null)
   343                             return false;
   344 							return false;
   344                     }
   345 					}
   345                 }
   346 				}
   346                 if (steps == 7 && rows == 1)
   347 				if (steps == 7 && rows == 1)
   347                     return true;
   348 					return true;
   348                 if (steps == 14 && rows == 2)
   349 				if (steps == 14 && rows == 2)
   349                     return true;
   350 					return true;
   350                 if (steps == 21 && rows == 3)
   351 				if (steps == 21 && rows == 3)
   351                     return true;
   352 					return true;
   352                 if (steps == 28 && rows == 4)
   353 				if (steps == 28 && rows == 4)
   353                     return true;
   354 					return true;
   354                 if (steps == 35 && rows == 5)
   355 				if (steps == 35 && rows == 5)
   355                     return true;
   356 					return true;
   356                 if (steps == 42 && rows == 6)
   357 				if (steps == 42 && rows == 6)
   357                     return true;
   358 					return true;
   358                 if (steps == 49 && rows == 7)
   359 				if (steps == 49 && rows == 7)
   359                     return true;
   360 					return true;
   360             }
   361 			}
   361 
   362 
   362             if (steps % 9 == 0) {
   363 			if (steps % 9 == 0 ) {
   363                 if (t > o) {
   364 				if ( t>o ) {
   364                     for (dx = o + 9; dx < t; dx += 9) {
   365 					for ( dx = o+9;  dx < t; dx+=9 ) {
   365                         if (getColor(dx) != null)
   366 						if (getColor(dx) != null)
   366                             return false;
   367 							return false;
   367                     }
   368 					}
   368                 } else {
   369 				} else {
   369                     for (dx = o - 9; dx > t; dx -= 9) {
   370 					for ( dx = o-9;  dx > t; dx -=9 ) {
   370                         if (getColor(dx) != null)
   371 						if (getColor(dx) != null)
   371                             return false;
   372 							return false;
   372                     }
   373 					}
   373                 }
   374 				}
   374                 if (steps == 9 && rows == 1)
   375 				if (steps == 9 && rows == 1)
   375                     return true;
   376 					return true;
   376                 if (steps == 18 && rows == 2)
   377 				if (steps == 18 && rows == 2)
   377                     return true;
   378 					return true;
   378                 if (steps == 27 && rows == 3)
   379 				if (steps == 27 && rows == 3)
   379                     return true;
   380 					return true;
   380                 if (steps == 36 && rows == 4)
   381 				if (steps == 36 && rows == 4)
   381                     return true;
   382 					return true;
   382                 if (steps == 45 && rows == 5)
   383 				if (steps == 45 && rows == 5)
   383                     return true;
   384 					return true;
   384                 if (steps == 54 && rows == 6)
   385 				if (steps == 54 && rows == 6)
   385                     return true;
   386 					return true;
   386                 if (steps == 63 && rows == 7)
   387 				if (steps == 63 && rows == 7)
   387                     return true;
   388 					return true;
   388             }
   389 			}
   389             break;
   390 			break;
   390 
   391 
   391         case KING:
   392 		case KING:
   392             if ((steps == 1) && (rows == 0))
   393 			if ( (steps == 1) && (rows == 0) )
   393                 return true;
   394 				return true;
   394 
   395 
   395             if ((steps == 8) && (rows == 1))
   396 			if ( (steps == 8) && (rows == 1) )
   396                 return true;
   397 				return true;
   397 
   398 
   398             if ((steps == 7) && (rows == 1))
   399 			if ( (steps == 7) && (rows == 1) )
   399                 return true;
   400 				return true;
   400 
   401 
   401             if ((steps == 9) && (rows == 1))
   402 			if ( (steps == 9) && (rows == 1) )
   402                 return true;
   403 				return true;
   403 
   404 
   404             if ((steps == 2) && (rows == 0)) {
   405 			if ( (steps == 2) && (rows == 0) ) {
   405                 if (isWhiteKing(o)) { /* White: Rochade */
   406 				if ( isWhiteKing(o) ) {		/* White: Rochade */
   406                     if (isWhiteKing(60) && isEmpty(61) && isEmpty(62) && (t == 62)) {
   407 					if ( isWhiteKing(60) && isEmpty(61) && isEmpty(62) && (t==62) ) {
   407                         if ((getType(63) == ROOK) && isWhite(63)) {
   408 						if ( (getType(63) == ROOK) && isWhite(63) ) {
   408                             white_rochades_small = true;
   409 							white_rochades_small = true;
   409                             return true;
   410 							return true;
   410                         }
   411 						}
   411                     }
   412 					}
   412 
   413 
   413                     if (isWhiteKing(60) && isEmpty(59) && isEmpty(58) && isEmpty(57) && (t == 58)) {
   414 					if ( isWhiteKing(60) && isEmpty(59) && isEmpty(58) && isEmpty(57) && (t==58) ) {
   414                         if ((getType(56) == ROOK) && isWhite(56)) {
   415 						if ( (getType(56) == ROOK) && isWhite(56) ) {
   415                             white_rochades_big = true;
   416 							white_rochades_big = true;
   416                             return true;
   417 							return true;
   417                         }
   418 						}
   418                     }
   419 					}
   419                 }
   420 				}
   420 
   421 
   421                 if (isBlackKing(o)) { /* Black: Rochade */
   422 				if ( isBlackKing(o) ) {		/* Black: Rochade */
   422                     if (isBlackKing(4) && isEmpty(5) && isEmpty(6) && (t == 6)) {
   423 					if ( isBlackKing(4) && isEmpty(5) && isEmpty(6) && (t==6) ) {
   423                         if ((getType(7) == ROOK) && isBlack(7)) {
   424 						if ( (getType(7) == ROOK) && isBlack(7) ) {
   424                             black_rochades_small = true;
   425 							black_rochades_small = true;
   425                             return true;
   426 							return true;
   426                         }
   427 						}
   427                     }
   428 					}
   428 
   429 
   429                     if (isBlackKing(4) && isEmpty(3) && isEmpty(2) && isEmpty(1) && (t == 2)) {
   430 					if ( isBlackKing(4) && isEmpty(3) && isEmpty(2) && isEmpty(1) && (t==2) ) {
   430                         if ((getType(0) == ROOK) && isBlack(0)) {
   431 						if ( (getType(0) == ROOK) && isBlack(0) ) {
   431                             black_rochades_big = true;
   432 							black_rochades_big = true;
   432                             return true;
   433 							return true;
   433                         }
   434 						}
   434                     }
   435 					}
   435                 }
   436 				}
   436             }
   437 			}
   437             break;
   438 			break;
   438 
   439 
   439         case PAWN:
   440 		case PAWN:
   440             if (getColor(o).equals("white")) {
   441 			if (getColor(o).equals("white")) {
   441                 if (steps == 7 && rows == 1 && isEnemy(t, o) && t < o)
   442 				if (steps == 7 && rows == 1 && isEnemy(t, o) && t<o)
   442                     return true;
   443 					return true;
   443 
   444 
   444                 if (steps == 9 && rows == 1 && isEnemy(t, o) && t < o)
   445 				if (steps == 9 && rows == 1 && isEnemy(t, o) && t<o)
   445                     return true;
   446 					return true;
   446             } else {
   447 			} else {
   447                 if (steps == 7 && rows == 1 && isEnemy(t, o) && t > o)
   448 				if (steps == 7 && rows == 1 && isEnemy(t, o) && t>o)
   448                     return true;
   449 					return true;
   449 
   450 
   450                 if (steps == 9 && rows == 1 && isEnemy(t, o) && t > o)
   451 				if (steps == 9 && rows == 1 && isEnemy(t, o) && t>o)
   451                     return true;
   452 					return true;
   452             }
   453 			}
   453 
   454 
   454             if (steps % 8 == 0) {
   455 			if (steps % 8 == 0 ) {
   455                 if (t > o) {
   456 				if ( t>o ) {
   456                     for (dx = o + 8; dx <= t; dx += 8) {
   457 					for ( dx = o+8;  dx <= t; dx+=8 ) {
   457                         if (getColor(dx) != null)
   458 						if (getColor(dx) != null)
   458                             return false;
   459 							return false;
   459                     }
   460 					}
   460                 } else {
   461 				} else {
   461                     for (dx = o - 8; dx >= t; dx -= 8) {
   462 					for ( dx = o-8; dx >= t; dx-=8 ) {
   462                         if (getColor(dx) != null)
   463 						if (getColor(dx) != null)
   463                             return false;
   464 							return false;
   464                     }
   465 					}
   465                 }
   466 				}
   466 
   467 
   467                 if (steps == 8 && rows == 1) {
   468 				if (steps == 8 && rows == 1) {
   468                     if (getColor(o).equals("white") && t < o)
   469 					if ( getColor(o).equals("white") && t < o )
   469                         return true;
   470 						return true;
   470                     if (getColor(o).equals("black") && t > o)
   471 					if ( getColor(o).equals("black") && t > o )
   471                         return true;
   472 						return true;
   472                 }
   473 				}
   473 
   474 
   474                 if (steps == 16 && rows == 2 && (o >= 8 && o <= 15)) {
   475 				if (steps == 16 && rows == 2 && (o>= 8 && o <=15) ) {
   475                     if (getColor(o).equals("white") && t < o)
   476 					if ( getColor(o).equals("white") && t < o )
   476                         return true;
   477 						return true;
   477                     if (getColor(o).equals("black") && t > o)
   478 					if ( getColor(o).equals("black") && t > o )
   478                         return true;
   479 						return true;
   479                 }
   480 				}
   480 
   481 
   481                 if (steps == 16 && rows == 2 && (o >= 48 && o <= 55)) {
   482 				if (steps == 16 && rows == 2 && (o>=48 && o <=55) ) {
   482                     if (getColor(o).equals("white") && t < o)
   483 					if ( getColor(o).equals("white") && t < o )
   483                         return true;
   484 						return true;
   484                     if (getColor(o).equals("black") && t > o)
   485 					if ( getColor(o).equals("black") && t > o )
   485                         return true;
   486 						return true;
   486                 }
   487 				}
   487             }
   488 			}
   488             break;
   489 			break;
   489         }
   490 		}
   490         return false;
   491 		return false;
   491     }
   492 	}
   492 
   493 
   493     /**
   494 	/**
   494      * This method must be called immediately after isValidMove(t, o)
   495 	 * This method must be called immediately after isValidMove(t, o)
   495      */
   496 	 */
   496     boolean rochade() {
   497 	boolean rochade() {
   497         if (white_rochades_small) {
   498 		if (white_rochades_small) {
   498             setPiece(61, getPiece(63));
   499 			setPiece(61, getPiece(63));
   499             setPiece(63, new Piece(null, null, EMPTY, 0));
   500 			setPiece(63, new Piece(null, null, EMPTY, 0));
   500             return true;
   501 			return true;
   501         }
   502 		}
   502 
   503 
   503         if (white_rochades_big) {
   504 		if (white_rochades_big) {
   504             setPiece(59, getPiece(56));
   505 			setPiece(59, getPiece(56));
   505             setPiece(56, new Piece(null, null, EMPTY, 0));
   506 			setPiece(56, new Piece(null, null, EMPTY, 0));
   506             return true;
   507 			return true;
   507         }
   508 		}
   508 
   509 
   509         if (black_rochades_small) {
   510 		if (black_rochades_small) {
   510             setPiece(5, getPiece(7));
   511 			setPiece(5, getPiece(7));
   511             setPiece(7, new Piece(null, null, EMPTY, 0));
   512 			setPiece(7, new Piece(null, null, EMPTY, 0));
   512             return true;
   513 			return true;
   513         }
   514 		}
   514 
   515 
   515         if (black_rochades_big) {
   516 		if (black_rochades_big) {
   516             setPiece(3, getPiece(0));
   517 			setPiece(3, getPiece(0));
   517             setPiece(0, new Piece(null, null, EMPTY, 0));
   518 			setPiece(0, new Piece(null, null, EMPTY, 0));
   518             return true;
   519 			return true;
   519         }
   520 		}
   520 
   521 
   521         return false;
   522 		return false;
   522     }
   523 	}
   523 
   524 
   524     /**
   525 	/**
   525      * checks, whether color is in check or not.
   526 	 * checks, whether color is in check or not.
   526      */
   527 	 */
   527     public boolean isCheck(String color) {
   528 	public boolean isCheck(String color) {
   528         int i;
   529 		int i;
   529         int wking;
   530 		int wking;
   530         int bking;
   531 		int bking;
   531 
   532 
   532         wking = bking = -1;
   533 		wking = bking = -1;
   533 
   534 
   534         for (i = 0; i < 64; i++) {
   535 		for (i=0;i<64;i++) {
   535             if (isWhiteKing(i)) {
   536 			if (isWhiteKing(i)) {
   536                 wking = i;
   537 				wking = i;
   537             }
   538 			}
   538             if (isBlackKing(i)) {
   539 			if (isBlackKing(i)) {
   539                 bking = i;
   540 				bking = i;
   540             }
   541 			}
   541         }
   542 		}
   542 
   543 
   543         // returns 64 false positives
   544 		// returns 64 false positives
   544         if (wking == -1 || bking == -1)
   545 		if (wking == -1 || bking == -1)
   545             return true;
   546 			return true;
   546 
   547 
   547         if (color.equals("white")) {
   548 		if (color.equals("white")) {
   548             for (i = 0; i < 64; i++)
   549 			for (i=0;i<64;i++)
   549                 if (validMove(wking, i)) {
   550 				if (validMove(wking, i)) {
   550                     return true;
   551 					return true;
   551                 }
   552 				}
   552 
   553 
   553             return false;
   554 			return false;
   554         }
   555 		}
   555 
   556 
   556         if (color.equals("black")) {
   557 		if (color.equals("black")) {
   557             for (i = 0; i < 64; i++)
   558 			for (i=0;i<64;i++)
   558                 if (validMove(bking, i)) {
   559 				if (validMove(bking, i)) {
   559                     return true;
   560 					return true;
   560                 }
   561 				}
   561 
   562 
   562             return false;
   563 			return false;
   563         }
   564 		}
   564         return false;
   565 		return false;
   565     }
   566 	}
   566 
   567 
   567     /**
   568 	/**
   568      * simulates a "valid" move or returns false. all changes are relative to
   569 	 * simulates a "valid" move or returns false.
   569      * pieces and will not be painted. This method sets internal variables for
   570 	 * all changes are relative to pieces and will not be painted.
   570      * the rochade() method, which will be overwritten.
   571 	 * This method sets internal variables for the rochade() method, which will be overwritten.
   571      */
   572 	 */
   572     public boolean simulateMove(int t, int o) {
   573 	public boolean simulateMove(int t, int o) {
   573         if (!validMove(t, o)) {
   574 		if ( !validMove(t, o) ) {
   574             return false;
   575 			return false;
   575         }
   576 		}
   576 
   577 
   577         push(getPiece(t), t, getPiece(o), o);
   578 		push(getPiece(t), t, getPiece(o), o);
   578 
   579 
   579         if (getType(o) == PAWN) {
   580 		if ( getType(o) == PAWN ) {
   580             if (isBlack(o) && t >= 56)
   581 			if(isBlack(o) && t >=56)
   581                 setPiece(o, new Piece(black_queen));
   582 				setPiece(o, new Piece(black_queen));
   582 
   583 
   583             if (isWhite(o) && t <= 7)
   584 			if(isWhite(o) && t <=7)
   584                 setPiece(o, new Piece(white_queen));
   585 				setPiece(o, new Piece(white_queen));
   585         }
   586 		}
   586 
   587 
   587         setPiece(t, new Piece(getPiece(o)));
   588 		setPiece(t, new Piece(getPiece(o)));
   588         setPiece(o, new Piece(null, null, EMPTY, 0));
   589 		setPiece(o, new Piece(null, null, EMPTY, 0));
   589 
   590 
   590         if (isCheck(getColor(t))) {
   591 		if (isCheck(getColor(t))) {
   591             /* restore current position */
   592 			/* restore current position */
   592             if (UNDO_DEBUG)
   593 			if(UNDO_DEBUG)
   593                 print("UNDO", t, o, 0);
   594 				print("UNDO", t, o, 0);
   594             undo();
   595 			undo();
   595             return false;
   596 			return false;
   596         }
   597 		}
   597 
   598 
   598         return true;
   599 		return true;
   599     }
   600 	}
   600 
   601 
   601     /**
   602 	/**
   602      * performs a "valid" move or returns false. all changes are relative to
   603 	 * performs a "valid" move or returns false.
   603      * board_pieces and will be painted.
   604 	 * all changes are relative to board_pieces and will be painted.
   604      */
   605 	 */
   605     public boolean doMove(int t, int o) {
   606 	public boolean doMove(int t, int o) {
   606         if (!validMove(t, o)) {
   607 		if ( !validMove(t, o) ) {
   607             return false;
   608 			return false;
   608         }
   609 		}
   609 
   610 
   610         push(getPiece(t), t, getPiece(o), o);
   611 		push(getPiece(t), t, getPiece(o), o);
   611 
   612 
   612         if (getType(o) == PAWN) {
   613 		if ( getType(o) == PAWN ) {
   613             if (isBlack(o) && t >= 56)
   614 			if(isBlack(o) && t >=56)
   614                 setPiece(o, new Piece(black_queen));
   615 				setPiece(o, new Piece(black_queen));
   615 
   616 
   616             if (isWhite(o) && t <= 7)
   617 			if(isWhite(o) && t <=7)
   617                 setPiece(o, new Piece(white_queen));
   618 				setPiece(o, new Piece(white_queen));
   618         }
   619 		}
   619 
   620 
   620         setPiece(t, new Piece(getPiece(o)));
   621 		setPiece(t, new Piece(getPiece(o)));
   621         setPiece(o, new Piece(null, null, EMPTY, 0));
   622 		setPiece(o, new Piece(null, null, EMPTY, 0));
   622 
   623 
   623         if (rochade())
   624 		if (rochade())
   624             System.out.println("Rochade");
   625 			System.out.println("Rochade");
   625 
   626 
   626         if (isCheck(getColor(t))) {
   627 		if (isCheck(getColor(t))) {
   627             // restore current positions
   628 			// restore current positions
   628             if (UNDO_DEBUG)
   629 			if(UNDO_DEBUG)
   629                 print("UNDO", t, o, 0);
   630 				print("UNDO", t, o, 0);
   630             undo();
   631 			undo();
   631             return false;
   632 			return false;
   632         }
   633 		}
   633 
   634 
   634         board_pieces = getPieces();
   635 		board_pieces = getPieces();
   635         repaint();
   636 		repaint();
   636 
   637 
   637         moveNr = stack.size();
   638 		moveNr = stack.size();
   638 
   639 
   639         return true;
   640 		return true;
   640     }
   641 	}
   641 
   642 
   642     /**
   643 	/**
   643      * converts the internal representation into normal chess notation and
   644 	 * converts the internal representation into normal chess notation and writes it to stdout
   644      * writes it to stdout
   645 	 */
   645      */
   646 	public void print(String s, int t, int o, int value) {
   646     public void print(String s, int t, int o, int value) {
   647 		int row1, col1, row2, col2;
   647         int row1, col1, row2, col2;
   648 
   648 
   649 		row1 = o/8;
   649         row1 = o / 8;
   650 		col1 = o-(8*row1);
   650         col1 = o - (8 * row1);
   651 
   651 
   652 		row2 = t/8;
   652         row2 = t / 8;
   653 		col2 = t-(8*row2);
   653         col2 = t - (8 * row2);
   654 
   654 
   655 		System.out.printf("%s: %C%d-%c%d = %3d%n", s,
   655         System.out.printf("%s: %C%d-%c%d = %3d%n", s, ('A') + col1, 8 - row1, ('A') + col2, 8 - row2, value);
   656 				('A')+col1, 8-row1,
   656     }
   657 				('A')+col2, 8-row2, value);
       
   658 	}
       
   659 }
   657 }