forked from KanishkM08/Sudo-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_user_input.py
123 lines (95 loc) · 3.8 KB
/
console_user_input.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
from os import name as os_name
from os import system
import board_prettyprint
from config_validity_checker import fullboard_isvalid as board_isvalid
__SMALLER_TITLE_FILE = "assets/ascii_smaller_title.txt"
def __cls():
system("cls" if os_name == "nt" else "clear")
def __print_smaller_title():
with open(__SMALLER_TITLE_FILE, encoding="utf-8") as stf:
print(stf.read())
# Calculate the number of lines to move the cursor UP by
def calc_up_num(row_idx: int, board_repr: list[list[str]]):
"""
@returns str: Escape Sequence to move the cursor UP by some number of lines
"""
up_num = 0
if 0 <= row_idx < 3:
up_num = (len(board_repr) - row_idx) + 2 # skip both horz. lns
elif 3 <= row_idx < 6:
up_num = (len(board_repr) - row_idx) + 1 # skip only 2nd horz. ln
elif row_idx >= 6:
up_num = len(board_repr) - row_idx # no horizontal lns to skip
escape_seq = f"\x1B[{up_num}A\x1B[25C" # move cursor UP by up_num lns
return escape_seq
def get_input() -> list[list[int]]:
"""
@returns list[list[int]]: Board configuration to be solved,
as per user input
"""
board_repr = [
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
]
for row_idx, row in enumerate(board_repr):
for col_idx, _ in enumerate(row):
board_repr[row_idx][col_idx] = "_"
board_str = board_prettyprint.prettyprint(board_repr, ret=True)
escape_seq = calc_up_num(row_idx, board_repr)
while True:
__print_smaller_title()
input_num = input(
board_str + escape_seq + "<------ [ ] INTEGER FROM 1-9. 0 or No Input for an "
"empty square.\x1B[55D"
)
try:
int(input_num)
except ValueError:
input_num = "0"
if input_num == "0":
input_num = " "
elif 1 <= int(input_num) <= 9:
pass
else:
__cls()
continue
cp_board_repr = board_repr.copy()
cp_board_repr[row_idx][col_idx] = int(input_num) if input_num != " " else " "
__cls()
# Check the validity of entered board config. in current state
valid, row_inv, col_inv, box_inv = board_isvalid(cp_board_repr)
if valid:
break
else:
if row_inv:
repeat_location = "row"
elif col_inv:
repeat_location = "col"
elif box_inv:
repeat_location = "3x3 box"
print(
f"Invalid board configuration! '{input_num}' has been "
f"repeated in the same {repeat_location}"
)
board_repr[row_idx][col_idx] = cp_board_repr[row_idx][
col_idx
]
__cls()
print(
f"You have entered the following board configuration:\n"
f"{board_prettyprint.prettyprint(board_repr, ret=True)}"
)
for row in board_repr:
for c_idx, item in enumerate(row):
if item == " ":
row[c_idx] = 0
return board_repr
if __name__ == "__main__":
test_uin = get_input()