How do i make a ai that plays tick tac toe against you

i want to make a bot/ai that can play tick tac toe

i already have a beginning but it is kinda trash

Repl link:

if(player_2_name != Fore.BLUE + "bot"):  
      player_2_input = int(input(f"\n{white}what is you move {player_2_name} {white}(type number): "))#player 2 move

      player_2_input = random.randint(1, 9)#bot move

Welcome back @JacobBeston!

You can use Replit Bounties to ask for an AI that plays Tic Tac Toe against you.


i am too broke to make a bounty


You could try to look at this RealPython template.


Try using the minimax algorithm.

1 Like

what is the minimax algorithim?

ok I have been reading into it and it looks like it would work but it also looks like it can’t be beaten.

i want to make it to where it is possible to beat the bot but i don’t want it to be too easy

1 Like

never mind I rednecked up a bot

thank yall for trying to help.

By the way, you could use a simple neural network, if you are looking for a challenge.


ok thank you i will try

Im lost I get the gist of it but I don’t know how to code it :man_facepalming:t4:

I’m slightly confused: you don’t get how to code Neural Networks, or you don’t get how to code minimax?

As you probably know, Neural Networks are quite complex, so only do that if you have some time to spare. Minimax is simpler, but it will guarantee that the AI never loses (if you do it right!).
For minimax, there are plenty of resources, and I recommend Daniel Shiffman’s video.
For neural networks, I recommend you take an Object-Oriented approach, defining a class for Neurons, then Layers, and then Networks. Once you get this working, you can start thinking about training methods. I used simulated annealing, a method of minimizing mathematical functions which is based on thermodynamic cooling processes. If you are still confused about the theory, which is understandable, I recommend either 3Blue1Brown or Sebastian Lague’s videos about it.

Video references:
Coding Train Minimax: Tic Tac Toe AI with Minimax Algorithm
3Blue1Brown Neural Networks (First video in a series, more theoretical):
Sebastian Lague (One video, more practically focused):

I hope I was able to help. If you have any more questions, feel free to ask.


This is not what the OP wants, though:


You could potentially “hardwire” the robot to play specific moves which are obvious, such as preventing an instant win, but make random moves in other places, where there are no instant wins available.



The bot would block the top row because it would be an instant win.

You can implement this by checking each row and/or column for the presence of two “X”-es and one blank grid space. This would be easier than a minimax algorithm.


Acknowledged, I was just attempting to provide the pros and cons of each method. Sorry, I am new to this:)


what i was lost about was the neural network because im still kind of a beginner at code

im going to try this method

1 Like

woo hoo my bot is now semi compitent now all I have to do is write down 15 more if statements or maby 16.