-
Notifications
You must be signed in to change notification settings - Fork 0
/
9x9.py
134 lines (112 loc) · 3.22 KB
/
9x9.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
# 3x3 sudoku
import random
def duplicate_checker(a):
b = set(a)
result = len(a) != len(b)
#print(result)
if(result == True):
return True
def checkrow_horz(a):
for x in a:
if(duplicate_checker(x) == True):
return True
def checkrow_vert(a):
for y in range(len(a)):
temp = []
for x in a:
temp.append(x[y])
if(duplicate_checker(temp) == True):
return True
def display_grid(grid):
print("")
print("")
print(" Martin's Sudoku")
print("")
count =0
print(" 1 2 3 4 5 6 7 8 9")
print(" ---------------------------")
for x in grid:
print(count+1,end=" ")
for y in range(len(x)):
if(y % 3 != 0):
print(x[y],end=" ")
else:
print("|",x[y],end=" ")
print("|",end=" ")
count+=1
print()
if(count % 3 == 0):
print(" ---------------------------")
def rearrange(a):
temp=[[],[],[],[],[],[],[],[],[]]
count =0
ch = 0
for e in range(len(a)):
for x in range(3):
if(a[e][x]!=0):
temp[ch].append(a[e][x])
count+=1
if(count ==3):
ch+=1
count = 0
for e in range(len(a)):
for x in range(3,6):
if(a[e][x]!=0):
temp[ch].append(a[e][x])
count+=1
if(count ==3):
ch+=1
count = 0
for e in range(len(a)):
for x in range(6,9):
if(a[e][x]!=0):
temp[ch].append(a[e][x])
count+=1
if(count ==3):
ch+=1
count = 0
return temp
def scramble():
global grid
amount = 40
for i in range(amount):
y = random.randint(0,len(grid)-1)
x = random.randint(0,len(grid)-1)
num = random.randint(1,len(grid))
allow = 0
for e in range(len(grid)):
if num not in grid[x] and num != grid[e][y]:
allow +=1
grid[x][y] = num
tempo = grid
tempo = rearrange(tempo)
for e in range(len(grid)):
if(duplicate_checker(tempo[e])):
allow = 0
if allow !=len(grid):
grid[x][y] = 0
grid = [[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]
solved = False
scramble()
display_grid(grid)
while solved != True:
x = int(input("enter horizontal [1,9]"))-1
y = int(input("enter vertical [1,9] "))-1
num = int(input("enter number"))
if grid[y][x] == 0:
grid[y][x] = num
else:
if input("overwrite? [y/n]") == "y":
grid[y][x] = num
display_grid(grid)
full = True
for x in grid:
for y in x:
if(y == 0):
full = False
if(full ==True):
if(checkrow_horz(grid)==True or checkrow_vert(grid) == True):
print("try again")
else:
print("solved")
solved =True