-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_1
More file actions
137 lines (122 loc) · 3.25 KB
/
1_1
File metadata and controls
137 lines (122 loc) · 3.25 KB
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
#Крестики-нолики 3 на 3
import random
X, O = 'x', 'o'
W = 0
L = 0
sym = []
field = [' ']*9
def create_field(f):
for i in range(len(f)):
if (i)%3==2:
print(f[i], end='.\n')
else:
print(f[i], end='.')
def interface(intf):
print('\n')
for i in range(9):
if (i+1)%3==0:
print(i+1, end='\n')
else:
print(i+1, end=' ')
def win(pos, sym):
if pos[1]==pos[2]==pos[0]==sym:
return True
if pos[4]==pos[5]==pos[3]==sym:
return True
if pos[7]==pos[8]==pos[6]==sym:
return True
if pos[1]==pos[4]==pos[7]==sym:
return True
if pos[2]==pos[5]==pos[8]==sym:
return True
if pos[3]==pos[6]==pos[0]==sym:
return True
if pos[0]==pos[4]==pos[8]==sym:
return True
if pos[2]==pos[4]==pos[6]==sym:
return True
else:
return False #рандом победил
def full(pos):
t = 0
if not pos[1]==' ' and not pos[2]==' ' and not pos[3]==' '\
and not pos[4] ==' ' and not pos[5]==' ' and not pos[6]==' '\
and not pos[7]==' ' and not pos[8]==' ' and not pos[0]==' ':
return
def role(sym):
if sym == 'x':
return 'o'
else:
return 'x'
def comp_step(f, sym):
while(True):
opponent = random.randint(0,8)
if (f[opponent]=='x' or f[opponent]=='o'):
continue
else:
f[opponent] = role(sym)
return
def steps(f, turn, sym):
while(not win(f, sym) and not win(f, sym) and not full(f)):
if turn==False:
comp_step(f, sym)
turn = True
else:
if turn==True:
step(f, sym)
turn = False
if win(f, sym):
print("You won!\n")
global W
W+=1
return
if win(f, role(sym)):
print("You lost :c\n")
global L
L+=1
return
if full(field):
print("Draw :c")
return
def step(f, role):
print("\nYour turn!")
while(True):
create_field(f)
interface(f)
target = int(input())
if (target<1 or target>9):
print("Try again")
continue
elif (f[target-1]=='x' or f[target-1]=='o'):
print("Try again")
continue
else:
f[target-1] = role
return
def game():
while(True):
global sym
if (W+L==0):
#global sym
print("Score:", W, ":", L, "\n", "Press o to play by noughts," "\n",
"Press x to play by crosses", '\n' ,
"Press ex to exit")
sym = input()
else:
#global sym
sym = role(sym)
if sym=='ex':
return 0
elif sym=='x':
crnt_field = field[:]
steps(crnt_field, True, 'x')
elif sym=='o':
crnt_field = field[:]
steps(crnt_field, False, 'o')
else:
print("Repeat that please! ._. \n")
continue
print("Play again?[y/n]")
if input()=='n':
return 0
game()