Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # -*- coding: utf-8 -*-
- '''
- Projeto final: Etapa 4
- Após o jogador solicitar o encerramento, apresentar a quantidade de partidas jogadas, a quantidade de vezes em que o jogador 1
- foi vencedor, a quantidade de vezes em que o jogador 2 foi vencedor e quem foi o ganhador geral de todas as partidas.
- '''
- import re
- def verificaStatus(t):
- # Se retornar 1, jogador 1 venceu
- # Se retornar 2, jogador 2 venceu
- # Se retornar 0, deu empate
- # Se retornar 3, jogo ainda não terminou
- if t[0] != ' ':
- if t[0] == t[1] and t[1] == t[2]:
- return t[0]
- if t[0] == t[4] and t[4] == t[8]:
- return t[0]
- if t[0] == t[3] and t[3] == t[6]:
- return t[0]
- if t[6] != ' ':
- if t[6] == t[4] and t[4] == t[2]:
- return t[6]
- if t[6] == t[7] and t[7] == t[8]:
- return t[6]
- if t[3] != ' ' and t[3] == t[4] and t[4] == t[5]:
- return t[3]
- if t[1] != ' ' and t[1] == t[4] and t[4] == t[7]:
- return t[1]
- if t[2] != ' ' and t[2] == t[5] and t[5] == t[8]:
- return t[2]
- for i in range(9):
- if t[i] == ' ': # tem um casa não jogada - jogo continua
- return 3
- return 0
- def imprimeTabuleiro(tabuleiro):
- print(" {} | {} | {} ".format(tabuleiro[0], tabuleiro[1], tabuleiro[2]))
- print("---|---|---")
- print(" {} | {} | {} ".format(tabuleiro[3], tabuleiro[4], tabuleiro[5]))
- print("---|---|---")
- print(" {} | {} | {} ".format(tabuleiro[6], tabuleiro[7], tabuleiro[8]))
- tabuleiroPos = [ '1', '2', '3', '4', '5', '6', '7', '8', '9']
- jogador = [ '', '' ]
- jogador[0] = input("Jogador 1, digite seu nome: ")
- jogador[1] = input("Jogador 2, digite seu nome: ")
- def leJogada(vez, tabuleiro):
- jogadaValida = 0
- while jogadaValida == 0:
- print("{}, onde quer jogar? ".format(jogador[int(vez) - 1]))
- imprimeTabuleiro(tabuleiroPos)
- pos = input()
- if not re.match('^[1-9]$', pos):
- print("Essa posição não é válida - tente de novo")
- continue
- pos = int(pos)
- if tabuleiro[pos - 1] != ' ':
- print("Essa posição já está ocupada - tente de novo")
- continue
- jogadaValida = 1
- tabuleiro[pos - 1] = vez
- vez = 1
- tabuleiro = [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
- vitorias = [ 0, 0 ]
- empates = 0
- while 1:
- leJogada(vez, tabuleiro)
- imprimeTabuleiro(tabuleiro)
- status = verificaStatus(tabuleiro)
- if status == 3:
- vez = 2 if vez == 1 else 1
- else:
- if status == 1 or status == 2:
- print("Parabéns {}, você venceu".format(jogador[int(status) - 1]))
- vitorias[int(status) - 1] += 1
- else:
- print("Houve um empate entre os jogadores")
- empates += 1
- jogarDeNovo = 0
- while 1:
- jogarDeNovo = input("Querem jogar de novo? (S/N) ")
- if re.match('(S|N)', jogarDeNovo):
- jogarDeNovo = 1 if jogarDeNovo == 'S' else 0
- break
- if jogarDeNovo == 0:
- break
- else:
- # Reiniciar o tabuleiro e a vez
- vez = 1
- tabuleiro = [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
- print("De um total de {} partidas, {} foram empates".format(empates + vitorias[0] + vitorias[1], empates))
- print("{} ganhou {} partidas e {} ganhou {} partidas".format(jogador[0], vitorias[0], jogador[1], vitorias[1]))
- if (vitorias[0] > vitorias[1]):
- print("Parabéns {}, você foi o grande vencedor".format(jogador[0]))
- elif (vitorias[1] > vitorias[0]):
- print("Parabens {}, você foi o grande vencedor".format(jogador[1]))
- else:
- print("Ninguém venceu. Houve um empate em partidas")
Add Comment
Please, Sign In to add comment