-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
158 lines (125 loc) · 4.25 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 24 18:10:07 2024
Author: Alexandros Stratoudakis
e-mail: alexstrat4@gmail.com
Utilities used throughout the project
"""
from colorama import just_fix_windows_console # for colored output on CMD
from typing import Any, Tuple
import sys
import os
import time
import builtins
bj_vals = {'A': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10,
'J': 10, 'Q': 10, 'K': 10}
suits_map = {'heart': '♥', 'diamond': '♦', 'spade': '♠', 'club': '♣'}
vals_map = {'1': 'A', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6',
'7': '7', '8': '8', '9': '9', '10': '10', '11': 'J', '12': 'Q', '13': 'K'}
def count_value(cards: list[object]) -> Tuple[int, bool]:
"""
Function that calculates the Blackjack-value of a given hand of cards.
Args:
cards (list): List (or tuple) of bj_components.card objects.
Returns:
val (int): The BJ value of the hand.
soft (bool): Whether or not this value is with one or more Aces counting as 11 (instead of 1).
"""
val = 0
aces = 0 # num of Aces
ace_cnt = 0
soft = False # if all A=1 or there are no A, soft = False
for card in cards:
val += card.bj_value()
if card.value == 'A':
aces += 1
ace_cnt += 1 # aces with val 11
else:
continue
while val > 21 and ace_cnt != 0:
val -= 10
ace_cnt -= 1
if ace_cnt != 0:
soft = True
return val, soft
actions = {'h': 'hit', 's': 'stay', 'd': 'double down', 'x': 'split'}
class colors:
# Taken from https://www.geeksforgeeks.org/print-colors-python-terminal/
'''Colors class:reset all colors with colors.reset; two
sub classes fg for foreground
and bg for background; use as colors.subclass.colorname.
i.e. colors.fg.red or colors.bg.greenalso, the generic bold, disable,
underline, reverse, strike through,
and invisible work with the main class i.e. colors.bold'''
just_fix_windows_console() # to be compatible with CMD
reset = '\033[0m'
bold = '\033[01m'
disable = '\033[02m'
underline = '\033[04m'
reverse = '\033[07m'
strikethrough = '\033[09m'
invisible = '\033[08m'
class fg:
black = '\033[30m'
red = '\033[31m'
green = '\033[32m'
orange = '\033[33m'
blue = '\033[34m'
purple = '\033[35m'
cyan = '\033[36m'
lightgrey = '\033[37m'
darkgrey = '\033[90m'
lightred = '\033[91m'
lightgreen = '\033[92m'
yellow = '\033[93m'
lightblue = '\033[94m'
pink = '\033[95m'
lightcyan = '\033[96m'
class bg:
al = ''
black = '\033[40m'
red = '\033[41m'
green = '\033[42m'
orange = '\033[43m'
blue = '\033[44m'
purple = '\033[45m'
cyan = '\033[46m'
lightgrey = '\033[47m' # white
win_lose = [colors.fg.red + 'loses' + colors.reset, colors.fg.green +
'wins' + colors.reset, colors.fg.cyan + 'pushes' + colors.reset]
def is_numerical(a: Any) -> bool:
"""
Args:
a (Any): Any object.
Returns:
bool: True if input can be converted to float, False otherwise.
"""
try:
float(a)
except:
return False
else:
return True
class Mute:
""" Class that mutes inbuild print when in a 'with' block """
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w', encoding='utf-8')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
def slow_print(delay: int | float):
"""
Function that adds delay after prints.
Args:
delay (int|float): Delay after print in seconds.
Returns:
None.
"""
original_print = builtins.print
# Define the custom print function
def slowed_print(*args, delay=1, **kwargs):
original_print(*args, **kwargs)
time.sleep(delay)
# Override the built-in print function with the custom one
builtins.print = slowed_print