Created
March 31, 2014 03:53
-
-
Save colltoaction/9884944 to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python2 | |
# coding=utf8 | |
class Cruz(object): | |
def __repr__(self): | |
return "X" | |
def __eq__(self, other): | |
return isinstance(other, self.__class__) | |
class Circulo(object): | |
def __repr__(self): | |
return "0" | |
def __eq__(self, other): | |
return isinstance(other, self.__class__) | |
class Casillero(object): | |
def __init__(self): | |
self.valor = None | |
self._resaltar = False | |
def ponerX(self): | |
self.valor = Cruz() | |
def poner0(self): | |
self.valor = Circulo() | |
def resaltar(self): | |
self._resaltar = True | |
def __repr__(self): | |
if self.valor is None: | |
return " " | |
if self._resaltar: | |
return "\033[31m%s\033[m" % repr(self.valor) | |
return repr(self.valor) | |
class Linea(object): | |
def __init__(self, c1, c2, c3): | |
self.c1 = c1 | |
self.c2 = c2 | |
self.c3 = c3 | |
def __repr__(self): | |
return "[%s, %s, %s]" % (self.c1, self.c2, self.c3) | |
def completa(self): | |
return self.c1.valor == self.c2.valor == self.c3.valor != None | |
def resaltar_casilleros(self): | |
self.c1.resaltar() | |
self.c2.resaltar() | |
self.c3.resaltar() | |
class Tablero(object): | |
def __init__(self, casilleros, lineas): | |
self.casilleros = casilleros | |
self.lineas = lineas | |
def __repr__(self): | |
return "%s\n%s\n%s\n" % (self.casilleros[0:3], self.casilleros[3:6], self.casilleros[6:9]) | |
def main(): | |
casilleros = [Casillero() for i in range(9)] | |
lineas = (Linea(casilleros[0], casilleros[1], casilleros[2]), Linea(casilleros[3], casilleros[4], casilleros[5]), Linea(casilleros[6], casilleros[7], casilleros[8]), \ | |
Linea(casilleros[0], casilleros[3], casilleros[6]), Linea(casilleros[1], casilleros[4], casilleros[7]), Linea(casilleros[2], casilleros[5], casilleros[8]), \ | |
Linea(casilleros[0], casilleros[4], casilleros[8]), Linea(casilleros[2], casilleros[4], casilleros[6])) | |
tablero = Tablero(casilleros, lineas) | |
while not any(linea.completa() for linea in lineas): | |
print tablero | |
pos = input("pos [0-8]") | |
casilleros[pos].ponerX() | |
pos = input("pos [0-8]") | |
casilleros[pos].poner0() | |
for linea in lineas: | |
if linea.completa(): | |
linea.resaltar_casilleros() | |
print tablero | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment