This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-09-10 21:49:56

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Tic Tac Toe in Javascript

Tic Tac Toe (Easy)
Tic Tac Toe (Medium)
Tic Tac Toe (Hard)
Tic Tac Toe (Expert) (unbeatable)

In some of my free time during the past months (especially the summer), I've been taking the time to learn javascript - a language used to make web pages interactive on your browser. The main resources I have used to learn the language are w3schools and Codecademy.

Although the language isn't that hard to learn in my opinion, there's quite a lot you can do with it. And so to practice my skills I decided to make a Tic Tac Toe game with it. Part of my inspiration for doing this was also this xkcd comic. I figured that if I wanted to try making game AIs, Tic Tac Toe would be the first game I should try.

It was overall quite a fun project to make. Once I got the buttons working, all there was left to do was code the main game logic. The AI was relatively easy to make, except for making sure it avoided certain types of "forks".

What do you guys think? Are you able to beat the different levels?  tongue  And have you ever tried coding in javascript? If you've found a bug or you're interested in how it works, feel free to ask.  smile

p.s. Thanks to fg123 for some help with the coding.

Last edited by Lucario621 (2012-09-10 21:52:11)


http://i.imgur.com/WBkM2QQ.png

Offline

 

#2 2012-09-10 22:21:16

adriangl
Scratcher
Registered: 2007-07-02
Posts: 1000+

Re: Tic Tac Toe in Javascript

Pretty cool, been a while since I've seen one of your collaborations with fg123  tongue


Scratchin' since 2007

Offline

 

#3 2012-09-10 22:31:45

fg123
Scratcher
Registered: 2008-11-13
Posts: 1000+

Re: Tic Tac Toe in Javascript

adriangl wrote:

Pretty cool, been a while since I've seen one of your collaborations with fg123  tongue

Not really a collab. I just helped him with pieces of code here and there, but hey.

And it's been a while since I've heard you!  tongue


Hai.

Offline

 

#4 2012-09-10 22:36:48

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Tic Tac Toe in Javascript

I won unbeatable! This is very good  smile

EDIT: I am learning Javascript also, with Codeacademy. I know the basics but I still want to learn how to use it on webpages  smile

Last edited by jji7skyline (2012-09-10 22:37:30)


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#5 2012-09-10 22:38:53

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Tic Tac Toe in Javascript

I didn't see a difference whilst playing hard and veggie modes.


Posts: 20000 - Show all posts

Offline

 

#6 2012-09-10 22:46:19

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Tic Tac Toe in Javascript

jji7skyline wrote:

I won unbeatable! This is very good  smile

EDIT: I am learning Javascript also, with Codeacademy. I know the basics but I still want to learn how to use it on webpages  smile

Wow, really? Can you tell me how? xD I couldn't figure out how to beat it myself, but if there's a certain tactic that works, then consider it unintended.  tongue


http://i.imgur.com/WBkM2QQ.png

Offline

 

#7 2012-09-10 22:46:56

Wickimen
Scratcher
Registered: 2009-08-02
Posts: 1000+

Re: Tic Tac Toe in Javascript

veggieman001 wrote:

I didn't see a difference whilst playing hard and veggie modes.

Hail the exprt!1  yikes

Last edited by Wickimen (2012-09-10 22:47:02)


xDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD

Offline

 

#8 2012-09-11 00:39:40

kayybee
Scratcher
Registered: 2009-12-07
Posts: 1000+

Re: Tic Tac Toe in Javascript

How to beat expert:
1. Center
2. If gone on side, restart. (it may be possible to beat it like this, but I don't have time yet to figure it out). If in corner, go in direct opposite corner.
3. If made 2 in a row, block. If not, go to the place to make a fork.
4. Win.

Does this work?

Offline

 

#9 2012-09-11 00:50:15

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Tic Tac Toe in Javascript

kayybee wrote:

How to beat expert:
1. Center
2. If gone on side, restart. (it may be possible to beat it like this, but I don't have time yet to figure it out). If in corner, go in direct opposite corner.
3. If made 2 in a row, block. If not, go to the place to make a fork.
4. Win.

Does this work?

It does.


Posts: 20000 - Show all posts

Offline

 

#10 2012-09-11 01:18:19

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Tic Tac Toe in Javascript

It's only sometimes possible to win though, depending on the opponent's move. Otherwise, a draw is the best possible.


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#11 2012-09-11 01:36:59

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Tic Tac Toe in Javascript

kayybee wrote:

How to beat expert:
1. Center
2. If gone on side, restart. (it may be possible to beat it like this, but I don't have time yet to figure it out). If in corner, go in direct opposite corner.
3. If made 2 in a row, block. If not, go to the place to make a fork.
4. Win.

Does this work?

Yup, it does.


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#12 2012-09-11 07:51:51

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Tic Tac Toe in Javascript

kayybee wrote:

If gone on side, restart. (it may be possible to beat it like this, but I don't have time yet to figure it out)

3. Move in the corner next to the opponent's piece.
4. Opponent will try to avoid your win by placing in opposite corner
5. Place it on the side square which touches both your first and second moves.
6. Win.
wink


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#13 2012-09-11 07:54:17

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Tic Tac Toe in Javascript

@Lucario: I made a perfect, truly unbeatable player: http://pastehtml.com/view/cb9pbi0gs.html
Check it out, it's quite fun.  smile

Last edited by Hardmath123 (2012-09-11 07:57:46)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#14 2012-09-11 08:05:41

nathanprocks
Scratcher
Registered: 2011-04-14
Posts: 1000+

Re: Tic Tac Toe in Javascript

This does not work in Internet Explorer (Chrome is broken).


http://carrot.cassiedragonandfriends.org/Scratch_Signature/randomsig.php
http://trinary.site40.net/images/scratchrank.php?username=nathanprocks&display=small

Offline

 

#15 2012-09-11 08:32:25

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Tic Tac Toe in Javascript

Who....cool!


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#16 2012-09-11 09:29:09

ProgrammingPro01
Scratcher
Registered: 2011-07-30
Posts: 1000+

Re: Tic Tac Toe in Javascript

That's awesome, good job.  big_smile


Bye 1.4!
http://www.reactiongifs.com/wp-content/uploads/2013/04/sobbing.gifhttp://25.media.tumblr.com/tumblr_mbfh6bIamQ1qky11ho1_500.png

Offline

 

#17 2012-09-11 15:37:02

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Tic Tac Toe in Javascript

kayybee wrote:

How to beat expert:
1. Center
2. If gone on side, restart. (it may be possible to beat it like this, but I don't have time yet to figure it out). If in corner, go in direct opposite corner.
3. If made 2 in a row, block. If not, go to the place to make a fork.
4. Win.

Does this work?

Ah, I never tested that! I'll try to fix that when I get a chance.

Hardmath123 wrote:

@Lucario: I made a perfect, truly unbeatable player: http://pastehtml.com/view/cb9pbi0gs.html
Check it out, it's quite fun.

Wow, that's awesome! I like how you made the buttons actually un-clickable once they are activated - something I was a bit too lazy to do.  tongue  I looked at your code, and although I'm unable see how all of it works (since I'm not that experienced), I can tell that there are some differences, like how you store the values (you use a multi-dimensional array, while I use an indexed array). If you're interested, my Tic Tac Toe program actually has some console logging scripts, which you can see with Firebug or something else like the Chrome Dev Tools.

nathanprocks wrote:

This does not work in Internet Explorer (Chrome is broken).

Hmm, I don't know how to help you with that.  hmm  I can't get it to work with my IE9 browser either, but I don't really know why.


http://i.imgur.com/WBkM2QQ.png

Offline

 

#18 2012-09-11 17:49:30

evo12
Scratcher
Registered: 2012-03-17
Posts: 100+

Re: Tic Tac Toe in Javascript

cool!


http://i46.tinypic.com/2qjvqcw.jpg

Offline

 

#19 2012-09-11 18:40:33

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Tic Tac Toe in Javascript

Lucario621 wrote:

nathanprocks wrote:

This does not work in Internet Explorer (Chrome is broken).

Hmm, I don't know how to help you with that.  hmm  I can't get it to work with my IE9 browser either, but I don't really know why.

Because Microsoft sucks at web standards??


Posts: 20000 - Show all posts

Offline

 

#20 2012-09-11 18:50:01

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Tic Tac Toe in Javascript

veggieman001 wrote:

Lucario621 wrote:

nathanprocks wrote:

This does not work in Internet Explorer (Chrome is broken).

Hmm, I don't know how to help you with that.  hmm  I can't get it to work with my IE9 browser either, but I don't really know why.

Because Microsoft sucks at web standards??

I'm not sure about that, but IE does have some weird default settings for javascript, that's for sure...


http://i.imgur.com/WBkM2QQ.png

Offline

 

#21 2012-09-11 19:09:37

soupoftomato
Scratcher
Registered: 2009-07-18
Posts: 1000+

Re: Tic Tac Toe in Javascript

Lucario621 wrote:

veggieman001 wrote:

Lucario621 wrote:


Hmm, I don't know how to help you with that.  hmm  I can't get it to work with my IE9 browser either, but I don't really know why.

Because Microsoft sucks at web standards??

I'm not sure about that, but IE does have some weird default settings for javascript, that's for sure...

Veggie is right. They just sort of
do what they feel


I'm glad to think that the community will always be kind and helpful, the language will always be a fun and easy way to be introduced into programming, the motto will always be: Imagine, Program, Share - Nomolos

Offline

 

#22 2012-09-11 19:48:52

dvd4
Scratcher
Registered: 2010-06-30
Posts: 1000+

Re: Tic Tac Toe in Javascript


I made a mod  big_smile  It's called blook!
http://i49.tinypic.com/16ia63p.png

Offline

 

#23 2012-09-11 21:15:38

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Tic Tac Toe in Javascript

Lucario621 wrote:

kayybee wrote:

How to beat expert:
1. Center
2. If gone on side, restart. (it may be possible to beat it like this, but I don't have time yet to figure it out). If in corner, go in direct opposite corner.
3. If made 2 in a row, block. If not, go to the place to make a fork.
4. Win.

Does this work?

Ah, I never tested that! I'll try to fix that when I get a chance.

Hardmath123 wrote:

@Lucario: I made a perfect, truly unbeatable player: http://pastehtml.com/view/cb9pbi0gs.html
Check it out, it's quite fun.

Wow, that's awesome! I like how you made the buttons actually un-clickable once they are activated - something I was a bit too lazy to do.  tongue  I looked at your code, and although I'm unable see how all of it works (since I'm not that experienced), I can tell that there are some differences, like how you store the values (you use a multi-dimensional array, while I use an indexed array). If you're interested, my Tic Tac Toe program actually has some console logging scripts, which you can see with Firebug or something else like the Chrome Dev Tools.

I set button.disabled to "disabled" (I think).

My code uses a true game tree — for the given position, I play all completions to the end, and select a move which leads to me winning in the end. http://wiki.scratch.mit.edu/wiki/Game_Tree  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#24 2012-09-11 21:59:46

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: Tic Tac Toe in Javascript

Hardmath123 wrote:

My code uses a true game tree — for the given position, I play all completions to the end, and select a move which leads to me winning in the end. http://wiki.scratch.mit.edu/wiki/Game_Tree  smile

That's interesting! I forgot we had a wiki article about that.  tongue  I guess that involves recursion, right?


http://i.imgur.com/WBkM2QQ.png

Offline

 

#25 2012-09-11 22:07:36

thebriculator
Scratcher
Registered: 2011-07-11
Posts: 500+

Re: Tic Tac Toe in Javascript

It doesn't work for me.

but hardmath's does, although it is very slow


.     http://tiny.cc/2cwgpw    http://tiny.cc/viwgpw    http://tiny.cc/iwwgpw

Offline

 

Board footer