If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Programming "tic-tac-toe" with <canvas><canvas/>

Hi everyone, is this a right place to get help with javascript code? I'm a student from Lithuania and i need to make an artificial intelligence program. I selected a game "tic-tac-toe". I made it in a very simple way, game against friend, but professor doesn't like it, she says that user should be able to play against pc. Problem is that i am not a programmer, and my javascript programming skills is very low. I'm joined here to get any help from JS masters
Folder of my files is attached below.tic-tac-toe.zip
Just extract a folder and run index.html to view it in browser. Code to play against opponent is in file "code1.js" code to play against pc should be in file "code2.js".

What are you using <canvas> for with a simple script like that. I also wouldn't call it artificial intelligence when there are only eleven combinations possible in the entire game.

See http://www.felgall.com/jstip123.htm for a working script that you can examine to see one way to do it (the player wins code is commented out as it will never be run anyway as the best the player can get with that code is a draw). As you can see the script is not very long because there are not that many alternatives to check - most being rotations of one of the eleven positions.

I know there are only eleven possibilities in the game because thirty five years ago a friend and I set up a "matchbox" computer consisting of eleven matchboxes with positions drawn on to with the matchboxe's possible moves drawn in different colours and one or at most two coloured matches inside to select the "computer's" move. So perhaps my friend and I invented intelligent matchboxes since the best you could do against them was a draw - provided you wanted to play the one game they knew how to play.

Users who have thanked felgall for this post:

Thank you for your quick reply. First of all, sorry for my incorrect english (i'm not good in this area). I have already saw this link. But my work was already sent to professor. She saw all style and canvas that i made and said me to make this game able to play against pc in the same style and also with canvas Thats why i came here, hoping that anyone can help me with this...

The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.

I know there are only eleven possibilities in the game because thirty five years ago a friend and I set up a "matchbox" computer consisting of eleven matchboxes ...

Well, abut 52 years ago, Popular Electronics showed how to create a TicTacToe playing machine using nothing but DPST switches and one rotary switch (driven by a stepping motor in the most sophisitcated version).

I didn't have a rotary switch, much less a stepping motor. But I did have a bunch of DPDT switches and some diodes and some Christmas tree lights. So I created a TicTacToe "computer" with those. The worst hack being that, if the computer failed to make a move, you had to then resort to flipping the next switch in a bank of switches that caused it to choose the best still-available square. (Basically, all it did was make sure it blocked the human player. So the computer's first move, for example, had to come from that fixed bank of moves. Needless to say, it chose center square and then corner squares first from that bank.) The other major "trick" to it was that it had (as I recall...that *was* a long time ago!) 4 DPDT switches in the center square and 3 in each of the corner squares. All that to keep the various circuits separated. And, yes, it worked. And of course never lost.

Isn't it amazing how what goes around comes around?

What ever happened to "Hunt the Wumpus"? That would be a better first computer game than TicTacToe.

So I guess it won't matter who shall win the most, but as long as you could show your professor that your javascript machine is capable to learn something then it's done.

As far as I know, the easiest AI to code is called "Genetic Algorithm", and is commonly used to solve "Travelling Salesman" problem and the like. I haven't heard though any implementations of it to "Tic Tac Toe" game.

In which case tic-tac-toe (which a lot of people know as noughts and crosses) is definitely not an AI candidate as there is nothing for the script to learn. There are only 11 basic positions and rotations thereof in the entire game. To set the game up so the computer plays at random to start with and then learns which moves not to make would require hundreds of times as much code as the code needed to just get it to play the best move it can each time. In the amount of code you'd need to have it learn something you can get it to learn something far more complicated than those eleven positions.

Even if AI is not necessary for Tic-Tac-Toe (or Noughts and Crosses) , the small depth of this game allows to develop an algorithm, which explores only some branches of the tree of the positions, and gives the real values of the game.

With a deeper game, like Connect 4, it is moreover necessary to build a function for approximatively evaluate non terminal positions.

Guys, how about making this Noughts and Crosses to play not through 3x3 windows, but whatever player wants (for example 4x4, 6x6, 10x10)? What changes i need to do in my javascript code? Any suggestions?

if you’re ok with never being able to win... if it is PvP then there is not much code to change, but for player vs computer you’ll need some kind of AI.

The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.