1
0
Fork 0
forked from boyska/infissi
infissi/tictacpoe.py
2016-06-04 13:57:17 +02:00

132 lines
3.8 KiB
Python

import socket
import sys
import math
import os
from subprocess import check_output
BUFFER_SIZE = 4096
def client(other_ip):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
s.connect((other_ip, 9966))
return s
def server():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 9966))
s.listen(1)
conn, addr = s.accept()
return conn, addr
def provacella():
print(Cella(['X']) | Cella(['O']) | Cella(['u']) | Cella(['X']) | Cella(['O']) | Cella(['u']))
print()
t = _ | X | _ | _ | _ | _ | _ | _ | _
print(t)
print(eval(str(t)))
class Cella:
def __init__(self, states):
self.states = states
def get_eval_output(self):
s = ''
width = math.ceil(math.sqrt(len(self.states)))
ncols = int(os.getenv('COLUMNS')) if 'COLUMNS' in os.environ else int(check_output('tput cols'.split()))
for i in range(width):
s+= '| ' if i > 0 else ' '
if i*width == len(self.states): break
line_s = ' | '.join(self.states[i*width:i*width+width])
s += line_s
s+= ' '* (ncols - len(line_s) - 2)
return s.rstrip()
def printTabella(self):
width = math.ceil(math.sqrt(len(self.states)))
print('-'*(width*2-1))
for i in range(width):
print('|'.join(self.states[i*width:i*width+width]))
print('+'.join('-'*width))
return True
def __str__(self):
return self.get_eval_output()
# return 'Cella <%s>' % ','.join(self.states)
def __or__(self, other):
return Cella(self.states+other.states)
def __rshift__(self, ip):
self.gioca(ip)
def __gt__(self, ip):
self.gioca(ip)
def gioca(self, ip):
s = client(ip)
s.send(str(self).encode('ascii'))
vittoria = s.recv(1)
if not vittoria:
s.close()
coin()
else:
s.close()
print('Hai vinto che forte')
@property
def vinto(self):
matrix = []
width = math.ceil(math.sqrt(len(self.states)))
# TODO: assert per vedere che e' proprio quadrata
for i in range(width):
matrix.append(self.states[i*width:i*width+width])
for line in matrix:
if all(c == 'X' for c in line) or all(c == 'O' for c in line):
print('hai perso ma ti voglio comunque bene', line)
return True
for col_idx in range(width):
line = [line[col_idx] for line in matrix]
if all(c == 'X' for c in line) or all(c == 'O' for c in line):
print('hai perso per te niente gelato', line)
return True
# diagonale
diagonali = [
[matrix[i][i] for i in range(width)],
[matrix[i][width-i-1] for i in range(width)]
]
for line in diagonali:
if all(c == 'X' for c in line) or all(c == 'O' for c in line):
print('che poca stima che ho di te', line)
return True
return False
def coin():
s, other_ip = server()
table = eval(s.recv(BUFFER_SIZE).decode('ascii'))
if table.vinto:
s.send(b'W')
# s.send(1) solo se hai vinto
s.close()
print(str(table) + ' > "' + other_ip[0] + '"')
X = Cella(['X'])
O = Cella(['O'])
_ = Cella(['_'])
z = Cella(['z'])
if __name__ == '__main__':
if len(sys.argv)>1:
if sys.argv[1] == '--test':
provacella()
sys.exit(0)
other_ip = sys.argv[1]
_ |_ |_ |_ |_ |_ |_ |_ |_ >> other_ip
else:
print("To play, insert coin()")
__all__ = ['_', 'X', 'O', 'coin', 'z']