Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''a memory game using 2d arrays'''
- from random import shuffle
- #function to print the grid
- def print_grid(element):
- #print the grid of guesses
- for i in range(5):
- for j in range(4):
- if i +j ==0:
- print(' ', sep='', end='')
- print(' ',sep='',end='')
- if i == 0:
- print(j, sep='',end='')
- print(' ',sep='',end='')
- else:
- print(element[i-1][j],end='')
- print(' ',sep='',end='')
- print()
- print()
- if i < 4:
- print(i,sep='', end='')
- print(' ',sep='',end='')
- def digit(user_input):
- '''make sure input is digit'''
- user_input_array = []
- if user_input.isdigit():
- #returns converted digit separated into an array
- user_input_array.append(int(user_input[0]))
- user_input_array.append(int(user_input[1]))
- return user_input_array
- else:
- return "alpha"
- #the data
- cards = ['A', 'A', 'A', 'A', 'K', 'K', 'K', 'K', 'Q', 'Q', 'Q', 'Q', 'J', 'J', 'J', 'J']
- guesses = [['?','?','?','?',],['?','?','?','?',],['?','?','?','?',],['?','?','?','?',]]
- shuffle(cards) #shuffle the deck
- cards2d = [[],[],[],[]]
- for i in range(16): # add the shuffled cards to the 2d array
- if i < 4:
- cards2d[0].append(cards[i])
- elif i < 9 and i > 4:
- cards2d[1].append(cards[i])
- elif i < 13 and i > 8:
- cards2d[2].append(cards[i])
- else:
- cards2d[3].append(cards[i])
- print_grid(cards2d)
- #print_grid(guesses)
- #get user input and validate it
- correct_guesses = 0
- correct_coordinates = [[],[]]
- print_grid(guesses)
- while (correct_guesses < 8):
- valid_input = False
- while not valid_input:
- input1 = input('''Enter a card position by specifying the row position followed by the column position,
- (e.g. 12 for row 1 column 2): ''')
- guess1 = digit(input1)
- input2 = input('''Enter a card position by specifying the row position followed by the column position,
- (e.g. 12 for row 1 column 2): ''')
- guess2 = digit(input2)
- if guess1 == "alpha" or guess2 =="alpha":
- print('Only user integers!')
- 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 :
- print('Out of range!')
- elif input1 == input2:
- print("Guesses cannot be the same!")
- elif input1 in correct_coordinates[0] or input2 in correct_coordinates[1]:
- print("You've already guessed that!")
- else:
- valid_input = True
- #check the arrays to see if user found a match
- user_row_guess1 = guess1[0]
- user_column_guess1 = guess1[1]
- card_guess1 = cards2d[user_row_guess1][user_column_guess1]
- #print(card_guess1)
- user_row_guess2 = guess2[0]
- user_column_guess2 = guess2[1]
- card_guess2 = cards2d[user_row_guess2][user_column_guess2]
- #print(card_guess2)
- #tell the user the outsome and update the grid
- if card_guess1 == card_guess2:
- #uncomment these to display cards matched rather than x
- # guesses[user_row_guess1][user_column_guess1] = cards2d[user_row_guess1][user_column_guess1]
- # guesses[user_row_guess2][user_column_guess2] = cards2d[user_row_guess2][user_column_guess2]
- #comment out these two
- guesses[user_row_guess1][user_column_guess1] = 'X'
- guesses[user_row_guess2][user_column_guess2] = 'X'
- correct_coordinates[0].append(input1)
- correct_coordinates[1].append(input2)
- print(correct_coordinates)
- print("Match!")
- print_grid(guesses)
- correct_guesses +=1
- else:
- print("Unlucky!")
- print_grid(guesses)
Advertisement
Add Comment
Please, Sign In to add comment