Write a Python 3.6.3 program that simulates playing Bingo…

Question:

Write a Python 3.6.3 program that simulates playing Bingo. In Bingo, each player has a card of numbers arranged in a 5×5 grid with five randomly-chosen numbers from 1 to 15 in the B column, 16 to 30 in the I column, 31 to 45 in the N column, 46 to 60 in the G column, and 61 to 75 in the O column. The central space is “free” and is considered occupied. The 24 numbers on a Bingo card are unique.

A caller randomly calls numbers from 1 to 75 without replacement, each player marking the corresponding number, if it is present on their card, as occupied. The first player to have five occupied numbers in a row horizontally, in a column vertically, or along either of the two major diagonals is the winner. How to represent a Bingo card in Python. A dictionary will be used to represent a Bingo card. Here, the keys are the letters B, I, N, G, and O and the value for each key is a list of five integers.

card= {,B:[7,15 , 11 ,2,10], 「: [25 , 22, 30, 28, 27] N [44,40,0,37,39], G ,0%(62,70 ,74,68,75] } [57,50,46,55,59]

Note. The main(), print_card(), and open_file_command() have been written for you. These functions drive the program, prints a Bingo card stored in a dictionary, and reads a Bingo card from a file, respectively.

Link to code needed to be completed: https://repl.it/repls/ImpressionableWorthlessCockerspaniel

Your job is to write the three functions (check_bingo(), generate_random_card(), simulate_bingo()) described above.

a) Write the check_bingo(card) function. Here, card is a Bingo card represented as a dictionary as described on the previous page. Your code will return True if card has Bingo. Otherwise, it will return False.

b) Write the generate_random_card() function. You will write code to randomly generate a Bingo card. Your Bingo card must be represented as a dictionary as described earlier (also see comments in code). Your code will return a dictionary that represents a valid Bingo card. When writing your function, you might find the random.shuffle() command for lists helpful.

c) Write the simulate_bingo(n,k) function. Complete this function to simulate k games (or trials) of Bingo, where n cards (or players) are active in the simulation. Your function will return three values: the minimum, maximum, and average number of calls required to get Bingo among the k games of n cards. For example, if n = 10 and k = 100, then your simulation would report results for playing Bingo 100 times among 10 players. Remember, for each of the k games, you should generate a new set of Bingo cards. To complete the code for this function, make sure to make use of the previous code in parts (a) and (b) that you have written.

Answer:

import random
import ast

def pop(a,accumulated=None):
print "type q to move on to verification"
if accumulated is None:
accumulated = set()
v = 'I can put any string here, as long as it is not "q" :-)'
while v.lower() != 'q':
b = a.pop(0)
if b <= 15:
print 'b', b,
elif b > 15 and b <=30:
print 'i', b,
elif b > 30 and b <=45:
print 'n', b,
elif b > 45 and b <=60:
print 'g', b,
else:
print 'o', b,
accumulated.add(b)
v = raw_input('')
return accumulated

def verify(numbers):
new_nums = raw_input("enter numbers separated by ',': ")
nums = ast.literal_eval(new_nums)
assert( len(nums) == 5 ) #Need 5 numbers to win
result = set(nums).issubset(numbers)
print "Verified? ",result
return result
#alternatively, and probably slightly more efficient
# print numbers.issuperset(nums)
#I prefer the other though as `subset` is easier for me to remember


def robust_verify(numbers):
"""
keep trying to verify the numbers until we succeed
"""
try:
verify(numbers)
except (AssertionError,SyntaxError,TypeError): #other error conditions might go here too, but these are the ones I can think of right now ...
robust_verify(numbers)


def play_bingo(game_vals=None,accumulated=None):
if game_vals is None:
game_vals = list(range(1,76)) #list strictly not necessary here, but why not?
random.shuffle(game_vals)

nums = pop(game_vals,accumulated=accumulated)
if not robust_verify(nums):
play_bingo(game_vals=game_vals,accumulated=nums)


play_bingo()

Leave a Comment

Your email address will not be published. Required fields are marked *