-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXO_Game.py
125 lines (98 loc) · 4.77 KB
/
XO_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
120
121
122
123
# Copyright 2021 by Alexei Bezborodov <[email protected]>
import random
SPACE = " "
X = "X"
O = "O"
# True/False
# Проверка на то что всё поле заполнено, при этом возвращается True. Иначе False
def AllDataFill(w, h, data):
result = True
for i in range(w * h):
if data[i] == SPACE:
result = False
break
return result
# Один ход компьютерного игрока, который случайно ставит либо крестики либо нолики (put_element)
def ComputerAI_PutElement(w, h, data, put_element):
while True:
r = int(random.random() * w * h) # random.randint(0, w * h - 1)
if data[r] == SPACE:
data[r] = put_element
break
return data
# Отрисовка поля
def Draw(w, h, data):
line = '+--'
for s in range(h):
print(line * w + '+')
print('| ' + '| '.join(data[s*w:(s+1)*w]) + '|')
print(line * w + '+')
# True/False
# Выиграл ли Х или О
def CheckWin(w, h, data, check_element):
def GetElement(data, x, y, w, h):
return data[w*y + x]
# минимально возможное значение
l = min(w, h)
# Все значения
all_seq_fill = [i for i in range(l)] # Например, для l = 3: [0, 1, 2]
all_seq_fill_by_l = [i // l for i in range(l*l)] # Например, для l = 3: [0, 0, 0, 1, 1, 1, 2, 2, 2]
# строки столбцы 1 диагональ 2 диагональ
check_x = all_seq_fill * l + all_seq_fill_by_l + all_seq_fill + all_seq_fill[::-1]
check_y = all_seq_fill_by_l + all_seq_fill * l + all_seq_fill + all_seq_fill
for i in range(0, len(check_x), l):
check_line = [GetElement(data, check_x[i+k], check_y[i+k], w, h) for k in range(l)]
result = True
for element in check_line:
if element != check_element:
result = False
if result:
break
return result
def Test():
print("Test CheckWin")
for element in X, O:
print(1, CheckWin(3, 3, [element, SPACE, SPACE, element, SPACE, SPACE, element, SPACE, SPACE], element), True)
print(2, CheckWin(3, 3, [SPACE, SPACE, element, SPACE, SPACE, element, SPACE, SPACE, element], element), True)
print(3, CheckWin(3, 3, [SPACE , SPACE, element, SPACE, SPACE, SPACE, element, SPACE, SPACE], element), False)
print(4, CheckWin(2, 2, [element, SPACE, SPACE, element], element), True)
print(5, CheckWin(2, 2, [SPACE, element, SPACE, element], element), True)
print(6, CheckWin(3, 3, [element, element, element, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), True)
print(7, CheckWin(3, 3, [SPACE, SPACE, SPACE, element, element, element, SPACE, SPACE, SPACE], element), True)
print(8, CheckWin(3, 3, [SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, element, element, element], element), True)
print(9, CheckWin(3, 3, [SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), False)
print(10, CheckWin(3, 3, [element, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), False)
print(11, CheckWin(3, 3, [element, element, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE], element), False)
print(12, CheckWin(3, 3, [element, SPACE, SPACE, SPACE, SPACE, element, SPACE, SPACE, SPACE], element), False)
print(13, CheckWin(3, 3, [element, element, SPACE, SPACE, SPACE, SPACE, element, SPACE, SPACE], element), False)
print(14, CheckWin(3, 3, [element, SPACE, SPACE, SPACE, element, SPACE, SPACE, SPACE, element], element), True)
print(15, CheckWin(3, 3, [SPACE, SPACE, element, SPACE, element, SPACE, element, SPACE, SPACE], element), True)
print("Test CheckForWin")
# Game start
#Test()
#exit()
w = 3
h = 3
data = [SPACE] * w * h
while True:
Draw(w, h, data)
x = int(input("Введите координату (x):"))
y = int(input("Введите координату (y):"))
if x >= 1 and x <= w and y >= 1 and y <= h and data[w * (y - 1) + (x - 1)] == SPACE:
data[w * (y - 1) + (x - 1)] = X
else:
print("Так ходить нельзя")
continue
if CheckWin(w, h, data, X) == True:
print("Пользователь победил")
Draw(w, h, data)
break
if AllDataFill(w, h, data) == True:
print("Ничья")
Draw(w, h, data)
break
ComputerAI_PutElement(w, h, data, O)
if CheckWin(w, h, data, O) == True:
print("Компьютер победил")
Draw(w, h, data)
break