joaorafaelmatos 0 Denunciar post Postado Agosto 9, 2018 |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[0][0],mt[0][1],mt[0][2],mt[1][0],mt[1][1],mt[1][2],mt[2][0],mt[2][1],mt[2][2])) def mark(play,mark): #função marca no tabuleiro a posição escolhida if play == '1': mt[0][0] = mark elif play == '2': mt[0][1] = mark elif play == '3': mt[0][2] = mark elif play == '4': mt[1][0] = mark elif play == '5': mt[1][1] = mark elif play == '6': mt[1][2] = mark elif play == '7': mt[2][0] = mark elif play == '8': mt[2][1] = mark elif play == '9': mt[2][2] = 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) def s_play(a,b,c,d,play,e,f): #algoritimo da segunda jogada do cpu (bloqueia a jogada do adversário) 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 = '' cont = 0 #contador do número d jogadas quad_left = ['1','4','7','2','5','8','3','6','9'] #lista dos quadrantes livres 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() while playp not in quad_left: print ('Quadrante Inválido!\n') playp = input('Escolha o quadrante em que deseja marcar:').upper() mark(playp,'X') cont = 1+cont quad_left.remove(playp) try: c_left.remove(playp) except: playp not in c_left print_board(t) if cont == 1: print ('Jogada do adversário:') 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 quad_left.remove(playcpu) c_left.remove(playcpu) print_board(t) print (playcpu) if cont==3 or cont==5 or cont==7: print ('Jogada do adversário:') 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: playcpu = random.choice(quad_left) 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: # playcpu = random.choice(quad_left) # 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: playcpu = random.choice(quad_left) 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 ("--------------------------------------") Compartilhar este post Link para o post Compartilhar em outros sites