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

``````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

else:
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.

2 Likes

i am too broke to make a bounty

2 Likes

You could try to look at this RealPython template.

6 Likes

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.

2 Likes

ok thank you i will try

Im lost I get the gist of it but I donâ€™t know how to code it

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): https://www.youtube.com/watch?v=aircAruvnKk
Sebastian Lague (One video, more practically focused): https://www.youtube.com/watch?v=hfMk-kjRv4c

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

2 Likes

This is not what the OP wants, though:

3 Likes

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.

E.g.:

``````X-X
OO-
---
``````

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.

3 Likes

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

4 Likes

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.

2 Likes