-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
119 lines (109 loc) · 2.54 KB
/
game.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
def row(n):
arr=[]
for i in range(0,n):
arr+=[False]
return arr
def col(m,n):
mat=[]
for i in range(0,m):
mat+=[row(n)]
return mat
def printMat(mat):
for line in mat:
print(line)
mat=col(10,10)
ignore=[]
total=0
def checkInt(msg):
for i in range(0,10):
msg=msg.replace(str(i),"")
return len(msg)==0
def inputXY():
while True:
x=input("X: ")
y=input("Y: ")
if not checkInt(x):
print("Неверный X")
continue
elif not checkInt(y):
print("Неверный Y")
continue
else:
x=int(x)
y=int(y)
if x<1 or x>10:
print("X вне диапазона")
continue
if y<1 or y>10:
print("Y вне диапазона")
continue
if mat[x-1][y-1]:
print("Точка занята")
continue
else:
mat[x-1][y-1]=True
break
return (x-1,y-1)
def inRange(x,y):
if x<0:
return False
if y<0:
return False
if x>9:
return False
if y>9:
return False
return True
def border(x,y):
dots=[]
if inRange(x-1,y-1):
dots+=[(x-1,y-1)]
if inRange(x-1,y):
dots+=[(x-1,y)]
if inRange(x-1,y+1):
dots+=[(x-1,y+1)]
if inRange(x,y+1):
dots+=[(x,y+1)]
if inRange(x,y-1):
dots+=[(x,y-1)]
if inRange(x+1,y):
dots+=[(x+1,y)]
if inRange(x+1,y+1):
dots+=[(x+1,y+1)]
if inRange(x+1,y-1):
dots+=[(x+1,y-1)]
return dots
def contain(item, arr):
for elem in arr:
if elem[0]==item[0] and elem[1]==item[1]:
return True
return False
def check(x,y):
global ignore
global total
global mat
ignore+=[(x,y)]
if mat[x][y]:
total+=1
for dot in border(x,y):
if contain((dot[0],dot[1]),ignore):
continue
else:
check(dot[0],dot[1])
else:
return total
while True:
print("Поле клондайк:")
printMat(mat)
print("Введите координату:")
(x,y)=inputXY()
mat[x][y]=True
total=0
ignore=[]
check(x,y)
if total>2:
printMat(mat)
print("Вы проиграли")
break
else:
continue