-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxboard.rb
executable file
·104 lines (94 loc) · 2.02 KB
/
xboard.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env ruby
require 'position'
require 'search'
# TODO: put all this into a proper class
confirm = false
$stdout.sync = true
$stderr.sync = true
Thread.abort_on_exception = true
st = Stats.new
p = Position.new(st)
s = Search.new(p,st)
def time_it label
yield
end
def logout msg
#$stderr.print "Out:#{msg}\n"
print "#{msg}\n"
end
module Enumerable
def rand
self[Kernel.rand(self.size)]
end
end
def pv_str(p,s, pv)
"#{pv.size} #{s.tree.best.score} #{(s.tree.current_time*100).to_i} #{0} #{pv.join(" ")}"
end
def play p, s
Thread.new { s.play }
pv = nil
until s.done
sleep(0.2)
best = s.tree.pv
if best and best != pv
pv = best
logout pv_str(p,s, best)
end
end
move = s.move
if not move
if p.side == BLACK
logout "RESULT 1-0 {White mates}"
else
logout "RESULT 0-1 {Blacks mates}"
end
end
logout "move #{move.to_s(:xboard)}"
#logout "tellopponent #{move}: #{s.score}"
move
end
$stdin.each do |move|
move.strip!
$stderr.print "In :#{move}\n"
case move
when "xboard" then
logout ""
logout "tellics set f5 1=1"
when "confirm_moves"
confirm = true
logout "Will now confirm moves."
when /.{0,1}new/
p.reset_to_starting_position
logout "tellics set 1 MyTeacher"
when /^protover/ then
logout "feature sigint=0 sigterm=0 ping=1 done=1"
when /^ping\s+(.*)/ then
logout "pong #{$1}"
when "quit"
exit
when "remove"
p.unmake
p.unmake
when /^st/ then
when /^level/ then
when /^time/ then
when /^otim/ then
when "hard" then
when "random" then
when "post" then
when /^accepted/ then
# ignore
else
move.gsub!(/\?/, '')
begin
p.make_from_input(move)
logout "Legal move: #{move}" if confirm
play(p,s)
rescue IllegalMoveException
logout "Illegal move: #{move}"
rescue Exception=> e
logout "tellopponent Error: #{move} #{e.message}"
puts e
end
end
end