From 1fbed03fa3046f5f615048c2d88179f210d503b1 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Mon, 22 Apr 2019 21:43:22 +0200 Subject: [PATCH] Working solution for L --- ttt_tomek.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ ttt_tomek.txt | 30 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 ttt_tomek.py create mode 100644 ttt_tomek.txt diff --git a/ttt_tomek.py b/ttt_tomek.py new file mode 100644 index 0000000..e03d20f --- /dev/null +++ b/ttt_tomek.py @@ -0,0 +1,79 @@ +#!/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() diff --git a/ttt_tomek.txt b/ttt_tomek.txt new file mode 100644 index 0000000..b914d78 --- /dev/null +++ b/ttt_tomek.txt @@ -0,0 +1,30 @@ +6 +XXXT +.... +OO.. +.... + +XOXT +XXOO +OXOX +XXOO + +XOX. +OX.. +.... +.... + +OOXX +OXXX +OX.T +O..O + +XXXO +..O. +.O.. +T... + +OXXX +XO.. +..O. +...O