Last active
May 31, 2021 12:39
-
-
Save Tremeschin/f65a6ee252b7569b60ab72e6a88b9dfd to your computer and use it in GitHub Desktop.
8 lines tic tac toe
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 numpy as n | |
g,p,w=n.array([[0]*3]*3),1,"-OX" | |
while True: | |
for r in g: print(' '.join(w[x]for x in r)) | |
c,p=int(input(w[-p])),-p | |
g[divmod(c-1,3)]=p | |
s=n.concatenate((n.sum(g,axis=0),n.sum(g.T,axis=0),[sum(g.diagonal())],[sum(n.fliplr(g).diagonal())])) | |
if e:=w[1]if 3 in s else w[2]if-3 in s else 0: exit(e+"wins") |
Need Python 3.8 because walrus operator := on line 8 ( or until I find a shorter way to exit and determine if anyone wins)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can make improvements on
[sum([grid[i][i] for i in range(3)])], [sum([grid[i][2-i] for i in range(3)])]
:p