001 package aima.test.search.eightpuzzle; 002 003 import junit.framework.TestCase; 004 import aima.search.eightpuzzle.EightPuzzleBoard; 005 006 /** 007 * @author Ravi Mohan 008 * 009 */ 010 011 public class EightPuzzleBoardMoveTest extends TestCase { 012 EightPuzzleBoard board; 013 014 @Override 015 public void setUp() { 016 board = new EightPuzzleBoard(new int[] { 0, 5, 4, 6, 1, 8, 7, 3, 2 }); 017 } 018 019 // Position 1 020 public void testPosition1Movabilty() { 021 assertEquals(false, board.canMoveGap(EightPuzzleBoard.UP)); 022 assertEquals(true, board.canMoveGap(EightPuzzleBoard.DOWN)); 023 assertEquals(false, board.canMoveGap(EightPuzzleBoard.LEFT)); 024 assertEquals(true, board.canMoveGap(EightPuzzleBoard.RIGHT)); 025 } 026 027 public void testPosition1MoveUp() { 028 board.moveGapUp(); 029 assertEquals(new EightPuzzleBoard( 030 new int[] { 0, 5, 4, 6, 1, 8, 7, 3, 2 }), board); 031 } 032 033 public void testPosition1MoveDown() { 034 board.moveGapDown(); 035 assertEquals(new EightPuzzleBoard( 036 new int[] { 6, 5, 4, 0, 1, 8, 7, 3, 2 }), board); 037 } 038 039 public void testPosition1MoveLeft() { 040 board.moveGapLeft(); 041 assertEquals(new EightPuzzleBoard( 042 new int[] { 0, 5, 4, 6, 1, 8, 7, 3, 2 }), board); 043 } 044 045 public void testPosition1MoveRight() { 046 board.moveGapRight(); 047 assertEquals(new EightPuzzleBoard( 048 new int[] { 5, 0, 4, 6, 1, 8, 7, 3, 2 }), board); 049 } 050 051 // Position 2 052 public void testPosition2Movabilty() { 053 setGapToPosition2(); 054 assertEquals(false, board.canMoveGap(EightPuzzleBoard.UP)); 055 assertEquals(true, board.canMoveGap(EightPuzzleBoard.DOWN)); 056 assertEquals(true, board.canMoveGap(EightPuzzleBoard.LEFT)); 057 assertEquals(true, board.canMoveGap(EightPuzzleBoard.RIGHT)); 058 } 059 060 public void testPosition2MoveUp() { 061 // { 5, 0, 4, 6, 1, 8, 7, 3, 2 } 062 setGapToPosition2(); 063 board.moveGapUp(); 064 assertEquals(new EightPuzzleBoard( 065 new int[] { 5, 0, 4, 6, 1, 8, 7, 3, 2 }), board); 066 } 067 068 public void testPosition2MoveDown() { 069 // { 5, 0, 4, 6, 1, 8, 7, 3, 2 } 070 setGapToPosition2(); 071 board.moveGapDown(); 072 assertEquals(new EightPuzzleBoard( 073 new int[] { 5, 1, 4, 6, 0, 8, 7, 3, 2 }), board); 074 } 075 076 public void testPosition2MoveLeft() { 077 // { 5, 0, 4, 6, 1, 8, 7, 3, 2 } 078 setGapToPosition2(); 079 board.moveGapLeft(); 080 assertEquals(new EightPuzzleBoard( 081 new int[] { 0, 5, 4, 6, 1, 8, 7, 3, 2 }), board); 082 } 083 084 public void testPosition2MoveRight() { 085 // { 5, 0, 4, 6, 1, 8, 7, 3, 2 } 086 setGapToPosition2(); 087 board.moveGapRight(); 088 assertEquals(new EightPuzzleBoard( 089 new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 }), board); 090 } 091 092 // Position 3 093 public void testPosition3Movabilty() { 094 setGapToPosition3(); 095 assertEquals(false, board.canMoveGap(EightPuzzleBoard.UP)); 096 assertEquals(true, board.canMoveGap(EightPuzzleBoard.DOWN)); 097 assertEquals(true, board.canMoveGap(EightPuzzleBoard.LEFT)); 098 assertEquals(false, board.canMoveGap(EightPuzzleBoard.RIGHT)); 099 } 100 101 public void testPosition3MoveUp() { 102 // { 5, 4, 0, 6, 1, 8, 7, 3, 2 } 103 setGapToPosition3(); 104 board.moveGapUp(); 105 assertEquals(new EightPuzzleBoard( 106 new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 }), board); 107 } 108 109 public void testPosition3MoveDown() { 110 // { 5, 4, 0, 6, 1, 8, 7, 3, 2 } 111 setGapToPosition3(); 112 board.moveGapDown(); 113 assertEquals(new EightPuzzleBoard( 114 new int[] { 5, 4, 8, 6, 1, 0, 7, 3, 2 }), board); 115 } 116 117 public void testPosition3MoveLeft() { 118 // { 5, 4, 0, 6, 1, 8, 7, 3, 2 } 119 setGapToPosition3(); 120 board.moveGapLeft(); 121 assertEquals(new EightPuzzleBoard( 122 new int[] { 5, 0, 4, 6, 1, 8, 7, 3, 2 }), board); 123 } 124 125 public void testPosition3MoveRight() { 126 // { 5, 4, 0, 6, 1, 8, 7, 3, 2 } 127 setGapToPosition3(); 128 board.moveGapRight(); 129 assertEquals(new EightPuzzleBoard( 130 new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 }), board); 131 } 132 133 // Position 4 134 public void testPosition4Movabilty() { 135 setGapToPosition4(); 136 assertEquals(true, board.canMoveGap(EightPuzzleBoard.UP)); 137 assertEquals(true, board.canMoveGap(EightPuzzleBoard.DOWN)); 138 assertEquals(false, board.canMoveGap(EightPuzzleBoard.LEFT)); 139 assertEquals(true, board.canMoveGap(EightPuzzleBoard.RIGHT)); 140 } 141 142 public void testPosition4MoveUp() { 143 // { 6, 5, 4, 0, 1, 8, 7, 3, 2 } 144 setGapToPosition4(); 145 board.moveGapUp(); 146 assertEquals(new EightPuzzleBoard( 147 new int[] { 0, 5, 4, 6, 1, 8, 7, 3, 2 }), board); 148 } 149 150 public void testPosition4MoveDown() { 151 // { 6, 5, 4, 0, 1, 8, 7, 3, 2 } 152 setGapToPosition4(); 153 board.moveGapDown(); 154 assertEquals(new EightPuzzleBoard( 155 new int[] { 6, 5, 4, 7, 1, 8, 0, 3, 2 }), board); 156 } 157 158 public void testPosition4MoveLeft() { 159 // { 6, 5, 4, 0, 1, 8, 7, 3, 2 } 160 setGapToPosition4(); 161 board.moveGapLeft(); 162 assertEquals(new EightPuzzleBoard( 163 new int[] { 6, 5, 4, 0, 1, 8, 7, 3, 2 }), board); 164 } 165 166 public void testPosition4MoveRight() { 167 // { 6, 5, 4, 0, 1, 8, 7, 3, 2 } 168 setGapToPosition4(); 169 board.moveGapRight(); 170 assertEquals(new EightPuzzleBoard( 171 new int[] { 6, 5, 4, 1, 0, 8, 7, 3, 2 }), board); 172 } 173 174 // Position 5 175 public void testPosition5Movabilty() { 176 setGapToPosition5(); 177 assertEquals(true, board.canMoveGap(EightPuzzleBoard.UP)); 178 assertEquals(true, board.canMoveGap(EightPuzzleBoard.DOWN)); 179 assertEquals(true, board.canMoveGap(EightPuzzleBoard.LEFT)); 180 assertEquals(true, board.canMoveGap(EightPuzzleBoard.RIGHT)); 181 } 182 183 public void testPosition5MoveUp() { 184 // { 6, 5, 4, 1, 0, 8, 7, 3, 2 } 185 setGapToPosition5(); 186 board.moveGapUp(); 187 assertEquals(new EightPuzzleBoard( 188 new int[] { 6, 0, 4, 1, 5, 8, 7, 3, 2 }), board); 189 } 190 191 public void testPosition5MoveDown() { 192 // { 6, 5, 4, 1, 0, 8, 7, 3, 2 } 193 setGapToPosition5(); 194 board.moveGapDown(); 195 assertEquals(new EightPuzzleBoard( 196 new int[] { 6, 5, 4, 1, 3, 8, 7, 0, 2 }), board); 197 } 198 199 public void testPosition5MoveLeft() { 200 // { 6, 5, 4, 1, 0, 8, 7, 3, 2 } 201 setGapToPosition5(); 202 board.moveGapLeft(); 203 assertEquals(new EightPuzzleBoard( 204 new int[] { 6, 5, 4, 0, 1, 8, 7, 3, 2 }), board); 205 } 206 207 public void testPosition5MoveRight() { 208 // { 6, 5, 4, 1, 0, 8, 7, 3, 2 } 209 setGapToPosition5(); 210 board.moveGapRight(); 211 assertEquals(new EightPuzzleBoard( 212 new int[] { 6, 5, 4, 1, 8, 0, 7, 3, 2 }), board); 213 } 214 215 // Position 6 216 public void testPosition6Movabilty() { 217 setGapToPosition6(); 218 assertEquals(true, board.canMoveGap(EightPuzzleBoard.UP)); 219 assertEquals(true, board.canMoveGap(EightPuzzleBoard.DOWN)); 220 assertEquals(true, board.canMoveGap(EightPuzzleBoard.LEFT)); 221 assertEquals(false, board.canMoveGap(EightPuzzleBoard.RIGHT)); 222 } 223 224 public void testPosition6MoveUp() { 225 // { 6, 5, 4, 1, 8, 0, 7, 3, 2 } 226 setGapToPosition6(); 227 board.moveGapUp(); 228 assertEquals(new EightPuzzleBoard( 229 new int[] { 6, 5, 0, 1, 8, 4, 7, 3, 2 }), board); 230 } 231 232 public void testPosition6MoveDown() { 233 // { 6, 5, 4, 1, 8, 0, 7, 3, 2 } 234 setGapToPosition6(); 235 board.moveGapDown(); 236 assertEquals(new EightPuzzleBoard( 237 new int[] { 6, 5, 4, 1, 8, 2, 7, 3, 0 }), board); 238 } 239 240 public void testPosition6MoveLeft() { 241 // { 6, 5, 4, 1, 8, 0, 7, 3, 2 } 242 setGapToPosition6(); 243 board.moveGapLeft(); 244 assertEquals(new EightPuzzleBoard( 245 new int[] { 6, 5, 4, 1, 0, 8, 7, 3, 2 }), board); 246 } 247 248 public void testPosition6MoveRight() { 249 // { 6, 5, 4, 1, 8, 0, 7, 3, 2 } 250 setGapToPosition6(); 251 board.moveGapRight(); 252 assertEquals(new EightPuzzleBoard( 253 new int[] { 6, 5, 4, 1, 8, 0, 7, 3, 2 }), board); 254 } 255 256 // Position 7 257 public void testPosition7Movabilty() { 258 setGapToPosition7(); 259 assertEquals(true, board.canMoveGap(EightPuzzleBoard.UP)); 260 assertEquals(false, board.canMoveGap(EightPuzzleBoard.DOWN)); 261 assertEquals(false, board.canMoveGap(EightPuzzleBoard.LEFT)); 262 assertEquals(true, board.canMoveGap(EightPuzzleBoard.RIGHT)); 263 } 264 265 public void testPosition7MoveUp() { 266 // { 6, 5, 4, 7, 1, 8, 0, 3, 2 } 267 setGapToPosition7(); 268 board.moveGapUp(); 269 assertEquals(new EightPuzzleBoard( 270 new int[] { 6, 5, 4, 0, 1, 8, 7, 3, 2 }), board); 271 } 272 273 public void testPosition7MoveDown() { 274 // { 6, 5, 4, 7, 1, 8, 0, 3, 2 } 275 setGapToPosition7(); 276 board.moveGapDown(); 277 assertEquals(new EightPuzzleBoard( 278 new int[] { 6, 5, 4, 7, 1, 8, 0, 3, 2 }), board); 279 } 280 281 public void testPosition7MoveLeft() { 282 // { 6, 5, 4, 7, 1, 8, 0, 3, 2 } 283 setGapToPosition7(); 284 board.moveGapLeft(); 285 assertEquals(new EightPuzzleBoard( 286 new int[] { 6, 5, 4, 7, 1, 8, 0, 3, 2 }), board); 287 } 288 289 public void testPosition7MoveRight() { 290 // { 6, 5, 4, 7, 1, 8, 0, 3, 2 } 291 setGapToPosition7(); 292 board.moveGapRight(); 293 assertEquals(new EightPuzzleBoard( 294 new int[] { 6, 5, 4, 7, 1, 8, 3, 0, 2 }), board); 295 } 296 297 // Position 8 298 public void testPosition8Movabilty() { 299 setGapToPosition8(); 300 assertEquals(true, board.canMoveGap(EightPuzzleBoard.UP)); 301 assertEquals(false, board.canMoveGap(EightPuzzleBoard.DOWN)); 302 assertEquals(true, board.canMoveGap(EightPuzzleBoard.LEFT)); 303 assertEquals(true, board.canMoveGap(EightPuzzleBoard.RIGHT)); 304 } 305 306 public void testPosition8MoveUp() { 307 // { 6, 5, 4, 7, 1, 8, 3, 0, 2 } 308 setGapToPosition8(); 309 board.moveGapUp(); 310 assertEquals(new EightPuzzleBoard( 311 new int[] { 6, 5, 4, 7, 0, 8, 3, 1, 2 }), board); 312 } 313 314 public void testPosition8MoveDown() { 315 // { 6, 5, 4, 7, 1, 8, 3, 0, 2 } 316 setGapToPosition8(); 317 board.moveGapDown(); 318 assertEquals(new EightPuzzleBoard( 319 new int[] { 6, 5, 4, 7, 1, 8, 3, 0, 2 }), board); 320 } 321 322 public void testPosition8MoveLeft() { 323 // { 6, 5, 4, 7, 1, 8, 3, 0, 2 } 324 setGapToPosition8(); 325 board.moveGapLeft(); 326 assertEquals(new EightPuzzleBoard( 327 new int[] { 6, 5, 4, 7, 1, 8, 0, 3, 2 }), board); 328 } 329 330 public void testPosition8MoveRight() { 331 // { 6, 5, 4, 7, 1, 8, 3, 0, 2 } 332 setGapToPosition8(); 333 board.moveGapRight(); 334 assertEquals(new EightPuzzleBoard( 335 new int[] { 6, 5, 4, 7, 1, 8, 3, 2, 0 }), board); 336 } 337 338 // Position 9 339 public void testPosition9Movabilty() { 340 setGapToPosition9(); 341 assertEquals(true, board.canMoveGap(EightPuzzleBoard.UP)); 342 assertEquals(false, board.canMoveGap(EightPuzzleBoard.DOWN)); 343 assertEquals(true, board.canMoveGap(EightPuzzleBoard.LEFT)); 344 assertEquals(false, board.canMoveGap(EightPuzzleBoard.RIGHT)); 345 } 346 347 public void testPosition9MoveUp() { 348 // { 6, 5, 4, 7, 1, 8, 3, 2, 0 } 349 setGapToPosition9(); 350 board.moveGapUp(); 351 assertEquals(new EightPuzzleBoard( 352 new int[] { 6, 5, 4, 7, 1, 0, 3, 2, 8 }), board); 353 } 354 355 public void testPosition9MoveDown() { 356 // { 6, 5, 4, 7, 1, 8, 3, 2, 0 } 357 setGapToPosition9(); 358 board.moveGapDown(); 359 assertEquals(new EightPuzzleBoard( 360 new int[] { 6, 5, 4, 7, 1, 8, 3, 2, 0 }), board); 361 } 362 363 public void testPosition9MoveLeft() { 364 // { 6, 5, 4, 7, 1, 8, 3, 2, 0 } 365 setGapToPosition9(); 366 board.moveGapLeft(); 367 assertEquals(new EightPuzzleBoard( 368 new int[] { 6, 5, 4, 7, 1, 8, 3, 0, 2 }), board); 369 } 370 371 public void testPosition9MoveRight() { 372 // { 6, 5, 4, 7, 1, 8, 3, 2, 0 } 373 setGapToPosition9(); 374 board.moveGapRight(); 375 assertEquals(new EightPuzzleBoard( 376 new int[] { 6, 5, 4, 7, 1, 8, 3, 2, 0 }), board); 377 } 378 379 public void setGapToPosition2() { 380 board.moveGapRight(); 381 } 382 383 public void setGapToPosition3() { 384 board.moveGapRight(); 385 board.moveGapRight(); 386 } 387 388 public void setGapToPosition4() { 389 board.moveGapDown(); 390 } 391 392 public void setGapToPosition5() { 393 // { 6, 5, 4, 1, 0, 8, 7, 3, 2 } 394 board.moveGapDown(); 395 board.moveGapRight(); 396 } 397 398 public void setGapToPosition6() { 399 // { 6, 5, 4, 1, 8, 0, 7, 3, 2 } 400 board.moveGapDown(); 401 board.moveGapRight(); 402 board.moveGapRight(); 403 } 404 405 public void setGapToPosition7() { 406 // { 6, 5, 4, 7, 1, 8, 0, 3, 2 } 407 board.moveGapDown(); 408 board.moveGapDown(); 409 } 410 411 public void setGapToPosition8() { 412 // { 6, 5, 4, 7, 1, 8, 3, 0, 2 } 413 board.moveGapDown(); 414 board.moveGapDown(); 415 board.moveGapRight(); 416 } 417 418 public void setGapToPosition9() { 419 // { 6, 5, 4, 7, 1, 8, 3, 2, 0 } 420 board.moveGapDown(); 421 board.moveGapDown(); 422 board.moveGapRight(); 423 board.moveGapRight(); 424 } 425 426 }