Recommended Posts

|Estou tendo problema em resolver um problema quando a cpu joga, na 2ª jogada as vezes ela joga 2 vezes. Não consegui resolver se puderem me ajudar eu agradeço.

Nova = "S"
while Nova == "S":
import random
t = '''
{0} | {1} | {2}
---+---+---
{3} | {4} | {5}
---+---+---
{6} | {7} | {8}

'''
cont1=0;cont2=0 ; vet=[]

def print_board(t): #função imprime o tabuleiro com as marcações atuais
print ("""
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
""")
print (t.format(mt,mt,mt,mt,mt,mt,mt,mt,mt))

def mark(play,mark):    #função marca no tabuleiro a posição escolhida
if play == '1':
mt = mark
elif play == '2':
mt = mark
elif play == '3':
mt = mark
elif play == '4':
mt = mark
elif play == '5':
mt = mark
elif play == '6':
mt = mark
elif play == '7':
mt = mark
elif play == '8':
mt = mark
elif play == '9':
mt = mark

def end_game(a,b,c,d,e,f): #função determina se alguém ganhou o jogo
if mt[a] == 'X' and mt[c][d] == 'X' and mt[e][f] == 'X':
winner = 'Player'
vet.append(winner)
if mt[a] == 'O' and mt[c][d] == 'O' and mt[e][f] == 'O':
winner = 'CPU'
vet.append(winner)
else:
winner = 'Deu velha'
vet.append(winner)

if mt[a] == mt[c][d] and mt[a] == 'X':
if mt[e][f] == ' ' :
playcpu = play
mark(play,'O')
cont3=1

else:
playcpu = 'blank'
return playcpu

winner = ''
c_left = ['1','7','3','9']
c_left2 = ['8','2','6']
mt = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']] #matriz do tabuleiro

print_board(t)
#jogar=input('Digite S para continuar o jogo')
cont = 0
#while jogar=='S':
while cont <= 9 :
playp =input('Escolha o quadrante em que deseja marcar:').upper()
playp = input('Escolha o quadrante em que deseja marcar:').upper()
mark(playp,'X')
cont = 1+cont
try:
c_left.remove(playp)
except:
playp not in c_left
print_board(t)

if cont == 1:
if playp == '1':
playcpu = '9'
mark(playcpu,'O')
elif playp == '9':
playcpu = '1'
mark(playcpu,'O')
elif playp == '7':
playcpu = '3'
mark(playcpu,'O')
elif playp == '3':
playcpu = '7'
mark(playcpu,'O')
else:
playcpu = random.choice(c_left)
mark(playcpu,'O')
cont += 1
c_left.remove(playcpu)
print_board(t)
print (playcpu)

if cont==3 or cont==5 or cont==7:
count1=0
for i in range (0,3):
for j in range (0,3):
if mt.count('O')==1 and mt.count('X')==0:
s_play(i,0,2,0,'1',0,0)
s_play(i,1,1,2,'4',1,0)
s_play(i,1,2,1,'2',0,1)
s_play(i,0,1,2,'5',1,1)
s_play(i,2,2,2,'3',0,2)
s_play(i,0,1,1,'6',1,2)
#else:
mark(playcpu,'O')
#print (playcpu)
break
if mt[j]=='O':
count1=count1+1
if count1==2:
s_play(i,0,j,0,'1',0,0)
s_play(i,1,j,2,'4',1,0)
s_play(i,1,j,1,'2',0,1)
s_play(i,0,j,2,'5',1,1)
s_play(i,2,j,2,'3',0,2)
s_play(i,0,j,1,'6',1,2)
break

else:
s_play(1,0,2,0,'1',0,0)
s_play(0,1,0,2,'1',0,0)
s_play(2,2,1,1,'1',0,0)
s_play(0,0,2,0,'4',1,0)
s_play(1,1,1,2,'4',1,0)
s_play(2,2,2,1,'4',1,0)
s_play(0,2,1,1,'7',2,0)
s_play(2,1,2,2,'7',2,0)
s_play(0,0,1,0,'7',2,0)
s_play(0,1,1,1,'8',2,1)
s_play(1,1,2,1,'2',0,1)
s_play(0,0,0,2,'2',0,1)
s_play(1,0,1,2,'5',1,1)
s_play(0,1,2,1,'5',1,1)
s_play(2,0,2,2,'8',2,1)
s_play(0,2,2,0,'5',1,1)
s_play(0,0,2,2,'5',1,1)
s_play(0,2,1,2,'9',2,2)
s_play(1,2,2,2,'3',0,2)
s_play(0,0,0,1,'3',0,2)
s_play(1,0,1,1,'6',1,2)
s_play(2,0,2,1,'9',2,2)
s_play(2,0,1,1,'3',0,2)
s_play(0,0,1,1,'9',2,2)
s_play(0,0,1,1,'3',0,2)
s_play(0,2,2,2,'6',1,2)
break
cont += 1
print_board(t)
print (playcpu)

#      else:
#      mark(playcpu,'O')
#     print (playcpu)

if cont >=4:
end_game(0,0,0,1,0,2)
end_game(1,0,1,1,1,2)
end_game(2,0,2,1,2,2)
end_game(0,0,1,0,2,0)
end_game(0,1,1,1,2,1)
end_game(0,2,1,2,2,2)
end_game(0,0,1,1,2,2)
end_game(2,0,1,1,0,2)
#else:
mark(playcpu,'O')
print (playcpu)

if vet.count('Player')>=1:
winner='Player'
break
if vet.count('CPU')>=1:
winner='CPU'
break
else:
winner='Deu velha'
if cont==9:
break

print (winner)
Nova = input("Para jogar novamente digite S ")
print ("--------------------------------------")

Create an account

Register a new account

• AES - instrução WHERE da consulta

By mmalainho, in MySQL

• Formulário condicional

By Hudson Medeiros, in .NET

• WebAPI REST - SELECT através do método GET

By Neto Dinizz, in .NET

×