This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
ProgrammingChallenges/ttt_tomek.py

80 lines
2.3 KiB
Python
Raw Permalink Normal View History

2019-04-22 19:43:22 +00:00
#!/usr/bin/env python3
def ttt_state(M):
row_res = [[0,0],[0,0],[0,0],[0,0]]
row_t = [False] * 4
col_res = [[0,0],[0,0],[0,0],[0,0]]
col_t = [False] * 4
diag_res = [[0, 0], [0, 0]]
diag_t = [False, False]
board_not_full = False
for i in range(4):
for j in range(4):
if M[i][j] == '.':
board_not_full = True
continue
inc_r = True
inc_c = True
if M[i][j] == 'T':
inc_r = not row_t[i]
inc_c = not col_t[j]
row_t[i] = True
col_t[j] = True
IS = [M[i][j] == 'X' or M[i][j] == 'T', M[i][j] == 'O' or M[i][j] == 'T']
for k in range(2):
row_res[i][k] += 1 if inc_r and IS[k] else 0
col_res[j][k] += 1 if inc_c and IS[k] else 0
for i in range(4):
j = 3 - i
inc_diag = [True, True]
if M[i][i] == 'T':
inc_diag[0] = not diag_t[0]
diag_t[0] = True
if M[i][j] == 'T':
inc_diag[1] = not diag_t[1]
diag_t[1] = True
IS = [[M[i][i] == 'X' or M[i][i] == 'T', M[i][i] == 'O' or M[i][i] == 'T'],
[M[i][j] == 'X' or M[i][j] == 'T', M[i][j] == 'O' or M[i][j] == 'T']]
for k in range(2):
for l in range(2):
diag_res[k][l] += 1 if inc_diag[k] and IS[k][l] else 0
x_won = False
o_won = False
for row in row_res:
if row[0] == 4:
x_won = True
if row[1] == 4:
o_won = True
for col in col_res:
if col[0] == 4:
x_won = True
if col[1] == 4:
o_won = True
for i in range(2):
if diag_res[i][0] == 4:
x_won = True
if diag_res[i][1] == 4:
o_won = True
if (x_won and o_won) or (not x_won and not o_won and not board_not_full):
return "Draw"
elif x_won:
return "X won"
elif o_won:
return "O won"
else:
return "Game has not completed"
if __name__ == "__main__":
n = int(input())
for i in range(1, n + 1):
M = []
for j in range(4):
M.append(input())
print("Case #" + str(i) + ": " + ttt_state(M))
if i < n:
input()