Programmering 1 Bingo
Hej,
Har nyss börjat med programmering och ville bara höra om någon kunde ge input på min kod.
Försöker få ett A i denna kurs så tar all input jag kan få för att bli bättre!
ursäktar att den inte blir strukturerad korrekt mitt första upplägg ^^'
import random
red_color_code = '\033[91m'
reset_color_code = '\033[0m'
#checks for Bingo on player card
def check_bingo(card):
for row in card:
if all(number != 0 for number in row):
return True
for column in range(5):
if all(row[column] != 0 for row in card):
return True
if all(card[i][i] != 0 for i in range(5)):
return True
if all(card[i][4-i] != 0 for i in range(5)):
return True
return False
def bingo_game():
Pulled = []
bingo_card = [[0 for _ in range(5)] for _ in range(5)]
bingo_card2 = [[0 for _ in range(5)] for _ in range(5)]
numbers = list(range(1, 26))
random.shuffle(numbers)
#Setting up cordinates
for row_index in range(5):
for col_index in range(5):
bingo_card[row_index][col_index] = numbers[row_index*5 + col_index]
#Game loop
while len(numbers) > 0:
random.shuffle(numbers)
random_number = numbers[0]
Pulled.append(random_number)
numbers.remove(random_number)
print (Pulled)
print("Your Bingo card")
for row in bingo_card:
for number in row:
if number in Pulled:
print(f"{red_color_code}{number}{reset_color_code}", end='\t') #Makes the number red for the player to easly distinguish what number has shown.
bingo_card2[bingo_card.index(row)][row.index(number)] = number
else:
print(number, end='\t')
print()
if check_bingo(bingo_card2): #Break when game checks that player has a bingo.
print("Bingo!")
break
(input("press Enter to continue")) #To make the game play in the pace of the player, Did also consider making it timebased but figured it would feel better as a player this way
play_again = "yes"
while play_again.lower() == "yes":
bingo_game()
play_again = input('If you wish to play again type "yes" and if you are done playing then just press enter: ')
print ("Thank you for playing!")