Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
My champ solution in Clear category for Xs and Os Champion by Tinus_Trotyl
def x_and_o(grid, me) : # Play the game X and O.
you = {"X":"O", "O":"X"}[me] # Who is who ?
grid = "".join(grid) # Make grid to a string
for player in (me, you) : # For both players :
for i in ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), # check for the winning combinations,
(1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)) : # ,,
line = [grid[i[j]] for j in (0, 1, 2)] # ,,
if line.count(player) == 2 and "." in line : # then win "me" or
return divmod(i[line.index(".")], 3) # else prevent "you" to win.
for i,j in (0, 8), (2, 6): # Check if "you" has two opposite corners
if "".join(grid[k] for k in (i, j, 4 ,1)) == 2*you + me + ".": # with "me" being smacked between,
return (0, 1) # then occupie (0, 1) when still free.
for mov in 4, 0, 2, 6, 8, 1, 3, 5, 7 : # Or else take another field
if grid[mov] == "." : return divmod(mov, 3) # (the middle one first, then corners).
Oct. 9, 2017
Comments: