-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgames.py
executable file
·181 lines (135 loc) · 5.11 KB
/
games.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import random
import requests
from codetiming import Timer
from telegram import Update
from telegram.ext import ContextTypes
import config
from utils import no_can_do, printlog
async def sassocartaforbici(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if await no_can_do(update, context):
return
lista = ["sasso", "carta", "forbici", "lucertola", "spock"]
if not context.args or (context.args[0].lower() not in lista):
await update.message.reply_text("Devi scegliere uno tra: carta, sasso, forbici, lucertola, spock")
return
a = context.args[0].lower()
b = random.choice(lista)
winners = {
"sasso": {"lucertola": "schiaccia la", "forbici": "rompe le"},
"carta": {"sasso": "avvolge il", "spock": "invalida"},
"forbici": {"carta": "tagliano la", "lucertola": "decapitano la"},
"lucertola": {"carta": "mangia la", "spock": "avvelena"},
"spock": {"forbici": "rompe le", "sasso": "vaporizza il"},
}
await printlog(update, "gioca a sasso carta forbici lucertola spock", f"{a.capitalize()} vs {b.capitalize()}")
if a == b:
await update.message.reply_text(f"Scelgo {b.capitalize()}!\n🌝 Pari!")
return
try:
verbo = winners[a][b]
await update.message.reply_text(
f"Scelgo {b.capitalize()}!\n\n{a.capitalize()} {verbo} {b.capitalize()}\n✅ Hai vinto!"
)
return
except KeyError:
verbo = winners[b][a]
await update.message.reply_text(
f"Scelgo {b.capitalize()}!\n\n{b.capitalize()} {verbo} {a.capitalize()}\n❌ Hai perso!"
)
return
def sudoku():
def get_sudoku(diff):
url = "https://sudoku-board.p.rapidapi.com/new-board"
querystring = {"diff": str(diff), "stype": "list", "solu": "true"}
headers = {"X-RapidAPI-Key": config.RAPIDAPI_KEY, "X-RapidAPI-Host": "sudoku-board.p.rapidapi.com"}
response = requests.request("GET", url, headers=headers, params=querystring).json()
puzzle = response["response"]["unsolved-sudoku"]
for c in range(9):
for r in range(9):
if puzzle[c][r] == 0:
puzzle[c][r] = -1
return puzzle
def print_sudoku(puzzle):
max_len = len(puzzle[0])
# print(f"\n+{'---+' * max_len}")
print()
for c in range(max_len):
if c == 0:
pass
elif c % 3 == 0:
print(f"-{'----' * max_len}")
print(" ", end="")
for r in range(max_len):
if (r == 0) or ((r + 1) % 3 != 0) or (r == max_len - 1):
if puzzle[c][r] < 0:
print(" ", end="")
else:
print(f" {puzzle[c][r]} ", end="")
else:
if puzzle[c][r] < 0:
print(" |", end="")
else:
print(f" {puzzle[c][r]} |", end="")
# print(f"\n+{'---+' * max_len}")
print()
def find_next_empy_cell(puzzle):
max_len = len(puzzle[0])
for r in range(max_len):
for c in range(max_len):
if puzzle[r][c] == -1: # It's empty
return r, c
# Abbiamo riempito tutte le celle, assurdo
return None, None
def is_valid(puzzle, guess, row, col) -> bool:
max_len = len(puzzle[0])
# Horizontal Check
if guess in puzzle[row]:
return False
# Vertical Check
if guess in [puzzle[x][col] for x in range(max_len)]:
return False
# Square Check
row_i = (row // 3) * 3
col_i = (col // 3) * 3
for r in range(row_i, row_i + 3):
for c in range(col_i, col_i + 3):
if puzzle[r][c] == guess:
return False
return True
def solve_sudoku(puzzle):
row, col = find_next_empy_cell(puzzle)
if row is None: # Abbiamo riempito tutte le celle
return True
for guess in range(1, 10):
if is_valid(puzzle, guess, row, col):
puzzle[row][col] = guess
if solve_sudoku(puzzle):
return True # Vinto
puzzle[row][col] = -1
# print_sudoku(puzzle)
return False # unsolvable
# 3x3
# 6x6
# 9x9
sudoku9x9 = [
[8, 7, -1, -1, 3, 2, -1, -1, -1],
[-1, -1, -1, 6, -1, -1, -1, -1, 3],
[6, -1, -1, -1, -1, -1, -1, -1, -1],
[7, -1, 6, 8, -1, -1, 3, -1, -1],
[2, -1, 1, 7, 5, 3, 6, -1, 8],
[-1, -1, 8, -1, -1, 6, 5, -1, 2],
[-1, -1, -1, -1, -1, -1, -1, -1, 1],
[4, -1, -1, -1, -1, 7, -1, -1, -1],
[-1, -1, -1, 4, 6, -1, -1, 2, 5],
]
# hardest sudoku
# https://abcnews.go.com/blogs/headlines/2012/06/can-you-solve-the-hardest-ever-sudoku
# blank sudoku
sudoku = sudoku9x9
# get a new 9x9 solvable
# sudoku = get_sudoku(3)
print_sudoku(sudoku)
with Timer(name="solver"):
solve_sudoku(sudoku)
print_sudoku(sudoku)
# sudoku()