-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (68 loc) · 2.98 KB
/
app.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
import streamlit as st
import numpy as np
from generate_sudoku import generate_new_sudoku
from sudoku import is_valid_sudoku
if 'solved_sudoku' not in st.session_state:
st.session_state.solved_sudoku = None
if 'sudoku_puzzle' not in st.session_state:
st.session_state.sudoku_puzzle = None
if 'show_sudoku' not in st.session_state:
st.session_state.show_sudoku = False
def display_sudoku(grid):
st.write('<style>div.Widget.row-widget.stRadio > div{flex-direction: row;}</style>', unsafe_allow_html=True)
for i in range(9):
if i % 3 == 0 and i > 0:
st.write('<hr style="background-color: black; height: 2px; margin-top: 5px; margin-bottom: 5px;" />', unsafe_allow_html=True)
else:
st.write('<hr style="background-color: grey; height: 1px; margin-top: 5px; margin-bottom: 5px;" />', unsafe_allow_html=True)
cols = st.columns(9)
for j in range(9):
with cols[j].container():
if grid[i][j] != 0:
st.write(
f'<div style="border: 1px solid black; width: 40px; height: 40px; display: flex;'
f'align-items: center; justify-content: center;">{grid[i][j]}</div>',
unsafe_allow_html=True
)
else:
cell_key = f"{i}-{j}"
cell_value = cols[j].text_input("", key=cell_key)
if cell_value != '':
cell_value = int(cell_value)
if 1 <= cell_value <= 9:
grid[i][j] = cell_value
else:
st.error("Please enter a number between 1 and 9 in each cell.")
st.title("Sudoku")
cols_=st.columns(3)
if cols_[0].button("Check solution"):
if st.session_state.solved_sudoku :
if np.array_equal(st.session_state.solved_sudoku, st.session_state.sudoku_puzzle) or is_valid_sudoku(st.session_state.sudoku_puzzle):
st.success("You solved it!")
else:
st.error("Sorry, try again! Here is the solution : ")
display_sudoku(st.session_state.solved_sudoku)
st.session_state.show_sudoku= False
else:
st.error("Please generate a new game first!")
else:
pass
if cols_[1].button("Get solution"):
if st.session_state.solved_sudoku :
display_sudoku(st.session_state.solved_sudoku)
st.session_state.show_sudoku = False
else:
st.error("Please generate a new game first!")
else:
pass
if cols_[2].button("New game"):
solved_sudoku, sudoku_puzzle = generate_new_sudoku()
st.session_state.solved_sudoku = solved_sudoku
st.session_state.sudoku_puzzle = sudoku_puzzle
st.session_state.show_sudoku = True
#display_sudoku(st.session_state.sudoku_puzzle)
#print(st.session_state.solved_sudoku)
else:
pass
if st.session_state.show_sudoku:
display_sudoku(st.session_state.sudoku_puzzle)