Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: cp1252 -*-
- from record import record
- class Jugador(record):
- nombre = ''
- puntaje = 0.0
- ficha = ''
- #esta funcion es el constructor de Jugador, cuando hago Jugador(nombre, porcentaje) llama a esta funcion
- #en todas las funciones dentro de una clase, el primer parametro debe ser self (como el this en C++)
- def __init__ (self, nombre, ficha):
- self.nombre = nombre
- #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, 'X')
- def crea_jugador_circulo():
- nombre = raw_input('Ingrese nombre del jugador representado por el circulo: ')
- return Jugador(nombre, '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 (((triqui[0]== ficha) and (triqui[1] == ficha) and (triqui[2] == ficha)) or
- ((triqui[0]== ficha) and (triqui[4] == ficha) and (triqui[8] == ficha)) or
- ((triqui[0]== ficha) and (triqui[3] == ficha) and (triqui[6] == ficha)) or
- ((triqui[1]== ficha) and (triqui[4] == ficha) and (triqui[7] == ficha)) or
- ((triqui[2]== ficha) and (triqui[4] == ficha) and (triqui[6] == ficha)) or
- ((triqui[2]== ficha) and (triqui[5] == ficha) and (triqui[8] == ficha)) or
- ((triqui[3]== ficha) and (triqui[4] == ficha) and (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.jugadores = jugadores
- self.tablero = Tablero()
- self.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 not alguienGano() :
- print "Turno del jugador ", self.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
- num = alguienGano()
- if num == 3: #empate
- print "Se trata de un empate"
- else:
- print "Ha ganado el jugador " , num
- def alguienGano():
- if self.tablero.comprobar(self.jugadores[0]):
- return 1
- elif self.tablero.comprobar(self.jugadores[1]):
- return 2
- elif self.tablero.hayCasillaLibre():
- return 3
- 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[0] = crea_jugador_cruz()
- jugadores[1] = crea_jugador_circulo()
- 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