Advertisement
Guest User

intento2

a guest
Oct 30th, 2010
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1.  
  2. from record import record
  3.  
  4. class Jugador(record):
  5.   nombre     = ''
  6.   puntaje    = 0.0
  7.   ficha       = ''
  8.  
  9. #Solicitar el nombre de los jugadores y guarda en un archivo record.txt
  10. def crea_jugador_cruz():
  11.   nombre = raw_input("Ingrese nombre del jugador representado por la cruz: ")
  12.  
  13.   return Jugador(nombre=nombre, ficha='X')
  14.  
  15. def crea_jugador_circulo():
  16.   nombre = raw_input('Ingrese nombre del jugador representado por el circulo: ')
  17.   return Jugador(nombre=nombre, ficha='O')
  18.  
  19.  
  20. def guardar_record(record):
  21.   f = open("record.txt", 'w')
  22.   for jugador in record.lista:
  23.     f.write(jugador.nombre + '\n')
  24.   f.close()
  25.  
  26. class Tablero :
  27.  
  28.   def __init__(self):
  29.     self.triqui = [ '-', '-', '-', '-', '-', '-', '-', '-', '-' ]
  30.  
  31. #Esta funcion returna true si logra poner una x o una O en el triqui, de lo contrario retorna False
  32.   def ponerFicha(self, jugador, lugar):
  33.     if (self.triqui[lugar - 1] == '-' )  :
  34.       self.triqui[lugar - 1] = jugador.ficha
  35.       return True
  36.     else :
  37.       print "Movida Ilegal. La casilla esta ocupada o no existe."
  38.       return False
  39.  
  40.   def mostrar (self):
  41.     """ Muestra el tablero"""
  42.     print "%s %s %s\n%s %s %s\n%s %s %s" % tuple(self.triqui)
  43.  
  44.   def comprobar(self, jugador):
  45.     ficha = jugador.ficha
  46.     return (((self.triqui[0]== ficha) and (self.triqui[1] == ficha) and (self.triqui[2] == ficha)) or
  47.            ((self.triqui[0]== ficha) and (self.triqui[4] == ficha) and (self.triqui[8] == ficha)) or
  48.            ((self.triqui[0]== ficha) and (self.triqui[3] == ficha) and (self.triqui[6] == ficha)) or
  49.            ((self.triqui[1]== ficha) and (self.triqui[4] == ficha) and (self.triqui[7] == ficha)) or
  50.            ((self.triqui[2]== ficha) and (self.triqui[4] == ficha) and (self.triqui[6] == ficha)) or
  51.            ((self.triqui[2]== ficha) and (self.triqui[5] == ficha) and (self.triqui[8] == ficha)) or
  52.            ((self.triqui[3]== ficha) and (self.triqui[4] == ficha) and (self.triqui[5] == ficha)))
  53.  
  54.   def hayCasillaLibre(self):
  55.     for i in range(0,9) :
  56.       if (self.triqui[i] == '-') :
  57.         return True
  58.     return False
  59.  
  60.  
  61.    
  62. class Juego:
  63.  
  64.   def jugar(self, jugadores):
  65.     self.tablero = Tablero()
  66.     turno = 0 #representa al jugador en la posicion "n" de jugadores
  67.     print "Los numeros representan las casillas."
  68.     print "%d.%d.%d\n%d.%d.%d\n%d.%d.%d" % tuple(range(1, 10))
  69.    
  70.     while self.tablero.hayCasillaLibre() and not self.alguienGano():
  71.       print "Turno del jugador ", turno + 1
  72.       lugar=int(raw_input("Donde deseas poner? "))
  73.       while not self.tablero.ponerFicha(jugadores[turno], lugar):
  74.         lugar=int(raw_input("Intentalo de nuevo. "))
  75.       self.tablero.mostrar()
  76.       turno = (turno + 1) % 2
  77.     victoria = self.alguienGano()
  78.     if not victoria: #empate
  79.         print "Se trata de un empate"
  80.     else:
  81.         print "Ha ganado el jugador " , victoria
  82.  
  83.   def alguienGano(self):
  84.     if self.tablero.comprobar(jugadores[0]):
  85.         return 1
  86.     elif self.tablero.comprobar(jugadores[1]):
  87.         return 2
  88.     else:
  89.         return False
  90. #--------------------------------------------------------------------
  91.  
  92.  
  93. #menu!
  94. def menu():
  95.   opcion = 0
  96.   while opcion < 1 or opcion > 3:
  97.     print 'Bienvenido al juego tres en raya'
  98.     print 'Que decea hacer?'
  99.     print ' 1) Ingresar nombre de los jugadores.'
  100.     print ' 2) Jugar.'
  101.     print ' 3) Salir.'
  102.     opcion = int(raw_input('Escoge opción: '))
  103.   return opcion
  104.  
  105.  
  106.  
  107. #--------------------------------------------------------------------
  108. #--------------------------------------------------------------------
  109. #--------------------------------------------------------------------
  110.  
  111.  
  112. jugadores = [] # Inicialmente la lista de jugadores esta vacia
  113.  
  114. opcion = 0
  115. while opcion != 3:
  116.   opcion = menu()
  117.   if opcion == 1: # añade jugadores
  118.     jugadores.append( crea_jugador_cruz())
  119.     jugadores.append( crea_jugador_circulo())
  120.     print 'Se han agregado los nombres de los jugadores.'
  121.   if opcion == 2: #jugar
  122.     juego = Juego()
  123.     juego.jugar(jugadores)
  124.      
  125. print 'Gracias por usar el programa.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement