Tic Tac Toe with AI - Python Tutorial (Part 1)

preview_player
Показать описание
In this video I explain how to create a tic tac toe game in python using a simple AI. This is an intermediate level tutorial and is extremely useful in enhancing your skills with python and gives a basic introduction to AI. This is a text based tutorial but everything used here can be implemented into a graphical version.

Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud

Please leave a LIKE and SUBSCRIBE for more content!

Tags:
- Tech With Tim
- Python Tutorials
- Tic Tac Toe
- Python
- Python tic tac toe
- Coding tic tac toe in python
- Python tic tac toe tutorial
Рекомендации по теме
Комментарии
Автор

Download the full code from the link below!

TechWithTim
Автор

The code is clearly done by someone new to Python/coding, but it is still a nice Segway for me to get introduce to AI. And everyone need to start somewhere.
Just a short note to whoever is reading this, there is NO AI. This is pure algorithm. If player play at x1, y1 then computer play at x2, y2 kind of

yesme
Автор

i also appreciate that u just let ppl copy u and stuff. u explain so well and is just basically a type-along

firefennec
Автор

Thanks for giving lots of room for us to code ahead by starting with empty functions.

AaronProgZ
Автор

Love the tutorials! Also, for the board, I used for a smaller and more efficient board

LemonChad
Автор

i have made at least three games from u and have learnt a lot from ur channel. keep making these tutorials. ur rlly good

firefennec
Автор

I come here to learn how to make an AI to implement into my already existing tic tac toe program...




And I find that you did almost everything the exact same, just better.

(I'll keep my existing code and put the AI into it while I'm following along, very convenient)

turtlelife
Автор

Thank you! Your tutorial series is simple yet so informative.

braveleuterio
Автор

The only issue I had so far with this was with the is_winner(bo, le) function. Python now makes us use a \ to split a statement onto another line. Other than that, dang man, you're a good teacher. I just did a 22-hour Python boot camp and this is a great follow up. I can't wait to dive into some of your other tutorials. On to video 2. . .

UlyssesOfOmaha
Автор

ur approach is very clear look really easy man...

shivamtyagi
Автор

hi welcome to the tutorial so basically just copy everything i write and gg

cafezinho
Автор

Just finished your Pygame Tutorial and can't wait to follow this. Your channel is gonna be huge

NatCorry
Автор

Please dear expert. Is there somewhere a tic-tac-toe code that plays against itself. And learns from scratch . It progresses beyong human thought . It would be zero players.

MrDavidcanet
Автор

I just have some code fixes that make it way easier to write the code.


First, for your print board function (I used a 2D Array to make it easier):


def printBoard(bo):
for i in range(3):
if i == 0:
print(" ")
else:


for j in range(3):
print("|", end=" ")

if j == 2:
print(bo[i][j], end=" |\n")
else:
print(str(bo[i][j]), end=" ")
if bo[i][j] == " ":
bo[i][j] = 0
print(" ")


Next, for your isWinner function:


def isWinner(board, le):
for i in range(3):
if board[i][0] == le and board[i][1] == le and board[i][2] == le:
return True
if board[0][i] == le and board[1][i] == le and board[2][i] == le:
return True
for i in range(3):
if not board[i][i] == le:
break
if i == 2:
return True
for i in range(3):
if not board[i][2 - i] == le:
break
if i == 2:
return True
return False


It first checks rows and columns and then diagonals. These are just what I used to save myself from boring typing. Anybody who wants to copy this can do so, it does the same thing.

krishnans
Автор

which algorithm is implement to solve this tic tac toe ?

coolpanda
Автор

Is there any particular reason to use this:

run = True
while run:
run = false (when you don't want the loop to repeat)

instead of this, like you used in other loops:

while True:
break


?

thiagoteixeira
Автор

keep on doing what you are doing, keep uploading regularly, I sincerely believe this channel gonna be huge in near future.

milanpaudel
Автор

Its pretty easy to beat. I played it twice and won both times. If this was a perfect AI this should not have happened. Since tic tac toe is solved the AI should always either win or tie.

Janav_Gupta
Автор

how to implement minimax algorithm here?

saitech
Автор

I like this tutorial because you explain everything very clearly! Looking forward to part 2!

Chartcombat