-
Notifications
You must be signed in to change notification settings - Fork 0
/
square.rb
68 lines (60 loc) · 1.89 KB
/
square.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
# square.rb
require 'colorize'
require_relative 'display'
# Creates a graph node named 'square'
class Square
attr_reader :x, :y
attr_accessor :name,
:neighbors,
:position,
:contents,
:color,
:dist,
:prev
def initialize(name)
@name = name
@neighbors = { n: nil, ne: nil, e: nil, se: nil, s: nil, sw: nil, w: nil, nw: nil }
@position = []
@contents = nil
@color = nil
@x = nil
@y = nil
# Required for Knight's Travails
@dist = 0
@prev = nil
end
# Assign square color based on position within board. Upper left & Lower right squares == whitejj
def assign_color
if (@position[0].even? && @position[1].even?) || (@position[0].odd? && @position[1].odd?)
@color = 'black'
elsif (@position[0].even? && @position[1].odd?) || (@position[0].odd? && @position[1].even?)
@color = 'white'
end
end
# Return true/false if square at beginning of row
def head_of_row?
position[1].zero?
end
# Return true/false if square occupied
def occupied?
!@contents.nil?
end
# Build square display
def pixels
pad = ' '.on_black if @color == 'black'
pad = ' '.on_white if @color == 'white'
mid = " #{@contents.name} ".on_black if @color == 'black' && !@contents.nil?
mid = " #{@contents.name} ".on_white if @color == 'white' && !@contents.nil?
# uncomment for square names in center of each square
# mid = " #{name} ".on_black if @color == 'black' && @contents.nil?
# mid = " #{name} ".on_white if @color == 'white' && @contents.nil?
mid = pad.on_black if @color == 'black' && @contents.nil?
mid = pad.on_white if @color == 'white' && @contents.nil?
{ top: pad, middle: mid, bottom: pad }
end
def assign_positions(arr)
@position = arr
@x = @position[0]
@y = @position[1]
end
end