I am writing a Bingo game as homework for class. I am super proud of it so far but it doesn't register some wins.
Here is the code that generates the card:
def generate_card():
card = {
"B": [],
"I": [],
"N": [],
"G": [],
"O": [],
}
min = 1
max = 15
for letter in card:
card[letter] = random.sample(range(min, max), 5)
min += 15
max += 15
if letter == "N":
card[letter][2] = "X" # free space!
return card
and here is the method that checks for a win:
def check_win(card):
win = False
if card["B"][0] == "X" and card["I"][1] == "X" and card["N"][2] == "X" and card["G"][3] == "X" and card["O"][4] == "X":
win = True
elif card["O"][0] == "X" and card["G"][1] == "X" and card["N"][2] == "X" and card["I"][3] == "X" and card["B"][4] == "X":
win = True
elif card["B"][0] == "X" and card["O"][4] == "X" and card["B"][4] == "X" and card["O"][0] == "X":
win = True
for letter in card:
if(len(set(card[letter]))==1):
win = True
for letter in card:
for number in letter:
i = 0
if card[letter][i] == "X":
i += 1
if i == 5:
win == True
break
return win
It first checks for a diagonal win, then a four-corner win, then checks for a horizontal win, and, finally, a vertical win. I just tested and it looks like the code that checks for a vertical win isn't working. I am not sure why, but I do know it was hard trying to rack my brain to write the code for it. Any help is appreciated!
Also, if you see any other problems, let me know. I'm finding this super hard to test.
Maybe this:
for i in range(5):
cnt = 0
for letter in card:
if letter[i] == "X":
cnt += 1
if cnt == 5:
win == True
break
There wasn't a logic error in my code, rather a simple syntax mistake on my part. In the if statement where it's checking if i is equal to 5, I had a conditional win == True
and I should have been setting win equal to true: win = True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.