Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. from record import record
  2.  
  3.  
  4. class Jugador(record):
  5.     nombre      = ''
  6.     puntos      = 0
  7.    
  8. def insertarJugador(lista):
  9.     nombre = raw_input("Inserte el nombre del jugador")
  10.     lista.append(Jugador(nombre=nombre))
  11.    
  12. #funciones para manejar los records
  13. class Records:
  14.     lista       = []
  15.    
  16. def cargarRecords(records):
  17.     records.lista = []
  18.     f = open("records.txt", "r+")
  19.     while 1:
  20.         nombre_jugador = f.readline()
  21.         puntos_jugador = f.readline()
  22.         if nombre_jugador:
  23.             print "Nombre ", nombre_jugador
  24.         if not nombre_jugador or not puntos_jugador:
  25.             break
  26.         puntos_jugador = int(puntos_jugador)
  27.         jugador = Jugador(nombre=nombre_jugador, puntos=puntos_jugador)
  28.         records.lista.append(jugador)
  29.         for jugador in records.lista:
  30.             print "Jugador" ,jugador
  31.             print jugador.nombre
  32.             print jugador.puntos
  33.        
  34.     f.close()
  35.    
  36. def guardarRecords(records):
  37.     f = open("records.txt", "w")
  38.     for jugador in records.lista:
  39.         print "Nombre", jugador.nombre
  40.         f.write(jugador.nombre + '\n')
  41.         f.write(str(jugador.puntos) + '\n')
  42.     f.close()
  43.    
  44. def buscarRecord(records, nombre):
  45.     for jugador in records.lista:
  46.         if jugador.nombre == nombre:
  47.             return jugador
  48.     return Jugador(nombre='', puntos=0)
  49.    
  50. class Tablero:
  51.     triqui      = [ '-', '-', '-',
  52.                                 '-', '-', '-',
  53.                                 '-', '-', '-'
  54.                             ]
  55.  
  56. def insertarFicha(triqui, ficha, lugar):
  57.     if (str.isdigit(lugar)):
  58.         lugar = int(lugar)
  59.     else:
  60.         return False
  61.     if (lugar >= 1 and lugar <= 9 and triqui[int(lugar)-1] == '-'):
  62.         triqui[int(lugar)-1] = ficha
  63.         return True
  64.     else:
  65.         return False
  66.  
  67. def mostrarTablero(triqui):
  68.     print "%s %s %s\n%s %s %s\n%s %s %s" % tuple(triqui)
  69.    
  70. def comprobarVictoria(triqui, ficha):
  71.     return (((triqui[0]== ficha) and (triqui[1] == ficha) and (triqui[2] == ficha)) or
  72.            ((triqui[0]== ficha) and (triqui[4] == ficha) and (triqui[8] == ficha)) or
  73.            ((triqui[0]== ficha) and (triqui[3] == ficha) and (triqui[6] == ficha)) or
  74.            ((triqui[1]== ficha) and (triqui[4] == ficha) and (triqui[7] == ficha)) or
  75.            ((triqui[2]== ficha) and (triqui[4] == ficha) and (triqui[6] == ficha)) or
  76.            ((triqui[2]== ficha) and (triqui[5] == ficha) and (triqui[8] == ficha)) or
  77.            ((triqui[3]== ficha) and (triqui[4] == ficha) and (triqui[5] == ficha)))
  78.  
  79. def hayCasillaLibre(triqui):
  80.     for i in range(0,9) :
  81.         if (triqui[i] == '-') :
  82.             return True
  83.     return False
  84.  
  85. def jugar(jugadores):
  86.     for jugador in jugadores:
  87.         print jugador
  88.         print jugador.nombre
  89.         print jugador.puntos
  90.     tablero = Tablero()
  91.     fichas = ['X', 'O' ]
  92.     turno = 0 #representa al jugador en la posicion "n" de jugadores
  93.     print "Los numeros representan las casillas."
  94.     print "%d.%d.%d\n%d.%d.%d\n%d.%d.%d" % tuple(range(1, 10))
  95.    
  96.     #mientras quede alguna casilla libre y no haya ganado nadie seguimos poniendo
  97.     while hayCasillaLibre(tablero.triqui) and not alguienGano(tablero):
  98.         print "Turno del jugador ", turno + 1
  99.         lugar=raw_input("Donde deseas poner? ")
  100.         while not insertarFicha(tablero.triqui ,fichas[turno], lugar):
  101.             lugar=raw_input("Intentalo de nuevo. ")
  102.         mostrarTablero(tablero.triqui)
  103.         turno = (turno + 1) % 2 #esto hace que cambie entre 0 y 1 siempre
  104.        
  105.     #hemos terminado de jugar
  106.     victoria = alguienGano(tablero)
  107.     records = Records()
  108.     cargarRecords(records)
  109.     if not victoria: #empate
  110.       print "Se trata de un empate"
  111.     else:
  112.         print "Ha ganado el jugador " , victoria
  113.         nombre_victorioso = jugadores[victoria - 1].nombre # -1 porque victoria es 1 o 2, mientras que el array es 0 o 1
  114.         entrada = buscarRecord(records, nombre_victorioso)
  115.         if entrada.nombre != '': #el jugador ya estaba en el registro de records
  116.             entrada.puntos += 1 #aumentamos el numero de partidas ganadas
  117.             print "El jugador ya estaba"
  118.         else:
  119.             jugador = Jugador(nombre=nombre_victorioso, puntos=1)
  120.             records.lista.append(jugador)
  121.     #mostramos todas las estadisticas
  122.  
  123.         guardarRecords(records)
  124.    
  125. def alguienGano(tablero):
  126.     if comprobarVictoria(tablero.triqui, 'X'): #ha ganado cruz?
  127.             return 1
  128.     elif comprobarVictoria(tablero.triqui, 'O'): #ha ganado circulo?
  129.             return 2
  130.     else:
  131.             return False
  132.    
  133.  
  134. #menu!
  135. def menu():
  136.   opcion = '0'
  137.   while opcion < '1' or opcion > '3':
  138.     print 'Bienvenido al juego tres en raya'
  139.     print 'Que decea hacer?'
  140.     print ' 1) Ingresar nombre de los jugadores.'
  141.     print ' 2) Jugar.'
  142.     print ' 3) Salir.'
  143.     opcion = (raw_input('Escoge opcion: '))
  144.   return opcion
  145.  
  146.  
  147.  
  148. #--------------------------------------------------------------------
  149. #--------------------------------------------------------------------
  150. #--------------------------------------------------------------------
  151.  
  152.  
  153. jugadores = [] # Inicialmente la lista de jugadores esta vacia
  154.  
  155.  
  156. opcion = 0
  157. jugadores_creados = False
  158. while opcion != '3':
  159.   opcion = menu()
  160.   if opcion == '1': # aniade jugadores
  161.         cadena = raw_input("Inserte el nombre del jugador cruz: ")
  162.         jugador = Jugador(nombre=cadena,puntos=0)
  163.         jugadores.append(jugador)  
  164.         cadena = raw_input("Inserte el nombre del jugador circulo: ")
  165.         jugador = Jugador(nombre=cadena,puntos=0)
  166.         jugadores.append(jugador)
  167.         jugadores_creados = True
  168.         print 'Se han agregado los nombres de los jugadores.'
  169.   if opcion == '2': #jugar
  170.     if jugadores_creados:
  171.       jugar(jugadores)
  172.     else:
  173.       print "Tienes que crear los jugadores primero"
  174.      
  175. print 'Gracias por usar el programa.'
  176.  
  177. raw_input("Fin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement