Jan-23-2022, 12:55 PM
Greetings to everyone,
I am a student from Spain, so I am sorry if there's some words wrong. I am doing a project for school and now I have to do a bot who plays tic Tac toe. First, I programmed the game and after I watched a video to see how to do the bot ("https://www.youtube.com/watch?v=2Tr8LkyU78c"). Then I changed some parts of my code and some of the video code. Now, the bot "plays" but it shouldn't lose never and he does. The bot plays first. For example, in the sequence: (a1, b2, a2, a3, b1, c1) you can win. Another problem it is that at the beginning, it takes a lot of time to play the first move (more than expected), I don't have a lot of experience and I don¡t know why it happens. However, it's not a big problem, but, If you know why, I will also solve that problem.
I will be grateful for any help.
Daniel G
I am a student from Spain, so I am sorry if there's some words wrong. I am doing a project for school and now I have to do a bot who plays tic Tac toe. First, I programmed the game and after I watched a video to see how to do the bot ("https://www.youtube.com/watch?v=2Tr8LkyU78c"). Then I changed some parts of my code and some of the video code. Now, the bot "plays" but it shouldn't lose never and he does. The bot plays first. For example, in the sequence: (a1, b2, a2, a3, b1, c1) you can win. Another problem it is that at the beginning, it takes a lot of time to play the first move (more than expected), I don't have a lot of experience and I don¡t know why it happens. However, it's not a big problem, but, If you know why, I will also solve that problem.
I will be grateful for any help.
Daniel G
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
possible_choices = [ "a1" , "a2" , "a3" , "b1" , "b2" , "b3" , "c1" , "c2" , "c3" ] init = { "a1" : 0 , "a2" : 0 , "a3" : 0 , "b1" : 0 , "b2" : 0 , "b3" : 0 , "c1" : 0 , "c2" : 0 , "c3" : 0 } def jugadahumano(): jugada = str ( input ( "inserte aqui su jugada del tipo : a1" )) if (jugada in init.keys()) and (jugada in possible_choices): init[jugada] = 1 possible_choices.remove(jugada) else : print ( "su jugada es incorrecta o bien ya hay una ficha sobre esa casilla" ) jugadahumano() def jugadarobi(): bestScore = - 1000 bestMove = 0 for key in init.keys(): if (init[key] = = 0 ): init[key] = 2 score = minimax(init, False ) init[key] = 0 if (score > bestScore): bestScore = score bestMove = key init[bestMove] = 2 def minimax(board, isMaximizing): if (whichmarkwon( 2 )): return 1 elif (whichmarkwon( 1 )): return - 1 elif (checkDraw()): return 0 elif (isMaximizing = = True ): bestScore = - 800 for key in init.keys(): if (init[key] = = 0 ): board[key] = 2 score = minimax(init, False ) board[key] = 0 if (score>bestScore): bestScore = score return bestScore elif (isMaximizing = = False ): bestScore = 800 for key in init.keys(): if (init[key] = = 0 ): init[key] = = 1 score = minimax(init, True ) init[key] = = 0 if (score<bestScore): bestScore = score return bestScore def checkDraw(): for key in init: if init[key] = = 0 : return False return True def whowin(): if (init[ "a1" ] = = init[ "a2" ] = = init[ "a3" ] = = 1 ) or (init[ "b1" ] = = init[ "b2" ] = = init[ "b3" ] = = 1 ) or (init[ "c1" ] = = init[ "c2" ] = = init[ "c3" ] = = 1 ) or (init[ "a1" ] = = init[ "b2" ] = = init[ "c3" ] = = 1 ) or (init[ "c1" ] = = init[ "b2" ] = = init[ "a3" ] = = 1 ) or (init[ "a1" ] = = init[ "b1" ] = = init[ "c1" ] = = 1 ) or (init[ "a2" ] = = init[ "b2" ] = = init[ "c2" ] = = 1 ) or (init[ "a3" ] = = init[ "b3" ] = = init[ "c3" ] = = 1 ): print ( "el ganador de la partida es el humano" ) return True elif (init[ "a1" ] = = init[ "a2" ] = = init[ "a3" ] = = 2 ) or (init[ "b1" ] = = init[ "b2" ] = = init[ "b3" ] = = 2 ) or (init[ "c1" ] = = init[ "c2" ] = = init[ "c3" ] = = 2 ) or (init[ "a1" ] = = init[ "b2" ] = = init[ "c3" ] = = 2 ) or (init[ "c1" ] = = init[ "b2" ] = = init[ "a3" ] = = 2 ) or (init[ "a1" ] = = init[ "b1" ] = = init[ "c1" ] = = 2 ) or (init[ "a2" ] = = init[ "b2" ] = = init[ "c2" ] = = 2 ) or (init[ "a3" ] = = init[ "b3" ] = = init[ "c3" ] = = 2 ): print ( "el ganador es el robot" ) return True elif checkDraw() = = True : print ( "Ha habido empate." ) return True else : return False def whichmarkwon(mark): if (init[ "a1" ] = = init[ "a2" ] = = init[ "a3" ] = = mark) or (init[ "b1" ] = = init[ "b2" ] = = init[ "b3" ] = = mark) or (init[ "c1" ] = = init[ "c2" ] = = init[ "c3" ] = = mark) or (init[ "a1" ] = = init[ "b2" ] = = init[ "c3" ] = = mark) or (init[ "c1" ] = = init[ "b2" ] = = init[ "a3" ] = = mark) or (init[ "a1" ] = = init[ "b1" ] = = init[ "c1" ] = = mark) or (init[ "a2" ] = = init[ "b2" ] = = init[ "c2" ] = = mark) or (init[ "a3" ] = = init[ "b3" ] = = init[ "c3" ] = = mark): return True else : return False print ( "esto es el juego del tres en ralla, primero juegan los unos unos despues los doses" ) while (whowin() = = False ) or (checkDraw = = False ): whowin() jugadarobi() print (init.get( "a1" ), init.get( "a2" ), init.get( "a3" )) print (init.get( "b1" ), init.get( "b2" ), init.get( "b3" )) print (init.get( "c1" ), init.get( "c2" ), init.get( "c3" )) whowin() jugadahumano() |