rubyknight 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ RubyKnight
2
+ ==========
3
+
4
+ RubyKnight is a very naive implementation of a chess library and engine.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ gem install rubyknight
10
+
11
+
12
+ Usage
13
+ -----
14
+
15
+ ### Play Chess
16
+
17
+ `> rubyknight`
18
+
19
+ ### Write Chess Code
20
+
21
+ require 'rubygems'
22
+ require 'rubyknight'
23
+ board = RubyKnight::Board.new
24
+
25
+ puts board.to_s
26
+ print "Enter move: "
27
+ $stdin.each do |move|
28
+ move.strip!
29
+ begin
30
+ board.cnotation_move move
31
+ rescue RubyKnight::IllegalMoveException
32
+ print "Enter a real move! #{$!.to_s}\n"
33
+ end
34
+ puts board.to_s
35
+ print "Enter move: "
36
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,7 +1,26 @@
1
+ # = rubyknight.rb - Ruby Chess Library
2
+ #
3
+ # == Example
4
+ # board = RubyKnight::Board.new
5
+ #
6
+ # puts board.to_s
7
+ # print "Enter move: "
8
+ # $stdin.each do |move|
9
+ # move.strip!
10
+ # begin
11
+ # board.cnotation_move move
12
+ # rescue RubyKnight::IllegalMoveException
13
+ # print "Enter a real move! #{$!.to_s}\n"
14
+ # end
15
+ # puts board.to_s
16
+ # print "Enter move: "
17
+ # end
18
+
1
19
  module RubyKnight
2
20
  class IllegalMoveException < RuntimeError
3
21
  end
4
22
 
23
+ # A Chess Board and State
5
24
  class Board
6
25
  attr_reader :history, :WHITE, :BLACK, :to_play
7
26
 
@@ -21,10 +40,12 @@ module RubyKnight
21
40
  setup_start
22
41
  end
23
42
 
43
+ # Is it white's turn?
24
44
  def white_to_play?
25
45
  @to_play == WHITE
26
46
  end
27
47
 
48
+ # Dump the board state to a string
28
49
  def dump
29
50
  @bitboards[@bitboards.size] = @history
30
51
  @bitboards[@bitboards.size] = @to_play
@@ -34,6 +55,7 @@ module RubyKnight
34
55
  ret
35
56
  end
36
57
 
58
+ # Load the board state from a string
37
59
  def load dmp
38
60
  @bitboards = Marshal.load( dmp)
39
61
  @to_play = @bitboards.pop
@@ -78,10 +100,12 @@ module RubyKnight
78
100
  else WHITE end
79
101
  end
80
102
 
103
+ # Roll back the last move, specify two to roll back a whole player
81
104
  def undo num = 1
82
105
  num.times { _undo}
83
106
  end
84
107
 
108
+ # Set the boards to the initial state
85
109
  def setup_start
86
110
  @to_play = WHITE
87
111
  @bitboards = Array.new LAST_BOARD+1, 0
@@ -122,6 +146,7 @@ module RubyKnight
122
146
  "#{(file + 97).chr}#{rank}"
123
147
  end
124
148
 
149
+ # Make a move in coordinate notation, ex. e2e4
125
150
  def cnotation_move cnot
126
151
  start, dest, promotion = cnotation_to_bits cnot
127
152
  raise IllegalMoveException, "Unreadable move" unless start
@@ -151,6 +176,7 @@ module RubyKnight
151
176
  end
152
177
  end
153
178
 
179
+ # find out the piece at a given location
154
180
  def whats_at position
155
181
  positionbit = (1 << position)
156
182
  if @bitboards[WALL] & positionbit > 0
@@ -171,6 +197,7 @@ module RubyKnight
171
197
  somethingthere
172
198
  end
173
199
 
200
+ # get a simple board notation
174
201
  def to_s
175
202
  out = ""
176
203
  (0..63).each do |position|
@@ -1,8 +1,5 @@
1
1
  def time_it label
2
- start = Time.now
3
- res = yield
4
- puts "TIMING( '#{label}'): #{Time.now - start} seconds"
5
- res
2
+ yield
6
3
  end
7
4
 
8
5
 
@@ -97,21 +94,12 @@ class RubyKnight::Board
97
94
 
98
95
  def gen_moves player
99
96
  white = player==WHITE
100
- if 1
101
- gen_pawn_moves(white) +
102
- gen_knight_moves(white) +
103
- gen_rook_moves(white) +
104
- gen_bishop_moves(white) +
105
- gen_king_moves(white) +
106
- gen_queen_moves(white)
107
- else
108
- time_it("gen_pawn") { gen_pawn_moves(white)} +
97
+ time_it("gen_pawn") { gen_pawn_moves(white)} +
109
98
  time_it("gen_knight") { gen_knight_moves(white)} +
110
- time_it("gen_rook") { gen_rook_moves(white)} +
99
+ time_it("gen_rook") { gen_rook_moves(white)} +
111
100
  time_it("gen_bishop") { gen_bishop_moves(white)} +
112
- time_it("gen_king") { gen_king_moves(white)} +
113
- time_it("gen_queen") {gen_queen_moves(white)}
114
- end
101
+ time_it("gen_king") { gen_king_moves(white)} +
102
+ time_it("gen_queen") { gen_queen_moves(white)}
115
103
  end
116
104
 
117
105
  def different_colors white, piece
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyknight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Moyer
@@ -21,9 +21,9 @@ executables:
21
21
  extensions: []
22
22
 
23
23
  extra_rdoc_files:
24
- - README
24
+ - README.markdown
25
25
  files:
26
- - README
26
+ - README.markdown
27
27
  - Rakefile
28
28
  - TODO
29
29
  - VERSION
data/README DELETED
@@ -1,4 +0,0 @@
1
- RubyKnight
2
- ==========
3
-
4
- RubyKnight is a very naive implementation of a chess library and engine.