Esempio n. 1
0
    def test_heuristic(self):
        g = _2048()

        board = [[3, 0, 0, 0], [3, 1, 2, 0], [3, 1, 2, 0], [3, 0, 0, 0]]

        state = board

        up = g.my_move(np.copy(state), 2)
        down = g.my_move(np.copy(state), 0)
        right = g.my_move(np.copy(state), 1)
        left = g.my_move(np.copy(state), 3)

        b_h = gradient_heuristic(board)
        u_h = gradient_heuristic(up)
        d_h = gradient_heuristic(down)
        r_h = gradient_heuristic(right)
        l_h = gradient_heuristic(left)

        self.assertLess(b_h, u_h)
        self.assertLess(b_h, d_h)
        self.assertLess(r_h, u_h)
        self.assertLess(l_h, d_h)

        self.assertTrue(d_h - u_h < 1)
        self.assertTrue(r_h - l_h < 1)
Esempio n. 2
0
 def __init__(self, master=None):
     Frame.__init__(self, master)
     self.font = font.Font(master, family="Verdana", size=40, weight="bold")
     self.score_font = font.Font(master, family="Verdana", size=20)
     self.master.title('2048')
     self.grid()
     self.game = _2048()
     self.grid_cells = []
     self.score = 0
     self.score_board = None
     self.timer = None
     self.init_grid()
Esempio n. 3
0
    def test_move_left(self):
        g = _2048()
        board = [[1, 2, 3, 4], [4, 4, 5, 5], [2, 2, 3, 3], [1, 1, 1, 1]]

        state = board
        new = g.my_move(np.copy(state), 3)

        solution = [[1, 2, 3, 4], [5, 6, 0, 0], [3, 4, 0, 0], [2, 2, 0, 0]]

        not_true = np.array_equal(state, new)

        self.assertFalse(not_true)
        self.assertTrue(np.array_equal(solution, new))
Esempio n. 4
0
    def test_move_up(self):
        g = _2048()
        board = [[1, 1, 3, 1], [1, 1, 3, 1], [2, 1, 2, 1], [2, 1, 2, 1]]

        state = board
        new = g.my_move(np.copy(state), 2)

        solution = [[2, 2, 4, 2], [3, 2, 3, 2], [0, 0, 0, 0], [0, 0, 0, 0]]

        not_true = np.array_equal(state, new)

        self.assertFalse(not_true)
        self.assertTrue(np.array_equal(solution, new))
Esempio n. 5
0
def play_random():
    g = game._2048()
    g.initial = g.adv_move(g.initial)
    g.initial = g.adv_move(g.initial)
    state = g.initial

    for play in range(50):
        state = g.initial
        actions = list(g.actions(state))
        while actions:
            move = randint(0, 3)
            state = g.my_move(state, move)
            state = g.adv_move(state)
            actions = list(g.actions(state))

        print("RANDOM: ", 2**max(np.asarray(state).flatten().tolist()))
        add_highest_tile(state, "adversary")
Esempio n. 6
0
def play(pre, turn):
    states, labels, scores = [], [], []
    with open('modul6/new_training_set.txt') as training_file:
        for line in training_file:
            state, move, score = eval(line)
            if pre == 0:
                states.append(np.asarray(preprocess(state)))
            else:
                states.append(np.asarray(score_tune(state, score)))

            labels.append(move)
            scores.append(score)

        states = np.asarray(frobeus_norm(states))

    ann = ANN(states, labels, scores, [300],
              [tensor.tanh, tensor.tanh, Tann.softmax], 0.0001, 50, 5, 'mean')

    #ann = ANN(states, labels, scores, [300], [tensor.tanh, tensor.tanh, Tann.softmax], 0.0001, 50, 5, 'mean')
    #ann = ANN(states, labels, scores, [400, 400, 400], [rectify, rectify, rectify, rectify, Tann.softmax], 0.01, 20, 10, 'mean')

    #ann = ANN(states, labels, scores, [500, 500, 500], [tensor.tanh, tensor.tanh, tensor.tanh, tensor.tanh, Tann.softmax], 0.0001, 100, 5, 'mean')
    #ann = ANN(states, labels, scores, [500, 500, 500], [rectify, rectify, rectify, rectify, Tann.softmax], 0.0001, 100, 5, 'mean')
    #ann = ANN(states, labels, scores, [50], [tensor.tanh, tensor.tanh, Tann.softmax], 0.001, 50, 1, 'mean')

    ann.run()

    g = game._2048()
    g.initial = g.adv_move(g.initial)
    g.initial = g.adv_move(g.initial)
    state = g.initial

    move_dict = {0: 2, 1: 0, 2: 3, 3: 1}
    for play in range(50):
        score = 0
        state = g.initial
        actions = list(g.actions(state))
        while actions:
            prev = state

            temp = np.asarray(state).flatten().tolist()

            if pre == 0:
                move = ann.predict_move(preprocess(temp))
                next_move = ann.predict_next_move(preprocess(temp))
            else:
                move = ann.predict_move(temp)
                next_move = ann.predict_next_move(temp)

            move_two = sorted(list(next_move[0])).index(
                sorted(list(next_move[0]))[-2])
            move_three = sorted(list(next_move[0])).index(
                sorted(list(next_move[0]))[-3])
            move_four = sorted(list(next_move[0])).index(
                sorted(list(next_move[0]))[-4])

            current_state = np.copy(state)
            next_state = g.my_move(current_state, move_dict[move])
            next_state_two = g.my_move(current_state, move_dict[move_two])
            next_state_three = g.my_move(current_state, move_dict[move_three])
            next_state_four = g.my_move(current_state, move_dict[move_four])

            if not np.array_equal(current_state, next_state):
                state = g.my_move(state, move_dict[move])
            elif not np.array_equal(current_state, next_state_two):
                state = g.my_move(state, move_dict[move_two])
            elif not np.array_equal(current_state, next_state_three):
                state = g.my_move(state, move_dict[move_three])
            else:
                state = g.my_move(state, move_dict[move_four])
            # app.update_view(state, score, play)

            prev_diff = np.setdiff1d(prev.reshape(-1), state.reshape(-1))
            if prev_diff.size:
                for i in prev_diff:
                    score += 1 << i

            state = g.adv_move(state)
            # app.update_view(state, score, play)

            actions = list(g.actions(state))

        print("PLAYER: ", 2**max(np.asarray(state).flatten().tolist()))

        add_highest_tile(state, turn)
Esempio n. 7
0
from tkinter import Tk
from modul4.gui import GameWindow
from modul4.gamelogic import _2048
from modul4.adversial import expectimax_top
import time
import numpy as np

if __name__ == '__main__':
    g = _2048()
    g.initial = g.adv_move(g.initial)
    g.initial = g.adv_move(g.initial)
    state = g.initial

    root = Tk()
    app = GameWindow(master=root)
    score = 0

    t1 = time.time()
    timer = ""
    for i in range(10):
        state = g.initial
        actions = list(g.actions(state))
        while actions:
            timer = '%.2f' % (time.time() - t1)
            app.update_view(state, score, timer)
            thing = np.array(state).flatten().tolist()
            print(thing)
            prev = state
            _, state = expectimax_top(g, state)

            prev_diff = np.setdiff1d(prev.reshape(-1), state.reshape(-1))