Backgammon, Part 2

A backgammon game has 3 clear stages – the opening, mid-game, and bearing off. Since both players are starting off evenly, and there’s a limit to what combination of dice can be rolled, the first few moves are pretty much standardized.

Opening

There are three goals at the beginning of the game, which are kind of mutually exclusive, depending on what numbers you get versus your opponent’s. First is to control points 7 and 18. These are the points both players are more likely to land on if they’re open, so controlling them with 2 or more of your stones gives you a major advantage. Second is to build up blocks of points with 2 or more of your stones on them to trap your opponent “behind the lines”. Third is to get your farthest stones out of your opponent’s home field so that you can start bearing off.

(If you get double 6’s.)

With these three goals in mind, what you obviously want (and probably won’t get) on your first roll is double 6’s. Assuming that you’re playing red, move both stones from point 24 to 18, and 2 stones from 13 to 7. You now control the most important points on the board. Nice start.

Another good first roll is double 1’s. And this is where risk comes in.

(Double 1’s, leaving a blot on point-8.)

You have 2 choices – move two stones from 6 to 5, then two from 8 to 7. This lets you control the 7 point, but leaves you vulnerable with a blot on 8. A safer option is two stones from 6 to 5, then one stone from 8 to 7 to 6. I’ll talk about this trade off later.

(Double 1’s, the safe way with no blots.)

(Rolling a 5-3 to make a block on point-3.)

If your starting roll is 3 and 5, move one stone from 8 to 3, and one from 6 to 3. This creates a block on the 3 point. Similarly, you can move the same stones with starting rolls of 1-3, 2-4, or 4-6 to make blocks on the 5, 4 or 2 points, respectively.

If your starting roll is double 2’s, 3’s, 4’s or 5’s, you can use two stones each from the 6 point to make blocks at the 4, 3 or 2 point; and/or two stones from the 8 point, or two from the 13 point. Again, the disadvantage of taking two stones from the 8 point is that you leave a vulnerable blot behind.

Most of the other starting rolls are less desirable because you can’t make a new block with them, and they may leave you with one, two or three blots.

(Rolling a 7 (5-2) and playing it safe.)

You can play it safe, such as with a 2-5. Move one stone from the 13 point to 8, then continue to the 6 point. Then, just keep playing safe this way, either making new blocks, or only moving stones on existing blocks. The downside of a safe game is that you become less flexible, unable to make new blocks as easily, and your opponent can escape past you faster.

Wow, that was a lot of writing, just for the first roll of the game. Woof.

To be continued.

Gakken Activity

There’s been a little activity over on the Otona no Kagaku facebook page.

First was an announcement for the release of the Rainbow Loom kit for girls.

This was followed yesterday by an announcement of a workshop for making Rainbow bracelets, to be held on Aug. 23.

Finally, we have Miku Hatsune’s Magical Mirai (Magical Future), a music event in Osaka on Aug. 30 (Gakken is one of the sponsors). Th same event will e in Tokyo on Sept. 20.

Backgammon, Part 1

I was first introduced to backgammon when I was a part of the Minnesota SF group in Minneapolis in the 80’s. I was never very good, relying more on luck than skill, but I still liked playing whenever I had the chance. I’d win maybe a third of my games, but when I went against a better player who only played for money, my ratio dropped to at best 1 out of 4 (if not less).

Backgammon is virtually unheard of in Japan. The boards aren’t sold in book or department stores (you can easily get Othello and Reversi boards here, though). So, it’s been decades since I’ve been able to find a human opponent. A few days ago, I gave up and tried out the backgammon page on Yahoo games. I know I shouldn’t expect much from something that’s free, but the AI in the yahoo game is pretty pathetic – I have about an 80% win rate.

I’ve long wanted to write a role-playing game that revolves around backgammon, but until I can afford to buy a copy of Adobe Flash (\$400 USD?) that probably won’t happen soon. In the meantime, I think I’ll write a blog series on what would be necessary for a good game AI.

Starting out

A typical backgammon board is divided up into quadrants, with alternating light and dark triangles, called “points”. There’s a total of 24 points, 12 on each side. (The alternating colors don’t mean anything, they’re just there to make counting them easier.) There are 2 players, with 15 checkers, called “stones”. The stones are generally placed on the board as shown (image taken from the wiki page).

The players take turns rolling 2 dice, and moving their stones in a horseshoe direction in order to get all of their stones into the quadrant directly in front of them (points 6-1 for black, 19-24 for red or white). When they reach this goal, they can start removing the stones from the board to the tray; the first one to take off all of their stones wins. You move a stone exactly the number of points given on each die, and you can either move two stones one die each, or one stone twice. If you roll doubles, you get 4 moves.

A single stone on a point is called a “blot”. You can land on your opponent’s blot and send it to the bar (the area in the middle of the board). If a point has 2 or more stones on it, the other player can’t land on it. You CAN land on a point that has your own stones on it, a point that has no stones on it, or your opponent’s blot. If a blot is hit and put on the bar, that player has to roll the dice and try to get the blot back onto the board at the far end of their horseshoe pattern (points 6-1 for red or white, 19-24 for black) before proceeding.

Play starts by laying out the stones as shown in the image. Then each player rolls one die, and the player with the higher number goes first. If both players roll the same number, then you advance the doubling cube by one, and roll again. (The yahoo game doesn’t use the doubling cube).

Right now, you have enough information to try out the yahoo game, if you want.

Doubling Cube

If you play for money, or just want to keep track of your score, then the doubling cube becomes more important. This looks like a 6-sided die, but it’s printed with the values 2, 4, 8, 16, 32 and 64. When the cube is on the bar, it has a value of 1. If both players roll the same number when determining who goes first, then the cube goes to 2. Roll doubles again, it goes to 4, etc. Then, during the course of the game, one player can challenge the other by “doubling the game”. If the challengee accepts, then he/she gets control of the cube and the game is now worth twice as many points. If the challengee declines, he/she loses the game and it’s worth whatever value the cube had been standing at (1, 2, 4, etc.) Whoever has control of the cube can then double the game again, if they think they can win, or if they just want to end a game that they’re obviously going to win.

I really wish the yahoo game had a doubling cube to end boring games, rather than having to play them all the way to the end all the time. On the other hand, I think the game invokes a double – occasinally when I’m winning, yahoo games crashes Firefox. I hate that.

Games are normally worth 1 point, unless they’ve been doubled. Since the games are fairly quick – 5-10 minutes – players can decide to play until one person reaches 10 points, or whatever. Or, play for money, with 1 point = \$1.

To be continued.

Playing with GoComics, Part 2

I’ve been helping one of the webcomic artists that has a strip on GoComics, and one of the things I found myself having to do was going back through several month’s worth of strips to find one specific comment posted by another user. It would be nice if GoComics provided a way to search comments directly, but they don’t. This pushed me over the edge on another VBScript project I’ve had at the back of my mind for a while now.

If you want the comments of a particular strip, you already have them embedded in the Javascript pushed into your browser. Taking the code from last week’s blog entry, just do “instr(objHttp.ResponseText, “<li class=’load-comments’>”)”. The comments are in the subsequent section.

Kind of. What GoComics did was to embed the first 15 comments as Javascript. You can do an “instr(objHttp.ResponseText, “Comments (“)” to get the actual comment count. If there’s more than 15 comments, you need to do another:

objHttp.Send

This one’s a bit trickier, though. The directory containing all of the comments for a specific strip is a plain number apparently assigned at random. It’s going to look something like: “/comments/1161211/page/1?show_all=true”. You want that “1161211” number (or whatever it is for the page you’re on). Once you have that, then just build up the CommentsURL as:

Directory  = “1161211”

This gives you all of the other comments that aren’t embedded in the page with the strip. Unfortunately, the comments embedded with the strip are formatted for Javascript, while the next set in objHttp.ResponseText will be in HTML format. This means having two sets of deformatters for stripping code out of the comments to make them more human-friendly.

What’s a real pain, though, is that certain comments can include Unicode characters, which VBS will barf on. And, the Microsoft VBScript Reference page fails to mention the existence of the ASCW() function, which is the one you want for getting the ASCII codes of Unicode characters. Use ASCW() to test for unicode and then replace it with something safe, like “.”, before writing the comments to an output file.

What I did was to create a while loop to run the script on every page of the comic strip from day 1 up to the present, stripped out the Javascript and HTML code so I just had the commenters’ names and the comments, and then saved the mass to separate .txt files, one file per strip (including the full URL of the strip in the file so I can go back to the original comments if needed). Now, if I have to locate someone’s comment again, I can grep one directory on my hard drive and be done with it in a couple seconds. Entire project took about 4 hours to write and debug only because I was tired and kept making stupid coding mistakes. Probably could have done it in an hour if I was rested.

Playing with GoComics, Part 1

If you’re like me, you like reading American newspaper comic strips. And, if you’re like me, you like reading strips that aren’t carried by your local paper. Which means that, like me, you visit GoComics every day. But, perhaps unlike me, you can tolerate their interface or are patient enough to wait for your subscribed strips to arrive in your email inbox in the morning. Personally, I like seeing a specific group of strips right when the server updates at 12:10 AM CST, which for me is 2:10 PM here in Japan. So, no, I don’t want to wait 8 hours for them to show up in email.

Additionally, I don’t like looking at the comments on some of the strips, because several of the regular readers can be either very abusive of the artist, or just very crude. Which means that I want a way to get the latest comics while still allowing me to easily post comments on a specific strip when desired.

I’ve mentioned before that I use VBScript because it’s easier to figure out than Perl, and it comes prepackaged with Windows. VBS isn’t perfect, and it’s not as powerful as Perl when it comes to things like regular expression support. It’s especially problematic if you want to read and write binary files (which VBS deliberately doesn’t allow). But, it’s good enough for most of the tasks I need it for.

The first key is to activate Http support. You do this by making a call to CreateObject, and specifying the XMLHTTP server object. Once you have this object, then you Open it, passing the URL of the webpage you want to access. Follow this with Send, and you’re almost done. The page is stored in the ResponseText member of the object as one really long text string.

Set objHttp = CreateObject(“Msxml2.ServerXMLHTTP”)

objHttp.Open “GET”, “http://www.gocomics.com/garfield/2014/06/04&#8221;, False

objHttp.Send

str = objHttp.ResponseText

GoComics uses a lot of Javascript, making navigation through the page a bit cumbersome when trying to find the URL for the actual comic strip you want. What you’re looking for is the section identified with the string: “<p class=”feature_item””. Once you have this, then getting the image URL is pretty trivial.

To make things more convenient, you can write the script to have all of the comics page URLs that you like placed into an array, and then just append the day’s date to the comic URL to access each one, one at a time. The last step is to place an HTML wrapper around the image URLs so you can read all of the day’s comics via your browser, and include “<a href=””>” links to the comics so that you can click on whatever image you want in order to post comments on the appropriate GoComics page.It’s just that easy.