Created
November 12, 2011 22:05
-
-
Save Werkov/1361190 to your computer and use it in GitHub Desktop.
Interlos.P1.Bazinga
This file contains 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
def play(desk, who): | |
bestResult = -2 | |
for row in range(0, 3): | |
for col in range(0, 3): | |
if desk[row][col] != 0: | |
continue | |
desk[row][col] = who | |
if testLoser(desk, row, col, who): | |
bestResult = max(bestResult, -1) | |
else: | |
bestResult = max(bestResult, -play(desk, 3 - who)) | |
desk[row][col] = 0 | |
return 0 if bestResult == -2 else bestResult | |
def testLoser(desk, row, col, who): | |
if desk[row] == 3*[who]: return True | |
if [desk[j][col] for j in range(3)] == 3*[who]: return True | |
if [desk[j][j] for j in range(3)] == 3*[who]: return True | |
if [desk[2-j][j] for j in range(3)] == 3*[who]: return True | |
return False | |
mapin = {'.':0, 'x':1, 'o':2} | |
mapout = {0: 'N', 1:'X', 2:'O'} | |
f = open("tris.txt") | |
n = int(f.readline()) | |
f.readline() | |
for i in range(n): | |
desk = [([mapin[x] for x in f.readline().strip()]) for i in range(3)] | |
player = len(filter(lambda x: x != 0, [x for row in desk for x in row])) % 2 + 1 | |
result = play(desk, player) | |
print mapout[(1-result)/2*3 + player*result], | |
f.readline() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment