Skip to content

Instantly share code, notes, and snippets.

@edwardinubuntu
Last active October 5, 2022 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardinubuntu/61c076fdf3f2a1638d4312f515ee3b3f to your computer and use it in GitHub Desktop.
Save edwardinubuntu/61c076fdf3f2a1638d4312f515ee3b3f to your computer and use it in GitHub Desktop.
Bingo game simulation run in python.
import random
import numpy as numpy
class Official:
@staticmethod
def non_repeat_numb(nums, minimum, maximus):
"""
Create un-repeat number
:param nums: Numbers already created
:param minimum: minimum number
:param maximus: Maximus number
:return: Un-repeat number
"""
rnd = random.randint(minimum, maximus)
if rnd not in nums:
return rnd
else:
return Official.non_repeat_numb(nums, minimum, maximus)
@staticmethod
def check_match(_probability, _open_number_list):
"""
Check the winning probability has match the open numbers or not.
:param _probability:
:param _open_number_list:
:return:
"""
for _it in _probability.values():
_mat_all = True
for _number in _it:
if _number not in _open_number_list:
_mat_all = False
if _mat_all:
return _mat_all, _it
return False, None
@staticmethod
def build_card(_matrix):
nums = list()
for _row in range(_matrix):
for column in range(_matrix):
nums.append(Official.non_repeat_numb(
nums, number_start, number_end))
return numpy.array(nums).reshape(_matrix, _matrix)
class Player:
card = None
probability = None
def __init__(self, _card):
self.card = _card
self.probability = self.build_win_cons()
def build_win_cons(self):
"""
Build bingo winning conditions
:return: winning conditions
"""
# Win Probability
_probability = dict()
# Win in row
for row in range(matrix):
_probability['r{}'.format(row + 1)] = self.card[row, :]
# Win in column
for col in range(matrix):
_probability['c{}'.format(col + 1)] = self.card[:, col]
# Win in slash
left_slash = list()
for row in range(matrix):
left_slash.append(self.card[row, row])
_probability['left_slash'] = numpy.array(left_slash)
right_slash = list()
for row in range(matrix):
right_slash.append(self.card[matrix - row - 1, matrix - row - 1])
_probability['right_slash'] = numpy.array(left_slash)
return _probability
if __name__ == '__main__':
matrix = 5
number_start = 1
number_end = 100
official = Official()
# How many players play
number_of_players = 1000
players = dict()
for each_player in range(number_of_players):
# Prepare players card
_get_one_card = official.build_card(matrix)
players['player {}'.format(each_player+1)] = Player(_get_one_card)
open_number_list = list()
_bingo = False
while not _bingo:
op_num = official.non_repeat_numb(open_number_list,
number_start, number_end)
open_number_list.append(op_num)
# Player all check their card
for player_name, player_info in players.items():
_match_all, _items = official.check_match(
player_info.probability, open_number_list)
if _match_all:
# Someone won
_bingo = True
# Who won
print('bingo! {}'.format(player_name))
# What's on his/her card
print(player_info.card)
# Which one won
print(_items)
# What's our open number
print(sorted(open_number_list))
# How many numbers have opened
print('Number open: {}'.format(len(open_number_list)))
"""
Console print example:
bingo! player 3
[[62 86 56 43 99]
[88 30 81 79 20]
[35 33 82 61 15]
[32 40 75 2 46]
[72 91 50 97 24]]
[56 81 82 75 50]
[6, 8, 9, 10, 12, 14, 22, 28, 30, 31, 32, 35, 36, 38, 41, 45,
47, 48, 50, 51, 53, 55, 56, 57, 61, 62, 63, 64, 70, 72, 73,
74, 75, 77, 81, 82, 86, 94, 95, 99]
Number open: 40
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment