wrote in message:
Does anyone know if there's a sort of library for the game Checkers in
Ruby? Something that'd, 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 is not a library (very likely), a complete game that's
open source, from which I could extract that,'d 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 does not yet
handle jumps and it does not 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)
@xxxxxxxxxxx = color
@xxxxxxxxxxx = king
end
attr_reader :color
def king_me!
@xxxxxxxxxxx = true
end
def king?
@xxxxxxxxxxx
end
def to_s
str = @xxxxxxxxxxx[0, 1]
@xxxxxxxxxxx ? 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
@xxxxxxxxxxx = Array.new(12) { Piece.new(:black) } +
Array.new(8) +
Array.new(12) { Piece.new(:red) }
@xxxxxxxxxxx = :black
end
def [](square_number)
@xxxxxxxxxxx[square_number - 1]
end
def moves(color = @xxxxxxxxxxx)
@xxxxxxxxxxx([]) 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 = @xxxxxxxxxxx)
moves(color).include? move
end
def to_s
leading_black = false
border = "+---+---" * 4 + "+\n"
@xxxxxxxxxxx(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
I smell a quiz coming up...---------------------------------------------------------------|
~Ari
"I do not suffer from insanity. I enjoy every minute of it" --1337est
man alive
Blogs related to Checkers library
Hunspell
Hunspell is an easy native
Ruby interface to the famous Hunspell spell
checker library which is part of OpenOffice and Mozilla products. With this bundle you can start to develop your own AJAX based spell
checker service for
Ruby on
...
php installing gd windows
php installing gd php installing gd
library php installing gd windows php installing linux php installing pear php instance php instance of php instance variables php instanceof php instant messenger php instant messenger script php
...
Bookmark
QuickRef.org - all your docs are belong to us - PHP, Perl, CSS, HTML, Java, JavaScript, MySQL,
Ruby, and more; Report Summary - ATRC Web
Checker; Site Menus - Links; Stu Nicholls _ CSSplay _ comments page _ Basics
...
25 new messages in 14 topics - digest
Write a good, clean web-based music
library/jukebox. This is something I've been thinking of doing for a while, but I welcome someone to beat me to the punch. There are a few such things out there already--the most active is probably
...
25 new messages in 13 topics - digest
It's that >> simple; the secret unit of exchange is probably seconds. >> >> If this were Brand X, using another platform's low-level
library would be >> eternal torment, but this is
Ruby, so just try require 'active_support'!
...
Ruby Screnshot of the Weeek #17: Spelll Checkinng
This wasn't available for the above Rails source file since it's in a
library outside of my projects. The
Ruby spell
checker currently skips words in comments that look like
Ruby identifiers, such as CamelCase words, method_name words,
...