예제 #1
0
def create_game_menu(board_list):

    board = None
    actions = None
    repeat = True
    while repeat:
        print("Choose a name for the game:")
        name = input()

        print("Height of the grid:")
        height = int_input_greater_than_one()

        print("Width of the grid:")
        width = int_input_greater_than_one()

        print("Number of players:")
        max_number_players = positive_int_input()

        piece_markers = []
        print("How many types of pieces?")
        number_types_pieces = positive_int_input()

        for i in range(number_types_pieces):
            print("Marker of piece number {}: (it can't be 0)".format(i + 1))
            need_to_ask = True
            while need_to_ask:
                marker = input()
                if marker != "0":
                    need_to_ask = False
                    piece_markers.append(marker)
                else:
                    print("Marker can't be 0! Try again")
                    need_to_ask = True
        #print("Verify win every turn? (y or n)")
        #end_type=input()
        #print("Length to win:")
        #length_to_win = positive_int_input()

        print("-------------------------------------")
        print("Recap: ")
        print("Name: {}".format(name))
        print("Grid height: {}".format(height))
        print("Grid width: {}".format(width))
        #print("End type: {}".format(end_type))
        #print("Length to win: {}".format(length_to_win))
        print("Pieces:")

        for i in range(number_types_pieces):
            print("  Piece number {}: {}".format(i + 1, piece_markers[i]))

        print("Everything OK so far? ")
        print("1: Yes, proceed")
        print("2: No, start over again")
        print("3: Go back to menu")
        option = input()

        if option == "1":

            print("Continuing...")

            # Create a new Boardgame Object
            board = Boardgame(name, height, width, max_number_players)

            # But we still need to define the following parameters in this object before we can use the board:
            # - type (defined inside add_turn_actions())
            # - piece_marker_dict
            # - turn (An object of Turn)
            # - players (List of players, will be well defined during game execution rather than creation)
            # At the moment (game creation), we'll define the first three

            # Define piece_marker_dict:
            # Create a dictionary to relate each type of piece with its marker
            marker_dict = {0: " "}

            # To well define the turn object, we need to define a set of actions
            actions = add_turn_actions(board, marker_dict, piece_markers)
            # Verify if list of actions creation was successful
            if actions is not None:
                # Instantiate a turn
                turn = Turn()
                # Define its actions
                turn.actions = actions

                # Then, put it into board as the selected game (Load into menu)
                board.turn = turn

                # Show the created actions
                for action in actions:
                    print("Action: {} with description: {}".format(
                        action.keyboard_input, action.description))

                # Add board into list
                board_list.append(board)

                # Save the created game in a file

                # Try to see if the file already exists
                try:
                    with open("saved_games.bin", "rb") as file_object:
                        # Load board list from file
                        board_list = pickle.load(file_object)

                        # Add newly created game into the loaded list
                        board_list.append(board)
                except FileNotFoundError:
                    print("saved_games.bin not found! Creating...")

                # Save board list:
                try:
                    with open("saved_games.bin", "wb") as file_object:
                        pickle.dump(board_list, file_object)
                except:
                    print("Error trying to create saved_games.bin!")

            repeat = False
        elif option == "2":
            repeat = True
        elif option == "3":
            repeat = False

    return board, actions