Как я понимаю, я использую текстовый редактор Atom для программирования на Python, поэтому у меня нет функций input()
, поэтому мне пришлось рандомизировать мой массив бинго
import random
import numpy as np
m=5 #lines
n=5 #columns/rows
mat=[]
bingo_numbers = np.linspace(1,n*m,n*m,dtype=int)
remaining_numbers = bingo_numbers # I need this later on to know what numbers are left
random.shuffle(bingo_numbers)
print(bingo_numbers)
completed_lines = 0
for i in range(m):
col=bingo_numbers[i*5:(i+1)*5]
mat.append(list(col))
def imprimecartela(mat, completed_lines): # Function to print the bingo card
print("+", end=branco)
print((16)*"-" + "+")
print("| ", end=branco)
if (completed_lines == 0):
print(5*"_ ", end=branco)
elif(completed_lines == 1):
print("B ", end=branco)
print(4*"_ ", end=branco)
elif(completed_lines == 2):
print("B ", end=branco)
print("I ", end=branco)
print(3*"_ ", end=branco)
elif(completed_lines == 3):
print("B ", end=branco)
print("I ", end=branco)
print("N ", end=branco)
print(2*"_ ", end=branco)
elif(completed_lines == 4):
print("B ", end=branco)
print("I ", end=branco)
print("N ", end=branco)
print("G ", end=branco)
print("_ ", end=branco)
else:
print("B ", end=branco)
print("I ", end=branco)
print("N ", end=branco)
print("G ", end=branco)
print("O ", end=branco)
print("|")
print("+" + (16)*"=" + "+")
for i in range(m):
print("| ", end=branco)
for j in range(n):
if mat[i][j] != 0: # Check values of <mat>: if non zero print number with 2 digits, if zero print 'XX'
print(str(mat[i][j]).zfill(2) + " ", end='')
else:
print("XX" + " ", end='')
print("|")
print("+" + (16)*"-" + "+")
def check_completed_lines(mat):
completed_lines = 0
for i in range(m):
temp = [x[i] for x in mat]
if (temp == [0,0,0,0,0]):
completed_lines += 1
for x in mat:
if x==[0,0,0,0,0]:
completed_lines += 1
if (mat[0][0] == 0 and mat[1][1] == 0 and mat[2][2] == 0 and mat[3][3] == 0 and mat[4][4] == 0):
completed_lines += 1
if (mat[0][4] == 0 and mat[1][3] == 0 and mat[2][2] == 0 and mat[3][1] == 0 and mat[4][0] == 0):
completed_lines += 1
return completed_lines
imprimecartela(mat,completed_lines)
while (len(remaining_numbers) != 0): # Looping through turns
call_number = random.choice(remaining_numbers) # <-- Next number
print("next number is : ", call_number)
remaining_numbers = np.delete(remaining_numbers, np.where(remaining_numbers==call_number)) # Remove the number so it doesn't occur again
for i in mat:
if call_number in i:
i[i.index(call_number)] = 0 # Change the value current round number to 0 in <mat>
completed_lines = check_completed_lines(mat) # This function checks rows and columns and diagonals for completeness, every completed line will add a letter to "BINGO" on the card
imprimecartela(mat, completed_lines)
if completed_lines == 5:
break # When 5 lines are completed, you win, break
Я добавил комментарии внутри кода, чтобы объяснить процесс, но в основном вам не нужно менять матрицу, чтобы включить 'XX'
, просто измените значение на 0
, поскольку ноль уже не является числом бинго и используйте вашу печать функция для печати 'XX'
, если значение 0
. Приветствия.