我的程序应该模拟Bingo游戏。它接收一个5X5矩阵(Bingo卡)作为输入,应逐个验证元素是否在卡上以及元素序列中,这些元素的数量(是整数)。目的是验证每个元素是否在矩阵中:如果是,程序应将相应元素替换为“ XX”。程序应以上述方式连续进行,直到所有元素都经过验证。如果将任何行,列或对角线的所有元素替换为“ XX”,则程序将打印最终方案(矩阵的最后阶段),并用“ XX”和单词BINGO替换正确的元素! ,否则就是最后一种情况。 矩阵的第一行包含字母BINGO,从而通过其相应的标签字母来标识每个矩阵的列,第一个字母为“ B”,第二个字母为“ I”,依此类推,以使输入应位于形成: label_letter-XY,其中X和Y代表数字。 我已经设法正确打印Bingo卡,但是我仍然无法遍历矩阵的行和列,验证是否有候选号码在其中
m=5 #lines
n=5 #columns/rows
mat=[]
data=[]
for i in range(m):
col=input().split()
mat.append(col)
num=int(input())
blank=''
def printbingocard(mat):
print("+", end=blank)
print((16)*"-" + "+")
print("| ", end=blank)
print("B ", end=blank)
print("I ", end=blank)
print("N ", end=blank)
print("G ", end=blank)
print("O ", end=blank)
print("|")
print("+" + (16)*"=" + "+")
for i in range(m):
print("| ", end=blank)
for j in range(n):
print(mat[i][j] + " ", end='')
print("|")
print("+" + (16)*"-" + "+")
printbingocard(mat)
for i in range(num):
input=str(input()).split("-")
input_data.append(input)
for j in range(n):
if input_data[i][0]=="B":
if mat[0][j]==input_data[i][1]:
mat[0][j]="XX"
printbingocard(mat)
if input_data[i][0]=="I":
if mat[1][j]==input_data[i][1]:
mat[1][j]="XX"
printbingocard(mat)
if input_data[i][0]=="N":
if mat[2][j]==input_data[i][1]:
mat[2][j]="XX"
printbingocard(mat)
if input_data[i][0]=="G":
if mat[3][j]==input_data[i][1]:
mat[3][j]="XX"
printbingocard(mat)
if input_data[i][0]=="O":
if mat[4][j]==input_data[i][1]:
mat[4][j]="XX"
printbingocard(mat)
for i in range(m):
for j in range(n):
if mat[i][j]== "XX":
bol=True
else:
bol=False
break
for j in range(n):
for i in range(m):
if mat[i][j]== "XX":
bol=True
else:
bol=False
break
printbingocard(mat)
if bol==True:
print("BINGO!")
for j in range(n):
for i in range(m):
if mat[j][j]=="XX" or mat[i][i]=="XX":
bol=True
else:
bol=False
break
printbingocard(mat)
if bol==True:
print("BINGO!")
for j in range(4,n,-1):
for i in range(1,m,1):
if mat[i][j]=="XX":
bol=True
else:
bol=False
break
printbingocard(mat)
if bol==True:
print("BINGO!")
答案 0 :(得分:0)
我愿意,我使用原子文本编辑器进行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
)。干杯。