Пример #1
0
    def __init__(self, gui, heuristic, depth):
        QThread.__init__(self, None)
        self.exiting = False

        self.gui = gui
        self.game = Play2048State(heuristic)

        self.minimax = Minimax(self.actions(), depth)
Пример #2
0
class TestPlay2048Player(unittest.TestCase):
    def setUp(self):
        depth = 4
        self.minimax = Minimax(Play2048Player.actions(), depth)

    def test_minimax_correct_move(self):
        game = Play2048State(LogGradient)
        game.board = [
            [256, 128, 64, 32],
            [64, 64, 16, 8],
            [0, 0, 4, 0],
            [2, 0, 0, 0]
        ]

        suggested_move = self.minimax.alpha_beta_decision(game)
        print(suggested_move)
        print(game.evaluation_function())

    def test_minimax_run(self):

        game = Play2048State(SnakeGradient)
        game.board = [
            [4, 16, 64, 16],
            [4, 8, 0, 0],
            [0, 2, 0, 2],
            [0, 0, 0, 0]
        ]

        preferred = ['up']

        # self.preferred = ['up', 'left', 'up', 'right']

        # Up => [8, 16, 64, 16], [0, 8, 0, 2], [0, 2, 0, 0], [0, 0, 0, 0]
        # Left => [8, 16, 64, 16], [8, 2, 0, 0], [2, 0 , 0, 0], [0, 0, 0, 0]
        # Up => [16, 16, 64, 16, [2, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]
        # Right => [0, 32, 64, 16], [0, 0, 0, 4], [0, 0, 0, 0], [0, 0, 0, 0]

        for preferred in preferred:
            suggested_move = self.minimax.alpha_beta_decision(game)

            if game.move(suggested_move):
                game.next_state()

            self.assertEqual(preferred, suggested_move)
Пример #3
0
class Play2048Player(QThread):
    """ Inherit QThread for easy threading """

    def __init__(self, gui, heuristic, depth):
        QThread.__init__(self, None)
        self.exiting = False

        self.gui = gui
        self.game = Play2048State(heuristic)

        self.minimax = Minimax(self.actions(), depth)

    @classmethod
    def actions(cls):
        return {'left': [-1, 0], 'up': [0, -1], 'right': [1, 0], 'down': [0, 1]}

    def run(self):
        ended = False

        while not ended:
            new_game = self.game.copy_with_board(self.game.board)
            move = self.minimax.alpha_beta_decision(new_game)

            if self.game.move(move):
                self.game.next_state()
                self.move_completed()

            if self.gui and self.gui.take_screenshots:
                self.gui.screenshot.emit()

            if not self.game.is_possible():
                ended = True

        if self.gui:
            self.gui.status_message.emit('Game ended')

    def move_completed(self):
        if not self.gui:
            return
        sleep(self.gui.delay / 1000.0)
        self.gui.score_message.emit('Score: {}'.format(self.game.score))
        self.gui.update()

    def end_player(self):
        self.setTerminationEnabled(True)
        self.terminate()
        self.gui.status_message.emit('Search killed')

    def __del__(self):
        self.exiting = True
        self.wait()

    def __str__(self):
        return '\n'.join(['-'.join(str(t) for t in r) for r in self.game.board])
Пример #4
0
 def setUp(self):
     depth = 4
     self.minimax = Minimax(Play2048Player.actions(), depth)