Created
October 23, 2019 07:55
-
-
Save slanterns/76b541f92443c8efae3ce6dd64908912 to your computer and use it in GitHub Desktop.
Engg 1330 Ass 2.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| size = int(input("Size--> ")) | |
| def matPrint(mat): | |
| width = int(math.ceil((math.log10(size ** 2 - 1)))) | |
| for i in range(size): | |
| rowStr = '' | |
| for j in range(size): | |
| k = size * i + j | |
| if mat[k] == 1: | |
| rowStr += ("{:>" + str(width) + "}").format('X') + ' ' | |
| elif mat[k] == -1: | |
| rowStr += ("{:>" + str(width) + "}").format('O') + ' ' | |
| else: | |
| rowStr += ("{:>" + str(width) + "}").format(str(k)) + ' ' | |
| print(rowStr.rstrip(' ')) | |
| def triJudge(line): | |
| return (0 if len(set(line)) > 1 else tuple(set(line))[0]) | |
| def judge(mat): | |
| return(sum([triJudge(row) for row in [[mat[size * i + j] for j in range(size)] for i in range(size)]]) + sum([triJudge(column) for column in [[mat[size * j + i] for j in range(size)] for i in range(size)]]) + triJudge([mat[i * size + i] for i in range(size)]) + triJudge([mat[(size - 1) * (i + 1)] for i in range(size)])) | |
| mat = [0] * (size ** 2) | |
| matPrint(mat) | |
| for i in range(size ** 2 + 1): | |
| if i == size ** 2: | |
| print("Winner: None") | |
| else: | |
| tag = (1 if i % 2 == 0 else -1) | |
| mat[int(input(('X' if tag == 1 else'O') + '--> '))] = tag | |
| matPrint(mat) | |
| result = judge(mat) | |
| if result != 0: | |
| print("Winner: " + ('X' if tag == 1 else'O')) | |
| break | |
| """ | |
| [triJudge(row) for row in [[mat[size * i + j] for j in range(size)] for i in range(size)]]: test each row in the matrix. | |
| [triJudge(column) for column in [[mat[size * j + i] for j in range(size)] for i in range(size)]]: test each column in the matrix. | |
| triJudge([mat[i * size + i] for i in range(size)]) + triJudge([mat[(size - 1) * (i + 1)] for i in range(size)]): test the two cross-lines in the matrix. | |
| """ | |
| # Code reused from 2 | |
| ############################ | |
| size = int(input("Size--> ")) | |
| mat = [0] * (size ** 2 + 1) | |
| print('\n'.join([(''.join([((("{:>" + ('1' if size == 3 else '2') + "}").format('X') + ' ') if mat[size * i + j] == 1 else ((("{:>" + ('1' if size == 3 else '2') + "}").format('O') + ' ') if mat[size * i + j] == -1 else (("{:>" + ('1' if size == 3 else '2') + "}").format(str(size * i + j)) + ' '))) for j in range(size)])).rstrip(' ') for i in range(size)])) | |
| for mat[size ** 2] in range(size ** 2): | |
| mat[int(input(('X' if (1 if mat[size ** 2] % 2 == 0 else -1) == 1 else'O') + '--> '))] = (1 if mat[size ** 2] % 2 == 0 else -1) | |
| print('\n'.join([(''.join([((("{:>" + ('1' if size == 3 else '2') + "}").format('X') + ' ') if mat[size * i + j] == 1 else ((("{:>" + ('1' if size == 3 else '2') + "}").format('O') + ' ') if mat[size * i + j] == -1 else (("{:>" + ('1' if size == 3 else '2') + "}").format(str(size * i + j)) + ' '))) for j in range(size)])).rstrip(' ') for i in range(size)])) | |
| if (sum([(0 if len(set(row)) > 1 else tuple(set(row))[0]) for row in [[mat[size * i + j] for j in range(size)] for i in range(size)]]) + sum([(0 if len(set(column)) > 1 else tuple(set(column))[0]) for column in [[mat[size * j + i] for j in range(size)] for i in range(size)]]) + (0 if len(set([mat[i * size + i] for i in range(size)])) > 1 else tuple(set([mat[i * size + i] for i in range(size)]))[0]) + (0 if len(set([mat[(size - 1) * (i + 1)] for i in range(size)])) > 1 else tuple(set([mat[(size - 1) * (i + 1)] for i in range(size)]))[0])) != 0:break | |
| print(("Winner: None") if mat[size ** 2] == (size ** 2 - 1) else ("Winner: " + ('X' if (1 if mat[size ** 2] % 2 == 0 else -1) == 1 else'O'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment