001 package aima.test.search.nqueens; 002 003 import junit.framework.TestCase; 004 import aima.basic.XYLocation; 005 import aima.search.nqueens.NQueensBoard; 006 007 /** 008 * @author Ravi Mohan 009 * 010 */ 011 012 public class NQueensBoardTest extends TestCase { 013 014 NQueensBoard board; 015 016 @Override 017 public void setUp() { 018 019 board = new NQueensBoard(8); 020 } 021 022 public void testBasics() { 023 024 assertEquals(0, board.getNumberOfQueensOnBoard()); 025 board.addQueenAt(new XYLocation(0, 0)); 026 assertEquals(1, board.getNumberOfQueensOnBoard()); 027 board.addQueenAt(new XYLocation(0, 0)); 028 assertEquals(1, board.getNumberOfQueensOnBoard()); 029 board.addQueenAt(new XYLocation(1, 1)); 030 assertEquals(2, board.getNumberOfQueensOnBoard()); 031 assertTrue(board.queenExistsAt(new XYLocation(1, 1))); 032 assertTrue(board.queenExistsAt(new XYLocation(0, 0))); 033 board.moveQueen(new XYLocation(1, 1), new XYLocation(3, 3)); 034 assertTrue(board.queenExistsAt(new XYLocation(3, 3))); 035 assertTrue(!(board.queenExistsAt(new XYLocation(1, 1)))); 036 assertEquals(2, board.getNumberOfQueensOnBoard()); 037 038 } 039 040 public void testCornerQueenAttack1() { 041 042 board.addQueenAt(new XYLocation(0, 0)); 043 assertEquals(false, board.isSquareUnderAttack(new XYLocation(0, 0))); // queen 044 // on 045 // square 046 // not 047 // included 048 assertEquals(true, board.isSquareUnderAttack(new XYLocation(1, 0))); 049 assertEquals(true, board.isSquareUnderAttack(new XYLocation(7, 0))); 050 assertEquals(true, board.isSquareUnderAttack(new XYLocation(0, 7))); 051 assertEquals(true, board.isSquareUnderAttack(new XYLocation(1, 1))); 052 assertEquals(true, board.isSquareUnderAttack(new XYLocation(2, 2))); 053 assertEquals(false, board.isSquareUnderAttack(new XYLocation(2, 1))); 054 assertEquals(false, board.isSquareUnderAttack(new XYLocation(1, 2))); 055 056 } 057 058 public void testCornerQueenAttack2() { 059 060 board.addQueenAt(new XYLocation(7, 7)); 061 assertEquals(true, board.isSquareUnderAttack(new XYLocation(0, 0))); 062 assertEquals(true, board.isSquareUnderAttack(new XYLocation(7, 0))); 063 assertEquals(true, board.isSquareUnderAttack(new XYLocation(0, 7))); 064 assertEquals(true, board.isSquareUnderAttack(new XYLocation(7, 0))); 065 assertEquals(true, board.isSquareUnderAttack(new XYLocation(6, 6))); 066 assertEquals(true, board.isSquareUnderAttack(new XYLocation(5, 5))); 067 assertEquals(false, board.isSquareUnderAttack(new XYLocation(6, 5))); 068 assertEquals(false, board.isSquareUnderAttack(new XYLocation(5, 6))); 069 070 } 071 072 public void testEdgeQueenAttack() { 073 074 board.addQueenAt(new XYLocation(0, 3)); 075 assertEquals(true, board.isSquareUnderAttack(new XYLocation(0, 0))); 076 assertEquals(true, board.isSquareUnderAttack(new XYLocation(0, 7))); 077 assertEquals(true, board.isSquareUnderAttack(new XYLocation(7, 3))); 078 assertEquals(true, board.isSquareUnderAttack(new XYLocation(3, 0))); 079 assertEquals(true, board.isSquareUnderAttack(new XYLocation(4, 7))); 080 081 } 082 083 public void testAttack2() { 084 085 board.addQueenAt(new XYLocation(7, 0)); 086 assertEquals(true, board.isSquareUnderAttack(new XYLocation(6, 1))); 087 } 088 089 public void testAttack3() { 090 091 board.addQueenAt(new XYLocation(0, 0)); 092 assertEquals(true, board.isSquareUnderAttack(new XYLocation(0, 1))); 093 } 094 095 public void testAttack4() { 096 097 board.addQueenAt(new XYLocation(0, 2)); 098 assertTrue(board.isSquareUnderAttack(new XYLocation(1, 1))); 099 } 100 101 public void testMidBoardDiagonalAttack() { 102 103 board.addQueenAt(new XYLocation(3, 3)); 104 // forwardDiagonal from the queen 105 assertTrue(board.isSquareUnderAttack(new XYLocation(4, 2))); 106 assertTrue(board.isSquareUnderAttack(new XYLocation(4, 4))); 107 // backwardDiagonal from the queen 108 assertTrue(board.isSquareUnderAttack(new XYLocation(2, 2))); 109 assertTrue(board.isSquareUnderAttack(new XYLocation(2, 4))); 110 } 111 112 public void testCornerDiagonalAttack() { 113 114 board.addQueenAt(new XYLocation(0, 0)); 115 // forwardDiagonal from the queen 116 assertTrue(board.isSquareUnderAttack(new XYLocation(1, 1))); 117 board.clear(); 118 119 board.addQueenAt(new XYLocation(7, 7)); 120 // backwardDiagonal from the queen 121 assertTrue(board.isSquareUnderAttack(new XYLocation(6, 6))); 122 123 // assertTrue(board.isSquareUnderAttack(new XYLocation(2, 2))); 124 // assertTrue(board.isSquareUnderAttack(new XYLocation(2, 4))); 125 } 126 127 public void testAttack6() { 128 129 board.addQueenAt(new XYLocation(1, 6)); 130 assertTrue(board.isSquareUnderAttack(new XYLocation(0, 7))); 131 } 132 133 public void testRemoveQueen() { 134 135 board.addQueenAt(new XYLocation(0, 0)); 136 assertEquals(1, board.getNumberOfQueensOnBoard()); 137 board.removeQueenFrom(new XYLocation(0, 0)); 138 assertEquals(0, board.getNumberOfQueensOnBoard()); 139 } 140 141 public void testMoveQueen() { 142 143 XYLocation from = new XYLocation(0, 0); 144 XYLocation to = new XYLocation(1, 1); 145 146 board.addQueenAt(from); 147 assertEquals(1, board.getNumberOfQueensOnBoard()); 148 assertTrue(board.queenExistsAt(from)); 149 assertFalse(board.queenExistsAt(to)); 150 151 board.moveQueen(from, to); 152 assertEquals(1, board.getNumberOfQueensOnBoard()); 153 assertFalse(board.queenExistsAt(from)); 154 assertTrue(board.queenExistsAt(to)); 155 } 156 157 public void testMoveNonExistentQueen() { 158 159 XYLocation from = new XYLocation(0, 0); 160 XYLocation to = new XYLocation(1, 1); 161 board.moveQueen(from, to); 162 163 assertEquals(0, board.getNumberOfQueensOnBoard()); 164 165 } 166 167 public void testRemoveNonExistentQueen() { 168 board.removeQueenFrom(new XYLocation(0, 0)); 169 assertEquals(0, board.getNumberOfQueensOnBoard()); 170 171 } 172 173 public void testEquality() { 174 175 board.addQueenAt(new XYLocation(0, 0)); 176 NQueensBoard board2 = new NQueensBoard(8); 177 board2.addQueenAt(new XYLocation(0, 0)); 178 assertEquals(board, board2); 179 NQueensBoard board3 = new NQueensBoard(8); 180 board3.addQueenAt(new XYLocation(0, 1)); 181 assertFalse(board.equals(board3)); 182 183 } 184 185 public void testPrint() { 186 187 NQueensBoard board2 = new NQueensBoard(2); 188 board2.addQueenAt(new XYLocation(0, 0)); 189 String expected = " Q - \n - - \n"; 190 assertEquals(expected, board2.getBoardPic()); 191 192 } 193 194 public void testDontPlaceTwoQueensOnOneSquare() { 195 196 board.addQueenAt(new XYLocation(0, 0)); 197 board.addQueenAt(new XYLocation(0, 0)); 198 assertEquals(1, board.getNumberOfQueensOnBoard()); 199 } 200 201 public void testSimpleHorizontalAttack() { 202 XYLocation loc = new XYLocation(0, 0); 203 board.addQueenAt(loc); 204 assertEquals(0, board.getNumberOfAttacksOn(loc)); 205 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(1, 0))); 206 assertEquals(1, board.getNumberOfAttacksOn(loc.right())); 207 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(7, 0))); 208 } 209 210 public void testSimpleVerticalAttack() { 211 XYLocation loc = new XYLocation(0, 0); 212 board.addQueenAt(loc); 213 assertEquals(0, board.getNumberOfAttacksOn(loc)); 214 assertEquals(1, board.getNumberOfAttacksOn(loc.down())); 215 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(0, 7))); 216 } 217 218 public void testSimpleDiagonalAttack() { 219 XYLocation loc = new XYLocation(3, 3); 220 board.addQueenAt(loc); 221 assertEquals(0, board.getNumberOfAttacksOn(loc)); 222 assertEquals(1, board.getNumberOfAttacksOn(loc.down().right())); 223 assertEquals(1, board.getNumberOfAttacksOn(loc.down().left())); 224 assertEquals(1, board.getNumberOfAttacksOn(loc.up().left())); 225 assertEquals(1, board.getNumberOfAttacksOn(loc.up().right())); 226 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(7, 7))); 227 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(0, 0))); 228 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(6, 0))); 229 assertEquals(1, board.getNumberOfAttacksOn(new XYLocation(0, 6))); 230 } 231 232 public void testMultipleQueens() { 233 XYLocation loc1 = new XYLocation(3, 3); 234 board.addQueenAt(loc1); 235 assertEquals(1, board.getNumberOfAttacksOn(loc1.right())); 236 237 board.addQueenAt(loc1.right().right()); 238 assertEquals(1, board.getNumberOfAttacksOn(loc1)); 239 assertEquals(2, board.getNumberOfAttacksOn(loc1.right())); 240 241 board.addQueenAt(loc1.right().down()); 242 assertEquals(2, board.getNumberOfAttacksOn(loc1)); 243 assertEquals(3, board.getNumberOfAttacksOn(loc1.right())); 244 assertEquals(2, board.getNumberOfAttacksOn(loc1.right().right())); 245 246 } 247 248 public void testBoardDisplay() { 249 board.addQueenAt(new XYLocation(0, 5)); 250 board.addQueenAt(new XYLocation(1, 6)); 251 board.addQueenAt(new XYLocation(2, 1)); 252 board.addQueenAt(new XYLocation(3, 3)); 253 board.addQueenAt(new XYLocation(4, 6)); 254 board.addQueenAt(new XYLocation(5, 4)); 255 board.addQueenAt(new XYLocation(6, 7)); 256 board.addQueenAt(new XYLocation(7, 7)); 257 assertEquals(" - - - - - - - - \n" 258 + " - - Q - - - - - \n" + " - - - - - - - - \n" 259 + " - - - Q - - - - \n" + " - - - - - Q - - \n" 260 + " Q - - - - - - - \n" + " - Q - - Q - - - \n" 261 + " - - - - - - Q Q \n", board.getBoardPic()); 262 263 assertEquals("--------\n" + "--Q-----\n" + "--------\n" + "---Q----\n" 264 + "-----Q--\n" + "Q-------\n" + "-Q--Q---\n" + "------QQ\n", 265 board.toString()); 266 } 267 268 }