-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rb
119 lines (98 loc) · 2.16 KB
/
utils.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require 'constants'
module MyTeacherUtils
include Constants
# 'b' => BBISHOP
def symbol_to_piece(sym)
SYMBOLS.index(sym)
end
# BBISHOP => 'b'
def piece_to_symbol(piece)
SYMBOLS[piece]
end
# "a1" => 0
def case_to_index(c)
SQUARENAME.index(c)
end
# 0 => "a1"
def index_to_case(c)
SQUARENAME[c]
end
# return KING for WKING or BKING (or KING)
def piece_type(piece)
return piece if piece < BLACKS_OFFSET
piece - BLACKS_OFFSET
end
# return the numeric value of the piece
# can pass KING / WKING / BKING, it works any way
# OBSOLETE: use Pïece_values[piece]
#def piece_value(p)
# VALUES[piece_type(p)]
#end
def colored_piece(piece, side)
# quicker than below....
if side == BLACK
return piece if piece >= BLACKS_OFFSET
return piece + BLACKS_OFFSET
else
return piece if piece < BLACKS_OFFSET
return piece - BLACKS_OFFSET
end
#piece = piece_type(piece)
#piece + (side == BLACK ? BLACKS_OFFSET : 0)
end
def color(piece)
return WHITE if piece <= WPAWN
return BLACK
end
def pretty_time(secs)
return "#{round(secs)}s" if secs < 60
min = (secs.to_f / 60).floor
return "#{min}m#{two_digits(round(secs-min*60,1))}" if min < 60
hour = (min.to_f / 60).floor
return "#{hour}h#{min-hour*60}m#{two_digits(round(secs-min*60,1))}" if hour < 24
day = (hour.to_f / 24).floor
return "#{day}d#{hour-day*24}h#{min-hour*60}m#{two_digits(round(secs-min*60,1))}"
end
def two_digits(n)
if n.to_i.to_s.size < 2
"0"+n.to_s
else
n
end
end
def round(n, f=0.1)
(n / f).round * f
end
def time_it
t = Time.now
rv = yield
puts Time.now-t
rv
end
def dec2bin(n)
n.to_s(2)
end
def bin2dec(number)
Integer("0b"+number)
end
def print_board(bb)
ind = indexes(bb)
i = 0
[56,48,40,32,24,16,8,0].each { |i|
(0..7).each { |j|
p = ind.include?(i+j)
if p
print ' X |'
else
if (i+j)%2 == 0
print ' |'
else
print ' |' # white
end
end
}
puts
}
puts
end
end