示例#1
0
def play ():
    
    grid = []
    util.create_grid (grid)
    add_block (grid)
    add_block (grid)
    won_message = False
    while (True):
        util.print_grid (grid)
        key = input ("Enter a direction:\n")
        if (key in ['x', 'u', 'd', 'l', 'r']):
            saved_grid = util.copy_grid (grid)
            if (key == 'x'):
                return
            elif (key == 'u'):
                push.push_up (grid)
            elif (key == 'd'):
                push.push_down (grid)
            elif (key == 'r'):
                push.push_right (grid)
            elif (key == 'l'):
                push.push_left (grid)
            if util.check_lost (grid):
                print ("Game Over!")
                return
            elif util.check_won (grid) and not won_message:
                print ("Won!")
                won_message = True  
            if not util.grid_equal (saved_grid, grid):
                add_block (grid)
示例#2
0
def play ():
    """generate grid and play game interactively"""
    # create grid
    grid = []
    util.create_grid (grid)
    # add 2 starting random numbers
    add_block (grid)
    add_block (grid)
    won_message = False
    while (True):
        util.print_grid (grid)
        key = input ("Enter a direction:\n")
        if (key in ['x', 'u', 'd', 'l', 'r']):
            # make a copy of the grid
            saved_grid = util.copy_grid (grid)
            if (key == 'x'):
                # quit the game
                return
            # manipulate the grid depending on input
            elif (key == 'u'):
                push.push_up (grid)
            elif (key == 'd'):
                push.push_down (grid)
            elif (key == 'r'):
                push.push_right (grid)
            elif (key == 'l'):
                #print(grid)
                push.push_left (grid)
                #print(grid)
            # check for a grid with no more gaps or legal moves
            if util.check_lost (grid):
                print ("Game Over!")
                return
            # check for a grid with the final number
            elif util.check_won (grid) and not won_message:
                print ("Won!")
                won_message = True
            # finally add a random block if the grid has changed    
            if not util.grid_equal (saved_grid, grid):
                add_block (grid)
示例#3
0
def play():
    """generate grid and play game interactively"""
    # create grid
    grid = []
    util.create_grid(grid)
    # add 2 starting random numbers
    add_block(grid)
    add_block(grid)
    won_message = False
    while (True):
        util.print_grid(grid)
        key = input("Enter a direction:\n")
        if (key in ['x', 'u', 'd', 'l', 'r']):
            # make a copy of the grid
            saved_grid = util.copy_grid(grid)
            if (key == 'x'):
                # quit the game
                return
            # manipulate the grid depending on input
            elif (key == 'u'):
                push.push_up(grid)
            elif (key == 'd'):
                push.push_down(grid)
            elif (key == 'r'):
                push.push_right(grid)
            elif (key == 'l'):
                push.push_left(grid)
            # check for a grid with no more gaps or legal moves
            if util.check_lost(grid):
                print("Game Over!")
                return
            # check for a grid with the final number
            elif util.check_won(grid) and not won_message:
                print("Won!")
                won_message = True
            # finally add a random block if the grid has changed
            if not util.grid_equal(saved_grid, grid):
                add_block(grid)
示例#4
0
def play ():
    # makes the game and plays
    # makes grid
    grid = []
    util.create_grid (grid)
    # adds 2 random numbers to start with
    add_block (grid)
    add_block (grid)
    won_message = False
    while (True):
        util.print_grid (grid)
        key = input ("Enter a direction:\n")
        if (key in ['x', 'u', 'd', 'l', 'r']):
            # copies the grid
            saved_grid = util.copy_grid (grid)
            if (key == 'x'):
                # end the game
                return
            # edit the grid depending on the input
            elif (key == 'u'):
                push.push_up (grid)
            elif (key == 'd'):
                push.push_down (grid)
            elif (key == 'r'):
                push.push_right (grid)
            elif (key == 'l'):
                push.push_left (grid)
            # check for a grid with no more gaps or legal moves
            if util.check_lost (grid):
                print ("Game Over!")
                return
            # check for a grid with the last number
            elif util.check_won (grid) and not won_message:
                print ("Won!")
                won_message = True
            # lastly add a random block if the grid has changed    
            if not util.grid_equal (saved_grid, grid):
                add_block (grid)
示例#5
0
def play():
    grid = []

    # CREATE THE GRID
    util.create_grid(grid)

    # PLACE THE TWO NUMBERS ON THE BOARD
    util.generate_next_position(grid)

    while (True):
        # Clear the terminal so that the game seems more interactive
        os.system('cls' if os.name == 'nt' else 'clear')

        # PRINT THE CURRENT GRID
        util.print_grid(grid)

        # set 'key' to a user input which represents the move they would like to do
        key = input("Enter a direction:\n")

        # check if 'key' is one of the available options
        if (key in ['x', 'u', 'd', 'l', 'r']):
            if (key == 'x'):
                # quit the game
                return
            # manipulate the grid depending on input
            elif (key == 'u'):
                grid = push.push_up(grid)
            elif (key == 'd'):
                grid = push.push_down(grid)
            elif (key == 'r'):
                grid = push.push_right(grid)
            elif (key == 'l'):
                grid = push.push_left(grid)

            print(grid)
            # check for a grid with no more gaps or legal moves
            util.check_for_loss(grid)
            util.check_for_win(grid)
示例#6
0
def play():
    """generate grid and play game interactively"""
    # create grid
    grid = []
    util.create_grid(grid)
    # add 2 starting random numbers
    add_block(grid)
    add_block(grid)
    # initially set 'won_message' to False. 'won_message' is used to determine if the user has been alerted that they won as to not keep printing the winning  message if they continue playing.
    won_message = False
    # start
    while (True):
        # Clear the terminal so that the game seems more interactive
        os.system('cls' if os.name == 'nt' else 'clear')
        # print the current state of the grid
        util.print_grid(grid)
        # set 'key' to a user input which represents the move they would like to do
        key = input("Enter a direction:\n")
        # check if 'key' is one of the available options
        if (key in ['x', 'u', 'd', 'l', 'r']):
            # make a copy of the grid
            saved_grid = util.copy_grid(grid)
            if (key == 'x'):
                # quit the game
                return
            # manipulate the grid depending on input
            elif (key == 'u'):
                push.push_up(grid)
            elif (key == 'd'):
                push.push_down(grid)
            elif (key == 'r'):
                push.push_right(grid)
            elif (key == 'l'):
                push.push_left(grid)
            # check for a grid with no more gaps or legal moves
            if util.check_lost(grid):
                print("Game Over!")
                return
            # check for a grid with the final number
            elif not won_message and util.check_won(grid):
                print("Won!")
                won_message = True
                # Clear the terminal so that the game seems more interactive
                os.system('cls' if os.name == 'nt' else 'clear')
                # print the current state of the grid
                util.print_grid(grid)
                if input('Would you like to keep playing? (Y)es or (N)o: '
                         ).lower() == 'n':
                    print('Game Over!')
                    return
            # finally add a random block if the grid has changed
            if not util.grid_equal(saved_grid, grid):
                add_block(grid)
            # check for a grid with no more gaps or legal moves
            if util.check_lost(grid):
                # Clear the terminal so that the game seems more interactive
                os.system('cls' if os.name == 'nt' else 'clear')
                # print last state of grid
                util.print_grid(grid)
                print("Game Over!")
                return
示例#7
0
文件: 2048.py 项目: h-kouame/2048
def play(agent='human', record=True, filename='human_games.dat', delay=1):
    """generate grid and play game interactively"""
    # create grid
    grid = []
    util.create_grid(grid)
    # add 2 starting random numbers
    add_block(grid)
    add_block(grid)
    won_message = False
    score = 0
    trajectory = []
    saved_grid = [[]]
    try:
        while (True):
            util.print_grid(grid)
            if agent == HUMAN:
                key = input("Enter a direction:\n")
                if (key == 'x'):
                    # quit the game
                    break
                try:
                    key = util.action_num_to_letter(int(key))
                except:
                    continue
            elif agent == SVM or agent == NN:
                if util.grid_equal(saved_grid,
                                   grid):  #previous action not allowed
                    #use pior agent since model is deterministic given the same input
                    key = agents.random_agent_with_prior()
                    print("Prior")
                else:
                    print("Model")
                    model_agent = agents.Model()
                    model_agent.load_model(filename="models/" + agent +
                                           ".joblib")
                    key = model_agent.predict([np.array(grid).flatten()])
                    key = util.action_num_to_letter(int(key[0]))
                print(key)
            elif agent == RANDOM:
                key = agents.random_agent()
            elif agent == RANDOM_WITH_PRIOR:
                key = agents.random_agent_with_prior()
            if (key in ['u', 'd', 'l', 'r']):
                # make a copy of the grid
                saved_grid = util.copy_grid(grid)
                # manipulate the grid depending on input
                if (key == 'u'):
                    _, add_score = push.push_up(grid)
                elif (key == 'd'):
                    _, add_score = push.push_down(grid)
                elif (key == 'r'):
                    _, add_score = push.push_right(grid)
                elif (key == 'l'):
                    _, add_score = push.push_left(grid)
                score += add_score
                print("Score: ", score)
                if record and key in 'ludr':
                    data_instance = np.array(saved_grid).flatten()
                    data_instance = np.append(
                        data_instance,
                        [util.action_letter_to_num(key), add_score, score])
                    data_instance = np.append(data_instance,
                                              np.array(grid).flatten())
                    trajectory.append(data_instance)
                # check for a grid with no more gaps or legal moves
                if util.check_lost(grid):
                    print("Game Over!")
                    break
                # check for a grid with the final number
                elif util.check_won(grid) and not won_message:
                    print("Won!")
                    won_message = True
                # finally add a random block if the grid has changed
                if not util.grid_equal(saved_grid, grid):
                    add_block(grid)
            if agent != HUMAN:
                time.sleep(delay)  #in seconds
    finally:
        if record:
            util.save_game(trajectory,
                           filename="games/" + agent.lower() + "/" +
                           agent.lower())