# Checkers library

Does anyone know if there’s a sort of library for the game Checkers in
Ruby? Something that would, at least:

1. given a board state and a player, tell me what the legal moves are;
2. given a board state and a move, tell me if it’s legal;
3. given a board state, tell me if it’s a draw or a win (and who won).

Also, if there isn’t a library (very likely), a complete game that’s
open source, from which I could extract that, would be good enough.

Cheers,

Helder

P.S.: for the curious, I’m looking for this because I want to
implement the checker player from Mitchell’s “Machine Learning” book
for a course assignment, but I don’t want to trouble with the checkers
rules themselves.

I smell a quiz coming up…

---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it” --1337est
man alive

Ari B. wrote:

Yeah, especially in light of the fact that there is a “perfect” checkers
program now – it can’t be beaten, only tied.

On Oct 27, 2007, at 12:20 PM, Helder R. wrote:

Does anyone know if there’s a sort of library for the game Checkers in
Ruby? Something that would, at least:

1. given a board state and a player, tell me what the legal moves are;
2. given a board state and a move, tell me if it’s legal;
3. given a board state, tell me if it’s a draw or a win (and who won).

Also, if there isn’t a library (very likely), a complete game that’s
open source, from which I could extract that, would be good enough.

I decided to see what I could put together for you request quickly.
Unfortunately, I’m now out of time as I’m about to walk out the door
for a concert.

This doesn’t yet handle jumps and it doesn’t determine win, lose, or
draw conditions. Hopefully it’s enough to get you started though:

#!/usr/bin/env ruby -wKU

require “enumerator”

module Checkers
class Piece
def initialize(color, king = false)
@color = color
@king = king
end

`````` attr_reader :color

def king_me!
@king = true
end

def king?
@king
end

def to_s
str = @color.to_s[0, 1]
@king ? str.upcase : str
end
``````

end

class Board
MOVES = {
1 => [ 5, 6], 2 => [ 6, 7], 3 => [ 7, 8],
4 => [ 8], 5 => [ 1, 9], 6 => [ 1,
2, 9, 10],
7 => [ 2, 3, 10, 11], 8 => [ 3, 4, 11, 12], 9 => [ 5, 6,
13, 14],
10 => [ 6, 7, 14, 15], 11 => [ 7, 8, 15, 16], 12 => [ 8, 16],
13 => [ 9, 17], 14 => [ 9, 10, 17, 18], 15 => [10, 11,
18, 19],
16 => [11, 12, 19, 20], 17 => [13, 14, 21, 22], 18 => [14, 15,
22, 23],
19 => [15, 16, 23, 24], 20 => [16, 24], 21 => [17, 25],
22 => [17, 18, 25, 26], 23 => [18, 19, 26, 27], 24 => [19, 20,
27, 28],
25 => [21, 22, 29, 30], 26 => [22, 23, 30, 31], 27 => [23, 24,
31, 32],
28 => [24, 32], 29 => [25], 30 => [25, 26],
31 => [26, 27], 32 => [27, 28]
}

`````` def initialize
@squares = Array.new(12) { Piece.new(:black) } +
Array.new(8)                        +
Array.new(12) { Piece.new(:red) }
@turn    = :black
end

def [](square_number)
@squares[square_number - 1]
end

def moves(color = @turn)
@squares.enum_with_index.inject([]) do |moves, (piece, i)|
next moves unless piece and piece.color == color
possible = MOVES[i + 1]
unless piece.king?
illegal = piece.color == :black ? :< : :>
possible.reject! { |n| n.send(illegal, i + 1) }
end
``````

moves +
possible.select { |to| self[to].nil? }.map { |m| “#{i + 1}-#
{m}” }
end
end

`````` def legal?(move, color = @turn)
moves(color).include? move
end

def to_s
border        = "+---+---" * 4 + "+\n"
@squares.enum_with_index.enum_slice(4).inject("") do |str, row|
pattern       = (leading_black ? "|###|%-3s" : "|%-3s|###")
``````
• 4 + “|\n”
str +
border +
pattern % [*row.map { |square| square.first }] +
pattern.delete("-") % [*row.map { |square| square.last + 1 }]
end + border
end
end
end

END

James Edward G. II

On Oct 28, 2007, at 10:11 PM, M. Edward (Ed) Borasky wrote:

Ari B. wrote:

I smell a quiz coming up…
---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it”
–1337est man alive
Yeah, especially in light of the fact that there is a “perfect”
checkers program now – it can’t be beaten, only tied.

I bet that took more than a weekend to build.

James Edward G. II

P.S. Blondie 24 is a good book to read about Checkers AI.

On 10/28/07, M. Edward (Ed) Borasky [email protected] wrote:

Ari B. wrote:

I smell a quiz coming up…

Yeah, especially in light of the fact that there is a “perfect” checkers
program now – it can’t be beaten, only tied.

Checkers! Hmmmmm. Now I can’t seem to get the phrase “A good
Republican cloth coat” out my mind!

Rick DeNatale

My blog on Ruby

James Edward G. II wrote:

I bet that took more than a weekend to build.

James Edward G. II

P.S. Blondie 24 is a good book to read about Checkers AI.

The original Samuels checker program was probably built in a couple of
weeks and probably would play perfect checkers on an infinitely fast IBM
704. I haven’t paid much attention to what’s inside the current
champion – Texas Hold 'Em seems like a lot more fun.

Rick DeNatale wrote:

On 10/28/07, M. Edward (Ed) Borasky [email protected] wrote:

Ari B. wrote:

I smell a quiz coming up…

Yeah, especially in light of the fact that there is a “perfect” checkers
program now – it can’t be beaten, only tied.

Checkers! Hmmmmm. Now I can’t seem to get the phrase “A good
Republican cloth coat” out my mind!

Sheesh … we’re going to keep that little dog.

On 10/28/07, M. Edward (Ed) Borasky [email protected] wrote:

James Edward G. II wrote:

On Oct 28, 2007, at 10:11 PM, M. Edward (Ed) Borasky wrote:

Yeah, especially in light of the fact that there is a “perfect”
checkers program now – it can’t be beaten, only tied.

I bet that took more than a weekend to build.

The original Samuels checker program was probably built in a couple of
weeks and probably would play perfect checkers on an infinitely fast IBM
704. I haven’t paid much attention to what’s inside the current
champion – Texas Hold 'Em seems like a lot more fun.

It might be my senility, but I seem to remember the Samuel’s program,
or a close descendant of the original beat the current world champion,
at least in a single game, quite early, like in the early 1960s.

## Or course, Checkers is a much simpler game than Chess, or Texas Hold 'em

Rick DeNatale

My blog on Ruby

Rick DeNatale wrote:

1. I haven’t paid much attention to what’s inside the current
champion – Texas Hold 'Em seems like a lot more fun.

It might be my senility, but I seem to remember the Samuel’s program,
or a close descendant of the original beat the current world champion,
at least in a single game, quite early, like in the early 1960s.

I don’t think it was that good. It was good enough, however, to remove
enough of the challenge from programming the game that nobody put in the
effort to make better programs. Instead, they moved on to more difficult
games.

Curiously enough, it looks like Go is nowhere near being even
competently played by a computer, while chess and poker have credible
computer players and checkers has gone the way of Tic-Tac-Toe and
Blackjack.

On Oct 28, 2007, at 11:11 PM, M. Edward (Ed) Borasky wrote:

Ari B. wrote:

I smell a quiz coming up…
---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it”
–1337est man alive
Yeah, especially in light of the fact that there is a “perfect”
checkers program now – it can’t be beaten, only tied.

I wish someone could find a way to always BEAT the computer at tic
tac toe.

And don’t tell me it’s impossible.
My parents are impossible.

aRi
-------------------------------------------|
Nietzsche is my copilot

On Oct 28, 2007, at 11:21 PM, James Edward G. II wrote:

I bet that took more than a weekend to build.

James Edward G. II

P.S. Blondie 24 is a good book to read about Checkers AI.

Maybe just a version that will play the game? Computer doesn’t have
to always win.

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.

On Oct 29, 2007, at 1:05 AM, M. Edward (Ed) Borasky wrote:

fast IBM
moved on to more difficult games.

Curiously enough, it looks like Go is nowhere near being even
competently played by a computer, while chess and poker have
credible computer players and checkers has gone the way of Tic-Tac-
Toe and Blackjack.

Apparently, you’ve never played some of the video game versions of Go
available in Japan. They’re competent enough!
Go is like Pente, but harder. Simple to learn, impossible to master.
Fatigue is a major factor in Go.

John J. wrote:

Apparently, you’ve never played some of the video game versions of Go
available in Japan. They’re competent enough!
Go is like Pente, but harder. Simple to learn, impossible to master.
Fatigue is a major factor in Go.

Not in my case … I’m as incompetent at Go and Chess when I’m fully
rested as I am when I’m tired. I don’t have the patience to play that
kind of game. You’re right – I’ve never played any video Go, and I
don’t even have Gnuchess loaded on any of my systems. About the only
computer game I enjoy these days is that one with the rotating red,
green and blue marbles. I sure wish I had invented that one.