コード例 #1
0
    def test_is_square_occupied_by_enemy(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        board.create_pieces(PieceFactory(), Color.WHITE)

        self.assertTrue(board.is_square_occupied_by_enemy(0, 0, Color.BLACK))
コード例 #2
0
    def test_is_square_not_empty(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        board.create_pieces(PieceFactory(), Color.WHITE)

        self.assertFalse(board.is_square_empty(0, 0))
コード例 #3
0
class TestValidateMoveService(unittest.TestCase):
    rows = 9
    columns = 9
    board = Board(rows, columns)
    board.initialize_board(PieceFactory())

    def test_execute_valid(self):
        ValidateMoveService.execute(
            ValidateMoveCommand(self.board, (0, 0), (1, 0), Color.WHITE))

    def test_execute_empty_square_error(self):
        with self.assertRaises(OriginSquareEmptyException):
            ValidateMoveService.execute(
                ValidateMoveCommand(self.board, (1, 0), (3, 0), Color.WHITE))

    def test_execute_square_occupied_enemy_error(self):
        with self.assertRaises(OriginSquareContainsEnemyPieceException):
            ValidateMoveService.execute(
                ValidateMoveCommand(self.board, (6, 0), (5, 0), Color.WHITE))

    def test_execute_square_occupied_friend_error(self):
        with self.assertRaises(DestinationSquareOccupiedException):
            ValidateMoveService.execute(
                ValidateMoveCommand(self.board, (0, 0), (2, 0), Color.WHITE))

    def test_execute_square_not_reachable_error(self):
        with self.assertRaises(InvalidMovementForPieceException):
            ValidateMoveService.execute(
                ValidateMoveCommand(self.board, (2, 0), (3, 1), Color.WHITE))

    def test_execute_path_obstructed_error(self):
        with self.assertRaises(PieceMovementPathObstructedException):
            ValidateMoveService.execute(
                ValidateMoveCommand(self.board, (1, 7), (3, 5), Color.WHITE))
コード例 #4
0
    def test_get_piece_in_square(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        board.create_pieces(PieceFactory(), Color.WHITE)

        piece = Pawn(Color.WHITE)
        board.get_grid()[0][0].set_piece(piece)

        self.assertEqual(board.get_piece_in_square(0, 0), piece)
コード例 #5
0
    def test_add_captured(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        game = Game(board, PieceFactory())
        game.start_game()
        self.assertEqual([], game.get_captured_by_color(Color.WHITE))
        self.assertEqual([], game.get_captured_by_color(Color.BLACK))

        pawn = Pawn(Color.BLACK)
        game.add_captured(Color.WHITE, pawn)
        self.assertEqual([pawn], game.get_captured_by_color(Color.WHITE))
        self.assertEqual([], game.get_captured_by_color(Color.BLACK))
コード例 #6
0
    def test_init(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        self.assertEqual(board.get_rows(), rows)
        self.assertEqual(board.get_columns(), columns)

        self.assertEqual(len(board.get_grid()), rows)
        for i in range(rows):
            self.assertEqual(len(board.get_grid()[i]), columns)
コード例 #7
0
    def test_next_turn(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        game = Game(board, PieceFactory())
        game.start_game()
        self.assertEqual(1, game.get_turn())
        self.assertEqual(Color.WHITE, game.get_current_player())

        game.next_turn()
        self.assertEqual(2, game.get_turn())
        self.assertEqual(Color.BLACK, game.get_current_player())

        game.next_turn()
        self.assertEqual(3, game.get_turn())
        self.assertEqual(Color.WHITE, game.get_current_player())
コード例 #8
0
    def test_create_pieces_black(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)
        board.create_pieces(PieceFactory(), Color.BLACK)

        first_row = -1
        second_row = -2
        third_row = -3

        for square in board.get_grid()[third_row]:
            self.assertIsInstance(square.get_piece(), Pawn)

        self.assertIsInstance(board.get_grid()[second_row][1].get_piece(),
                              Bishop)
        self.assertIsInstance(board.get_grid()[second_row][-2].get_piece(),
                              Rook)
        self.assertIsInstance(board.get_grid()[first_row][0].get_piece(),
                              Lance)
        self.assertIsInstance(board.get_grid()[first_row][-1].get_piece(),
                              Lance)
        self.assertIsInstance(board.get_grid()[first_row][1].get_piece(),
                              Knight)
        self.assertIsInstance(board.get_grid()[first_row][-2].get_piece(),
                              Knight)
        self.assertIsInstance(board.get_grid()[first_row][2].get_piece(),
                              SilverGeneral)
        self.assertIsInstance(board.get_grid()[first_row][-3].get_piece(),
                              SilverGeneral)
        self.assertIsInstance(board.get_grid()[first_row][3].get_piece(),
                              GoldGeneral)
        self.assertIsInstance(board.get_grid()[first_row][-4].get_piece(),
                              GoldGeneral)
        self.assertIsInstance(board.get_grid()[first_row][4].get_piece(), King)
コード例 #9
0
 def test_init_error(self):
     rows = 9
     columns = 0
     with self.assertRaises(ValueError):
         Board(rows, columns)
コード例 #10
0
    def test_is_square_empty(self):
        rows = 9
        columns = 9
        board = Board(rows, columns)

        self.assertTrue(board.is_square_empty(0, 0))
コード例 #11
0
from Domain.Entities.Board import Board
from Domain.Exceptions.CoordinatesOutOfBoundsException import CoordinatesOutOfBoundsException
from Domain.Exceptions.DestinationSquareOccupiedException import DestinationSquareOccupiedException
from Domain.Exceptions.InvalidMovementForPieceException import InvalidMovementForPieceException
from Domain.Exceptions.OriginSquareContainsEnemyPieceException import OriginSquareContainsEnemyPieceException
from Domain.Exceptions.OriginSquareEmptyException import OriginSquareEmptyException
from Domain.Entities.Game import Game
from Application.Services.MovePieceService import MovePieceService
from Domain.Entities.PieceFactory import PieceFactory
from Infrastructure.Clients.CommandLineClient import CommandLineClient

ROWS = 9
COLUMNS = 9
CAPTURED_ROW_INDEX = 9

board = Board(ROWS, COLUMNS)
piece_factory = PieceFactory()
drop_piece_service = DropPieceService()
move_piece_service = MovePieceService()
move_and_capture_piece_service = MoveAndCapturePieceService()
validate_coordinates_service = ValidateCoordinatesService()

command_line_client = CommandLineClient(Game(board, piece_factory))
command_line_client.get_game().start_game()

while not command_line_client.get_game().is_finished():
    command_line_client.show_game_state()
    color = command_line_client.get_game().get_current_player()
    captured = None

    while True: