Пример #1
0
 def __init__(self):
     super(HiveShellClient, self).__init__()
     self.hive = Hive()
     self.view = HiveView(self.hive)
     self.input = sys.stdin
     self.player = {1: None, 2: None}
     self.logger = None
Пример #2
0
    def setUp(self):
        #    / \ / \ / \ / \ / \
        #   |wB1|wS2|   |bB1|   |
        #  / \ / \ / \ / \ / \ /
        # |wG1|   |wS1|bS1|bG1|
        #  \ / \ / \ / \ / \ / \
        #   |   |wQ1|   |bQ1|   |
        #  / \ / \ / \ / \ / \ /
        # |   |   |   |bA1|   |
        #  \ / \ / \ / \ / \ /
        # next is black player turn

        self.hive = Hive()
        self.hive.setup()

        self.hive.action('play', ('wS1'))
        self.hive.action('play', ('bS1', 'wS1', self.hive.E))
        self.hive.action('play', ('wQ1', 'wS1', self.hive.SW))
        self.hive.action('play', ('bQ1', 'bS1', self.hive.SE))
        self.hive.action('play', ('wS2', 'wS1', self.hive.NW))
        self.hive.action('play', ('bG1', 'bS1', self.hive.E))
        self.hive.action('play', ('wB1', 'wS2', self.hive.W))
        self.hive.action('play', ('bA1', 'bQ1', self.hive.SW))
        self.hive.action('play', ('wG1', 'wB1', self.hive.SW))
        self.hive.place_piece(self.piece['bB1'], 'bS1', self.hive.NE)
Пример #3
0
 def __init__(self):
     super(HiveShellClient, self).__init__()
     self.hive = Hive()
     self.view = HiveView(self.hive)
     self.input = sys.stdin
     self.player = {1: None, 2: None}
     self.logger = None
Пример #4
0
    def setUp(self):
        #    / \ / \ / \ / \ / \
        #   |wB1|wS2|   |bB1|   |
        #  / \ / \ / \ / \ / \ /
        # |wG1|   |wS1|bS1|bG1|
        #  \ / \ / \ / \ / \ / \
        #   |   |wQ1|   |bQ1|   |
        #  / \ / \ / \ / \ / \ /
        # |   |   |   |bA1|   |
        #  \ / \ / \ / \ / \ /
        # next is black player turn

        self.hive = Hive()
        self.hive.setup()

        self.hive.action('play', ('wS1'))
        self.hive.action('play', ('bS1', 'wS1', self.hive.E))
        self.hive.action('play', ('wQ1', 'wS1', self.hive.SW))
        self.hive.action('play', ('bQ1', 'bS1', self.hive.SE))
        self.hive.action('play', ('wS2', 'wS1', self.hive.NW))
        self.hive.action('play', ('bG1', 'bS1', self.hive.E))
        self.hive.action('play', ('wB1', 'wS2', self.hive.W))
        self.hive.action('play', ('bA1', 'bQ1', self.hive.SW))
        self.hive.action('play', ('wG1', 'wB1', self.hive.SW))
        self.hive.place_piece(self.piece['bB1'], 'bS1', self.hive.NE)
Пример #5
0
class HiveShellClient(object):
    """HiveShellClient is a command line client to the Hive game."""
    def __init__(self):
        super(HiveShellClient, self).__init__()
        self.hive = Hive()
        self.view = HiveView(self.hive)
        self.input = sys.stdin
        self.player = {1: None, 2: None}
        self.logger = None

    def piece_set(self, color):
        """
        Return a full set of hive pieces
        """
        pieceSet = {}
        for i in range(3):
            ant = HivePiece(color, 'A', i + 1)
            pieceSet[str(ant)] = ant
            grasshopper = HivePiece(color, 'G', i + 1)
            pieceSet[str(grasshopper)] = grasshopper
        for i in range(2):
            spider = HivePiece(color, 'S', i + 1)
            pieceSet[str(spider)] = spider
            beetle = HivePiece(color, 'B', i + 1)
            pieceSet[str(beetle)] = beetle
        queen = HivePiece(color, 'Q', 1)
        pieceSet[str(queen)] = queen
        return pieceSet

    def parse_cmd(self, cmd):
        self.logger.write(cmd + '\n')

        if cmd == 'pass':
            return ('non_play', cmd)
        if len(cmd) == 3:
            movingPiece = cmd
            pointOfContact = None
            refPiece = None
        else:
            if len(cmd) != 8:
                raise Exception("Failed to parse command.")
            movingPiece = cmd[:3]
            pointOfContact = cmd[3:5]
            refPiece = cmd[5:]
        return ('play', (movingPiece, pointOfContact, refPiece))

    def ppoc2cell(self, pointOfContact, refPiece):
        direction = self.poc2direction(pointOfContact)
        return self.hive.poc2cell(refPiece, direction)

    def poc2direction(self, pointOfContact):
        "Parse point of contact to a Hive.direction"
        if pointOfContact == '|*':
            return Hive.W
        if pointOfContact == '/*':
            return Hive.NW
        if pointOfContact == '*\\':
            return Hive.NE
        if pointOfContact == '*|':
            return Hive.E
        if pointOfContact == '*/':
            return Hive.SE
        if pointOfContact == '\\*':
            return Hive.SW
        if pointOfContact == '=*':
            return Hive.O
        raise ValueError('Invalid input for point of contact: "%s"' %
                         pointOfContact)

    def exec_cmd(self, cmd, turn):
        try:
            (cmdType, value) = self.parse_cmd(cmd)
            if cmdType == 'play':
                (actPiece, pointOfContact, refPiece) = value
            if cmdType == 'non_play' and value == 'pass':
                self.hive.action(cmdType, value)
                return True
        except:
            return False

        if pointOfContact is None and turn > 1:
            return False

        actPlayer = (2 - (turn % 2))
        try:
            p = self.player[actPlayer][actPiece]
            direction = None
            if pointOfContact is not None:
                direction = self.poc2direction(pointOfContact)
        except:
            return False

        try:
            self.hive.action('play', (actPiece, refPiece, direction))
        except HiveException:
            return False
        return True

    def run(self):
        player2color = {1: 'w', 2: 'b'}
        self.logger = open('game.log', 'w')
        self.player[1] = self.piece_set('w')
        self.player[2] = self.piece_set('b')
        self.hive.turn += 1  # white player start
        self.hive.setup()

        while self.hive.check_victory() == self.hive.UNFINISHED:
            print("Turn: %s" % self.hive.turn)
            active_player = (2 - (self.hive.turn % 2))
            print(self.view)
            print(
                "pieces in hand: %s" %
                sorted(self.hive.unplayedPieces[player2color[active_player]]))
            print("player %s play: " % active_player)
            try:
                cmd = self.input.readline()
            except KeyboardInterrupt:
                break
            if self.exec_cmd(cmd.strip(), self.hive.turn):
                print("\n")
                print("=" * 79)
                print("\n")
            else:
                print("invalid play!")

        print("\nThanks for playing Hive. Have a nice day!")
Пример #6
0
class HiveShellClient(object):
    """HiveShellClient is a command line client to the Hive game."""
    def __init__(self):
        super(HiveShellClient, self).__init__()
        self.hive = Hive()
        self.view = HiveView(self.hive)
        self.input = sys.stdin
        self.player = {1: None, 2: None}
        self.logger = None

    def piece_set(self, color):
        """
        Return a full set of hive pieces
        """
        pieceSet = {}
        for i in xrange(3):
            ant = HivePiece(color, 'A', i + 1)
            pieceSet[str(ant)] = ant
            grasshopper = HivePiece(color, 'G', i + 1)
            pieceSet[str(grasshopper)] = grasshopper
        for i in xrange(2):
            spider = HivePiece(color, 'S', i + 1)
            pieceSet[str(spider)] = spider
            beetle = HivePiece(color, 'B', i + 1)
            pieceSet[str(beetle)] = beetle
        queen = HivePiece(color, 'Q', 1)
        pieceSet[str(queen)] = queen
        return pieceSet

    def parse_cmd(self, cmd):
        self.logger.write(cmd + '\n')

        if cmd == 'pass':
            return ('non_play', cmd)
        if len(cmd) == 3:
            movingPiece = cmd
            pointOfContact = None
            refPiece = None
        else:
            if len(cmd) != 8:
                raise Exception("Failed to parse command.")
            movingPiece = cmd[:3]
            pointOfContact = cmd[3:5]
            refPiece = cmd[5:]
        return ('play', (movingPiece, pointOfContact, refPiece))

    def ppoc2cell(self, pointOfContact, refPiece):
        direction = self.poc2direction(pointOfContact)
        return self.hive.poc2cell(refPiece, direction)

    def poc2direction(self, pointOfContact):
        "Parse point of contact to a Hive.direction"
        if pointOfContact == '|*':
            return Hive.W
        if pointOfContact == '/*':
            return Hive.NW
        if pointOfContact == '*\\':
            return Hive.NE
        if pointOfContact == '*|':
            return Hive.E
        if pointOfContact == '*/':
            return Hive.SE
        if pointOfContact == '\\*':
            return Hive.SW
        if pointOfContact == '=*':
            return Hive.O
        raise ValueError('Invalid input for point of contact: "%s"' %
                         pointOfContact)

    def exec_cmd(self, cmd, turn):
        try:
            (cmdType, value) = self.parse_cmd(cmd)
            if cmdType == 'play':
                (actPiece, pointOfContact, refPiece) = value
            if cmdType == 'non_play' and value == 'pass':
                self.hive.action(cmdType, value)
                return True
        except:
            return False

        if pointOfContact is None and turn > 1:
            return False

        actPlayer = (2 - (turn % 2))
        try:
            p = self.player[actPlayer][actPiece]
            direction = None
            if pointOfContact is not None:
                direction = self.poc2direction(pointOfContact)
        except Exception, e:
            return False

        try:
            self.hive.action('play', (actPiece, refPiece, direction))
        except HiveException:
            return False
        return True
Пример #7
0
class TestHive(TestCase):
    """Verify the game logic"""

    # Pieces used for testing
    piece = {
        'wQ1': HivePiece('w', 'Q', 1),
        'wS1': HivePiece('w', 'S', 1),
        'wS2': HivePiece('w', 'S', 2),
        'wB1': HivePiece('w', 'B', 1),
        'wB2': HivePiece('w', 'B', 2),
        'wG1': HivePiece('w', 'G', 1),
        'bS1': HivePiece('b', 'S', 1),
        'bS2': HivePiece('b', 'S', 2),
        'bQ1': HivePiece('b', 'Q', 1),
        'bB1': HivePiece('b', 'B', 1),
        'bA1': HivePiece('b', 'A', 1),
        'bG1': HivePiece('b', 'G', 1),
    }

    def setUp(self):
        #    / \ / \ / \ / \ / \
        #   |wB1|wS2|   |bB1|   |
        #  / \ / \ / \ / \ / \ /
        # |wG1|   |wS1|bS1|bG1|
        #  \ / \ / \ / \ / \ / \
        #   |   |wQ1|   |bQ1|   |
        #  / \ / \ / \ / \ / \ /
        # |   |   |   |bA1|   |
        #  \ / \ / \ / \ / \ /
        # next is black player turn

        self.hive = Hive()
        self.hive.setup()

        self.hive.action('play', ('wS1'))
        self.hive.action('play', ('bS1', 'wS1', self.hive.E))
        self.hive.action('play', ('wQ1', 'wS1', self.hive.SW))
        self.hive.action('play', ('bQ1', 'bS1', self.hive.SE))
        self.hive.action('play', ('wS2', 'wS1', self.hive.NW))
        self.hive.action('play', ('bG1', 'bS1', self.hive.E))
        self.hive.action('play', ('wB1', 'wS2', self.hive.W))
        self.hive.action('play', ('bA1', 'bQ1', self.hive.SW))
        self.hive.action('play', ('wG1', 'wB1', self.hive.SW))
        self.hive.place_piece(self.piece['bB1'], 'bS1', self.hive.NE)

    def test_one_hive(self):
        self.assertFalse(self.hive._one_hive(self.piece['wS1']))
        self.assertTrue(self.hive._one_hive(self.piece['wQ1']))

    def test_bee_moves(self):
        beePos = self.hive.locate('wQ1')  # (-1, 1)
        expected = [(-1, 0), (0, 1)]
        self.assertEquals(expected, self.hive._bee_moves(beePos))

        beePos = self.hive.locate('wS1')
        expected = []
        self.assertEquals(expected, self.hive._bee_moves(beePos))

        beePos = self.hive.locate('wS2')
        expected = [(-1, -2), (0, -1)]
        self.assertEquals(expected, self.hive._bee_moves(beePos))

    def test_ant_moves(self):
        startCell = self.hive.locate('bA1')
        endCell = self.hive._poc2cell('wS1', self.hive.W)
        self.assertFalse(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell))

        endCell = (-2, 2)
        self.assertFalse(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell))

        endCell = self.hive._poc2cell('bA1', self.hive.SW)
        self.assertFalse(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell))

        endCell = self.hive._poc2cell('bS1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell))

        endCell = self.hive._poc2cell('wS1', self.hive.NE)
        self.assertTrue(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell))

        endCell = self.hive._poc2cell('wQ1', self.hive.W)
        self.assertTrue(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell))

    def test_beetle_moves(self):
        # moving in the ground level
        beetle = self.piece['bB1']
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('wS2', self.hive.E)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell))

        endCell = self.hive._poc2cell('bB1', self.hive.NW)
        self.assertFalse(
            self.hive._valid_beetle_move(beetle, startCell, endCell))

        self.hive.turn = 11  # set turn to be white player turn
        self.hive.activePlayer = 0
        beetle = self.piece['wB2']
        self.hive.place_piece(beetle, 'wQ1', self.hive.W)
        startCell = self.hive._poc2cell('wQ1', self.hive.W)
        endCell = self.hive._poc2cell('wQ1', self.hive.NW)
        self.assertFalse(
            self.hive._valid_beetle_move(beetle, startCell, endCell))

        # moving from ground to top
        beetle = self.piece['bB1']
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('bS1', self.hive.O)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell))

        # moving on top of the pieces
        self.hive.turn = 12  # set turn to be black player turn
        self.hive.activePlayer = 1
        beetle = self.piece['bB1']
        self.hive.move_piece(beetle, 'bS1', self.hive.O)
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('bS1', self.hive.W)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell))

        # moving from top to ground
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('bS1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell))

    def test_grasshopper_moves(self):
        grasshopper = self.piece['bG1']
        startCell = self.hive.locate('bG1')
        endCell = self.hive._poc2cell('wS1', self.hive.W)
        self.assertTrue(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell))

        endCell = self.hive._poc2cell('bA1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell))

        endCell = self.hive._poc2cell('wG1', self.hive.W)
        self.assertFalse(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell))

        grasshopper = self.piece['wG1']
        startCell = self.hive.locate('wG1')
        endCell = self.hive._poc2cell('wB1', self.hive.NE)
        self.assertTrue(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell))

    def test_queen_moves(self):
        queen = self.piece['bQ1']
        startCell = self.hive.locate('bQ1')
        endCell = self.hive._poc2cell('bQ1', self.hive.E)
        self.assertTrue(self.hive._valid_queen_move(queen, startCell, endCell))

        endCell = self.hive._poc2cell('bQ1', self.hive.SW)
        self.assertFalse(self.hive._valid_queen_move(queen, startCell,
                                                     endCell))

        # moving out of a surrounding situation
        queen = self.piece['wQ1']
        bA1 = self.piece['bA1']
        self.hive.turn = 11  # set turn to be white player turn
        self.hive.activePlayer = 0
        self.hive.move_piece(queen, 'wS1', self.hive.W)
        self.hive.turn = 12  # set turn to be black player turn
        self.hive.activePlayer = 1
        self.hive.move_piece(bA1, 'wG1', self.hive.SE)

        startCell = self.hive.locate('wQ1')
        endCell = self.hive._poc2cell('wS1', self.hive.SW)
        self.assertFalse(self.hive._valid_queen_move(queen, startCell,
                                                     endCell))

    def test_spider_moves(self):
        spider = self.piece['bS2']
        self.hive.place_piece(spider, 'bA1', self.hive.SE)

        startCell = self.hive.locate('bS2')
        endCell = self.hive._poc2cell('wQ1', self.hive.E)
        self.assertFalse(
            self.hive._valid_spider_move(spider, startCell, endCell))

        endCell = self.hive._poc2cell('wQ1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_spider_move(spider, startCell, endCell))

        endCell = self.hive._poc2cell('wQ1', self.hive.W)
        self.assertFalse(
            self.hive._valid_spider_move(spider, startCell, endCell))

        endCell = self.hive._poc2cell('bG1', self.hive.E)
        self.assertTrue(
            self.hive._valid_spider_move(spider, startCell, endCell))

        endCell = self.hive._poc2cell('bA1', self.hive.E)
        self.assertFalse(
            self.hive._valid_spider_move(spider, startCell, endCell))

    def test_spider_moves2(self):
        spider = self.piece['bS2']
        self.hive.place_piece(spider, 'bB1', self.hive.NE)

        startCell = self.hive.locate('bS2')
        endCell = self.hive._poc2cell('wS2', self.hive.NE)
        self.assertTrue(
            self.hive._valid_spider_move(spider, startCell, endCell))

    def test_validate_place_piece(self):
        wA1 = HivePiece('w', 'A', 1)
        bB2 = HivePiece('b', 'B', 2)

        # place over another piece
        cell = self.hive._poc2cell('wS1', self.hive.SW)
        self.assertFalse(self.hive._validate_place_piece(wA1, cell))

        # valid placement
        cell = self.hive._poc2cell('bG1', self.hive.E)
        self.assertTrue(self.hive._validate_place_piece(bB2, cell))

        # wrong color
        cell = self.hive._poc2cell('wQ1', self.hive.E)
        self.assertFalse(self.hive._validate_place_piece(wA1, cell))

    def test_move_piece(self):
        # move beetle over spider
        bB1 = self.piece['bB1']
        cell = self.hive.locate('bS1')
        self.hive.move_piece(bB1, 'bS1', self.hive.O)
        pieces = self.hive.get_pieces(cell)

        self.assertEquals(cell, self.hive.locate('bB1'))
        self.assertEquals(2, len(pieces))
        self.assertTrue('bB1' in pieces)
        self.assertTrue('bS1' in pieces)

    def test_action(self):
        # place a piece and verify unplayedPieces dict
        self.hive.action('play', ('bS2', 'bQ1', self.hive.E))
        unplayedPieces = self.hive.get_unplayed_pieces('b')

        self.assertFalse('bS2' in unplayedPieces)

    def test_first_move(self):
        """Test that we can move a piece on the 3rd turn
        wA1, bA1/*wA1, wG1*|wA1, bS1/*bA1, wQ1\*wA1, bA2*\\bA1, wG1|*wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        hive.action('play', ('wG1', 'wA1', hive.E))
        hive.action('play', ('bS1', 'bA1', hive.NW))
        hive.action('play', ('wQ1', 'wA1', hive.SW))
        hive.action('play', ('bA2', 'bA1', hive.NE))
        hive.action('play', ('wG1', 'wA1', hive.W))

    def test_fail_placement(self):
        """Test that we can place a piece after an incorrect try.
        wA1, bA1/*wA1, wG1|*bA1, wG1*|wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        try:
            # This placement fails
            hive.action('play', ('wG1', 'bA1', hive.W))
        except:
            pass
        # This placement is correct
        hive.action('play', ('wG1', 'wA1', hive.E))

    def test_victory_conditions(self):
        """Test that we end the game when a victory/draw condition is meet."""
        hive = Hive()
        hive.setup()

        hive.action('play', ('wS1'))
        hive.action('play', ('bS1', 'wS1', hive.E))
        hive.action('play', ('wQ1', 'wS1', hive.NW))
        hive.action('play', ('bQ1', 'bS1', hive.NE))
        hive.action('play', ('wG1', 'wQ1', hive.W))
        hive.action('play', ('bS2', 'bS1', hive.E))
        hive.action('play', ('wA1', 'wQ1', hive.NE))
        hive.action('play', ('bA1', 'bQ1', hive.E))
        hive.action('play', ('wG1', 'wQ1', hive.E))
        hive.action('play', ('bG2', 'bQ1', hive.NE))
        hive.action('play', ('wA1', 'wG1', hive.NE))

        self.assertTrue(hive.check_victory() == hive.WHITE_WIN)
Пример #8
0
    def test_victory_conditions(self):
        """Test that we end the game when a victory/draw condition is meet."""
        hive = Hive()
        hive.setup()

        hive.action('play', ('wS1'))
        hive.action('play', ('bS1', 'wS1', hive.E))
        hive.action('play', ('wQ1', 'wS1', hive.NW))
        hive.action('play', ('bQ1', 'bS1', hive.NE))
        hive.action('play', ('wG1', 'wQ1', hive.W))
        hive.action('play', ('bS2', 'bS1', hive.E))
        hive.action('play', ('wA1', 'wQ1', hive.NE))
        hive.action('play', ('bA1', 'bQ1', hive.E))
        hive.action('play', ('wG1', 'wQ1', hive.E))
        hive.action('play', ('bG2', 'bQ1', hive.NE))
        hive.action('play', ('wA1', 'wG1', hive.NE))

        self.assertTrue(hive.check_victory() == hive.WHITE_WIN)
Пример #9
0
    def test_fail_placement(self):
        """Test that we can place a piece after an incorrect try.
        wA1, bA1/*wA1, wG1|*bA1, wG1*|wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        try:
            # This placement fails
            hive.action('play', ('wG1', 'bA1', hive.W))
        except:
            pass
        # This placement is correct
        hive.action('play', ('wG1', 'wA1', hive.E))
Пример #10
0
    def test_first_move(self):
        """Test that we can move a piece on the 3rd turn
        wA1, bA1/*wA1, wG1*|wA1, bS1/*bA1, wQ1\*wA1, bA2*\\bA1, wG1|*wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        hive.action('play', ('wG1', 'wA1', hive.E))
        hive.action('play', ('bS1', 'bA1', hive.NW))
        hive.action('play', ('wQ1', 'wA1', hive.SW))
        hive.action('play', ('bA2', 'bA1', hive.NE))
        hive.action('play', ('wG1', 'wA1', hive.W))
Пример #11
0
class HiveShellClient(object):
    """HiveShellClient is a command line client to the Hive game."""

    def __init__(self):
        super(HiveShellClient, self).__init__()
        self.hive = Hive()
        self.view = HiveView(self.hive)
        self.input = sys.stdin
        self.player = {1: None, 2: None}
        self.logger = None


    def piece_set(self, color):
        """
        Return a full set of hive pieces
        """
        pieceSet = {}
        for i in xrange(3):
            ant = HivePiece(color, 'A', i+1)
            pieceSet[str(ant)] = ant
            grasshopper = HivePiece(color, 'G', i+1)
            pieceSet[str(grasshopper)] = grasshopper
        for i in xrange(2):
            spider = HivePiece(color, 'S', i+1)
            pieceSet[str(spider)] = spider
            beetle = HivePiece(color, 'B', i+1)
            pieceSet[str(beetle)] = beetle
        queen = HivePiece(color, 'Q', 1)
        pieceSet[str(queen)] = queen
        return pieceSet

    def parse_cmd(self, cmd):
        self.logger.write(cmd+'\n')

        if len(cmd) == 3:
            movingPiece = cmd
            pointOfContact = None
            refPiece = None
        else:
            if len(cmd) != 8:
                raise Exception("Failed to parse command.")
            movingPiece = cmd[:3]
            pointOfContact = cmd[3:5]
            refPiece = cmd[5:]
        return (movingPiece, pointOfContact, refPiece)


    def ppoc2cell(self, pointOfContact, refPiece):
        direction = self.poc2direction(pointOfContact)
        return self.hive.poc2cell(refPiece, direction)


    def poc2direction(self, pointOfContact):
        "Parse point of contact to a Hive.direction"
        if pointOfContact == '|*':
            return Hive.W
        if pointOfContact == '/*':
            return Hive.NW
        if pointOfContact == '*\\':
            return Hive.NE
        if pointOfContact == '*|':
            return Hive.E
        if pointOfContact == '*/':
            return Hive.SE
        if pointOfContact == '\\*':
            return Hive.SW
        if pointOfContact == '=*':
            return Hive.O
        return None


    def exec_cmd(self, cmd, turn):
        try:
            (actPiece, pointOfContact, refPiece) = self.parse_cmd(cmd)
        except:
            return False

        if pointOfContact is None and turn > 1:
            return False

        actPlayer = (2 - (turn % 2))
        try:
            p = self.player[actPlayer][actPiece]
        except Exception, e:
            return False

        direction = None
        if pointOfContact is not None:
            direction = self.poc2direction(pointOfContact)

        try:
            self.hive.action(actPiece, refPiece, direction)
        except HiveException:
            return False
        return True
Пример #12
0
class TestHive(TestCase):
    """Verify the game logic"""

    # Pieces used for testing
    piece = {
        'wQ1': HivePiece('w', 'Q', 1),
        'wS1': HivePiece('w', 'S', 1),
        'wS2': HivePiece('w', 'S', 2),
        'wB1': HivePiece('w', 'B', 1),
        'wB2': HivePiece('w', 'B', 2),
        'wG1': HivePiece('w', 'G', 1),
        'bS1': HivePiece('b', 'S', 1),
        'bS2': HivePiece('b', 'S', 2),
        'bQ1': HivePiece('b', 'Q', 1),
        'bB1': HivePiece('b', 'B', 1),
        'bA1': HivePiece('b', 'A', 1),
        'bG1': HivePiece('b', 'G', 1),
    }

    def setUp(self):
        #    / \ / \ / \ / \ / \
        #   |wB1|wS2|   |bB1|   |
        #  / \ / \ / \ / \ / \ /
        # |wG1|   |wS1|bS1|bG1|
        #  \ / \ / \ / \ / \ / \
        #   |   |wQ1|   |bQ1|   |
        #  / \ / \ / \ / \ / \ /
        # |   |   |   |bA1|   |
        #  \ / \ / \ / \ / \ /
        # next is black player turn

        self.hive = Hive()
        self.hive.setup()

        self.hive.action('play', ('wS1'))
        self.hive.action('play', ('bS1', 'wS1', self.hive.E))
        self.hive.action('play', ('wQ1', 'wS1', self.hive.SW))
        self.hive.action('play', ('bQ1', 'bS1', self.hive.SE))
        self.hive.action('play', ('wS2', 'wS1', self.hive.NW))
        self.hive.action('play', ('bG1', 'bS1', self.hive.E))
        self.hive.action('play', ('wB1', 'wS2', self.hive.W))
        self.hive.action('play', ('bA1', 'bQ1', self.hive.SW))
        self.hive.action('play', ('wG1', 'wB1', self.hive.SW))
        self.hive.place_piece(self.piece['bB1'], 'bS1', self.hive.NE)


    def test_one_hive(self):
        self.assertFalse(self.hive._one_hive(self.piece['wS1']))
        self.assertTrue(self.hive._one_hive(self.piece['wQ1']))


    def test_bee_moves(self):
        beePos = self.hive.locate('wQ1')  # (-1, 1)
        expected = [(-1, 0), (0, 1)]
        self.assertEqual(expected, self.hive._bee_moves(beePos))

        beePos = self.hive.locate('wS1')
        expected = []
        self.assertEqual(expected, self.hive._bee_moves(beePos))

        beePos = self.hive.locate('wS2')
        expected = [(-1, -2), (0, -1)]
        self.assertEqual(expected, self.hive._bee_moves(beePos))


    def test_ant_moves(self):
        startCell = self.hive.locate('bA1')
        endCell = self.hive._poc2cell('wS1', self.hive.W)
        self.assertFalse(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell)
        )

        endCell = (-2, 2)
        self.assertFalse(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell)
        )

        endCell = self.hive._poc2cell('bA1', self.hive.SW)
        self.assertFalse(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell)
        )

        endCell = self.hive._poc2cell('bS1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell)
        )

        endCell = self.hive._poc2cell('wS1', self.hive.NE)
        self.assertTrue(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell)
        )

        endCell = self.hive._poc2cell('wQ1', self.hive.W)
        self.assertTrue(
            self.hive._valid_ant_move(self.piece['bA1'], startCell, endCell)
        )


    def test_beetle_moves(self):
        # moving in the ground level
        beetle = self.piece['bB1']
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('wS2', self.hive.E)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell)
        )

        endCell = self.hive._poc2cell('bB1', self.hive.NW)
        self.assertFalse(
            self.hive._valid_beetle_move(beetle, startCell, endCell)
        )

        self.hive.turn = 11  # set turn to be white player turn
        self.hive.activePlayer = 0
        beetle = self.piece['wB2']
        self.hive.place_piece(beetle, 'wQ1', self.hive.W)
        startCell = self.hive._poc2cell('wQ1', self.hive.W)
        endCell = self.hive._poc2cell('wQ1', self.hive.NW)
        self.assertFalse(
            self.hive._valid_beetle_move(beetle, startCell, endCell)
        )

        # moving from ground to top
        beetle = self.piece['bB1']
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('bS1', self.hive.O)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell)
        )

        # moving on top of the pieces
        self.hive.turn = 12  # set turn to be black player turn
        self.hive.activePlayer = 1
        beetle = self.piece['bB1']
        self.hive.move_piece(beetle, 'bS1', self.hive.O)
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('bS1', self.hive.W)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell)
        )

        # moving from top to ground
        startCell = self.hive.locate('bB1')
        endCell = self.hive._poc2cell('bS1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_beetle_move(beetle, startCell, endCell)
        )


    def test_grasshopper_moves(self):
        grasshopper = self.piece['bG1']
        startCell = self.hive.locate('bG1')
        endCell = self.hive._poc2cell('wS1', self.hive.W)
        self.assertTrue(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell)
        )

        endCell = self.hive._poc2cell('bA1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell)
        )

        endCell = self.hive._poc2cell('wG1', self.hive.W)
        self.assertFalse(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell)
        )

        grasshopper = self.piece['wG1']
        startCell = self.hive.locate('wG1')
        endCell = self.hive._poc2cell('wB1', self.hive.NE)
        self.assertTrue(
            self.hive._valid_grasshopper_move(grasshopper, startCell, endCell)
        )


    def test_queen_moves(self):
        queen = self.piece['bQ1']
        startCell = self.hive.locate('bQ1')
        endCell = self.hive._poc2cell('bQ1', self.hive.E)
        self.assertTrue(
            self.hive._valid_queen_move(queen, startCell, endCell)
        )

        endCell = self.hive._poc2cell('bQ1', self.hive.SW)
        self.assertFalse(
            self.hive._valid_queen_move(queen, startCell, endCell)
        )

        # moving out of a surrounding situation
        queen = self.piece['wQ1']
        bA1 = self.piece['bA1']
        self.hive.turn = 11  # set turn to be white player turn
        self.hive.activePlayer = 0
        self.hive.move_piece(queen, 'wS1', self.hive.W)
        self.hive.turn = 12  # set turn to be black player turn
        self.hive.activePlayer = 1
        self.hive.move_piece(bA1, 'wG1', self.hive.SE)

        startCell = self.hive.locate('wQ1')
        endCell = self.hive._poc2cell('wS1', self.hive.SW)
        self.assertFalse(
            self.hive._valid_queen_move(queen, startCell, endCell)
        )


    def test_spider_moves(self):
        spider = self.piece['bS2']
        self.hive.place_piece(spider, 'bA1', self.hive.SE)

        startCell = self.hive.locate('bS2')
        endCell = self.hive._poc2cell('wQ1', self.hive.E)
        self.assertFalse(
            self.hive._valid_spider_move(spider, startCell, endCell)
        )

        endCell = self.hive._poc2cell('wQ1', self.hive.SW)
        self.assertTrue(
            self.hive._valid_spider_move(spider, startCell, endCell)
        )

        endCell = self.hive._poc2cell('wQ1', self.hive.W)
        self.assertFalse(
            self.hive._valid_spider_move(spider, startCell, endCell)
        )

        endCell = self.hive._poc2cell('bG1', self.hive.E)
        self.assertTrue(
            self.hive._valid_spider_move(spider, startCell, endCell)
        )

        endCell = self.hive._poc2cell('bA1', self.hive.E)
        self.assertFalse(
            self.hive._valid_spider_move(spider, startCell, endCell)
        )


    def test_spider_moves2(self):
        spider = self.piece['bS2']
        self.hive.place_piece(spider, 'bB1', self.hive.NE)

        startCell = self.hive.locate('bS2')
        endCell = self.hive._poc2cell('wS2', self.hive.NE)
        self.assertTrue(
            self.hive._valid_spider_move(spider, startCell, endCell)
        )


    def test_validate_place_piece(self):
        wA1 = HivePiece('w', 'A', 1)
        bB2 = HivePiece('b', 'B', 2)

        # place over another piece
        cell = self.hive._poc2cell('wS1', self.hive.SW)
        self.assertFalse(
            self.hive._validate_place_piece(wA1, cell)
        )

        # valid placement
        cell = self.hive._poc2cell('bG1', self.hive.E)
        self.assertTrue(
            self.hive._validate_place_piece(bB2, cell)
        )

        # wrong color
        cell = self.hive._poc2cell('wQ1', self.hive.E)
        self.assertFalse(
            self.hive._validate_place_piece(wA1, cell)
        )


    def test_move_piece(self):
        # move beetle over spider
        bB1 = self.piece['bB1']
        cell = self.hive.locate('bS1')
        self.hive.move_piece(bB1, 'bS1', self.hive.O)
        pieces = self.hive.get_pieces(cell)

        self.assertEqual(cell, self.hive.locate('bB1'))
        self.assertEqual(2, len(pieces))
        self.assertTrue('bB1' in pieces)
        self.assertTrue('bS1' in pieces)


    def test_action(self):
        # place a piece and verify unplayedPieces dict
        self.hive.action('play', ('bS2', 'bQ1', self.hive.E))
        unplayedPieces = self.hive.get_unplayed_pieces('b')

        self.assertFalse('bS2' in unplayedPieces)


    def test_first_move(self):
        """Test that we can move a piece on the 3rd turn
        wA1, bA1/*wA1, wG1*|wA1, bS1/*bA1, wQ1\*wA1, bA2*\\bA1, wG1|*wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        hive.action('play', ('wG1', 'wA1', hive.E))
        hive.action('play', ('bS1', 'bA1', hive.NW))
        hive.action('play', ('wQ1', 'wA1', hive.SW))
        hive.action('play', ('bA2', 'bA1', hive.NE))
        hive.action('play', ('wG1', 'wA1', hive.W))


    def test_fail_placement(self):
        """Test that we can place a piece after an incorrect try.
        wA1, bA1/*wA1, wG1|*bA1, wG1*|wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        try:
            # This placement fails
            hive.action('play', ('wG1', 'bA1', hive.W))
        except:
            pass
        # This placement is correct
        hive.action('play', ('wG1', 'wA1', hive.E))


    def test_victory_conditions(self):
        """Test that we end the game when a victory/draw condition is meet."""
        hive = Hive()
        hive.setup()

        hive.action('play', ('wS1'))
        hive.action('play', ('bS1', 'wS1', hive.E))
        hive.action('play', ('wQ1', 'wS1', hive.NW))
        hive.action('play', ('bQ1', 'bS1', hive.NE))
        hive.action('play', ('wG1', 'wQ1', hive.W))
        hive.action('play', ('bS2', 'bS1', hive.E))
        hive.action('play', ('wA1', 'wQ1', hive.NE))
        hive.action('play', ('bA1', 'bQ1', hive.E))
        hive.action('play', ('wG1', 'wQ1', hive.E))
        hive.action('play', ('bG2', 'bQ1', hive.NE))
        hive.action('play', ('wA1', 'wG1', hive.NE))

        self.assertTrue(hive.check_victory() == hive.WHITE_WIN)
Пример #13
0
    def test_victory_conditions(self):
        """Test that we end the game when a victory/draw condition is meet."""
        hive = Hive()
        hive.setup()

        hive.action('play', ('wS1'))
        hive.action('play', ('bS1', 'wS1', hive.E))
        hive.action('play', ('wQ1', 'wS1', hive.NW))
        hive.action('play', ('bQ1', 'bS1', hive.NE))
        hive.action('play', ('wG1', 'wQ1', hive.W))
        hive.action('play', ('bS2', 'bS1', hive.E))
        hive.action('play', ('wA1', 'wQ1', hive.NE))
        hive.action('play', ('bA1', 'bQ1', hive.E))
        hive.action('play', ('wG1', 'wQ1', hive.E))
        hive.action('play', ('bG2', 'bQ1', hive.NE))
        hive.action('play', ('wA1', 'wG1', hive.NE))

        self.assertTrue(hive.check_victory() == hive.WHITE_WIN)
Пример #14
0
    def test_fail_placement(self):
        """Test that we can place a piece after an incorrect try.
        wA1, bA1/*wA1, wG1|*bA1, wG1*|wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        try:
            # This placement fails
            hive.action('play', ('wG1', 'bA1', hive.W))
        except:
            pass
        # This placement is correct
        hive.action('play', ('wG1', 'wA1', hive.E))
Пример #15
0
    def test_first_move(self):
        """Test that we can move a piece on the 3rd turn
        wA1, bA1/*wA1, wG1*|wA1, bS1/*bA1, wQ1\*wA1, bA2*\\bA1, wG1|*wA1
        """
        hive = Hive()
        hive.setup()

        hive.action('play', ('wA1'))
        hive.action('play', ('bA1', 'wA1', hive.NW))
        hive.action('play', ('wG1', 'wA1', hive.E))
        hive.action('play', ('bS1', 'bA1', hive.NW))
        hive.action('play', ('wQ1', 'wA1', hive.SW))
        hive.action('play', ('bA2', 'bA1', hive.NE))
        hive.action('play', ('wG1', 'wA1', hive.W))
Пример #16
0
class HiveShellClient(object):
    """HiveShellClient is a command line client to the Hive game."""

    def __init__(self):
        super(HiveShellClient, self).__init__()
        self.hive = Hive()
        self.view = HiveView(self.hive)
        self.input = sys.stdin
        self.player = {1: None, 2: None}
        self.logger = None


    def piece_set(self, color):
        """
        Return a full set of hive pieces
        """
        pieceSet = {}
        for i in xrange(3):
            ant = HivePiece(color, 'A', i+1)
            pieceSet[str(ant)] = ant
            grasshopper = HivePiece(color, 'G', i+1)
            pieceSet[str(grasshopper)] = grasshopper
        for i in xrange(2):
            spider = HivePiece(color, 'S', i+1)
            pieceSet[str(spider)] = spider
            beetle = HivePiece(color, 'B', i+1)
            pieceSet[str(beetle)] = beetle
        queen = HivePiece(color, 'Q', 1)
        pieceSet[str(queen)] = queen
        return pieceSet

    def parse_cmd(self, cmd):
        self.logger.write(cmd+'\n')

        if len(cmd) == 3:
            movingPiece = cmd
            pointOfContact = None
            refPiece = None
        else:
            if len(cmd) != 8:
                raise Exception("Failed to parse command.")
            movingPiece = cmd[:3]
            pointOfContact = cmd[3:5]
            refPiece = cmd[5:]
        return (movingPiece, pointOfContact, refPiece)


    def ppoc2cell(self, pointOfContact, refPiece):
        direction = self.poc2direction(pointOfContact)
        return self.hive.poc2cell(refPiece, direction)


    def poc2direction(self, pointOfContact):
        "Parse point of contact to a Hive.direction"
        if pointOfContact == '|*':
            return Hive.W
        if pointOfContact == '/*':
            return Hive.NW
        if pointOfContact == '*\\':
            return Hive.NE
        if pointOfContact == '*|':
            return Hive.E
        if pointOfContact == '*/':
            return Hive.SE
        if pointOfContact == '\\*':
            return Hive.SW
        if pointOfContact == '=*':
            return Hive.O
        return None


    def exec_cmd(self, cmd, turn):
        try:
            (actPiece, pointOfContact, refPiece) = self.parse_cmd(cmd)
        except:
            return False

        if pointOfContact is None and turn > 1:
            return False

        actPlayer = (2 - (turn % 2))
        try:
            p = self.player[actPlayer][actPiece]
        except:
            return False

        direction = None
        if pointOfContact is not None:
            direction = self.poc2direction(pointOfContact)

        try:
            self.hive.action(actPiece, refPiece, direction)
        except HiveException:
            return False
        return True


    def run(self):
        self.logger = open('game.log', 'w')
        self.player[1] = self.piece_set('w')
        self.player[2] = self.piece_set('b')
        self.hive.turn += 1 # white player start
        self.hive.setup()

        while self.hive.check_victory() == self.hive.UNFINISHED:
            print ("Turn: %s" % self.hive.turn)
            active_player = (2 - (self.hive.turn % 2))
            print (self.view)
            print ("pieces available: %s" % sorted(
                self.player[active_player].keys()
            ))
            waiting = True
            while (waiting):
                try:
                    cmd = self.input.readline()
					# cmd = self.input.readline()
                except KeyboardInterrupt, e:
                    break
				

            print("player %s play: " % active_player)
			
            if self.exec_cmd(cmd.strip(), self.hive.turn):
                print (" =  "*79)
                print("\n")
            else:
                print("invalid play!")


        print( "\nThanks for playing Hive. Have a nice day!")