Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from record import record
- class Jugador(record):
- nombre = ''
- puntaje = 0.0
- ficha = ''
- #Solicitar el nombre de los jugadores y guarda en un archivo record.txt
- def crea_jugador_cruz():
- nombre = raw_input("Ingrese nombre del jugador representado por la cruz: ")
- return Jugador(nombre=nombre, ficha='X')
- def crea_jugador_circulo():
- nombre = raw_input('Ingrese nombre del jugador representado por el circulo: ')
- return Jugador(nombre=nombre, ficha='O')
- def guardar_record(record):
- f = open("record.txt", 'w')
- for jugador in record.lista:
- f.write(jugador.nombre + '\n')
- f.close()
- class Tablero :
- def __init__(self):
- self.triqui = [ '-', '-', '-', '-', '-', '-', '-', '-', '-' ]
- #Esta funcion returna true si logra poner una x o una O en el triqui, de lo contrario retorna False
- def ponerFicha(self, jugador, lugar):
- if (self.triqui[lugar - 1] == '-' ) :
- self.triqui[lugar - 1] = jugador.ficha
- return True
- else :
- print "Movida Ilegal. La casilla esta ocupada o no existe."
- return False
- def mostrar (self):
- """ Muestra el tablero"""
- print "%s %s %s\n%s %s %s\n%s %s %s" % tuple(self.triqui)
- def comprobar(self, jugador):
- ficha = jugador.ficha
- return (((self.triqui[0]== ficha) and (self.triqui[1] == ficha) and (self.triqui[2] == ficha)) or
- ((self.triqui[0]== ficha) and (self.triqui[4] == ficha) and (self.triqui[8] == ficha)) or
- ((self.triqui[0]== ficha) and (self.triqui[3] == ficha) and (self.triqui[6] == ficha)) or
- ((self.triqui[1]== ficha) and (self.triqui[4] == ficha) and (self.triqui[7] == ficha)) or
- ((self.triqui[2]== ficha) and (self.triqui[4] == ficha) and (self.triqui[6] == ficha)) or
- ((self.triqui[2]== ficha) and (self.triqui[5] == ficha) and (self.triqui[8] == ficha)) or
- ((self.triqui[3]== ficha) and (self.triqui[4] == ficha) and (self.triqui[5] == ficha)))
- def hayCasillaLibre(self):
- for i in range(0,9) :
- if (self.triqui[i] == '-') :
- return True
- return False
- class Juego:
- def jugar(self, jugadores):
- self.tablero = Tablero()
- turno = 0 #representa al jugador en la posicion "n" de jugadores
- print "Los numeros representan las casillas."
- print "%d.%d.%d\n%d.%d.%d\n%d.%d.%d" % tuple(range(1, 10))
- while self.tablero.hayCasillaLibre() and not self.alguienGano():
- print "Turno del jugador ", turno + 1
- lugar=int(raw_input("Donde deseas poner? "))
- while not self.tablero.ponerFicha(jugadores[turno], lugar):
- lugar=int(raw_input("Intentalo de nuevo. "))
- self.tablero.mostrar()
- turno = (turno + 1) % 2
- victoria = self.alguienGano()
- if not victoria: #empate
- print "Se trata de un empate"
- else:
- print "Ha ganado el jugador " , victoria
- def alguienGano(self):
- if self.tablero.comprobar(jugadores[0]):
- return 1
- elif self.tablero.comprobar(jugadores[1]):
- return 2
- else:
- return False
- #--------------------------------------------------------------------
- #menu!
- def menu():
- opcion = 0
- while opcion < 1 or opcion > 3:
- print 'Bienvenido al juego tres en raya'
- print 'Que decea hacer?'
- print ' 1) Ingresar nombre de los jugadores.'
- print ' 2) Jugar.'
- print ' 3) Salir.'
- opcion = int(raw_input('Escoge opción: '))
- return opcion
- #--------------------------------------------------------------------
- #--------------------------------------------------------------------
- #--------------------------------------------------------------------
- jugadores = [] # Inicialmente la lista de jugadores esta vacia
- opcion = 0
- while opcion != 3:
- opcion = menu()
- if opcion == 1: # añade jugadores
- jugadores.append( crea_jugador_cruz())
- jugadores.append( crea_jugador_circulo())
- print 'Se han agregado los nombres de los jugadores.'
- if opcion == 2: #jugar
- juego = Juego()
- juego.jugar(jugadores)
- print 'Gracias por usar el programa.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement