Ir para conteúdo
Fórum Script Brasil
  • 0

Programa Jogo-da-Velha


Michelcyc

Pergunta

Olá, começei a aprender python tem menos de 3 meses... no entanto com as aulas da faculdade tentei elaborar um jogo_da_velha com os conhecimentos de function,condicionais e estruturas de repeticao...

Pois bem terminei o programa para player x player , tentei criar a maquina... Criei a maquina num nivel rasuavel ... com condicoes para vencer e se defender, sei que posso criar uma maquina invencivel (gostaria de saber se isso influenciaria no meu conhecimento de programacao, invés de simples conhecimentos de jogodavelha...), fui mostrar a uma pessoa já experiente no ramo e ela disse q meu programa estava "sujo" , porque só havia 3 funcoes para um programa de 300 linhas ...

Já tentei mudá-lo mas sem muito sucesso... Queria algumas dicas ... Tentei fazer comentarios para ficar bem claro o programa

O programa está nesse link : http://www.megaupload.com/?d=2FDUENHW

Quando colei aqui ele perdeu a indentacao... ai complica...

#Programa Jogo_da_velha

# -*- coding: cp1252 -*-

a = ' '

b = ' '

c = ' '

d = ' '

e = ' '

f = ' '

g = ' '

h = ' '

i = ' '

velha = 1

#Acima são definidos 9 variaveis que equivalem as 9 posições dentro da função

#que imprime o jogo da velha na tela - jogo_velha - conforme os valores das

#variaveis recebem X ou O, o espaço vazio é substituido, preenchendo assim o

#jogo da velha.

import random

def jogo_velha(a,b,c,d,e,f,g,h,i):

print(" %s | %s | %s " %(a,b,c)),(" 1 | 2 | 3 ")

print("___|___|___"), (" ___|___|___")

print(" %s | %s | %s ") %(d,e,f), (" 4 | 5 | 6 ")

print("___|___|___"), (" ___|___|___")

print(" %s | %s | %s ") %(g,h,i), (" 7 | 8 | 9 ")

print(" | | "), (" | | ")

#Acima a funcao que imprime o jogo da velha que será impressa a cada rodada

def multiplayer(a,b,c,d,e,f,g,h,i,velha):

nome1 = raw_input("Jogador 1 digite seu nome ")

nome2 = raw_input("Jogador 2 digite seu nome ")

print

print

benzocriol = random.randrange(1,3) #Essa variavel receberá 1 ou 2, definindo o primeiro que irá jogar

fim_loop = benzocriol + 9 #Como serão 9 rodadas o loop que será criado terá um fim apenas após 9 jogadas

if benzocriol == 1 :

print("%s" %(nome1), " você é o primeiro!!")

else:

print("%s" %(nome2), " você é o primeiro!!") #If nomeando o primeiro

print("Esta é sua lista de escolhas")

print(" 1 | 2 | 3 ")

print("___|___|___")

print(" 4 | 5 | 6 ")

print("___|___|___")

print(" 7 | 8 | 9 ")

print(" | | ") #Interface do jogo para começarem as escolhas

while benzocriol < fim_loop : #Loop para cada jogada,como benzocriol tem valor 1 ou 2, a jogada do player 1 é feita com resto <> 0, e do player 2 com resto 0, numa divisão de benzocriol por 2.

if benzocriol%2 <> 0:

print('%s' %(nome1), 'escreva sua escolha' )

escolha = input()

if escolha == 1 and a == ' ' : #Se escolha for posicao e ela estiver vazia,faça:

a = 'O' #Preencha a variavel antes ' ', pelo simbolo do jogador

jogo_velha(a,b,c,d,e,f,g,h,i) #Imprima a função que desenha o jogo da velha

elif escolha == 2 and b == ' ' :

b = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 3 and c == ' ' :

c = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 4 and d == ' ' :

d = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 5 and e == ' ' :

e = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 6 and f == ' ' :

f = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 7 and g == ' ' :

g = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 8 and h == ' ' :

h = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 9 and i == ' ' :

i = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

else: #Se não for digitada uma escolha valida de posicao,msg de erro e benzocriol-1 para que a jogada não conte no looping

print("Escolha incorreta ou escolha já feita !! Escolha novamente !")

benzocriol = benzocriol - 1

elif benzocriol%2 == 0: #Mesma coisa para o jogador 2

print('%s' %(nome2), 'escreva sua escolha' )

escolha = input()

if escolha == 1 and a == ' ' :

a = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 2 and b == ' ' :

b = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 3 and c == ' ' :

c = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 4 and d == ' ' :

d = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 5 and e == ' ' :

e = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 6 and f == ' ' :

f = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 7 and g == ' ' :

g = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 8 and h == ' ' :

h = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 9 and i == ' ' :

i = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

else:

print("Escolha incorreta ou escolha já feita !! Escolha novamente !")

benzocriol = benzocriol - 1

if ((a==b)and(a==c) and (a == 'O')) or ((d==e)and(d==f)and (d == 'O')) or ((g==h)and(g==i)and (g == 'O')) or ((a==d)and(a==g)and (a == 'O')) or ((b==e)and(b==h)and (b == 'O')) or ((c==f)and(c==i)and (c == 'O')) or ((a==e)and(a==i)and (a == 'O')) or ((c==e)and(c==g)and (c == 'O')) :

print("%s é o vencedor") %(nome1)

velha = 0

break

elif ((a==b)and(a==c) and (a == 'X')) or ((d==e)and(d==f)and (d == 'X')) or ((g==h)and(g==i)and (g == 'X')) or ((a==d)and(a==g)and (a == 'X')) or ((b==e)and(b==h)and (b == 'X')) or ((c==f)and(c==i)and (c == 'X')) or ((a==e)and(a==i)and (a == 'X')) or ((c==e)and(c==g)and (c == 'X')) :

print("%s é o vencedor") %(nome2)

velha = 0

break #Condiçoes de vitoria com 1 break para parar o jogo caso alguém vença, e velha=0 para não ser impreso 'velha' no fim

benzocriol = benzocriol + 1

if velha <> 0:

print("Velha")

def singleplayer(a,b,c,d,e,f,g,h,i,velha): #Nessa aqui a maquina joga

nome1 = raw_input("Digite seu nome ")

print

print

benzocriol = random.randrange(1,3)

fim_loop = benzocriol + 9

if benzocriol == 1 :

print("%s" %(nome1), " você é o primeiro!!")

else:

print("A maquina comecou jogando...")

print("Esta é sua lista de escolhas")

print(" 1 | 2 | 3 ")

print("___|___|___")

print(" 4 | 5 | 6 ")

print("___|___|___")

print(" 7 | 8 | 9 ")

print(" | | ")

while benzocriol < fim_loop :

if benzocriol%2 <> 0:

print('%s' %(nome1), 'escreva sua escolha' )

escolha = input()

if escolha == 1 and a == ' ' :

a = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 2 and b == ' ' :

b = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 3 and c == ' ' :

c = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 4 and d == ' ' :

d = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 5 and e == ' ' :

e = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 6 and f == ' ' :

f = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 7 and g == ' ' :

g = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 8 and h == ' ' :

h = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif escolha == 9 and i == ' ' :

i = 'O'

jogo_velha(a,b,c,d,e,f,g,h,i)

else:

print("Escolha incorreta ou escolha já feita !! Escolha novamente !")

benzocriol = benzocriol - 1

elif benzocriol%2 == 0:

print("Maquina jogou !!")

if ((b==c) and (a == ' ') and (b == 'X')) or ((e==i)and (a == ' ') and (e == 'X')) or ((d==g)and (a == ' ') and (d == 'X')) :

a = 'X' #Essas são condiçoes de vitoria(Se posicoes == X), a maquina(X) tem q ganhar o jogo se tiver condições de fechar

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==c) and (b == ' ') and (a == 'X')) or ((e==h)and (b == ' ') and (e == 'X')) :

b = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==B) and (c == ' ') and (a == 'X')) or ((g==e) and (c == ' ') and (g == 'X')) or ((f==i) and (c == ' ') and (f == 'X')) :

c = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((e==f) and (d == ' ') and (e == 'X')) or ((a==g) and (d == ' ') and (a == 'X')) :

d = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==i) and (e == ' ') and (a == 'X')) or ((b==h) and (e == ' ') and (b == 'X')) or ((c==g) and (e == ' ') and (c == 'X')) or ((d==f) and (e == ' ') and (d == 'X')) :

e = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((d==e) and (f == ' ') and (d == 'X')) or ((c==i) and (f == ' ') and (c == 'X')) :

f = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==d) and (g == ' ') and (a == 'X')) or ((e==c) and (g == ' ') and (e == 'X')) or ((h==i) and (g == ' ') and (h == 'X')) :

g = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((g==i) and (h == ' ') and (g == 'X')) or ((b==e) and (h == ' ') and (b == 'X')) :

h = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((g==h) and (i == ' ') and (g == 'X')) or ((a==e) and (i == ' ') and (a == 'X')) or ((c==f) and (i == ' ') and (c == 'X')) :

i = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((b==c) and (a == ' ') and (b == 'O')) or ((e==i)and (a == ' ') and (e == 'O')) or ((d==g)and (a == ' ') and (d == 'O')) :

a = 'X' #Aqui - após as condições de vencer (Se posicoes =='O') - estão as condições de defesa, a maquina tem q se defender se ouver possibilidade de vitoria do player

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==c) and (b == ' ') and (a == 'O')) or ((e==h)and (b == ' ') and (e == 'O')) :

b = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==B) and (c == ' ') and (a == 'O')) or ((g==e) and (c == ' ') and (g == 'O')) or ((f==i) and (c == ' ') and (f == 'O')) :

c = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((e==f) and (d == ' ') and (e == 'O')) or ((a==g) and (d == ' ') and (a == 'O')) :

d = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==i) and (e == ' ') and (a == 'O')) or ((b==h) and (e == ' ') and (b == 'O')) or ((c==g) and (e == ' ') and (c == 'O')) or ((d==f) and (e == ' ') and (d == 'O')) :

e = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((d==e) and (f == ' ') and (d == 'O')) or ((c==i) and (f == ' ') and (c == 'O')) :

f = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((a==d) and (g == ' ') and (a == 'O')) or ((e==c) and (g == ' ') and (e == 'O')) or ((h==i) and (g == ' ') and (h == 'O')) :

g = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((g==i) and (h == ' ') and (g == 'O')) or ((b==e) and (h == ' ') and (b == 'O')) :

h = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

elif ((g==h) and (i == ' ') and (g == 'O')) or ((a==e) and (i == ' ') and (a == 'O')) or ((c==f) and (i == ' ') and (c == 'O')) :

i = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

else:

while True : #Se não houver situacoes de vitoria ou defesa,a maquina sorteia uma posicao, confere se ela esta vazia e imprime

escolha = random.randrange(1,10)

if escolha == 1 and a == ' ' :

a = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break #O loop é eterno, até que o sorteio seja valido

if escolha == 2 and b == ' ' :

b = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 3 and c == ' ' :

c = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 4 and d == ' ' :

d = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 5 and e == ' ' :

e = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 6 and f == ' ' :

f = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 7 and g == ' ' :

g = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 8 and h == ' ' :

h = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if escolha == 9 and i == ' ' :

i = 'X'

jogo_velha(a,b,c,d,e,f,g,h,i)

break

if ((a==b)and(a==c) and (a == 'O')) or ((d==e)and(d==f)and (d == 'O')) or ((g==h)and(g==i)and (g == 'O')) or ((a==d)and(a==g)and (a == 'O')) or ((b==e)and(b==h)and (b == 'O')) or ((c==f)and(c==i)and (c == 'O')) or ((a==e)and(a==i)and (a == 'O')) or ((c==e)and(c==g)and (c == 'O')) :

print("%s é o vencedor") %(nome1)

velha = 0

break

elif ((a==b)and(a==c) and (a == 'X')) or ((d==e)and(d==f)and (d == 'X')) or ((g==h)and(g==i)and (g == 'X')) or ((a==d)and(a==g)and (a == 'X')) or ((b==e)and(b==h)and (b == 'X')) or ((c==f)and(c==i)and (c == 'X')) or ((a==e)and(a==i)and (a == 'X')) or ((c==e)and(c==g)and (c == 'X')) :

print("Maquina venceu!! Ai que burrico...Kkkk....")

velha = 0

break

benzocriol = benzocriol + 1

if velha <> 0:

print("Velha")

print("Modo de jogo :")

print("1 - Single Player")

print("2 - Multiplayer")

modo_jogo = input()

if modo_jogo == 1 :

singleplayer(a,b,c,d,e,f,g,h,i,velha)

elif modo_jogo == 2 :

multiplayer(a,b,c,d,e,f,g,h,i,velha)

#Menu de escolhas com chamada das funções, acima

>> Grato desde já pela boa vontade de ler e pelas opiniões expostas

Editado por Michelcyc
Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...