KDT85

pairs.py

Nov 11th, 2022 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. '''a memory game using 2d arrays'''
  2. from random import shuffle
  3.  
  4. #function to print the grid
  5. def print_grid(element):
  6.     #print the grid of guesses
  7.     for i in range(5):
  8.         for j in range(4):
  9.             if i +j ==0:
  10.                 print(' ', sep='', end='')
  11.                 print(' ',sep='',end='')
  12.             if i == 0:
  13.                 print(j, sep='',end='')
  14.                 print(' ',sep='',end='')
  15.             else:
  16.                 print(element[i-1][j],end='')
  17.                 print(' ',sep='',end='')
  18.         print()
  19.         print()
  20.         if i < 4:
  21.             print(i,sep='', end='')
  22.             print(' ',sep='',end='')
  23.        
  24. def digit(user_input):
  25.     '''make sure input is digit'''
  26.     user_input_array = []
  27.     if user_input.isdigit():
  28.         #returns converted digit separated into an array
  29.         user_input_array.append(int(user_input[0]))
  30.         user_input_array.append(int(user_input[1]))
  31.         return user_input_array
  32.     else:
  33.         return "alpha"
  34.  
  35.  
  36. #the data
  37. cards = ['A', 'A', 'A', 'A', 'K', 'K', 'K', 'K', 'Q', 'Q', 'Q', 'Q', 'J', 'J', 'J', 'J']
  38. guesses = [['?','?','?','?',],['?','?','?','?',],['?','?','?','?',],['?','?','?','?',]]
  39. shuffle(cards) #shuffle the deck
  40. cards2d = [[],[],[],[]]
  41. for i in range(16): # add the shuffled cards to the 2d array
  42.     if i < 4:
  43.         cards2d[0].append(cards[i])
  44.     elif i < 9 and i > 4:
  45.         cards2d[1].append(cards[i])
  46.     elif i < 13 and i > 8:
  47.         cards2d[2].append(cards[i])
  48.     else:
  49.         cards2d[3].append(cards[i])
  50.  
  51. print_grid(cards2d)
  52. #print_grid(guesses)
  53.  
  54. #get user input and validate it
  55. correct_guesses = 0
  56. correct_coordinates = [[],[]]
  57. print_grid(guesses)
  58.  
  59. while (correct_guesses < 8):
  60.     valid_input = False
  61.     while not valid_input:
  62.         input1 = input('''Enter a card position by specifying the row position followed by the column position,
  63.    (e.g. 12 for row 1 column 2): ''')
  64.         guess1 = digit(input1)
  65.         input2 = input('''Enter a card position by specifying the row position followed by the column position,
  66.    (e.g. 12 for row 1 column 2): ''')
  67.         guess2 = digit(input2)
  68.         if guess1 == "alpha" or guess2 =="alpha":
  69.             print('Only user integers!')
  70.         elif guess1[0]>3 or guess2[0] >3 or guess1[0] <0 or guess2[0] <0 or guess1[1]>3 or guess2[1] >3 or guess1[1] <0 or guess2[1] <0 :
  71.             print('Out of range!')
  72.         elif input1 == input2:
  73.             print("Guesses cannot be the same!")
  74.         elif input1 in correct_coordinates[0] or input2 in correct_coordinates[1]:
  75.             print("You've already guessed that!")
  76.         else:
  77.             valid_input = True
  78.            
  79. #check the arrays to see if user found a match
  80.     user_row_guess1 = guess1[0]
  81.     user_column_guess1 = guess1[1]
  82.     card_guess1 = cards2d[user_row_guess1][user_column_guess1]
  83.     #print(card_guess1)
  84.     user_row_guess2 = guess2[0]
  85.     user_column_guess2 = guess2[1]
  86.     card_guess2 = cards2d[user_row_guess2][user_column_guess2]
  87.     #print(card_guess2)
  88.  
  89. #tell the user the outsome and update the grid
  90.     if card_guess1 == card_guess2:
  91.         #uncomment these to display cards matched rather than x
  92. #        guesses[user_row_guess1][user_column_guess1] = cards2d[user_row_guess1][user_column_guess1]
  93. #        guesses[user_row_guess2][user_column_guess2] = cards2d[user_row_guess2][user_column_guess2]
  94.         #comment out these two
  95.         guesses[user_row_guess1][user_column_guess1] = 'X'
  96.         guesses[user_row_guess2][user_column_guess2] = 'X'
  97.        
  98.         correct_coordinates[0].append(input1)
  99.         correct_coordinates[1].append(input2)
  100.         print(correct_coordinates)
  101.         print("Match!")
  102.         print_grid(guesses)
  103.         correct_guesses +=1
  104.     else:
  105.         print("Unlucky!")
  106.         print_grid(guesses)
Advertisement
Add Comment
Please, Sign In to add comment