-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-code
157 lines (133 loc) · 4.33 KB
/
game-code
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from datetime import datetime
cells = list('_________')
row1 = ('|', cells[0], cells[1], cells[2], '|')
row2 = ('|', cells[3], cells[4], cells[5], '|')
row3 = ('|', cells[6], cells[7], cells[8], '|')
def write_results(name1, name2, won):
string = ''
now = datetime.now()
if won == "X":
string = f'{name1} win'
if won == "O":
string = f'{name2} win'
if won == "":
string = 'Draw'
open('text.txt', 'a').write(f'{name1} : {name2} - {now} - {string}\n')
def grid():
print('---------')
print('|', cells[0], cells[1], cells[2], '|')
print('|', cells[3], cells[4], cells[5], '|')
print('|', cells[6], cells[7], cells[8], '|')
print('---------')
def player_check():
while 1:
player1 = input('Player 1: ')
if not player1.isalnum() or len(player1) < 3:
print('Choose normal nickname!')
else:
break
while 1:
player2 = input('Player 2: ')
if not player2.isalnum() or len(player2) < 3:
print('Choose normal nickname!')
else:
break
return player1, player2
def move(player):
while 1:
cords = input('Enter the coordinates: ')
if not (cords[0].isdigit() and cords[1].isdigit()):
print('You should enter numbers!')
continue
x = int(cords[0])
y = int(cords[1])
n = int(((x - 1) * 3 + (y + 2)) - 3)
if x > 3 or x < 1 or y > 3 or y < 1:
print('Coordinates should be from 1 to 3!')
continue
elif cells[n] != '_':
print('This cell is occupied! Choose another one!')
continue
else:
cells[n] = player
grid()
break
check_winners = 0
def check(player, name1, name2):
global check_winners
for i in range(0, 9, 3):
if cells[i] == cells[i+1] == cells[i+2] == player:
check_winners += 1
print(f'{player} wins')
write_results(name1, name2, player)
for i in range(3):
if cells[i] == cells[i+3] == cells[i+6] == player:
check_winners += 1
print(f'{player} wins')
write_results(name1, name2, player)
if cells[0] == cells[4] == cells[8] == player or cells[2] == cells[4] == cells[6] == player:
check_winners += 1
print(f'{player} wins')
write_results(name1, name2, player)
elif cells.count('_') == 0:
check_winners += 1
print('Draw')
write_results(name1, name2, '')
def play():
name1, name2 = player_check()
grid()
while check_winners <= 0:
print(f'{name1}, it is your turn!')
move('X')
check('X', name1, name2)
if check_winners > 0:
break
print(f'{name2}, it is your turn!')
move('O')
check('O', name1, name2)
if check_winners > 0:
break
def read_results(datafile):
file = open(datafile)
line = file.readline()
table = []
while line:
table.append(line.split())
line = file.readline()
return table
def statistics(data):
statistics = dict()
for row in data:
player1, player2 = row[0], row[2]
winner = row[7]
if player1 not in statistics:
statistics[player1] = {'win': 0, 'draw': 0, 'lose': 0}
if player2 not in statistics:
statistics[player2] = {'win': 0, 'draw': 0, 'lose': 0}
if winner == 'Draw':
statistics[player1]['draw'] += 1
statistics[player2]['draw'] += 1
elif winner == row[0]:
statistics[player1]['win'] += 1
statistics[player2]['lose'] += 1
elif winner == row[2]:
statistics[player1]['lose'] += 1
statistics[player2]['win'] += 1
result = []
for player, inner in statistics.items():
line = f'\n{player} - \n'
result.append(line)
for game_state, num_wins in inner.items():
line1 = f' {game_state}:{num_wins} '
result.append(line1)
return "".join(result)
def command_check():
command = input('What do you want to do?\n')
if command == 'play':
play()
if command == 'last-games':
print("".join(open('text.txt', 'r').readlines()[-10:]))
if command == 'statistics':
print(statistics(read_results('text.txt')))
while 1:
command_check()