Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from record import record
- class Jugador(record):
- nombre = ''
- puntos = 0
- def insertarJugador(lista):
- nombre = raw_input("Inserte el nombre del jugador")
- lista.append(Jugador(nombre=nombre))
- #funciones para manejar los records
- class Records:
- lista = []
- def cargarRecords(records):
- records.lista = []
- f = open("records.txt", "r+")
- while 1:
- nombre_jugador = f.readline()
- puntos_jugador = f.readline()
- if nombre_jugador:
- print "Nombre ", nombre_jugador
- if not nombre_jugador or not puntos_jugador:
- break
- puntos_jugador = int(puntos_jugador)
- jugador = Jugador(nombre=nombre_jugador, puntos=puntos_jugador)
- records.lista.append(jugador)
- for jugador in records.lista:
- print "Jugador" ,jugador
- print jugador.nombre
- print jugador.puntos
- f.close()
- def guardarRecords(records):
- f = open("records.txt", "w")
- for jugador in records.lista:
- print "Nombre", jugador.nombre
- f.write(jugador.nombre + '\n')
- f.write(str(jugador.puntos) + '\n')
- f.close()
- def buscarRecord(records, nombre):
- for jugador in records.lista:
- if jugador.nombre == nombre:
- return jugador
- return Jugador(nombre='', puntos=0)
- class Tablero:
- triqui = [ '-', '-', '-',
- '-', '-', '-',
- '-', '-', '-'
- ]
- def insertarFicha(triqui, ficha, lugar):
- if (str.isdigit(lugar)):
- lugar = int(lugar)
- else:
- return False
- if (lugar >= 1 and lugar <= 9 and triqui[int(lugar)-1] == '-'):
- triqui[int(lugar)-1] = ficha
- return True
- else:
- return False
- def mostrarTablero(triqui):
- print "%s %s %s\n%s %s %s\n%s %s %s" % tuple(triqui)
- def comprobarVictoria(triqui, 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(triqui):
- for i in range(0,9) :
- if (triqui[i] == '-') :
- return True
- return False
- def jugar(jugadores):
- for jugador in jugadores:
- print jugador
- print jugador.nombre
- print jugador.puntos
- tablero = Tablero()
- fichas = ['X', 'O' ]
- 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))
- #mientras quede alguna casilla libre y no haya ganado nadie seguimos poniendo
- while hayCasillaLibre(tablero.triqui) and not alguienGano(tablero):
- print "Turno del jugador ", turno + 1
- lugar=raw_input("Donde deseas poner? ")
- while not insertarFicha(tablero.triqui ,fichas[turno], lugar):
- lugar=raw_input("Intentalo de nuevo. ")
- mostrarTablero(tablero.triqui)
- turno = (turno + 1) % 2 #esto hace que cambie entre 0 y 1 siempre
- #hemos terminado de jugar
- victoria = alguienGano(tablero)
- records = Records()
- cargarRecords(records)
- if not victoria: #empate
- print "Se trata de un empate"
- else:
- print "Ha ganado el jugador " , victoria
- nombre_victorioso = jugadores[victoria - 1].nombre # -1 porque victoria es 1 o 2, mientras que el array es 0 o 1
- entrada = buscarRecord(records, nombre_victorioso)
- if entrada.nombre != '': #el jugador ya estaba en el registro de records
- entrada.puntos += 1 #aumentamos el numero de partidas ganadas
- print "El jugador ya estaba"
- else:
- jugador = Jugador(nombre=nombre_victorioso, puntos=1)
- records.lista.append(jugador)
- #mostramos todas las estadisticas
- guardarRecords(records)
- def alguienGano(tablero):
- if comprobarVictoria(tablero.triqui, 'X'): #ha ganado cruz?
- return 1
- elif comprobarVictoria(tablero.triqui, 'O'): #ha ganado circulo?
- return 2
- 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 = (raw_input('Escoge opcion: '))
- return opcion
- #--------------------------------------------------------------------
- #--------------------------------------------------------------------
- #--------------------------------------------------------------------
- jugadores = [] # Inicialmente la lista de jugadores esta vacia
- opcion = 0
- jugadores_creados = False
- while opcion != '3':
- opcion = menu()
- if opcion == '1': # aniade jugadores
- cadena = raw_input("Inserte el nombre del jugador cruz: ")
- jugador = Jugador(nombre=cadena,puntos=0)
- jugadores.append(jugador)
- cadena = raw_input("Inserte el nombre del jugador circulo: ")
- jugador = Jugador(nombre=cadena,puntos=0)
- jugadores.append(jugador)
- jugadores_creados = True
- print 'Se han agregado los nombres de los jugadores.'
- if opcion == '2': #jugar
- if jugadores_creados:
- jugar(jugadores)
- else:
- print "Tienes que crear los jugadores primero"
- print 'Gracias por usar el programa.'
- raw_input("Fin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement