Advertisement
Guest User

final

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