August 23, 2009

Javascript Reversi

OthelloThere should be more Javascript games. Programming in Flash requires either bushwhacking through tools like MTASC or buying a commercial tool from Adobe. Javascript is more like BASIC on the Apple II or TRS 80: it is a programming language that comes with every computer, that any kid can learn.

Here is Javascript Reversi, an exercise in a basic turn-based board game. The computer player limits itself using wall time, so the faster the Javascript interpreter, the stronger the play. On Chrome, the Javascript opponent can beat me (though I am a very poor player).

If you know how to write a good reversi position-evaluation function that would make the computer stronger, please let me know.

Posted by David at August 23, 2009 09:21 PM

Flash is tough, but personally, I don't typically view Javascript as being much better. I mean, the reversi example requires 500 lines, and the use of two separate, somewhat clunky models -- one fragile document model (HTML), one not-terribly-intuitive abstract OO model (Javascript). For most people, you need to dive into a serverside technology and database to make JS worth using. Not that it can't do neat stuff on its own, just that most people view things like AJAX as worth the price of admission. And that's skipping over the use of JQuery to make the language palatable. (Which isn't and shouldn't be a dealbreaker -- everyone's got their stdlib. But still.)

I think my 8-year-old self would still take QBASIC over JS. I have hope that the additions to HTML 5 will make simple procedural graphics easier, and it might solve some of the quibbles I have. But it's still a lot harder to go from nothing to something you want to do a second iteration of -- something that looks "grown up" -- in JS.

All this said, I think you're right -- there's no language except JS any more that's more or less an out-of-the-box experience. I want to say Ruby on OS X, maybe, but even it's a stretch. Graphics and "game" programming are bigger deals to someone first getting into writing code, and as it stands, I really can't think of a single language where this isn't a huge pain in the ass.

Posted by: Josh at August 24, 2009 03:17 AM

In terms of lines of code, Javascript isn't that bad - here's an QBasic Othello for comparison - at 488 lines, it's about the same size. It is more straightforward but less ambitious.

Maybe the problem with Javascript isn't the lack of specific technology like procedural graphics or persistent state, but the lack of the ability to do blocking programming - the equivalent to QBasic's "INKEY" or "INPUT". Javascript forces you into event-based programming from the start, which seems like it requires more planning.

On the other hand, my son is doing great things in Scratch, which is also an event model. Also, in Scratch, he almost never uses line-drawing graphics, preferring to just move sprites around the screen.

I wonder if it would be possible to make a javascript library that would give Javascript a Scratch-like accessibility for kids.

Posted by: David at August 24, 2009 07:22 AM

Hey David,

I'm trying to write some js games too, and I'm kind growing a js-library that supposed to help in this process.

Maybe you will take a look when you have time it is much faster than jquery, has advanced OOP features, plus things like observers, options, Array#shuffle etc.

Just thought you might be interested,

Posted by: Nik at August 25, 2009 12:56 AM

please make a multiplayer version

Posted by: mahen23 at February 10, 2011 04:52 AM

Bug Report : When both players have no moves left, the game should end. Instead, the white player passes and my only choice is to pass too...

Posted by: Jan at February 13, 2011 07:09 PM

There should be a restart button.

Posted by: Reverser at March 10, 2011 04:09 AM

Sometimes the computer just won't move.

Posted by: Reverser at March 10, 2011 04:11 AM

the computer won't do the last move :S

Posted by: chris at May 22, 2011 05:28 PM

is there a way to make the game easier?

Posted by: at June 15, 2011 02:31 AM

would love to be able to NOT got first and see how a game ends?

Posted by: at June 20, 2011 12:51 PM

Game is too difficult & becomes boring after about 10goes.

Posted by: at June 28, 2011 03:54 PM

This game should be EXACTLY like Othello.

Posted by: evilshorty38 at July 11, 2011 09:01 AM

this game is kinda boring
(no offence)

Posted by: at July 11, 2011 09:03 AM

ur clock is messed up

Posted by: evilshorty38 at July 11, 2011 09:05 AM

this game needs multiplayer

Posted by: its 7:04 and ur clock says 9:03 at July 11, 2011 09:07 AM


Posted by: at July 11, 2011 09:08 AM

Computer and the player pass at same time.
It doesn't restart the game.

Posted by: at July 26, 2011 05:03 PM

The game is too easy. you should add a difficulty option.

Posted by: George at August 17, 2011 08:04 AM

Guide to the perfect game (starting bottom left A to H vertical, 1 to 8 horizontal)

c5, f3, f6, d4, f4, d6, f5, e2, d8, g8, d8, b8, f7, c7, h7, h5, h4, d1, h8, b6, a5, b4, g4, a8, a6, b2, a1, a2, a4, c2, g2, h3, g1, h1

Note : sometimes the computer reacts differently. try it 2-3 times you will get there...

Posted by: George at August 20, 2011 04:22 PM

Não consigo ganhar uma.. sou burro mesmo

Posted by: at September 27, 2011 09:38 PM

je n'arrive pas a gagnée !!! trop chiant !
en plus obliger de commencé !!

Posted by: kevin at October 20, 2011 07:06 PM

This game is very hard for me and needs multiplay mode essentially,

Posted by: K at October 24, 2011 10:51 AM

This game cheats. the "computer" made a move where only one should have flipped. there were 26 piece on the board. Then it flipped over an entire diagonal line and there were 28 pieces on the board. I am glad to know this because I spent far too much time trying to beat this thing. Now I can chalk it up to not working properly!

Posted by: Nikki at October 24, 2011 08:36 PM


Posted by: habin at October 31, 2011 08:45 AM

This game is way too hard... I have only gotten 1 corner so far in many tries. I used to beat this game all the time on my phone... I may not be the best player, but Id like to think I know the basics.

Posted by: matt at November 1, 2011 03:21 PM

Really easy. You should make it to consider some basic strategies. Check this site. I'm sure it would help you a lot to improve your AI. It also helped me a lot, too. :D

Posted by: jun at November 2, 2011 11:07 AM

Just a small bug I noticed, the game can't recognize a draw.

Posted by: at November 4, 2011 08:07 PM


Posted by: at November 7, 2011 12:42 PM

great to find a version that is actually a challenge. All the others I've played are too easy.

Posted by: at November 18, 2011 08:02 PM

Love it, love it, love it. The game is wonderful. I did see some issues when there is a draw, although hitting reset just has it start over. It does have a mild game play which I would rate between 1 and 10, in a 7 for the hardest play. I play every reversi game I can find and beat 99% of them. So this was a bit refreshing. I would love to see a copy of the source code as I'm a fellow programmer and I stick to Not that I want to copy your program, but simply to see the logic you use for the AI part. I've considered to make my own reversi game, but what I fail to see is how to capture my method of beating people/programs.

All the best.

Posted by: Bryan at December 2, 2011 02:40 AM


Posted by: miriam at January 28, 2012 06:22 PM


Posted by: andrew at February 3, 2012 04:19 PM

this reversi isnt hard

Posted by: a pro at March 13, 2012 04:03 AM

It'd be nice to see the available moves, ideally labelled with a single character, so that you could play via keyboard. We did this at Acornsoft for the BBC micro back in 1983, so I'm sure you could do it now...

Posted by: Spikey at March 18, 2012 08:23 PM

It'd be nice to see the available moves, ideally labelled with a single character, so that you could play via keyboard. We did this at Acornsoft for the BBC micro back in 1983, so I'm sure you could do it now...

Posted by: Spikey at March 18, 2012 08:24 PM

Here I found a truly strong version..

Try playing the various modes... :)

Posted by: Hans at April 28, 2012 12:33 PM

Computer and the player pass at same time.
It doesn't restart the game.
Too weak. Check WZebra, Cassio, Saio. They are the best program.

Posted by: joepattume at May 15, 2012 11:55 AM

Played it once, won 60-4. Reading the various comments, it looks like multiple levels of strength would be a good thing to have. I wrote my own reversi program in Qbasic, a program which has three levels of play. It is faster and much stronger than this program.

Posted by: Husker at May 19, 2012 12:11 AM

Please please make a multi-player version where I can play someone who is not as good as this computer.

Posted by: Sethero at June 21, 2012 06:39 PM

too simple AI... i scored 64:0 3 times out of 10 plays..

Posted by: at August 18, 2012 04:54 PM

game gets boring after awhile. needs more options for gameplay.

Posted by: at November 18, 2012 09:59 PM

this game is to hard you should include some difficulty settings

Posted by: at March 5, 2013 12:34 AM

Perfect score
A-H Rows
1-8 Columns
Top Left is A1
Bottom Right is H8

F5, C4, F4, C5, E3, C3, B4, E6, B3, A6, A2, A1, D1, B6, F1, B1, A8, E2, G1, B7, C8, D8, F8, E7, G6, G5, G8, H7, H8, H3, H2, H1, H4, H6

The game is too predictable and makes stupid decisions.

Posted by: ABooth at March 11, 2013 05:07 PM

The game will stuck forever once 2 players cannot move

Posted by: Kris at May 9, 2013 03:28 PM

Abooth, you are wrong. i tried that. it stops working at F4. THE THIRD MOVE. impossible to win. computer wont make a move if you are on the brink of victory.

Posted by: youarewrong at July 21, 2013 10:53 PM

@youarewrong. Try it a couple of times. Occasionally it deviates after a half dozen moves, but 9 times out of 10, my pattern will work.

Posted by: ABooth at August 29, 2013 05:46 PM

@youarewrong: I think you must have the orientation wrong. A-H are the rows, as in across, as in horizontal, as in from left to right
1-8 are columns, as in top to bottom, as in up/down, as in vertical

..A B C D E F G H
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .

Posted by: ABooth at August 29, 2013 05:57 PM

Think of a spreadsheet of columns and rows

Columns are vertical, Rows horizontal

Row A ...A1.................................................................A8
Row B
Row C
Row D...D1...........................D4...................................D8
Row E
Row F
Row G
Row H...H1..................................................................H8

Sometimes after A6 it moves differently, but the majority of times, the pattern works.

Posted by: ABooth at August 29, 2013 06:20 PM

there should be an option for difficulty level. I use to be a VERY good othello player but I can barely beat this. I wish I could opt to go second.

Posted by: Dave at May 2, 2014 08:11 PM

it appears you may have missed a programming check to determine a situation where both players must 'pass' for eternity. i can provide a screenshot showing the case upon request. thanks for a great game.

Posted by: Spence at August 3, 2014 02:18 PM

Hello loved the website would you mind helping me create a reversi, maybe send me the js file of this or just contact me (Thxx (:)

Posted by: Zohar at September 3, 2019 04:20 AM

Hello loved the website would you mind helping me create a reversi, maybe send me the js file of this or just contact me (Thxx (:)

Posted by: Zohar at September 3, 2019 04:20 AM

this game is a slap on my ass.
other implementations that I played did not implement the strategy,
but tried to get the most out of the current move, so they were very simple.
here, while the computer is thinking about the move, I can already see what move could ruin everything for me.
and this son of a bitch walks like that!

Posted by: Theodore at December 27, 2020 03:17 PM
Post a comment

Remember personal info?