Creating Memory game (flipping tiles game)

Viewed 32

I am having trouble with coordinates and flipping the cards separately. This is my first time handling with coordinates in python.

When trying to flip the cards separately, the code registers the rows of cards as lists. I did use a list of lists to display the cards.

The code is shown below.

import random

wrong = 0
used = []
cards = deck()
def mmain():
  random.shuffle(cards)
  selected_cards = cards[:int(result / 2)]
  selected_cards = selected_cards * 2
  random.shuffle(selected_cards)
  i = 0
  while i < len(selected_cards):
    row = selected_cards[i: i + columns]
    i = i + columns
    grid1.append(row1)
  squares = [""]
  grid = []
  row = []
  for i in range(rows):
    row.append(squares)
  for e in range(columns):
    grid.append(row)
  while True:
    for i in range(len(grid)):
      print(*grid[i])
    squares = str(squares)
    coordinate1 = int(input("Enter the first coordinate: "))
    coordinate2 = int(input("Enter the second coordinate: "))
    used.append(coordinate1)
    used.append(coordinate2)
    if coordinate1 in range(len(grid[i])):
      for k in grid[i]:
        k[coordinate1] = str(selected_cards[coordinate1])
    elif coordinate2 in range(len(grid[i])):
      for k in grid[i]:
        k[coordinate2] = str(selected_cards[coordinate2])
    elif selected_cards[coordinate1] == selected_cards[coordinate1]:
      grid9 = "⬛"
      grid10 = "⬛"
    else:
      wrong = wrong + 1
    if grid[i] == "⬛":
      print("You win! ")
      print("Your score is: ")
      break
mmain()

I would like help on this since I am struggling with it for weeks. I appreciate the answers to solve the problem. Thank you.

Note: I already have a program that helps displays the cards.

Edit: I am sure if someone helps me with this, the question I asked could help others.

Edit2: I use Google Colab for coding in python.

1 Answers

Your code needs to be rewritten for clarity

Here's a template you can use, then you just need to implement each function

Test each function with test cases to ensure it works properly, then move to the next one

def main():
    # create your deck
    cards = initialize_cards()

    # iterates until the winning condition is satisfied.
    # could be a simple check that the deck is empty.
    while not check_winning_condition(cards):

        # ask the user for coordinates, do all the checks to ensure they are valid coordinates.
        coord1, coord2 = ask_user_coordinates()

        # reveals the cards to the user. Either a simple console o a sophisticated GUI
        card1, card2 = reveal_cards(cards, coord1, coord2)
        
        # checks if the cards match and removes them from the deck if needed
        cards = check_cards(cards, card1, card2)

Ideally you should use classes, but for now try organizing like this.

You might need additional parameters, feel free to add the ones you need and avoid global variables.

Each function does one thing, if it does two things make two functions.

You see how simple the main function is?

Just make now each function do what is designed to do and test test test.

Related