Checkers library

H

Helder Ribeiro

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.
 
J

James Edward Gray II

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
leading_black = false
border = "+---+---" * 4 + "+\n"
@squares.enum_with_index.enum_slice(4).inject("") do |str, row|
leading_black = !leading_black
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 Gray II
 
A

Ari Brown

I smell a quiz coming up...


---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive
 
M

M. Edward (Ed) Borasky

Ari said:
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.
 
J

James Edward Gray II

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 Gray II

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

Rick DeNatale

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!
 
M

M. Edward (Ed) Borasky

Rick said:
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. ;)
 
M

M. Edward (Ed) Borasky

James said:
I bet that took more than a weekend to build. ;)

James Edward Gray 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.
 
R

Rick DeNatale

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
 
M

M. Edward (Ed) Borasky

Rick said:
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.
 
J

John Joyce

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.
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.
 
A

Ari Brown

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
 
A

Ari Brown

I bet that took more than a weekend to build. ;)

James Edward Gray 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top