0

This is the code I have so far, my teacher wants the game to "flip the X's over" when you guess a number and when they match the numbers stay but when the numbers are different the numbers "flip back over" and become X's again. And he wants the game to say that "you win" when all the numbers have been exposed.

import random

visual=[['X','X','X','X','X'],['X','X','X','X','X'],['X','X','X','X','X'],['X','X','X','X','X'],['X','X','X','X','X']]
data=[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]


random.shuffle(data[0])
random.shuffle(data[1])
random.shuffle(data[2])
random.shuffle(data[3])
random.shuffle(data[4])


while True:


print(visual[0])
print(visual[1])
print(visual[2])
print(visual[3])
print(visual[4])


   user_input_1 = int(input('enter a number 0 thru 4 to pick your first X position: '))

    user_input_2 = int(input('enter a number 0 thru 4 to pick your first Y position: '))
    user_input_3 = int(input('enter a number 0 thru 4 to pick your second X position: '))


user_input_4 = int(input('enter a number 0 thru 4 to pick your second Y position: '))


if data[user_input_1][user_input_2] == data[user_input_3][user_input_4]:
    visual[user_input_1][user_input_2] = str(data[user_input_1][user_input_2])
    visual[user_input_3][user_input_4] = str(data[user_input_3][user_input_4])

    print(visual[0])
    print(visual[1])
    print(visual[2])
    print(visual[3])
    print(visual[4])
    print('Congratulations you won the game!')
    break
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    so what doesn't work exactly? – depperm Dec 12 '18 at 20:55
  • 4
    Your indentation is off, please edit your code again – Xion Dec 12 '18 at 20:58
  • 1
    I'm not sure that "please help me debug my homework" is an on-topic question on Stack Overflow. Could you focus it more? At the very least, provide a [mcve]. Debugging is an important skill that should be learned by practice rather than outsourced to Stack Overflow. – John Coleman Dec 12 '18 at 21:01
  • what doesn't work is that the numbers don't appear and they don't stay when the user gets a match – Milo Del Campo Dec 12 '18 at 21:10
  • You break out of the loop when the user finds a single match. The game needs to keep going until they find all the matches. – Barmar Dec 12 '18 at 22:27
  • You don't display the numbers at all when they enter something that doesn't match. You need an `else:` block for that. – Barmar Dec 12 '18 at 22:28

2 Answers2

1

Here are the discrete steps in the game as I understand it:

  1. Initialize the board and data.
  2. Shuffle the data
  3. Enter Loop
  4. Print the board
  5. Ask user for their first guess
  6. Ask user for their second guess
  7. Print the board with reveals
  8. Cover them back up if user missed
  9. Check win condition (everything revealed?)
  10. Loop back or print win

Your code successfully initializes the board (step 1), shuffles the data (2), enter loop (3), prints board (4), and asks for the guesses (5)(6).

Here is some guidance on the pieces you are missing:

  • After you get the inputs, you always want to print the board with the reveals (7). To do this you need to update visuals first with the piece you have written:

    visual[user_input_1][user_input_2] = str(data[user_input_1][user_input_2])
    visual[user_input_3][user_input_4] = str(data[user_input_3][user_input_4])
    

    and then print your visuals. This does not need to happen with an if statement, because you always want to do a print of the revealed board.

  • Afterwards, you want to do the swap back to X's if the user misses (8). You have this condition already basically written. You need to check if it's a miss in the data board and swap those visuals back to X's if true:

        if data[user_input_1][user_input_2] != data[user_input_3][user_input_4]:
            visual[user_input_1][user_input_2] = 'X'
            visual[user_input_3][user_input_4] = 'X'
    

    Side Note: Ideally, you want to clear the previous board print of the reveals to test memory. This part is kind of tricky. There are no simple ways to clear the print of the reveals without using operating system commands which are almost certainly beyond the scope of your class. I would check with your teacher about expectations around flipping back over.

    If you are interested in how this is achievable using operating system command in Python here is a relevant StackOverflow post. You would need to import os and import time. After you print reveal, use time.sleep(number_of_seconds) to give the user however many seconds to try to memorize placements and then use os.system('clear') for linux/mac os or os.system('CLS') for windows to clear the screen. Again, it's very unlikely that this is what your teacher is looking for.

  • Now let's deal with the win condition (9). Currently you are using a "while True:" with a break. While this may be functional in this case, using "while True:" should be almost always avoided (there are many reasons why-- here is a post that addresses one reason). Loop structures like "while", "do-while", and "for" have built-in stopping conditions which will be checked each time you loop. In this program, your stopping condition is that the board is completely revealed (that is how you know the game is over and user no longer needs to guess). So, until this happens you want your while loop to keep going. This looks like this:

        while #win condition is not met#:
            #stuff you want to loop#
        #congrats, you have met your win condition# 
    
  • There are multiple ways to check your win condition is not yet met. One option is that you know you have not won yet if the visual board is still not the same as data board:

        while visuals != data:
            #stuff you want to loop#
        print(congrats....)
    

In summary, TLDR:

import random
visuals = [...]
data = [...]
    #shuffle
    while visuals != data:
        #print visuals
        #get input
        #update the inputted visuals with reveals
        #print visuals with reveals
        if #not a match:
            #update the inputted visuals back to X's
    print("Congratulations you won the game!")

There are a handful of smaller points that could improve your code as well if you're interested: be very vigilant about indents, a do-while loops is more appropriate here (can you think of why?), you can write functions and loops that reduce a lot of the repeating code (like when you print the visuals). But this should be enough for you to grapple with.

rLevv
  • 498
  • 3
  • 12
0

Some suggestions for your code:

  1. Since you display the board so often, you may want to write a separate function that takes the variable visual as input and prints the content. Then you can just call the function instead of writing all of those print statements.
  2. In your current code, you only display the cards when there's a match. One of the whole strategies for playing a memory game is that if you overturn two cards and see "5" and "2" for example, it's not a match, but if you overturn a new pair of cards and one of them is another "5", you might remember where that first "5" was you overturned in your previous turn and thus know how to make a match. So perhaps you should do this: update and display visual after selecting the first card, then do the same with the second card. If there's a match, then leave visual as is. If they don't match, then replace the two spots back with 'X' and go back to the top of the loop.
  3. Your code seems to consider a single match a "win". Shouldn't it be considered won once all of the cards have been matched? In this case, you can follow up the previous step by checking to see if 'X' shows up anywhere in visual. If it doesn't, then that means all of the cards have been uncovered, thus the player has won.
Bill M.
  • 1,388
  • 1
  • 8
  • 16