Skip to content

Instantly share code, notes, and snippets.

@math314
Created May 19, 2014 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save math314/34ff1da0b4e169b1ed33 to your computer and use it in GitHub Desktop.
Save math314/34ff1da0b4e169b1ed33 to your computer and use it in GitHub Desktop.
#coding=utf-8
from subprocess import Popen,STDOUT,PIPE # Python 2.4以上が必要
import os
def next_line(stdout):
line = ""
while True:
r = stdout.read(1)
if r == '\n':
break
line += r
if line == "Choose Wisely (x,y,z): " :
break
print line
return line
def write(stdin,val):
stdin.write(val)
print "write : " + val
os.environ['PATH'] = "/bin:/usr/bin:." # PATHが使用されるので、必要に応じて指定
cwd = "./" # 作業ディレクトリ
cmdline = "nc 3dttt_87277cd86e7cc53d2671888c417f62aa.2014.shallweplayaga.me 1234" # スペースやタブで区切る
p = Popen(cmdline, shell=True, cwd=cwd, stdin=PIPE,
stdout=PIPE, stderr=STDOUT,close_fds=True)
def valid_tuple():
def ad(a,b):
return (a[0] + b[0],a[1] + b[1],a[2] + b[2])
all = [(x,y,z) for x in xrange(3) for y in xrange(3) for z in xrange(3)]
P = []
for i in xrange(27):
for j in xrange(i+1,27):
for k in xrange(j+1,27):
print i,j,k
_i = all[i]; _j = all[j]; _k = all[k]
if ad(_i,_j) == ad(_k,_k) or \
ad(_j,_k) == ad(_i,_i) or \
ad(_i,_k) == ad(_j,_j):
P.append((_i,_j,_k))
return P
tp = valid_tuple()
print len(tp)
print "-" * 80 # 区切り表示(開始)
def nl(): return next_line(p.stdout)
game_cnt = 0
board = [[[0] * 3 for i in xrange(3)] for j in xrange(3)]
while True:
line = nl()
if "Let's play again" in line:
board = [[[0] * 3 for i in xrange(3)] for j in xrange(3)]
game_cnt = 0
if line == " x 0 1 2 z=0":
for z in xrange(3):
nl()
for y in xrange(3):
l = nl()
for x in xrange(3):
if l[3+x*4] == 'O':
board[z][y][x] = 'O'
elif l[3+x*4] == 'X':
board[z][y][x]= 'X'
nl()
nl()
nl()
if line == "Choose Wisely (x,y,z): ":
print board
wx = 0
wy = 0
wz = 0
if game_cnt == 0 :
wx = wy = wz = 1
elif game_cnt == 1:
for a,b,c in tp:
if b == (1,1,1):
if board[c[2]][c[1]][c[0]] == 'O':
continue
if board[a[2]][a[1]][a[0]] == 'O':
continue
wx,wy,wz = a
break
else:
def h():
points = 0
for i in tp:
ocnt = xcnt = 0
for a in i:
if board[a[2]][a[1]][a[0]] == 'O':
ocnt += 1
if board[a[2]][a[1]][a[0]] == 'X':
xcnt += 1
if ocnt == 0 and xcnt == 3:
points += 3
if ocnt == 0 and xcnt == 2:
points += 1
elif ocnt == 2 and xcnt != 1:
points -= 1
return points
cur = cur_pt = -100
for x,y,z in [(x,y,z) for x in xrange(3) for y in xrange(3) for z in xrange(3)]:
if board[z][y][x] == 0:
board[z][y][x] = 'X'
pt = h()
board[z][y][x] = 0
if cur_pt < pt :
cur_pt = pt
cur = (x,y,z)
wx,wy,wz = cur
write(p.stdin,"%d,%d,%d\n" % (wx,wy,wz))
game_cnt += 1
print "-" * 80 # 区切り表示(終了)
ret = p.wait() # 戻り値が入る
print "Return code: %d" % ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment