Skip to content
← Back to Community
Tic Tac Toe (now with versus ai mode!!)
Profile icon
h
has Hacker Plan
CodingCactus

The classic game of Tic Tac Toe


2 Modes:

  • CPU; play against computer (now with three difficulty settings)
    • Easy
    • Hard
    • Impossible
      • If you manage to beat impossible mode please comment with
        proof and how you did it.
  • 2 Player; play with friends

Aim of the game:

  • Get 3 of your symbols in a row

How to play:

  • Enter coordinate of place that you would like put your symbol (1,1 (column, row))

Thanks to:

But mainly extremely large thanks to @Coder100 for explaining them!!


Have fun and if you do: make sure to upvote! :)


edit: Now has colours :)

edit 2: Now has difficulty levels :)

Edit 3: Now has Impossible difficulty level :)

Voters
Profile icon
vrv
Profile icon
LuisGustavo17
Profile icon
RichardHorak
Profile icon
GoodFrom
Profile icon
nathanielfrede2
Profile icon
DanielRayyan1
Profile icon
JohnRuzzel
Profile icon
KimSante
Profile icon
AnelSecic
Profile icon
Faze-IconicIcon
Comments
hotnewtop
Profile icon
Coder100

Great work!
Lol thanks for the credit! :)

Profile icon
Pacomatic

Noice

Profile icon
CodingCactus

@Pacomatic Thank you

:)

Profile icon
Mahmoudk3m

Capture :3
1,2-2,1-1,1-1,3 Guaranteed win

Profile icon
CodingCactus
Profile icon
Mahmoudk3m

@CodingCactus works vs human beings too lol

Profile icon
CodingCactus

@Mahmoudk3m hehe lol

:)

Profile icon
CodingCactus

@Mahmoudouk3m, try and beat impossible mode now :) :) :) :)

Profile icon
Mahmoudk3m

@CodingCactus I failed :") Great work !!

Profile icon
CodingCactus
Profile icon
AmazingMech2418

@Mahmoudk3m There is an easier way...

Profile icon
ArielFuxman

Made this one a while ago, 1 month into programming

Good job mate
I suggest you learn about backtracking and try to design the computer's moves using backtracking.

Profile icon
CodingCactus

@ArielFuxman Ok, glad you liked it, I will try and learn about that. Thanks for the suggestion:)

Profile icon
[deleted]

I wa thinking, maybe add more intelligence in the artificial intelligence? Try making the computer smarter, and make it control what it does.

Profile icon
CodingCactus

@CodeABC123 I'm not really sure on how to do that, would it just be adding more possibilities to the CPU and making it more proactive?

Profile icon
[deleted]

So it’s kind of like this:
So, maybe
AI = O , P1 = X

X O X

Mane you can teach the computer to add the symbol where it is needed most, in this case, the bottom left. If you want to go another step, you can make the computer predict what the person will do next.
Hope that makes sense.
@CodingCactus

Profile icon
CodingCactus

@CodeABC123 The computer already would go bottom left, because it must protect from the player winning

Profile icon
[deleted]

Then make it predict what the person would do next. @CodingCactus

Profile icon
CodingCactus

OK, I'll try and do that tomorrow. Thanks for the suggestion @CodeABC123

Profile icon
CodingCactus

@CodeABC123 how is it now?

Profile icon
CodingCactus

@CodeABC123 Now has difficulty levels.

Profile icon
ayush2007

coding cactus I think i know how u made impossible really impossible

Profile icon
ayush2007

@coding cactus I want to play this so bad but when I am entering difficulty level its saying invalid pls tell what to do
Screenshot (124)

Profile icon
CodingCactus

@ayush2007 just type in the number of the difficulty (1/2/3)

Profile icon
Imthebestthe

what do you think of my code?
https://repl.it/@Imthebestthe/tic-tac-toe

Profile icon
CodingCactus

@Imthebestthe erm, it looks a bit complicated, so I'm not sure what's going on really.

Profile icon
CodingCactus

@Imthebestthe maybe you could add some comments

Profile icon
ripull125

still so easy to beat though :D

Profile icon
CodingCactus

@ripull125 Now has difficulty levels, try to beat hard :)

Profile icon
ripull125

@CodingCactus nice, now its impossible to beat

Profile icon
CodingCactus

:)

Profile icon
CodingCactus

@ripull125 Actually, I did manage to beat it, see above decription

Profile icon
[deleted]

I loved this one, i can't beat the impossible XD

Profile icon
AmazingMech2418

The CPU in impossible mode knows every known Tic-Tac-Toe strategy there is! It's like I'm playing against myself! LOL! How do you not tie with the impossible mode CPU? Wait, what is this? It seems like a screenshot I just took which says I beat the impossible mode CPU...
image

On a completely unrelated note, don't look at my repl titled "test"

Profile icon
AmazingMech2418

LOL! I tried command injection, but you sanitized your inputs. ... so I did something else...

Profile icon
AmazingMech2418

A better screenshot that shows the whole screen:
image

Profile icon
AmazingMech2418

I would go ahead and say how I did it, but I kind of what to see how you react when you see the screenshot... However, the screenshot itself should have a huge giveaway...

Profile icon
CodingCactus

@AmazingMech2418 you have hacker, so just make test private (I didn't look)

What did you do? Did you cheat or was it legit?

Profile icon
AmazingMech2418

@CodingCactus I kind of stopped the program, opened a terminal using os.system('bash'), created a simple express server to host an alternate version of your python program, replaced all "Easy Mode" instances with "Impossible Mode", and used the curl command and piped the result into main.py. I then started it and selected 1 for easy mode, beat the CPU in easy mode, and then it said "Impossible Mode" at the top and took a screenshot...

"test" is the simple server by the way...

Profile icon
CodingCactus

@AmazingMech2418 ok... Bit over the top lol

Profile icon
AmazingMech2418

@CodingCactus Well, any other way to edit it would create a fork and then the screenshot wouldn't show it as your repl...

Profile icon
CodingCactus

@AmazingMech2418 it's called impossible mode for a reason

Profile icon
AmazingMech2418

@CodingCactus Yeah. I know, but it is possible if you just replace the Python code with an alternate version using the curl command in bash... However, to win without hacking, it is actually impossible because it knows every strategy for Tic-Tac-Toe that there is.

Profile icon
AmazingMech2418

@CodingCactus Though do you like my method of winning? I technically beat the CPU, but just in a different mode than what it displayed.

Profile icon
CodingCactus

@AmazingMech2418 well, anyone can beat easy mode soooo...

Profile icon
AmazingMech2418

@CodingCactus Yeah... Though hard mode is also fairly easy to beat, but I was trying to go somewhat quick and it seems to have some randomized features that caused a tie instead...

Profile icon
CodingCactus

@AmazingMech2418 lol. thanks for playing anyway

Profile icon
CodingCactus

@AmazingMech2418 yea, on hard mode it doesn't detect one of the win methods so it is easier to beat with that method, if it doesn't detect anything then it just goes in a random place, easy mode is just looking to stop lines which already have 2 in, so doesn't think ahead

Profile icon
AmazingMech2418

@CodingCactus You're welcome! I actually tried every single strategy I know in impossible mode before hacking it though... Great job on your AI! I use the same defensive methods as your AI uses in real games of Tic-Tac-Toe and since figuring out all of the Tic-Tac-Toe strategies, I have never lost unless I was tired and accidentally ignored the other player's move, so I actually think this AI is impossible to beat.

Profile icon
ad101lab

I would say that this is more an algorithm than an AI using TensorFlow or PyTorch to predict the best move, good job! How long did this take you, just wondering?

Profile icon
CodingCactus

@ad101lab I am aware that it isn't ai, just couldn't think of something else to call it.
took like 4 hours of actual coding

Profile icon
ad101lab

Maybe Tic Tac Toe algorithm instead of AI @CodingCactus

Profile icon
xxpertHacker

For maximum flexibility of the user interface, can the users be given the option to say "xy" or "yx" at the start? To determine how graphing user input works. Also, the x,y syntax is way too strict, maybe determine alternatives? x y, x, y, x ,y.

That is, if you are capable of doing this.

Profile icon
CodingCactus

@StudentFires why? It does clearly state how you should input your selections

Profile icon
xxpertHacker

@CodingCactus Because, why not? Giving the user as much control as possible is always the best solution, isn't it?

Profile icon
CodingCactus

@StudentFires I feel that it's alright the way it is :)

Profile icon
xxpertHacker

@CodingCactus But humans like "control," (or the illusion) so I implement this design philosophy where ever possible.

Profile icon
CodingCactus

@StudentFires how would you word the input prompt?

Profile icon
xxpertHacker

@CodingCactus Hmm... "What format do you prefer to plot a point using? Type "xy" or "vh" for the first number to be horizontal and the second number to be vertical, or vice versa, if you don't know what this means, type "default" to use the default"

Default will by "xy".

Profile icon
me002

2,1 dosen't work at all

Profile icon
me002

is is broken, try 1,3 then 2,1

Profile icon
me002

image
victory

Profile icon
me002

I beat impossible again, after many draws.

image

Profile icon
me002

I have defeated it in impossible mode:
image

Profile icon
me002

@CodingCactus, I did just realize that this looks fake, because I did not include the link in the picture. a way you could check is by looking through my account to see if I have mad a file, but I swear it is real.

Profile icon
CodingCactus

@me002 thank you for showing this, what moves did you do? I want to make it completely impossible. (Was it 2,3 then 1,2 then 1,3 then 1,1?)

Profile icon
me002

@CodingCactus Not certain, I believe it was.

Profile icon
CodingCactus
Profile icon
me002

@CodingCactus I've spent way too much time beating/trying to beat. your machine

Profile icon
CodingCactus
Profile icon
CodingCactus

@me002 Try and beat it now. hehe

:)

Profile icon
me002

@CodingCactus it just dosen't work. You won't let me use 2,2?

Profile icon
CodingCactus

@me002 you can if you go 2,2 first

Profile icon
me002

@CodingCactus it deletes it later, you are cheating.

Profile icon
CodingCactus

@me002 does it? It shouldn't please send me a picture

Profile icon
me002

@CodingCactus
image
2,1 1,3

Profile icon
CodingCactus

@me002 ok thanks, try now
should be fixed

Profile icon
CodeLongAndPros

This is cool, but I have one suggestion: Could you either make the ordered pair be split along char (33) or along whitespace (3 3)? Again, this is really cool.

Profile icon
CodingCactus

@CodeLongAndPros I could do it so that you can type in anything as long as the first and last index's are the numbers

Profile icon
CodeLongAndPros

@CodingCactus Thanks, that would be nice.
I found myself hitting space and getting "Invalid Input".

Profile icon
DUDEMASTER169

this is pretty cool.

Profile icon
CodingCactus
Profile icon
AdriaDonohue

#cool

Profile icon
CodingCactus

@AdriaDonohue thanks


:)

Profile icon
AdriaDonohue
Profile icon
ZestyLad

Really cool lol! But is there a shorter way to write this?

Profile icon
CodingCactus

@ZestyLad umm, not sure, don't think so. Though there probably is

Profile icon
ZestyLad

So the "AI" is a bunch of if and elif statements right?

Profile icon
CodingCactus
Profile icon
jackou

The input threw me off...For example, I put in:
2, 1
Because x goes first, y comes after.
But it put it under 1, 2 instead.
Otherwise, great game!

Profile icon
CodingCactus

@JacksonPhilips I changed it after a suggestion, It now works like a graph's coordinates (along the corridor, up the stairs (X,y))

Profile icon
jackou

@CodingCactus Oh, okay

I just noticed (column, row). Sorry for wasting your time :P

Profile icon
CodingCactus

@JacksonPhilips Its alright, which do you think is better (x,y or y,x). I want to make it as intuitive as possible

Profile icon
jackou

@CodingCactus In my personal opinion, (x, y) is more natural to me. But, if you want to keep it, that's fine too.

Profile icon
CodingCactus

@JacksonPhilips X,y is what it is at the moment

Profile icon
jackou

@CodingCactus https://imgur.com/a/BJjexZI
This is what it shows for me

Profile icon
CodingCactus

@JacksonPhilips sorry, but is it possible for you to send the actual image rather than the link, because that website is blocked by my parents because apparently it is a social networking site.

Profile icon
jackou

@CodingCactus Can't really do that.. sorry.

But, it's kinda like this:

1 2 3 1 2 3 select which one 2,1 1 2 3 1 x 2 3 o
Profile icon
CodingCactus

@JacksonPhilips hmm, it it should be clearing the screen after each input:

image

so that only one game board is shown.

Profile icon
jackou

@CodingCactus It does clear.
epic
Sorry about that, I forgot you could add files. 🤦

Profile icon
CodingCactus

@JacksonPhilips oh ok, it was just that your thing from earlier showed two on screen, but realise that had to be like that.

Anyway, do think it should be more like one of these

1 2 3 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3
Profile icon
Pygrammar

WOW

Profile icon
generationXcode

Good job. I wouldn't call it AI tho...

Profile icon
CodingCactus

@generationXcode ik, but I couldn't think of what else to call it in the title

Profile icon
generationXcode

@CodingCactus I guess ur right. Hehe

Profile icon
CodingCactus
Profile icon
KobeFF

Cool <3

Profile icon
EthanCulp2

@CodingCactus I have some suggestions. If it ends in a draw, it doesn't end the game, it just keeps asking us for input and says all input is invalid. Also, you should make the coordinate system go x,y not y,x

Profile icon
CodingCactus

@EthanCulp2 Why wouldn't I want it to end the game? I'll do the xy thing

Edit: done x,y thing

Profile icon
CodingCactus

@EthanCul2 oh, did you mean that sometimes it doesn't end the game in a draw. I am aware of that scenario and am trying to fix it

Edit: It should be fixed now, please let me know if you see it again.

Profile icon
EthanCulp2

Yes I did mean that, thanks. Just curious, what scenarios would cause the game not to end in a draw? @CodingCactus

Profile icon
CodingCactus

@EthanCulp2 sometimes the and statements didn't have the correct brackets on the cpu input, causing it to place its marker on a square which had already been placed on, which stopped it coming to draw (for some reason?). But should be fixed now (hopefully). I haven't experienced the problem again yet

Profile icon
Wilke000

Amazing great gob @CodingCactus
Though I would suggest difficulties
I don’t know how to do them that well though :(

Profile icon
Wilke000

Actually I don’t know how to do them at all but I bet someone out there does!

Profile icon
CodingCactus

@IndyCarter I wouldn't know how to do that, I remember @CyanCoding I think doing that, but it was like 2600+ lines in C++, so I don't think I'd be able to do that.

Profile icon
Wilke000

Yeah I was about to mention @CyanCoding @CodingCactus
You should copy/paste code

Profile icon
Wilke000

But first ask him @CodingCactus

Profile icon
Wilke000

Just import C++ @CodingCactus

Profile icon
CodingCactus

@IndyCarter I'm not going to just copy someone, if I was to do it, I would work it out myself

Profile icon
Wilke000

K ask how to do it on ask @CodingCactus

Profile icon
CodingCactus

@IndyCarter I'm happy with where it is now and I want to start a different project, but thanks for the suggestion.

Profile icon
Wilke000

ur welcome @CodingCactus

Profile icon
CodingCactus

Hi, @IndyCarter! I have added difficulty levels, thanks for suggestion. Try hard difficulty level :)

Profile icon
Wilke000
Profile icon
Wilke000

Very funny @CodingCactus hard is impossible!!!!

Profile icon
Wilke000

And I love it @CodingCactus!!!!
lol

Profile icon
CodingCactus

@IndyCarter

THANKS :)

Profile icon
CodingCactus

@IndyCarter I did just beat it though (only once). Just realise I broke it temporarily, which is why I was able to win.

Profile icon
Wilke000

Very funny @CodingCactus
lol bro

Profile icon
Wilke000
Profile icon
CodingCactus

@IndyCarter Try impossible mode, I beat hard mode

Profile icon
Wilke000
Profile icon
CodingCactus

@IndyCarter it should hopefully actually be impossible :)

Profile icon
Wilke000

Tied again in impossible @CodingCactus

Profile icon
nhatnam2

Good code. Kind of messy but good code.

Profile icon
CodingCactus

I'll try and clean it up @nhatnam2

edit: done it, hope it's more readable now?

Profile icon
nhatnam2

@CodingCactus something that would really help is adding files. make your main.py the main file that commands the game while having everything else that runs the game in different files.

Profile icon
[deleted]

you didn't need to give me credit. But it flatters me.

Profile icon
CodingCactus

@Thecrowbar1234 i feel like I've had this conversation before 🤔😂