-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chess_board.rb
executable file
·152 lines (126 loc) · 3.83 KB
/
Chess_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
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
require './Chess_piece'
class ChessBoard
attr_accessor :board, :kings, :black_king, :white_king
def initialize
@board = Array.new(8) { |row| Array.new(8) { nil } }
setup
end
def setup
piece_order = [Castle,Knight,Bishop,Queen,King,Bishop,Knight,Castle]
piece_order.each_with_index do |piece, index|
piece.new(self, [7, index], :w)
#Pawn.new(self, [6, index], :w)
piece.new(self, [0, index], :b)
Pawn.new(self, [1, index], :b)
end
@kings = {:b => self.board[0][5], :w => self.board[7][5]}
self.show
nil
end
def inspect
return ''
end
def populate_board
# Queen.new(self,[7,0], :w)
@black_king = King.new(self, [0,0], :b)
@black_pawn1 = Pawn.new(self, [1,0], :b)
@black_pawn1 = Pawn.new(self, [1,1], :b)
@white_pawn1 = Pawn.new(self, [1,1], :w)
@white_king = King.new(self, [7,7], :w)
@kings = {:b => self.black_king, :w => self.white_king}
Castle.new(self, [7,1], :w)
nil
end
def show
system("clear")
puts "\n\n\n\n\n\n"
puts "\t-- 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 --"
puts "\t-----------------------------------"
board.each_with_index do |row,index|
out = "\t#{index}|"
row.each do |item|
if item.nil?
out += " |"
else
out += item.class.to_s[0..1]+item.color.to_s.upcase+ "|"
end
end
puts out
puts "\t"+("-"*(out.length))[0..-2]
end
puts "\n\n"
return nil
end
def []=(pos, value)
row, col = pos
board[row][col] = value
end
def [](pos)
row, col = pos
board[row][col]
end
def move(start_pos,end_pos) # assumes color will not try to move opponents piece
piece = board[start_pos[0]][start_pos[1]]#refactor this line
if piece.possible_moves.include?(end_pos) && !invalid_move?(piece, end_pos)
piece.pos = end_pos
self[end_pos] = piece
self[start_pos] = nil
else
raise "This piece can't move here!"
end
end
def dumb_move(start_pos,end_pos) #modify as version of regular move
piece = board[start_pos[0]][start_pos[1]]#find_piece_at(start_pos)
piece.pos = end_pos
self[end_pos] = piece
self[start_pos] = nil
end
def all_active_pieces(color) #refctor to take optional block?
board.flatten.compact.keep_if { |piece| piece.color == color}
end
#
# def active_black_pieces
# all_active_pieces.keep_if { |piece| piece.color == :b}
# end
#
# def active_white_pieces
# all_active_pieces.keep_if { |piece| piece.color == :w}
# end
def checked?(color)
enemy_strike_zone(color).include? (kings[color].pos)
end
def check_mated?(color)
strike_zone = enemy_strike_zone(color)
return false unless kings[color].possible_moves.all? { |move| strike_zone.include? move }
#use as a shortcut to break from checkmate if king can escape
#(place in seperate method)
enemy = color == :w ? :b : :w
pieces = all_active_pieces(color)
pieces.each do |piece|
return false unless piece.possible_moves.all? { |move| invalid_move?(piece, move) }
end
true
end
private
def invalid_move?(piece, destination)
test_board = Marshal.load(Marshal.dump(self))
test_board.dumb_move(piece.pos, destination)
test_board.checked?(piece.color)
end
def enemy_strike_zone(color)# REFACTOR!
strike_zone = []
enemy_color = color == :w ? :b : :w
all_active_pieces(enemy_color).each do |piece|
strike_zone += piece.possible_moves #if piece.color == enemy_color
end
strike_zone
end
def find_piece_at(target_pos) #refactor
#fix by adjusting all active piece call
all_active_pieces.each { |piece| return piece if piece.pos == target_pos}
raise "There is no piece there!"
end
end
#unless $PROGRAM_NAME == __File__
#load './Chess_board.rb'; game = ChessBoard.new; game.populate_board; game.board
#end