def new_board(self): board_matrix = [[None] * self.size] * self.size # set square colours for x in range(self.size): for y in range(self.size): if x % 2 == 0: if y % 2 == 0: board_matrix[x][y] = Square(Colour.WHITE) else: board_matrix[x][y] = Square(Colour.BLACK) else: if y % 2 == 0: board_matrix[x][y] = Square(Colour.BLACK) else: board_matrix[x][y] = Square(Colour.WHITE) # set up pieces for y in range(self.size): for x in range(3): if board_matrix[x][y].color == Colour.BLACK: board_matrix[x][y].occupant = Piece(Colour.WHITE) for x in range(5, self.size): if board_matrix[x][y].color == Colour.BLACK: board_matrix[x][y].occupant = Piece(Colour.BLACK) return board_matrix
def test_valid_moves_border(self): board = Board() board.clean() king = King(board, Square(0, 0), BLACK) expected = [Square(0, 1), Square(1, 0), Square(1, 1)] result = king.valid_moves() self.assertEqual(expected, result)
def test_valid_moves_seventh_row_white(self): board = Board() board.set_config(flat=EMPTY * 64) # it doesn't matter much pawn = Pawn(board, Square(1, 0), WHITE) expected = [Square(0, 0)] result = pawn.valid_moves() self.assertEqual(expected, result)
def test_valid_moves_second_row_black(self): board = Board() board.set_config(flat=EMPTY * 64) # it doesn't matter much pawn = Pawn(board, Square(6, 0), BLACK) expected = [Square(7, 0)] result = pawn.valid_moves() self.assertEqual(expected, result)
def test___eq__(self): square = Square("a1") same = Square("a1") other = Square("a2") self.assertEqual(True, square.__eq__(same)) self.assertEqual(False, square.__eq__(other))
def draw_state(self, reversed_racers): squares = [] current_square = Square(reversed_racers[0].token.distance_from_finish) squares.append(current_square) for racer in reversed_racers: while True: if racer.token.distance_from_finish == current_square.position: if racer.token.on_right: current_square.right_token = racer.label else: current_square.left_token = racer.label break else: current_square = Square(current_square.position - 1) squares.append(current_square) self.cls() for square in squares: square.print_left_token() print('') for square in squares: square.print_right_token() print('') for square in squares: print(' ', end='') print(squares[len(squares) - 1].position)
def test___ne__(self): source = Square('a1') target = Square('a2') promotion = 'q' move1 = Move(source, target) move2 = Move(source, target) self.assertEqual(False, move1.__ne__(move2))
def loadMap(self): filename = self.level + ".txt" file = open(filename, "r") map = [] mapline = [] idcount = 1 xcount = 0 ycount = 0 for line in file: line = line.rstrip("\n") for c in line: if c == "X": newsquare = Square(self.squareSize, True, xcount, ycount) else: newsquare = Square(self.squareSize, False, xcount, ycount) if c == "1": self.start = Coordinates(xcount, ycount) elif c == "2": self.end = Coordinates(xcount, ycount) idcount += 1 xcount += 1 mapline.append(newsquare) xcount = 0 map.append(mapline) mapline = [] ycount += 1 file.close() return map
def __makeMaze(self): # Generando un tablero aleatorio de tamaño nxm self.__maze = [] for i in range(self.n): self.__maze.append([]) for j in range(self.m): cost = self.__getRandomCost() leftObstacle = bool(random.getrandbits(1)) rightObstacle = bool(random.getrandbits(1)) upObstacle = bool(random.getrandbits(1)) downObstacle = bool(random.getrandbits(1)) randomSquare = Square(i, j, cost, leftObstacle, rightObstacle, upObstacle, downObstacle) self.__maze[i].append(randomSquare) # Creamos el casillero de inicio y de fin self.start = Square(random.randint(0, self.n-1), random.randint(0, self.m-1), 0, False, False, False, False) self.end = Square(random.randint(0, self.n-1), random.randint(0, self.m-1), 2, False, False, False, False) # En caso de que la posición de partida sea igual a la final, # buscamos un nuevo valor para la posición final while(self.start.x == self.end.x and self.start.y == self.end.y): self.end.x = random.randint(0, self.n-1) self.end.y = random.randint(0, self.m-1) # Asignando las posiciones de spirit y la meta en el tablero self.__maze[self.start.x][self.start.y] = self.start self.__maze[self.end.x][self.end.y] = self.end
def test_valid_moves_with_attacks(self): board = Board() pawn = Pawn(board, Square(2, 0), BLACK) board.matrix[3][1] = "p" expected = [Square(3, 0), Square(4, 0), Square(3, 1)] result = pawn.valid_moves() self.assertEqual(expected, result)
def test_attacks_right_border_black(self): board = Board() pawn = Pawn(board, Square(2, 0), BLACK) board.matrix[3][1] = "p" expected = [Square(3, 1)] result = pawn.attacking_moves() self.assertEqual(expected, result)
def test_attacks_right_border_white(self): board = Board() pawn = Pawn(board, Square(6, 7), WHITE) board.matrix[5][6] = "P" expected = [Square(5, 6)] result = pawn.attacking_moves() self.assertEqual(expected, result)
def test_attacks_north(self): board = Board() board.clean() board.matrix[3][2] = "P" king = King(board, Square(4, 2), WHITE) expected = [Square(3, 2)] result = king.attacking_moves() self.assertEqual(expected, result)
def __init__(self): from square import Square for i in range(8): for j in range(8): if (i+j)%2: self.board[i].append(Square("White", i, j)) else: self.board[i].append(Square("Black", i, j))
def test_pawn_valid_change_white_rank2(): current_square = Square('e', '2') piece = Pawn(Colour.WHITE) current_square.set_piece(piece) to_square = Square('e', '3') assert piece.valid_change(to_square, is_capture=False) to_square = Square('e', '4') assert piece.valid_change(to_square, is_capture=False)
def test_pawn_valid_change_black_any_rank(): current_square = Square('e', '6') piece = Pawn(Colour.BLACK) current_square.set_piece(piece) to_square = Square('e', '5') assert piece.valid_change(to_square, is_capture=False) to_square = Square('e', '4') assert not piece.valid_change(to_square, is_capture=False)
def test_attacks_middle_position_white(self): board = Board() pawn = Pawn(board, Square(6, 3), WHITE) board.matrix[5][2] = "P" board.matrix[5][4] = "P" expected = [Square(5, 2), Square(5, 4)] result = pawn.attacking_moves() self.assertEqual(expected, result)
def test_attacks_middle_position_black(self): board = Board() pawn = Pawn(board, Square(2, 3), BLACK) board.matrix[3][2] = "p" board.matrix[3][4] = "p" expected = [Square(3, 2), Square(3, 4)] result = pawn.attacking_moves() self.assertEqual(expected, result)
def test_area(self): self.sq = Square(3) self.assertEqual(self.sq.area(), 9, "positive side length") self.sq = Square(-5) self.assertEqual(self.sq.area(), 25, "negative side length") self.sq = Square(0) self.assertEqual(self.sq.area(), 0, "zero side length") self.sq = Square(5.5) self.assertEqual(self.sq.area(), 30.25, "decimal side length")
def test___init__(self): with self.assertRaises(ValueError): Move(Square('a1'), Square('a1')) with self.assertRaises(ValueError): m = Move.from_uci('a1a2q') for move in gen_moves(): pass
def test_call(self): square_1 = Square() square_2 = Square() x = Variable(self.input) a = square_1(x) b = self.exp(a) y = square_2(b) self.assertEqual(0.25, a.data) self.assertEqual(1.2840254166877414, b.data) self.assertEqual(1.648721270700128, y.data)
def test_snap(self): with self.assertRaises(TypeError) as error: s1 = Square(2, 1, 0, 1, 0, 0, 2, 0) s2 = Square(1, 1, 0, 1, 0, 0, 1, 0) # Square # print(str(s2)) self.assertEqual(str(s2.snap()), str(s2)) s3 = Square(1.6, 3.2, -1.6, 3.2, -1.6, 0, 1.6, 0) self.assertEqual(str(s3.snap()), str(s3)) with self.assertRaises(TypeError) as error: Square(2, 4, -2, 4, -5, 0, 5, 0) # Trapezoid
def test_attacking_moves(self): b = Board() b.clean() b.matrix[4][7] = "p" b.matrix[1][2] = "P" b.matrix[4][1] = "p" rook = Rook(b, Square(4, 2), BLACK) expected = [Square(4, 1), Square(4, 7)] result = rook.attacking_moves() self.assertEqual(expected, result)
def test_get_set(self): """Tests the get and set methods.""" pos = Position() self.assertEqual(pos["b1"], Piece("N")) del pos["e2"] self.assertEqual(pos[Square("e2")], None) pos[Square("e4")] = Piece("r") self.assertEqual(pos["e4"], Piece("r"))
def test___ne__(self): print("Testing __ne__ method") A = Square(0, 0.0, -0.4, 0.0, -0.4, -0.4, 0, -0.4) B = Square(0, 0.0, -5, 0.0, -5, -5, 0, -5) C = Square(3.8, 3.7, 1.7, 3.7, 1.7, 1.6, 3.8, 1.6) self.assertFalse(A != A) self.assertTrue(A != B) self.assertFalse(B.__ne__(B)) self.assertTrue(B.__ne__(C)) print("Done testing __ne__ method successfully")
def generate_e2e3e4(): squares.clear() xOff = 200 squares["D2"] = Square("D2", xOff + SQUARESIZE, 300, SQUARESIZE, SQUARESIZE, DARK, None) squares["D3"] = Square("D3", xOff + SQUARESIZE, 300 - SQUARESIZE, SQUARESIZE, SQUARESIZE, LIGHT, None) squares["D4"] = Square("D4", xOff + SQUARESIZE, 300 - (SQUARESIZE * 2), SQUARESIZE, SQUARESIZE, DARK, None) squares["E2"] = Square("E2", xOff + (SQUARESIZE * 2), 300, SQUARESIZE, SQUARESIZE, LIGHT, None) squares["E3"] = Square("E3", xOff + (SQUARESIZE * 2), 300 - SQUARESIZE, SQUARESIZE, SQUARESIZE, DARK, None) squares["E4"] = Square("E4", xOff + (SQUARESIZE * 2), 300 - (SQUARESIZE * 2), SQUARESIZE, SQUARESIZE, LIGHT, None) squares["F2"] = Square("F2", xOff + (SQUARESIZE * 3), 300, SQUARESIZE, SQUARESIZE, DARK, None) squares["F3"] = Square("F3", xOff + (SQUARESIZE * 3), 300 - SQUARESIZE, SQUARESIZE, SQUARESIZE, LIGHT, None) squares["F4"] = Square("F4", xOff + (SQUARESIZE * 3), 300 - (SQUARESIZE * 2), SQUARESIZE, SQUARESIZE, DARK, None)
def test_snap(self): print("Testing snap method") A = Square(0, 0.0, -0.4, 0.0, -0.4, -0.4, 0, -0.4) B = Square(0, 0.0, -5, 0.0, -5, -5, 0, -5) C = Square(3.8, 3.7, 1.7, 3.7, 1.7, 1.6, 3.8, 1.6) D = Square(0, 0, -10.5, 0, -10.5, -10.5, 0, -10.5) self.assertEqual(A.snap(), A) self.assertNotEqual(A.snap(), B) self.assertEqual(B.snap(), Quadrilateral(0, 0, -5, 0, -5, -5, 0, -5)) self.assertEqual(C.snap(), Quadrilateral(4, 4, 2, 4, 2, 2, 4, 2)) self.assertNotEqual(D.snap(), Square(0, 0, -11, 0, -11, -11, 0, -11)) print("Done testing snap method successfully")
def from_uci(cls, uci): """Creates a move object from an UCI move string. :param move: An UCI move string like "a1a2" or "b7b8q". """ if len(uci) not in [4,5]: raise ValueError(uci) source = Square(uci[:2]) target = Square(uci[2:4]) promotion = uci[4:] return cls(source, target, promotion)
def test_pawn_valid_change_black_capture(): current_square = Square('e', '6') piece = Pawn(Colour.BLACK) current_square.set_piece(piece) to_square = Square('d', '5') to_square.set_piece(Pawn(Colour.BLACK)) assert piece.valid_change(to_square, is_capture=True) to_square = Square('f', '5') to_square.set_piece(Pawn(Colour.BLACK)) assert piece.valid_change(to_square, is_capture=True) to_square = Square('e', '4') to_square.set_piece(Pawn(Colour.BLACK)) assert not piece.valid_change(to_square, is_capture=True)
def test__str__(self): print("Testing __str__ method") A = Square(0, 0.0, -0.4, 0.0, -0.4, -0.4, 0, -0.4) B = Square(0, 0.0, -5, 0.0, -5, -5, 0, -5) C = Square(3.8, 3.7, 1.7, 3.7, 1.7, 1.6, 3.8, 1.6) aStr = "I am a Square with TwoDpoint (0, 0) , TwoDpoint (-0.4, 0) , TwoDpoint (-0.4, -0.4) , TwoDpoint (0, -0.4)" bStr = "I am a Square with TwoDpoint (0, 0) , TwoDpoint (-5, 0) , TwoDpoint (-5, -5) , TwoDpoint (0, -5)" self.assertEqual(str(A), aStr) self.assertEqual(str(B), bStr) self.assertNotEqual(str(A), bStr) self.assertNotEqual(str(A), str(C)) self.assertNotEqual(str(B), str(A)) print("Done testing __str__ method successfully")