Exemple #1
0
def main():
    """
    Main function
    :return: void
    """
    game = Game()
    game.add_random_cell()
    game.add_random_cell()
    renderer = Renderer(game)

    while True:
        old_field = deepcopy(game.get_field())
        renderer.print_field()

        if not game.has_moves():
            renderer.print_game_over()
            break

        renderer.get_key()

        field = game.get_field()

        if field != old_field and game.check_empty():
            game.add_random_cell()

    renderer.print_bye()
    sleep(1)
    curses.endwin()
def evaluate_effectivness(n, fct):  #runs 2024 game and saves score, n times
    ans = 0
    for _ in range(n):
        game = Game()
        game.spawn_tile()
        game.spawn_tile()
        cont = True
        while cont:
            k = ai.find_move(game, 3, fct, fct2, 1)
            if k != -1:
                if game.move(k):
                    game.spawn_tile()
            if k == 0:
                ans += ai.score(game.board)
                break
    return ans / n
Exemple #3
0
def create_tree(o_game, turns_ahead, fct, fct2, ignore_4=2, end_it=False):
    game = Game()
    game.board = copy_board(o_game.board)
    tree = Decision(game, [])
    assert (not tree.subtrees)

    for move in range(1, 5):
        g = Game()
        g.board = copy_board(game.board)
        g.move(move)
        if g.board != game.board:
            tree.moves.append(move)
            subtree = Reaction(g, None, fct, fct2)
            #print("appending to tree",tree)
            tree.add(subtree)

            ast = count_zeros(g.board)
            #print("stage2")
            if ast > 7:
                turns_ahead -= 1
            if ast < 3 and turns_ahead == 3 and not end_it:
                print("REEEEEEEEEEET")
                turns_ahead += 1
                end_it = True
            for k in range(ast * ignore_4):

                tg = Game()
                tg.board = copy_board(g.board)
                tg.spawn_tile(k % (ast), ((k // (ast)) * 2 + 2), True)
                #print("appending to tree",subtree)
                if turns_ahead > 1:

                    subtree.add(
                        create_tree(tg, turns_ahead - 1, fct, fct2, ignore_4,
                                    end_it))

    return tree
Exemple #4
0
                turns_ahead -= 1
            if ast < 3 and turns_ahead == 3 and not end_it:
                print("REEEEEEEEEEET")
                turns_ahead += 1
                end_it = True
            for k in range(ast * ignore_4):

                tg = Game()
                tg.board = copy_board(g.board)
                tg.spawn_tile(k % (ast), ((k // (ast)) * 2 + 2), True)
                #print("appending to tree",subtree)
                if turns_ahead > 1:

                    subtree.add(
                        create_tree(tg, turns_ahead - 1, fct, fct2, ignore_4,
                                    end_it))

    return tree


def find_move(game, m, fct, fct2, ignore_4=2):
    tree = create_tree(game, m, basic_fct, basic_fct2, ignore_4)
    return tree.deduct()


if __name__ == '__main__':
    game = Game()
    game.board = [[0, 2, 0, 8], [0, 0, 0, 4], [0, 0, 0, 2], [0, 0, 0, 0]]
    tree = create_tree(game, 1, basic_fct, basic_fct2)
    game.pb()
Exemple #5
0
def main():
    game = Game()
    game.initialize_board()
    game.board = game.computer_turn(game.board)
    target = "smooth"
    allowed_moves = -1
    while True:
        minimax_sol = MiniMaxSolver(target)
        minimax_sol.max_depth = 3

        #print "building tree..."
        tree = minimax_sol.build_tree(game)
        if tree == None:
            break
        #print "updating tree..."
        tree = minimax_sol.update_scores(tree)
        #tree.print_tree()

        new_action = tree.action
        print tree.score
        new_move = game.player_turn(new_action, game.board)
        game.board = new_move
        game.board = game.computer_turn(game.board)
        game.print_board(game.board)
        allowed_moves -= 1

        if allowed_moves == 0:
            if target == "smooth":
                target = "emptiness"
                allowed_moves = 3
            else:
                target = "smooth"
                allowed_moves = 9 * 5
    game.print_board(game.board)
Exemple #6
0
    g3 = g2[::-1, :, :]
    r0 = grid.swapaxes(0, 1)
    r1 = r0[::-1, :, :]
    r2 = r0[:, ::-1, :]
    r3 = r2[::-1, :, :]
    xtrain = np.array([g0, g1, g2, g3, r0, r1, r2, r3], dtype=floatX)
    ytrain = np.array([v] * 8, dtype=floatX)
    train_fn(xtrain, ytrain)


N = 100
s = 0
slen = 0
stat = {}
for i in range(N):
    game = Game()
    game_len = 0
    while not game.end and game.max() <= 2048:
        board = np.array([make_input(game.grid)], dtype=floatX)
        p = P(board)
        #print 'p.shape: ', p.shape
        #moves = np.argsort(p)[::-1]
        moves = np.argsort(P(board)[0])[::-1]
        p_sort = np.sort(P(board)[0])[::-1]
        prev_score = game.score
        for m in moves:
            if game.move(m):
                v = 2 * (game.score - prev_score) + p_sort[m]
                Vchange(deepcopy(game.grid), v)
                break
        game_len += 1
def main():
    """
    Main function
    :return: void
    """
    game = Game()
    game.add_random_cell()
    game.add_random_cell()

    while True:
        old_field = deepcopy(game.get_field())
        print_field(game, old_field)

        if not game.has_moves():
            print("No available moves left, game over.")
            break

        while True:
            try:
                but = getch.getch()  # pylint: disable=c-extension-no-member
            except (EOFError, KeyboardInterrupt):
                break

            if but in ('a', 'A'):
                game.move_left()
                break
            elif but in ('d', 'D'):
                game.move_right()
                break
            elif but in ('w', 'W'):
                game.move_up()
                break
            elif but in ('s', 'S'):
                game.move_down()
                break
            elif but in ('q', 'Q'):
                print("Bye!")
                exit()

        field = game.get_field()

        if field != old_field and game.check_empty():
            game.add_random_cell()

    print("Bye!")
Exemple #8
0
#!/usr/bin/env python
# -*- coding: utf8 -*-
from game2048 import Game
import sys
import PySimpleGUI as sg

size = 4

game = Game()

game.put_tile()
game.put_tile()
layout = [[
    sg.Button(game.board[i][j], size=(4, 2), key=(i, j), pad=(0, 0))
    for j in range(size)
] for i in range(size)]

window = sg.Window('Minesweeper', layout)

while True:  # Event Loop
    event, values = window.read()
    if event in (None, '終了'):
        break

window.close()