def pieces(): """ метод тестирования классов фигур """ # пешки p = [ Pawn(colors[0], position='D2'), Pawn(colors[1], position='D7'), Pawn(colors[0], position='A2'), Pawn(colors[1], position='A7') ] print('Test pawns') for item in p: print('Pawn at ' + item.position + '. Moves: ' + str(item.moves) + ' Attacks: ' + str(item.attacks)) # ферзи q = [Queen(colors[0], position='D1'), Queen(colors[1], position='D8')] print('Test queens') for item in q: print('Queen at ' + item.position + '. Moves: ' + str(item.moves) + '\nAttacks: ' + str(item.attacks)) # цель ферзя клетка D2 q[0].settarget('D2') if q[0].move(): print('New position: ' + q[0].position) else: print("Position can't be changed") # цель ферзя клетка C4 q[0].settarget('C4') if q[0].move(): print('New position: ' + q[0].position) else: print("Position can't be changed")
def test_pawn_isvalid(self): '''Pawn move validation''' testdata = {Pawn('white'): [(0, 1), (0, 2), True], Pawn('white'): [(0, 1), (1, 2), False], } for piece, (start, end, ans) in testdata.items(): result = piece.isvalid(start, end) self.assertEqual(result, ans)
def test_pawn_capture(self): '''Pawn captures diagonally''' game = gameSetupWithKings() game.add((0,3), Pawn('white')) game.add((1,4), Pawn('black')) game.turn, game.other_turn = 'white', 'black' game.update((0, 3), (1, 4)) self.assertEqual(game.get_piece((1, 4)).colour, 'white')
def desk(): """ метод тестирования класса доски """ mydesk = Desk() for i in 'ABCDEFGH': mydesk.insert(Pawn(colors[0]), i + '2') mydesk.insert(Pawn(colors[1]), i + '7') mydesk.insert(Queen(colors[0]), 'D1') mydesk.insert(Queen(colors[1]), 'D8') print(mydesk) mydesk.remove('A2') mydesk.remove('A7') print(mydesk)
def text_pawn(): p = Pawn((3, 3), "W") ok(p.move_cells()) == [[0x34]] ok(to_comparable_v(p.hit_cells())) == [0x24, 0x44] p = Pawn((3, 4), "W") ok(p.move_cells()) == [[0x35]] ok(to_comparable_v(p.hit_cells())) == [0x25, 0x45] p = Pawn((3, 4), "B") ok(p.move_cells()) == [[0x33]] ok(to_comparable_v(p.hit_cells())) == [0x23, 0x43] p = Pawn((0, 4), "W") ok(p.move_cells()) == [[0x05]] ok(p.hit_cells()) == [[0x15]] p = Pawn((1, 7), "W") ok(p.move_cells()) == [] ok(p.hit_cells()) == [] p = Pawn((2, 1), "W") ok(p.move_cells()) == [[0x22, 0x23]] p = Pawn((2, 1), "B") ok(p.move_cells()) == [[0x20]] p = Pawn((2, 6), "B") ok(p.move_cells()) == [[0x24, 0x25]]
def test_pawn_promotion(self): '''Pawn on last row is promoted''' for i in range(7): game = gameSetupWithKings() if i == 4: continue game.add((i, 6), Pawn('white')) game.turn, game.other_turn = 'white', 'black' game.update((i, 6), (i, 7)) self.assertEqual(game.get_piece((i, 7)).colour, 'white') self.assertEqual(game.get_piece((i, 7)).name, 'queen') game = gameSetupWithKings() game.add((i, 1), Pawn('black')) game.turn, game.other_turn = 'black', 'white' game.update((i, 1), (i, 0)) self.assertEqual(game.get_piece((i, 0)).colour, 'black') self.assertEqual(game.get_piece((i, 0)).name, 'queen')
def test_enpassant(self): '''Pawn can capture en passant''' game = gameSetupWithKings() game.add((3, 4), Pawn('white')) game.add((4, 6), Pawn('black')) game.turn, game.other_turn = 'black', 'white' game.update((4, 6), (4, 4)) game.update((3, 4), (4, 5)) self.assertEqual(game.get_piece((4, 5)).colour, 'white') self.assertIsNone(game.get_piece((4, 4))) game = gameSetupWithKings() game.add((3, 1), Pawn('white')) game.add((4, 3), Pawn('black')) game.turn, game.other_turn = 'white', 'black' game.update((3, 1), (3, 3)) game.update((4, 3), (3, 2)) self.assertEqual(game.get_piece((3, 2)).colour, 'black') self.assertIsNone(game.get_piece((3, 3)))
def test_pawn_first_move(self): '''Pawn can move two steps only on first move''' for col in range(7): game = gameSetupWithKings() game.add((col, 1), Pawn('white')) game.turn, game.other_turn = 'white', 'black' self.assertTrue(game.valid_move((col, 1), (col, 3))) game.update((col, 1), (col, 3)) self.assertEqual(game.get_piece((col, 3)).colour, 'white') self.assertIsNone(game.get_piece((col, 1))) self.assertFalse(game.valid_move((col, 3), (col, 5))) game = gameSetupWithKings() game.add((col, 6), Pawn('black')) game.turn, game.other_turn = 'black', 'white' self.assertTrue(game.valid_move((col, 6), (col, 4))) game.update((col, 6), (col, 4)) self.assertEqual(game.get_piece((col, 4)).colour, 'black') self.assertIsNone(game.get_piece((col, 6))) self.assertFalse(game.valid_move((col, 4), (col, 2)))