samedi 4 décembre 2021

Memory/Flipping tiles Game - trouble looping through it

I am making a memory game. Below is a part of the program. It is the part that takes input from the user and loops until the user has found all the pairs. It is a memory with only two different words so four "tiles" in total.

As long as you choose the correct coordinates and find pairs on both attempts, it works, but if the user enters coordinates for different words (not pairs), I want the game board with these words to be displayed and then print the "previous" game board so that the user can try again. My hope is to solve this using an if statement in "ask_coordinates_and_print_list". But I have big problems making it work. Lines 61 and 63 I have commented on. I suspect that it is the lines that need some type of operator to fix the problem and make the game work.

If you select coordinates [1, 1] and [2, 1] first loop and then [1, 2] and [2, 2] in the second loop, you pass the game. But if you select eg [1, 2] and [1, 1] (different words) the first loop, I want to print the game board with these words and then print the "previous" game board and let the player try again. As it works now, the same game board is printed twice and then the player can try again. How do I make the game work if the player enters coordinates for different words over and over again?

def ask_coordinates_return_coordinates_and_correspondning_words(list_of_found_pairs, nested_word_list_with_full_grading):
    while True:
        try:
            selected_row_word_one = int(input("Which ROW do you choose for the FIRST word:"))
            selected_column_word_one = int(input("Which COLUMN do you choose for the FIRST word:"))
            word_one = (nested_word_list_with_full_grading[selected_row_word_one][selected_column_word_one])
            coordinate_one = []
            coordinate_one.append(selected_row_word_one)
            coordinate_one.append(selected_column_word_one)
            selected_row_word_two = int(input("Which ROW do you choose for the SECOND word:"))
            selected_column_word_two = int(input("Which COLUMN do you choose for the FIRST word:"))
            word_two = (nested_word_list_with_full_grading[selected_row_word_two][selected_column_word_two])
            coordinate_two = []
            coordinate_two.append(selected_row_word_two)
            coordinate_two.append(selected_column_word_two)
            if selected_row_word_one > 0 and selected_column_word_one > 0 and selected_row_word_one <= (len(nested_word_list_with_full_grading)-1) and selected_column_word_one <= (len(nested_word_list_with_full_grading)-1) and selected_row_word_two > 0 and selected_column_word_two > 0 and selected_row_word_two <= (len(nested_word_list_with_full_grading)-1) and selected_column_word_two <= (len(nested_word_list_with_full_grading)-1) and coordinate_one != coordinate_two:
                if coordinate_one not in list_of_found_pairs and coordinate_two not in list_of_found_pairs:
                    if word_one == word_two:
                        list_of_found_pairs.append(coordinate_one)
                        list_of_found_pairs.append(coordinate_two)
                        return word_one, word_two, coordinate_one, coordinate_two
                    else:
                        return word_one, word_two,coordinate_one, coordinate_two
                else:
                    print("You have entered the coordinates of words already found, please try again")
            else:
                print("Not valid choice. Keep in mind that the playing field is the size " + str((len(nested_word_list_with_full_grading)-1)) + "X" + str((len(nested_word_list_with_full_grading)-1)) + " and that you can not select the same coordinates")
        except ValueError:
            print("Not an optional alternative! You must enter a number.")
        except IndexError:
            print("You must select a coordinate on the playing field")

def add_words_to_list(list,words_and_coordinates):
    first_word_coordinate = words_and_coordinates[2]
    second_word_coordinates = words_and_coordinates[3]
    a = first_word_coordinate[0]
    b = first_word_coordinate[1]
    c = second_word_coordinates[0]
    d = second_word_coordinates[1]
    (list[a][b]) = words_and_coordinates[0]
    (list[c][d]) = words_and_coordinates[1]
    return list

def show_list_in_terminal(list):
    for row in list:
        for elem in row:
            print(elem, end=' ')
        print()
def ask_coordinates_and_print_list(nested_word_list, nested_word_list_with_full_grading, improvement_list):
    list_of_found_pairs = []
    while True:
        if len(list_of_found_pairs) < (len(nested_word_list)*2):
            words_and_coordinates =  ask_coordinates_return_coordinates_and_correspondning_words(list_of_found_pairs, nested_word_list_with_full_grading)

            if words_and_coordinates[0] == words_and_coordinates[1]:
                list = add_words_to_list(improvement_list, words_and_coordinates)
                show_list_in_terminal(list)
                print("YOU FOUND A PAIR")
            else:
                list = add_words_to_list(improvement_list,words_and_coordinates)
                show_list_in_terminal(list) # Here I want to print a list of the words that the user has entered
                print("DIFFERENT WORDS")
                show_list_in_terminal(list) #Here I want to print list without the words the user has entered, because the user entered the coordinates of different words. So I want to print the list of from one iteration earlier
        else:
            print("CONGRATULATIONS, YOU WON")
            name = input("enter your name: ") #"enter your name" I have only added to prevent "while true" from looping indefinitely

nested_word_list = [['dog', 'cat'], ['dog', 'cat']]
nested_word_list_with_full_grading = [['  1', '  2'], [1, 'dog', 'cat'], [2, 'dog', 'cat']]
nested_x_list_with_full_grading = [['  1', '  2'], [1, 'XXX', 'XXX'], [2, 'XXX', 'XXX']]
improvement_list = [['  1', '  2'], [1, 'XXX', 'XXX'], [2, 'XXX', 'XXX']]

def main_function():
    show_list_in_terminal(nested_x_list_with_full_grading)
    ask_coordinates_and_print_list(nested_word_list, nested_word_list_with_full_grading, improvement_list)

main_function()

Aucun commentaire:

Enregistrer un commentaire