Skip to content

Commit 74e49fe

Browse files
committed
XO
1 parent fc522b1 commit 74e49fe

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

XO_Game.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import random
2+
SPACE = " "
3+
X = "X"
4+
O = "O"
5+
6+
# True/False
7+
# Проверка на то что всё поле заполнено, при этом возвращается True. Иначе False
8+
def AllDataFill(w, h, data):
9+
result = True
10+
for i in range(w * h):
11+
if data[i] == SPACE:
12+
result = False
13+
break
14+
return result
15+
16+
# Один ход компьютерного игрока, который случайно ставит либо крестики либо нолики (put_element)
17+
def ComputerAI_PutElement(w, h, data, put_element):
18+
while True:
19+
r = int(random.random() * w * h) # random.randint(0, w * h - 1)
20+
if data[r] == SPACE:
21+
data[r] = put_element
22+
break
23+
return data
24+
25+
# Отрисовка поля
26+
def Draw(w, h, data):
27+
line = '+--'
28+
for s in range(h):
29+
print(line * w + '+')
30+
print('| ' + '| '.join(data[s*w:(s+1)*w]) + '|')
31+
32+
print(line * w + '+')
33+
34+
# True/False
35+
# Выиграл ли Х или О
36+
def CheckWin(w, h, data, check_element):
37+
def GetElement(data, x, y, w, h):
38+
return data[w*y + x]
39+
40+
# минимально возможное значение
41+
l = min(w, h)
42+
43+
# Все значения
44+
all_seq_fill = [i for i in range(l)] # Например, для l = 3: [0, 1, 2]
45+
46+
all_seq_fill_by_l = [i // l for i in range(l*l)] # Например, для l = 3: [0, 0, 0, 1, 1, 1, 2, 2, 2]
47+
48+
# строки столбцы 1 диагональ 2 диагональ
49+
check_x = all_seq_fill * l + all_seq_fill_by_l + all_seq_fill + all_seq_fill[::-1]
50+
check_y = all_seq_fill_by_l + all_seq_fill * l + all_seq_fill + all_seq_fill
51+
52+
for i in range(0, len(check_x), l):
53+
check_line = [GetElement(data, check_x[i+k], check_y[i+k], w, h) for k in range(l)]
54+
result = True
55+
for element in check_line:
56+
if element != check_element:
57+
result = False
58+
if result:
59+
break
60+
61+
return result
62+
63+
def Test():
64+
print("Test CheckWin")
65+
for element in X, O:
66+
print(1, CheckWin(3, 3, [element, SPACE, SPACE, element, SPACE, SPACE, element, SPACE, SPACE], element), True)
67+
print(2, CheckWin(3, 3, [SPACE, SPACE, element, SPACE, SPACE, element, SPACE, SPACE, element], element), True)
68+
print(3, CheckWin(3, 3, [SPACE , SPACE, element, SPACE, SPACE, SPACE, element, SPACE, SPACE], element), False)
69+
print(4, CheckWin(2, 2, [element, SPACE, SPACE, element], element), True)
70+
print(5, CheckWin(2, 2, [SPACE, element, SPACE, element], element), True)
71+
print(6, CheckWin(3, 3, [element, element, element, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), True)
72+
print(7, CheckWin(3, 3, [SPACE, SPACE, SPACE, element, element, element, SPACE, SPACE, SPACE], element), True)
73+
print(8, CheckWin(3, 3, [SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, element, element, element], element), True)
74+
print(9, CheckWin(3, 3, [SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), False)
75+
print(10, CheckWin(3, 3, [element, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), False)
76+
print(11, CheckWin(3, 3, [element, element, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), False)
77+
print(12, CheckWin(3, 3, [element, SPACE, SPACE, SPACE, SPACE, element, SPACE, SPACE, SPACE], element), False)
78+
print(13, CheckWin(3, 3, [element, element, SPACE, SPACE, SPACE, SPACE, element, SPACE, SPACE], element), False)
79+
print(14, CheckWin(3, 3, [element, SPACE, SPACE, SPACE, element, SPACE, SPACE, SPACE, element], element), True)
80+
print(15, CheckWin(3, 3, [SPACE, SPACE, element, SPACE, element, SPACE, element, SPACE, SPACE], element), True)
81+
82+
print("Test CheckForWin")
83+
84+
85+
# Game start
86+
87+
#Test()
88+
#exit()
89+
90+
w = 3
91+
h = 3
92+
data = [SPACE] * w * h
93+
94+
while True:
95+
Draw(w, h, data)
96+
97+
x = int(input("Введите координату (x):"))
98+
y = int(input("Введите координату (y):"))
99+
100+
if x >= 1 and x <= w and y >= 1 and y <= h and data[w * (y - 1) + (x - 1)] == SPACE:
101+
data[w * (y - 1) + (x - 1)] = X
102+
else:
103+
print("Так ходить нельзя")
104+
continue
105+
106+
if CheckWin(w, h, data, X) == True:
107+
print("Пользователь победил")
108+
Draw(w, h, data)
109+
break
110+
111+
if AllDataFill(w, h, data) == True:
112+
print("Ничья")
113+
Draw(w, h, data)
114+
break
115+
116+
ComputerAI_PutElement(w, h, data, O)
117+
118+
if CheckWin(w, h, data, O) == True:
119+
print("Компьютер победил")
120+
Draw(w, h, data)
121+
break
122+
123+

0 commit comments

Comments
 (0)