Share your repls and programming experiences

← Back to all posts
Tic Tac Toe (now with versus ai mode!!)
h
CodingCactus (3031)

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 :)

Commentshotnewtop
Coder100 (8574)

Great work!
Lol thanks for the credit! :)

Mahmoudk3m (10)

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

ArielFuxman (10)

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.

CodingCactus (3031)

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

[deleted]

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

CodingCactus (3031)

@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?

[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

CodingCactus (3031)

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

[deleted]

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

CodingCactus (3031)

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

ayush2007 (42)

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

ayush2007 (42)

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

CodingCactus (3031)

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

CodingCactus (3031)

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

CodingCactus (3031)

@Imthebestthe maybe you could add some comments

ripull125 (4)

still so easy to beat though :D

CodingCactus (3031)

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

ripull125 (4)

@CodingCactus nice, now its impossible to beat

CodingCactus (3031)

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

Nerd0000 (5)

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

AmazingMech2418 (910)

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

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

AmazingMech2418 (910)

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

AmazingMech2418 (910)

A better screenshot that shows the whole screen:

AmazingMech2418 (910)

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

CodingCactus (3031)

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

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

AmazingMech2418 (910)

@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...

AmazingMech2418 (910)

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

CodingCactus (3031)

@AmazingMech2418 it's called impossible mode for a reason

AmazingMech2418 (910)

@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.

AmazingMech2418 (910)

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

CodingCactus (3031)

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

AmazingMech2418 (910)

@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...

CodingCactus (3031)

@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

AmazingMech2418 (910)

@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.

ad101lab (0)

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?

CodingCactus (3031)

@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

ad101lab (0)

Maybe Tic Tac Toe algorithm instead of AI @CodingCactus

xxpertHacker (388)

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.

CodingCactus (3031)

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

xxpertHacker (388)

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

CodingCactus (3031)

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

xxpertHacker (388)

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

CodingCactus (3031)

@StudentFires how would you word the input prompt?

xxpertHacker (388)

@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".

me002 (7)

2,1 dosen't work at all

me002 (7)

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

me002 (7)

I beat impossible again, after many draws.

me002 (7)

I have defeated it in impossible mode:

me002 (7)

@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.

CodingCactus (3031)

@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?)

me002 (7)

@CodingCactus Not certain, I believe it was.

me002 (7)

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

CodingCactus (3031)

@me002 Try and beat it now. hehe

:)

me002 (7)

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

CodingCactus (3031)

@me002 you can if you go 2,2 first

me002 (7)

@CodingCactus it deletes it later, you are cheating.

CodingCactus (3031)

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

CodingCactus (3031)

@me002 ok thanks, try now
should be fixed

CodeLongAndPros (1364)

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.

CodingCactus (3031)

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

CodeLongAndPros (1364)

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

ZestyLad (11)

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

CodingCactus (3031)

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

ZestyLad (11)

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

JacksonPhilips (62)

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!

CodingCactus (3031)

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

JacksonPhilips (62)

@CodingCactus Oh, okay

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

CodingCactus (3031)

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

JacksonPhilips (62)

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

CodingCactus (3031)

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

CodingCactus (3031)

@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.

JacksonPhilips (62)

@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
CodingCactus (3031)

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

so that only one game board is shown.

JacksonPhilips (62)

@CodingCactus It does clear.

Sorry about that, I forgot you could add files. :facepalm:

CodingCactus (3031)

@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
generationXcode (265)

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

CodingCactus (3031)

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

EthanCulp2 (4)

@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

CodingCactus (3031)

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

Edit: done x,y thing

CodingCactus (3031)

@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.

EthanCulp2 (4)

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

CodingCactus (3031)

@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

Wilke000 (485)

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

Wilke000 (485)

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

CodingCactus (3031)

@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.

Wilke000 (485)

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

CodingCactus (3031)

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

Wilke000 (485)

K ask how to do it on ask @CodingCactus

CodingCactus (3031)

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

CodingCactus (3031)

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

Wilke000 (485)

Very funny @CodingCactus hard is impossible!!!!

CodingCactus (3031)

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

CodingCactus (3031)

@IndyCarter Try impossible mode, I beat hard mode

CodingCactus (3031)

@IndyCarter it should hopefully actually be impossible :)

Wilke000 (485)

Tied again in impossible @CodingCactus

nhatnam2 (2)

Good code. Kind of messy but good code.

CodingCactus (3031)

I'll try and clean it up @nhatnam2

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

nhatnam2 (2)

@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.

Thecrowbar1234 (136)

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

CodingCactus (3031)

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