Class: Gambit::Tools::Board

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Viewable
Defined in:
lib/gambit/tools/board.rb

Overview

A tool for managing a two-dimensional game board.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Viewable

append_features, #view

Constructor Details

#initialize(x_dim, y_dim, fill = nil) ⇒ Board

Creates a new Board of x_dim by y_dim with an optional fill object at each location. (The fill is dup()ed as it is placed.)



55
56
57
58
59
60
61
62
63
# File 'lib/gambit/tools/board.rb', line 55

def initialize( x_dim,  y_dim, fill = nil )
	@board = Array.new(x_dim) { Array.new(y_dim) };
	@include_diagonals = true;
	unless(fill.nil?)
		each_location do |loc|
			self[loc] = fill.dup
		end
	end
end

Instance Attribute Details

#include_diagonalsObject

When set (default), diagonals are considered neighboring squares.



66
67
68
# File 'lib/gambit/tools/board.rb', line 66

def include_diagonals
  @include_diagonals
end

Instance Method Details

#[](*coordinate) ⇒ Object

Returns the contents of a square. Arguments can be anything parse_location() understands.



107
108
109
110
# File 'lib/gambit/tools/board.rb', line 107

def []( *coordinate )
	x,y = parse_location(*coordinate)
	return @board[x][y]
end

#[]=(*values) ⇒ Object

Sets the contents of a square. Arguments can be anything parse_location() understands.



116
117
118
119
120
# File 'lib/gambit/tools/board.rb', line 116

def []=( *values )
	item = values.pop 
	x,y = parse_location(*values)
	@board[x][y] = item
end

#colsObject

Returns a column count.



74
75
76
# File 'lib/gambit/tools/board.rb', line 74

def cols(  )
	@board.length
end

#each(reverse = false, &block) ⇒ Object

Iterate over the squares of this Board. Pass true to reverse to swap the iteration order.



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/gambit/tools/board.rb', line 220

def each( reverse = false, &block )
	if reverse
		((0 ... @board[0].length).to_a.reverse).each do |index|
			@board.collect do |column|
				column[index]
			end.each(&block)
		end
	else
		@board[0].each_index do |index|
			@board.collect{ |column| column[index] }.each(&block)
		end
	end
end

#each_col(reverse = false, &block) ⇒ Object

Iterate over the columns of this Board. Pass true to reverse to swap the iteration order.



190
191
192
193
194
195
196
# File 'lib/gambit/tools/board.rb', line 190

def each_col( reverse = false, &block )
	if reverse
		@board.reverse.each(&block)
	else
		@board.each(&block)
	end
end

#each_location(reverse = false) ⇒ Object

Iterate over the locations of this Board. Pass true to reverse to swap the iteration order.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gambit/tools/board.rb', line 238

def each_location( reverse = false )
	if reverse
		(@board[0].length - 1).downto(0) do |row|
			0.upto(@board.length-1) do |column|
				yield to_chess(column,row)
			end
		end
	else
		0.upto(@board[0].length-1) do |row|
			0.upto(@board.length-1) do |column|
				yield to_chess(column,row)
			end
		end
	end
end

#each_row(reverse = true) ⇒ Object

Iterate over the rows of this Board. Pass true to reverse to swap the iteration order.



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gambit/tools/board.rb', line 202

def each_row( reverse = true )
	if reverse
		((0 ... @board[0].length).to_a.reverse).each do |index|
			yield @board.collect{ |column| column[index] }
		end
	else
		(0 ... @board[0].length).each do |index|
			yield @board.collect{ |column| column[index] }
		end
	end
end

#each_with_location(reverse = false) ⇒ Object

Iterate over the squares and locations of this Board. Pass true to reverse to swap the iteration order.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/gambit/tools/board.rb', line 258

def each_with_location( reverse = false )
	if reverse
		(@board[0].length - 1).downto(0) do |row|
			0.upto(@board.length-1) do |column|
				yield(@board[column][row], to_chess(column,row))
			end
		end
	else
		0.upto(@board[0].length-1) do |row|
			0.upto(@board.length-1) do |column|
				yield(@board[column][row], to_chess(column,row))
			end
		end
	end
end

#move(*coordinates) ⇒ Object

Moves the contents of the first square, to the second square. Arguments can be anything parse_location() understands.



175
176
177
178
179
180
181
182
183
184
# File 'lib/gambit/tools/board.rb', line 175

def move( *coordinates )
	if coordinates.length == 2
		self[coordinates[1]] = self[coordinates[0]].clone
		self[coordinates[0]] = nil
	else
		self[coordinates[2],coordinates[3]].clone = 
		        self[coordinates[0],coordinates[1]]
		self[coordinates[0],coordinates[1]] = nil
	end
end

#neighboring_locations(*coordinate) ⇒ Object

Returns an Array of neighboring locations, in chess notation. Arguments can be anything parse_location() understands.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gambit/tools/board.rb', line 126

def neighboring_locations( *coordinate )
	x,y = parse_location(*coordinate);
	neighbor_locations = []
	[-1,0,1].each do |xi|
		[-1,0,1].each do |yi|
			next if (xi == 0 and yi == 0)
			next if !@include_diagonals and 
			        ((xi + yi).abs != 1)
			if valid?(x + xi, y + yi)
				neighbor_locations << to_chess(x + xi,y + yi)
			end
		end
	end
	return neighbor_locations
end

#neighbors(*coordinate) ⇒ Object

Returns an Array of neighboring square contents, in chess notation. Arguments can be anything parse_location() understands.



146
147
148
149
150
151
152
153
154
155
# File 'lib/gambit/tools/board.rb', line 146

def neighbors( *coordinate )
	neighbors = []
	nl = neighboring_locations(*coordinate)
	nl.each do |loc|
		unless self[loc].nil?
			neighbors << self[loc]
		end
	end
	return neighbors;
end

#parse_location(*location) ⇒ Object

Returns the x and y indices of a given location, as indices or chess notation.

:call-seq:

parse_location(chess_notation_str)    -> x, y
parse_location(x, y)                  -> x, y


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gambit/tools/board.rb', line 86

def parse_location( *location )
	if location.length == 1
		letters = location.first[/[a-z]+/].split("").
		                   map { |l| l[0] - ?a }.reverse
		total = 0
		letters.each_with_index do |letter, index|
			total += letter + index * 26
		end
		x = total
		y = location.first[1..-1].to_i - 1
	else
		x = location[0]
		y = location[1]
	end
	return x, y
end

#rowsObject

Returns a row count.



69
70
71
# File 'lib/gambit/tools/board.rb', line 69

def rows(  )
	@board[0].length
end

#to_chess(x, y) ⇒ Object

Converts x and y to a String of chess notation.



158
159
160
# File 'lib/gambit/tools/board.rb', line 158

def to_chess( x, y )
	(x + ?a).chr + (y+1).to_s
end

#valid?(*coordinate) ⇒ Boolean

Ensures that coordinate is within the bounds of this Board. Arguments can be anything parse_location() understands.

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/gambit/tools/board.rb', line 166

def valid?( *coordinate )
	x,y = parse_location(*coordinate);
	(x >= 0 && x < cols() && y >= 0 && y < rows()) ? true : false
end