Advertisement
Guest User

Untitled

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