Log In  

Cart #28197 | 2016-09-08 | Code ▽ | Embed ▽ | No License

It's just a Tic-Tac-Toe game.

Rules :

  • Do a line with 3 cross/circle to win

To add later :

  • AI to play against the player
  • Add music/sound effect (SFX when drawing a cross/circle is pretty annoying)
P#28198 2016-09-08 11:16 ( Edited 2016-09-10 06:52)

Roucasse, years ago I pondered if there was some formula or table in to building an unbeatable Tic Tac Toe. That is, no thinking was required on the part of the computer. And - I solved it.

21673498

Player        Computer
1 | 2 | 3     2 | 1 | 6
--+---+--     --+---+--
4 | X | 6     7 | X | 3
--+---+--     --+---+--
7 | 8 | 9     4 | 9 | 8

The computer always starts, always uses X, and always starts in the center. Then it's the player's turn. From the table you can see that if the player goes to square 1 then it will 'think' and go to square 2.

For square 8, the computer will go to square 9.

There is no way the player can win, the computer does not overwrite its own or the player's move, and the game can only end in a CAT or the computer wins, one or the other.

Tic Tac Toe today is a little trickier. You want to create a situation where you can get a TIC TAC TOE from 2-different ways, so even if your opponent blocks one, you get them on the next. :)

P#28200 2016-09-08 11:31 ( Edited 2016-09-08 15:31)

This is not really fun to play against an unbeatable AI, but if I'm not too lazy (and when I'll have time) I'll do an AI with difficulty levels, and use this to do the impossible AI.

P#28202 2016-09-08 11:56 ( Edited 2018-03-02 13:22)

You might also look at this:

http://www.wikihow.com/Sample/Tic-Tac-Toe-Strategies

Now while Tic Tac Toe is interesting, what might be more challenging and fun would be to enter in the SIZE of the board, not just 3x3 but 3x4, 4x3, 4x4, or even 7x7.

Then you also get to choose how many you might have in a row, column, or diagonally.

So it might be possible to play on a 7x7 board, but the winning condition would be to only get 3 connected.

Makes for a unique and challenging game to play and program ! :)

P#28205 2016-09-08 14:31 ( Edited 2016-09-08 18:31)

What I'd suggest? Randomize the array of spaces for CPU's third option. Then:

IF youcanwin : win
ELSEIF youcanblock : block
ELSEIF space.previous + 1 (available) : space.previous+1
ELSEIF space.previous + 2 (available) : space.previous+2
...

Then since it's semi-random; let the AI go first.

P#28251 2016-09-10 00:12 ( Edited 2016-09-10 04:12)

Nice work. I like the color chalk board color scheme.

P#28254 2016-09-10 02:52 ( Edited 2016-09-10 06:52)

[Please log in to post a comment]