Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. '''
  5. Projeto final: Etapa 4
  6.  
  7. Após o jogador solicitar o encerramento, apresentar a quantidade de partidas jogadas, a quantidade de vezes em que o jogador 1
  8. foi vencedor, a quantidade de vezes em que o jogador 2 foi vencedor e quem foi o ganhador geral de todas as partidas.
  9. '''
  10. import re
  11.  
  12. def verificaStatus(t):
  13. # Se retornar 1, jogador 1 venceu
  14. # Se retornar 2, jogador 2 venceu
  15. # Se retornar 0, deu empate
  16. # Se retornar 3, jogo ainda não terminou
  17.  
  18. if t[0] != ' ':
  19. if t[0] == t[1] and t[1] == t[2]:
  20. return t[0]
  21. if t[0] == t[4] and t[4] == t[8]:
  22. return t[0]
  23. if t[0] == t[3] and t[3] == t[6]:
  24. return t[0]
  25. if t[6] != ' ':
  26. if t[6] == t[4] and t[4] == t[2]:
  27. return t[6]
  28. if t[6] == t[7] and t[7] == t[8]:
  29. return t[6]
  30. if t[3] != ' ' and t[3] == t[4] and t[4] == t[5]:
  31. return t[3]
  32. if t[1] != ' ' and t[1] == t[4] and t[4] == t[7]:
  33. return t[1]
  34. if t[2] != ' ' and t[2] == t[5] and t[5] == t[8]:
  35. return t[2]
  36.  
  37. for i in range(9):
  38. if t[i] == ' ': # tem um casa não jogada - jogo continua
  39. return 3
  40.  
  41. return 0
  42.  
  43. def imprimeTabuleiro(tabuleiro):
  44. print(" {} | {} | {} ".format(tabuleiro[0], tabuleiro[1], tabuleiro[2]))
  45. print("---|---|---")
  46. print(" {} | {} | {} ".format(tabuleiro[3], tabuleiro[4], tabuleiro[5]))
  47. print("---|---|---")
  48. print(" {} | {} | {} ".format(tabuleiro[6], tabuleiro[7], tabuleiro[8]))
  49.  
  50. tabuleiroPos = [ '1', '2', '3', '4', '5', '6', '7', '8', '9']
  51. jogador = [ '', '' ]
  52. jogador[0] = input("Jogador 1, digite seu nome: ")
  53. jogador[1] = input("Jogador 2, digite seu nome: ")
  54.  
  55. def leJogada(vez, tabuleiro):
  56. jogadaValida = 0
  57. while jogadaValida == 0:
  58. print("{}, onde quer jogar? ".format(jogador[int(vez) - 1]))
  59. imprimeTabuleiro(tabuleiroPos)
  60. pos = input()
  61. if not re.match('^[1-9]$', pos):
  62. print("Essa posição não é válida - tente de novo")
  63. continue
  64. pos = int(pos)
  65. if tabuleiro[pos - 1] != ' ':
  66. print("Essa posição já está ocupada - tente de novo")
  67. continue
  68. jogadaValida = 1
  69. tabuleiro[pos - 1] = vez
  70.  
  71. vez = 1
  72. tabuleiro = [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  73.  
  74. vitorias = [ 0, 0 ]
  75. empates = 0
  76. while 1:
  77. leJogada(vez, tabuleiro)
  78. imprimeTabuleiro(tabuleiro)
  79. status = verificaStatus(tabuleiro)
  80. if status == 3:
  81. vez = 2 if vez == 1 else 1
  82. else:
  83. if status == 1 or status == 2:
  84. print("Parabéns {}, você venceu".format(jogador[int(status) - 1]))
  85. vitorias[int(status) - 1] += 1
  86. else:
  87. print("Houve um empate entre os jogadores")
  88. empates += 1
  89. jogarDeNovo = 0
  90. while 1:
  91. jogarDeNovo = input("Querem jogar de novo? (S/N) ")
  92. if re.match('(S|N)', jogarDeNovo):
  93. jogarDeNovo = 1 if jogarDeNovo == 'S' else 0
  94. break
  95. if jogarDeNovo == 0:
  96. break
  97. else:
  98. # Reiniciar o tabuleiro e a vez
  99. vez = 1
  100. tabuleiro = [ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  101.  
  102.  
  103. print("De um total de {} partidas, {} foram empates".format(empates + vitorias[0] + vitorias[1], empates))
  104. print("{} ganhou {} partidas e {} ganhou {} partidas".format(jogador[0], vitorias[0], jogador[1], vitorias[1]))
  105. if (vitorias[0] > vitorias[1]):
  106. print("Parabéns {}, você foi o grande vencedor".format(jogador[0]))
  107. elif (vitorias[1] > vitorias[0]):
  108. print("Parabens {}, você foi o grande vencedor".format(jogador[1]))
  109. else:
  110. print("Ninguém venceu. Houve um empate em partidas")
Add Comment
Please, Sign In to add comment