mediocre_chess 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mediocre_chess.rb +70 -12
- data/spec/mediocre_chess_spec.rb +64 -0
- data/spec/spec_helper.rb +3 -0
- metadata +7 -6
data/lib/mediocre_chess.rb
CHANGED
@@ -3,19 +3,77 @@ require 'java'
|
|
3
3
|
require 'mediocre/mediocre_v0.34.jar'
|
4
4
|
java_import 'mediocre.board.Board'
|
5
5
|
java_import 'mediocre.board.Move'
|
6
|
+
java_import 'mediocre.engine.Engine'
|
6
7
|
|
7
8
|
module MediocreChess
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
class IllegalMoveException < Exception; end
|
10
|
+
class InvalidFenException < Exception; end
|
11
|
+
WHITE = 1; BLACK = -1
|
12
|
+
|
13
|
+
class Board
|
14
|
+
def initialize(fen_board = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
|
15
|
+
@board = Java::Mediocre.board.Board.new
|
16
|
+
self.fen = fen_board
|
17
|
+
end
|
18
|
+
|
19
|
+
# FEN Methods
|
20
|
+
def fen; @board.getFen; end
|
21
|
+
|
22
|
+
def fen=(fen_board)
|
23
|
+
@board.inputFen(fen_board)
|
24
|
+
rescue
|
25
|
+
raise MediocreChess::InvalidFenException.new("'#{fen_board}' is an invalid FEN board. The board may be in a bad state.")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns: Array of legal move strings, ['e2e4', 'd2d4', ...]
|
29
|
+
def moves
|
30
|
+
number_of_moves, moves = gen_legal_moves
|
31
|
+
results = []
|
32
|
+
number_of_moves.times do |i|
|
33
|
+
results << Java::mediocre.board.Move.inputNotation(moves[i])
|
34
|
+
end
|
35
|
+
results
|
36
|
+
end
|
37
|
+
|
38
|
+
# Input: move string, "e2e4"
|
39
|
+
# Raises: IllegalMoveException
|
40
|
+
def add!(move_string)
|
41
|
+
number_of_moves, moves = gen_legal_moves
|
42
|
+
number_of_moves.times do |i|
|
43
|
+
if Java::mediocre.board.Move.inputNotation(moves[i]) == move_string
|
44
|
+
@board.makeMove(moves[i])
|
45
|
+
return
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
raise MediocreChess::IllegalMoveException.new("'#{move_string}' is not a legal chess move.")
|
50
|
+
end
|
51
|
+
|
52
|
+
def stalemate?
|
53
|
+
number_of_moves, moves = gen_legal_moves
|
54
|
+
number_of_moves == 0 && !Java::Mediocre.engine.Engine.isInCheck(@board)
|
55
|
+
end
|
56
|
+
|
57
|
+
def checkmate?
|
58
|
+
number_of_moves, moves = gen_legal_moves
|
59
|
+
number_of_moves == 0 && Java::Mediocre.engine.Engine.isInCheck(@board)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns: MediocreChess::WHITE if white won
|
63
|
+
# MediocreChess::BLACK if black won
|
64
|
+
# nil if the game isn't checkmated
|
65
|
+
def winner
|
66
|
+
if checkmate?
|
67
|
+
-1 * @board.toMove
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def gen_legal_moves
|
74
|
+
moves = Java::int[128].new
|
75
|
+
number_of_moves = @board.gen_allLegalMoves(moves, 0)
|
76
|
+
[number_of_moves, moves]
|
77
|
+
end
|
20
78
|
end
|
21
79
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe MediocreChess::Board do
|
5
|
+
# The purpose of these tests are not to test Mediocre methods,
|
6
|
+
# but rather the JRuby wrapper.
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@board = MediocreChess::Board.new
|
10
|
+
end
|
11
|
+
|
12
|
+
context "fen boards" do
|
13
|
+
it "should be created with the default board" do
|
14
|
+
@board.fen.should == 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be properly received and generated" do
|
18
|
+
@board.fen = '8/3K4/2p5/p2b2r1/5k2/8/8/1q6 b - - 1 67'
|
19
|
+
@board.fen.should == '8/3K4/2p5/p2b2r1/5k2/8/8/1q6 b - - 1 67'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "move generation" do
|
24
|
+
it "should generate moves of the correct format" do
|
25
|
+
moves = @board.moves
|
26
|
+
moves.count.should == 20 # verifying the moves work
|
27
|
+
moves.each { |move| move.should match /[a-h][1-8][a-h][1-8]/ }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "move addition" do
|
32
|
+
it "should add legal moves" do
|
33
|
+
@board.add!('e2e4')
|
34
|
+
@board.fen.should == 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should only add legal moves" do
|
38
|
+
lambda { @board.add!('e7e5') }.should raise_error(MediocreChess::IllegalMoveException)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "game state" do
|
43
|
+
it "should be normal at the initial position" do
|
44
|
+
@board.checkmate?.should be false
|
45
|
+
@board.stalemate?.should be false
|
46
|
+
@board.winner.should be nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should find checkmate" do
|
50
|
+
@board.fen = '2Q5/p4pkp/3p2p1/3P4/3b4/2P5/PP6/6qK w - - 0 37'
|
51
|
+
@board.checkmate?.should be true
|
52
|
+
@board.stalemate?.should be false
|
53
|
+
@board.winner.should be MediocreChess::BLACK
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should find stalemate" do
|
57
|
+
@board.fen = '8/8/8/8/8/1K6/2Q5/k7 b - - 0 1'
|
58
|
+
@board.fen.should == '8/8/8/8/8/1K6/2Q5/k7 b - - 0 1'
|
59
|
+
@board.checkmate?.should be false
|
60
|
+
@board.stalemate?.should be true
|
61
|
+
@board.winner.should be nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -3,10 +3,10 @@ name: mediocre_chess
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
- 0
|
7
6
|
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joey Robert
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-11-
|
17
|
+
date: 2010-11-17 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -37,7 +37,7 @@ files:
|
|
37
37
|
- lib/mediocre_chess.rb
|
38
38
|
- README.md
|
39
39
|
has_rdoc: true
|
40
|
-
homepage:
|
40
|
+
homepage: https://github.com/joeyrobert/mediocre_chess
|
41
41
|
licenses: []
|
42
42
|
|
43
43
|
post_install_message:
|
@@ -66,5 +66,6 @@ rubygems_version: 1.3.6
|
|
66
66
|
signing_key:
|
67
67
|
specification_version: 3
|
68
68
|
summary: Chess utility functions using the Mediocre chess engine
|
69
|
-
test_files:
|
70
|
-
|
69
|
+
test_files:
|
70
|
+
- spec/mediocre_chess_spec.rb
|
71
|
+
- spec/spec_helper.rb
|