0

I have been asked to make a number matching game for a school project, which involves generating random numbers in a grid. I have managed to generate the grid full of random numbers, but I need them to be in pairs so that they can be matched. Any help would be greatly appreciated. Here is my code so far:

def play(): import random

sizeofgrid()

board = []

showboard = []

for row in range(gridsize):
    board.append([])
    for column in range(gridsize):
        board[row].append(random.randint(0, 9))

def print_board(board):
    for row in board:
        print(' '.join([str(i) for i in row]))

showboard = [['O' for _ in range(gridsize)] for _ in range(gridsize)]

print_board(showboard)
print_board(board)
2
  • Do you want there to be only two of each number? Or, numbers 0-9 repeated as often as needed, but end in an even number of each? Commented Sep 7, 2017 at 16:27
  • Yes, I need two of each number which are arranged in random positions around the grid. Thinking about it, it can be from 0 to however many is required to fill the grid (the size of the grid is chosen by the user). I have obviously made sure that there can only be an even number of spaces in the grid. Commented Sep 7, 2017 at 16:36

2 Answers 2

1

One approach might be to generate half of the numbers you need and then double that list. Then, shuffle the desired values and resize them into a grid.

from random import randint, shuffle

size = 4 # must be even
pairs = size**2/2

pool = range(pairs) * 2
# [0, 1, 2, 3, 4, 5, 6, 7, 
#  0, 1, 2, 3, 4, 5, 6, 7]

shuffle(pool)

grid = [pool[i:i+size] for i in range(0, size**2, size)]
# [[4, 5, 0, 7], [1, 7, 3, 5], [2, 1, 2, 6], [4, 0, 3, 6]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, I will give it a go.
0

You need to change your approach. You can try one of two things:

  1. Start with an empty board. Generate a random number, then generate two random locations on the board. Keep re-generating until you find an empty slot, twice. Put the random number into both locations.

    for _ in range(width * height // 2):
        number = pick_random_number()
        loc1 = find_random_empty_spot()
        board[loc1.row][loc1.col] = number
        loc2 = find_random_empty_spot()
        board[loc2.row][loc2.col] = number
    
  2. Compute how many locations will need to be filled. Generate a N/2 random numbers, put each random number into a list, twice. Now, shuffle the list, and then loop over the board, inserting items from the list into the board.

    num_picks = width * height // 2
    picks = []
    
    for _ in range(num_picks):
        number = pick_random_number()
        picks.extend([number, number])
    
    random.shuffle(picks)
    
    for row in range(height):
        for col in range(width):
            board[row][col] = picks.pop()
    

Either of these approaches will get you the paired behavior you want. Which one you choose is a matter of comfort.

3 Comments

Thanks for the help, how would I go about generating a random position? Would I use coordinates?
That's one way. Generate a random int in the range(width) and another in range(height).
Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.