-
Notifications
You must be signed in to change notification settings - Fork 2
/
man.rb
176 lines (132 loc) · 3.87 KB
/
man.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
=begin
ruby_chess
The Chess Board game implemented in the metalanguage ruby
Using the mighty GTK2 library
Copyright (C) 2006 Sébastien Boisvert
http://sebhtml.blogspot.com/
Sebastien.Boisvert<AT>USherbrooke<DOT>CA
This software is released under the GPL version 2 (1991)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
An online plain text version is available here:
http://www.gnu.org/licenses/gpl.txt
=end
require_relative 'move'
require_relative 'board'
class Man
IMAGE_PATH = 'pixmaps/'
IMAGE_EXT = '.png'
def self.convert_to_vectors_list arrays_list
v_list = []
arrays_list.each do |vector|
vectorial_move = VectorialMove.new vector[0], vector[1]
v_list << vectorial_move
end
v_list
end
def self.build black_image, white_image, moves
@black_image = black_image
@white_image = white_image
@moves = moves
end
def moves_history
@moves_history
end
def render_moves board, tile, vectorial_moves
moves = []
vectorial_moves.each do |move|
new_row = tile.row+move.delta_x
new_col = tile.col+move.delta_y
unless Board::ROW_8 <= new_row && new_row <= Board::ROW_1 &&
Board::COL_A <= new_col && new_col <= Board::COL_H
next
end
if board.board[new_row][new_col] != nil &&
board.board[new_row][new_col].player == @player
next
end
from = tile
to = Tile.new new_row, new_col
move = Move.new from, to
moves << move
end
moves
end
def self.possible_moves
[]
end
def player
@player
end
def color
@player.color
end
def possible_moves board, tile
self.class.possible_moves
end
def initialize player
@moves_history = Array.new
@player = player
if @player.black?
@image = self.class.black_image
else
@image = self.class.white_image
end
end
def image
IMAGE_PATH+ @image+ IMAGE_EXT
end
def letter
@image
end
def self.black_image
@black_image
end
def self.white_image
@white_image
end
def add_move move
@moves_history << move
end
private
def remove_moves_with_collisions board, moves
good_moves = []
moves.each do |move|
delta_row = move.to.row - move.from.row
delta_col = move.to.col - move.from.col
row_increment= delta_row > 0 ? +1 : -1
col_increment = delta_col > 0 ? +1 : -1
row_increment = delta_row == 0 ? delta_row : row_increment
col_increment = delta_col == 0 ? delta_col : col_increment
row_iterator = move.from.row + row_increment
col_iterator = move.from.col + col_increment
final_row = move.to.row
final_col = move.to.col
is_good = true
while row_iterator != final_row ||
col_iterator != final_col
#board.graphical_board.flash_piece row_iterator, col_iterator
content = board.cartesian_content row_iterator, col_iterator
if content != nil
is_good = false
break
end
row_iterator += row_increment
col_iterator += col_increment
end
if is_good == true
good_moves << move
end
#board.graphical_board.reset_colors
end
good_moves
end
end