lets make a bot :) (python) series part 2 TIC-TAC-TOE

😎💻 bots r cool


Hi and welcome to this tutorial. We are all disturbed kids and adults here who are very lonely and have nothing but coding with us. We are so lonely that we have nobody to play tictactoe with so we are going to code a bot to play it for us

Now I am not going to ruin the surprise as to where we are going with this series (the next part is the final and the toughest bit...)

Today we are making a bot to play tic tac toe. If you guys are confused as of how to build it you can look at the tic tac toe code I wrote and if you want I can make a tutorial on react js (what I used to make the tic tac toe webpage)

We arent giving it any skill or anything NONONO WE CANT LET IT TAKE OVER THE WORLD OH NO WE CANT STOP STOP STOP. We are going to make it play tic tac toe randomly.

I wrote out the webpage at https://tictactoe--generationxcode.repl.co/ if you want to play. if you're lonely like me you will want to continue through this tutorial and make something to play it for you.

the code for tic-tac-toe bot

Lets hit the road and make something OH YEAH!!!!!

importing stuff

Buh this the most repitive bit but for ppl who are new to this series HERE YOU GO! TAKE THAT CODE SMASH IT! CRUSH IT! MAKE IT BETTER :

Now we have 2 extra imports for this program...

  • time is used so that we can actually see the bot playing tic tac toe or else it will just open the browser play it withing a few seconds and close it. We dont want that so we are going to add some small waiting time between each play and after it ends so we can see the results
  • randint from random - this is for randomly selecting a tile and clicking on it

starting up the driver

this is short for webdriver. If you are new here, go back one tutorial to - https://repl.it/talk/learn/Lets-make-a-python-bot-part-1-for-beginners/43284 if you dint understand a lot of what I'm saying :)

now here the link is different so this is the code you will be putting in:

ok now we have got the page up and running

get the tic tac toe elements

Last tutorial I didnt explain the getting the path of the elements very well so I mean to patch that gap in this one. This is how:

right click on the element and click on "inspect element", then right click on the element in the devtools that just opened. then hover on copy, that opens a list of actions, click on "copy xpath".

For this one I have yall covered and I copied all the xpaths for you, but if you want you can do that yourself :)

Now that thats done we need these in an array. This is what I did:

getting the cell that tells us when to leave

Once X or O has won we got to yeet(leave) out of the browser
here is the element for winning:

now that we got that lets go to the fun stuff

clicking a random element

We have to randomly click an element, however we cant click one thats been clicked before so this is how we are going to do it:
we are going to write a function for clicking the element and call it random_click. If the element has already been clicked (we will know this by going through a list of already clicked element indexes(their positions in the element array we just created)) we will run the function again from inside the function. We are going to call the subroutine from itself.

My computer teacher has told me not to do this cus it reduces the speed of the program but Ima do it anyway lol.

here is the code:

oki now we done with that bit lets get to the MOST BORING BIT

clicking it

now we are going to run a loop to click on the tic tac toe randomly using the function we created before
the bad bit is that we have to stop the program when one of the players win (X or 0)
that we will understand by checking whether the text in the winner element (look up in the tutorial) is the same or is saying that X or O has won

Any normal person will run this loop for 8 times but me? I am a disturbed and dumb idiotic coder so me do it for 20 times because why not?

here is the code:

now we got that done

printing out the winner:

we already do this but for any winner to make the loser feel uncomfortable and hurt and sad and make them sob till they quit they have to say they have won many, many , many more times than any sane human. So this is how we do it:

this gets the text inside the winner element

closing the browser

all insane idiotic things must end even if they are bots - me(graph egg)

we got to finish this sickness now after we have made it. BUT HOW? AND WHY? to the second question I answer - why not? (checkmate) and to the first , here the code:

we need some time for the browser to sleep before dying. GOVE IT SOME REST BEFORE ETERNAL REST OK?


the end

I hope you learnt something from this tutorial and I hope all yall are safe in your homes away from the madness of 2020. And thats the end of my disturbing tutorial. You must be thinking, when WILL this mad idiot STOP? to be honest idk rlly. But I hope not very soon.

also do all this locally, doesnt work in replit


the bot in action ^

all the code:

Thats it for today and happy coding

^ that's me right there, no a bit to the side now a bit right - yes! Right there in the middle!

You are viewing a single comment. View All

Thats the tutorial for today!