Created
July 18, 2013 13:05
-
-
Save fmasanori/6029123 to your computer and use it in GitHub Desktop.
Jogo simples em Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
print ('Bem vindo!') | |
sorteado = randint(1, 100) | |
chute = 0 | |
while chute != sorteado: | |
chute = int(input ('Chute: ')) | |
if chute == sorteado: | |
print ('Você venceu!') | |
else: | |
if chute > sorteado: | |
print ('Alto') | |
else: | |
print ('Baixo') | |
print ('Fim do jogo!') |
** Acho que consegui!!!**
Eu apaguei os "elses" , ja no segundo "if" eu coloquei no "print('Menor')" e no ultimo "if" eu coloquei "chute < sorteado" colocar "Maior" . Pronto! 👍😉
from random import randint
print ('Bem vindo!')
sorteado = randint(1, 100)
chute = 0
while chute != sorteado:
chute = int(input ('Chute: '))
if chute == sorteado:
print ('Você venceu!')
if chute > sorteado:
print ('Menor')
if chute < sorteado:
print ('Maior')
print ('Fim do jogo!')
gostei, so acho q podia fazer um print explicando como funfa
fiz um jokenpo diferente, ve la no meu perfil (・ิω・ิ)
class JogoDaVela:
tabuleiro = {'7': ' ', '8': ' ', '9': ' ', '4': ' ', '5': ' ', '6': ' ', '1': ' ', '2': ' ', '3': ' '}
turno = None
def __init__(self, jogador_inicial="X"):
self.turno = jogador_inicial
def exibir_tabuleiro(self):
print("┌───┬───┬───┐")
print(f"│ {self.tabuleiro['7']} │ {self.tabuleiro['8']} │ {self.tabuleiro['9']} │")
print("├───┼───┼───┤")
print(f"│ {self.tabuleiro['4']} │ {self.tabuleiro['5']} │ {self.tabuleiro['6']} │")
print("├───┼───┼───┤")
print(f"│ {self.tabuleiro['1']} │ {self.tabuleiro['2']} │ {self.tabuleiro['3']} │")
print("└───┴───┴───┘")
def verificar_jogada(self, jogada):
if jogada in self.tabuleiro.keys():
if self.tabuleiro[jogada] == " ":
return True
return False
def verificar_tabuleiro(self):
# Verificações das 3 verticais
if self.tabuleiro['7'] == self.tabuleiro['4'] == self.tabuleiro['1'] != ' ':
return self.tabuleiro['7']
elif self.tabuleiro['8'] == self.tabuleiro['5'] == self.tabuleiro['2'] != ' ':
return self.tabuleiro['8']
elif self.tabuleiro['9'] == self.tabuleiro['6'] == self.tabuleiro['3'] != ' ':
return self.tabuleiro['9']
# Verificações das 3 horizontais
elif self.tabuleiro['7'] == self.tabuleiro['8'] == self.tabuleiro['9'] != ' ':
return self.tabuleiro['7']
elif self.tabuleiro['4'] == self.tabuleiro['5'] == self.tabuleiro['6'] != ' ':
return self.tabuleiro['8']
elif self.tabuleiro['1'] == self.tabuleiro['2'] == self.tabuleiro['3'] != ' ':
return self.tabuleiro['1']
# Verificações das 2 diagonais
elif self.tabuleiro['7'] == self.tabuleiro['5'] == self.tabuleiro['3'] != ' ':
return self.tabuleiro['7']
elif self.tabuleiro['1'] == self.tabuleiro['5'] == self.tabuleiro['9'] != ' ':
return self.tabuleiro['1']
# Verificando empate
if [*self.tabuleiro.values()].count(' ') == 0:
return "empate"
else:
return [*self.tabuleiro.values()].count(' ')
def jogar(self):
while True:
self.exibir_tabuleiro()
print(f"Turno do {self.turno}, qual sua jogada?")
# Enquanto o jogador não fizer uma jogada válida
while True:
jogada = input("Jogada: ")
if self.verificar_jogada(jogada): # Se a jogada for válida...
break # Encerra o loop
else:
print(f"jogado do {self.turno} inválida, jogue novamente.")
self.tabuleiro[jogada] = self.turno
estado = self.verificar_tabuleiro()
if estado == "X":
print("X é o vencedor!!!")
break
elif estado == "O":
print("O é o vencedor!!!")
break
if estado == "empate":
print("EMPATE!!!")
break
# Troca o jogador do próximo turno
self.turno = "X" if self.turno == "O" else "O"
self.exibir_tabuleiro()
jogo = JogoDaVela()
jogo.jogar()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
o jogo não funciona, oq preciso fazer para jogar?