[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Checkers library

Helder Ribeiro

10/27/2007 5:20:00 PM

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.

--
XING profile: https://www.xing.com/profile/Held...
LinkedIn profile: http://www.linkedin.com/in/hel...
Blog: http://obvio171.wor...

12 Answers

James Gray

10/27/2007 8:05:00 PM

0

On Oct 27, 2007, at 12:20 PM, Helder Ribeiro 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
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

Ari Brown

10/28/2007 3:31:00 PM

0

I smell a quiz coming up...


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




M. Edward (Ed) Borasky

10/29/2007 3:11:00 AM

0

Ari Brown 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.

James Gray

10/29/2007 3:22:00 AM

0

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

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

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


Rick DeNatale

10/29/2007 3:27:00 AM

0

On 10/28/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:
> Ari Brown 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
http://talklikeaduck.denh...

M. Edward (Ed) Borasky

10/29/2007 3:42:00 AM

0

Rick DeNatale wrote:
> On 10/28/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:
>> Ari Brown 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. ;)

M. Edward (Ed) Borasky

10/29/2007 3:46:00 AM

0

James Edward Gray II wrote:
> On Oct 28, 2007, at 10:11 PM, M. Edward (Ed) Borasky wrote:
>
>> Ari Brown 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 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.

Rick DeNatale

10/29/2007 4:45:00 AM

0

On 10/28/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:
> James Edward Gray 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
http://talklikeaduck.denh...

M. Edward (Ed) Borasky

10/29/2007 6:05:00 AM

0

Rick DeNatale wrote:
> On 10/28/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:
>> James Edward Gray 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.

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.

John Joyce

10/29/2007 3:59:00 PM

0


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

> Rick DeNatale wrote:
>> On 10/28/07, M. Edward (Ed) Borasky <znmeb@cesmail.net> wrote:
>>> James Edward Gray 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.
>
> 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.