-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
executable file
·116 lines (94 loc) · 2.69 KB
/
board.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
#!/bin/env ruby
# encoding: UTF-8
require 'debugger'
require 'colorize'
require_relative 'piece.rb'
class Board
attr_accessor :pieces, :rows
def initialize
@pieces = []
make_board
set_pieces
display
end
def inspect
nil
end
def make_board
@rows = Array.new(8) { Array.new(8) }
end
def set_pieces
@rows.each_with_index do |row, i|
row.each_with_index do |spot, j|
if i < 3 && (i+j) % 2 != 0
@rows[i][j] = Piece.new(:red, [i,j], self)
@pieces << @rows[i][j]
end
if i > 4 && (i+j) % 2 != 0
@rows[i][j] = Piece.new(:black, [i,j], self)
@pieces << @rows[i][j]
end
end
end
end
def piece_at(pos)
i, j = pos
@rows[i][j]
end
def display
system("clear")
puts" A B C D E F G H "
@rows.each_with_index do |row, i|
row_string = i.to_s + " "
row.each_with_index do |spot, j|
if (i+j) % 2 == 0
row_string << " ".colorize(:background => :white)
else
if spot == nil
row_string << " ".colorize(:background => :cyan)
elsif spot.color == :black
row_string << " ⬤ ".colorize(:color => :black, :background => :cyan) unless spot.is_kinged?
row_string << " Ⓚ ".colorize(:color => :black, :background => :cyan) if spot.is_kinged?
else
row_string << " ⬤ ".colorize(:color => :red, :background => :cyan) unless spot.is_kinged?
row_string << " Ⓚ ".colorize(:color => :red, :background => :cyan) if spot.is_kinged?
end
end
end
puts row_string
end
nil
end
def move_piece(start_pos, end_pos)
x, y = start_pos
i, j = end_pos
temp_vector = [ i - x, j - y ].map! { |x| x / (x.abs) }
if @rows[x][y].vector.include?(temp_vector) # ========> confirms vector before making move
@rows[x][y].pos = [i, j]
@rows[i][j], @rows[x][y] = @rows[x][y], @rows[i][j]
else
puts "You can't move like that"
end
display ### !!! this needs to be moved
end ### !!! display needs to be moved
def remove_piece(pos)
i, j = pos
@rows[i][j] = nil
@pieces.delete(@rows[i][j])
end
def find_neighbors(pos)
x, y = pos
piece = @rows[x][y]
return if piece.nil?
piece.neighbors = []
piece.vector.each do |i, j|
(i+x) < 8 && (j+y) < 8 ? piece.neighbors << @rows[i + x][j + y] : piece.neighbors << "oob"
end
piece.vector.each do |i, j|
i *= 2
j *= 2
(i+x) < 8 && (j+y) < 8 ? piece.neighbors << @rows[i + x][j + y] : piece.neighbors << "oob"
end
piece.neighbors
end
end