-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.rb
118 lines (99 loc) · 3.59 KB
/
player.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
class Player
attr_reader :team_color, :board
def self.player_input(input_string)
exit if input_string.downcase == "exit"
potential_backs = %w(return back select)
raise StandardError.new("\n\nReturning to Piece select\n\n") if potential_backs.any? {|back_string| back_string == input_string.downcase}
raise SaveThrow.new("\n\nSaving game!\n\n") if input_string.downcase == "save"
input_string.gsub(/\s+/, "").upcase
end
def self.coord_string(input_string)
raise ArgumentError.new("Input must be String") unless input_string.is_a? String
input_string = Player.player_input(input_string)
raise ArgumentError.new("Input incorrect. Example: C4, B2, A3, etc.") unless input_string =~ /[A-H][1-8]/
[8-input_string[1].to_i, input_string[0].ord - 65]
end
def initialize(team_sym=nil, host_board=nil)
t = team_sym.is_a? Symbol #Boolean
b = host_board.is_a? Board #Boolean
#Raise an Error unless both both have been set correctly
raise ArgumentError.new("Requires team_color Symbol, and Board reference") unless t && b
@team_color = team_sym
@board = host_board
end
def turn
begin
puts "\n\n IT IS NOW #{@team_color.upcase}'S TURN\n\n"
@board.display
puts "Please input the coordinates of piece to grab"
start_coord = Player.player_input(gets.chomp)
start_piece = select(start_coord)
#def moves(input_rows, start_pos, distance = @distance, display = true)
moves = start_piece.moves(start_coord)
raise StandardError.new("\n\nNo moves available for piece at #{start_coord}, returning to Piece select\n\n".upcase) if moves.empty?
acceptable_pos = ""
moves.each {|coord| acceptable_pos += "\t#{coord}\n"}
puts "You may move piece to:\n#{acceptable_pos}"
puts "Please enter destination coordinates"
destination_coord = Player.player_input(gets.chomp)
raise ArgumentError.new("Destination is invalid") unless moves.include?(destination_coord)
move(start_coord, destination_coord)
rescue StandardError => m
puts "#{m}"
retry
end
end
def checkmate?
our_king = find_king
return true if our_king.is_a? Array #AKA, does not find any king, as in it was taken.
king_moves = our_king.moves(false)
own_space = our_king.self_threatened
return true if king_moves.empty? && own_space
false
end
def stalemate?
@board.flip(@team_color)
our_pieces = gather_pieces
return our_pieces.all? {|piece| piece.moves(false).empty?}
end
def select(input_string)
piece = @board.select(input_string)
raise StandardError.new("Space is empty") if piece == " "
raise StandardError.new("Not your piece") unless piece.team_color == @team_color
piece
end
def move(start, destination, board_array = @board.rows)
start_piece = select(start)
#def moves(input_rows, start_pos, distance = @distance, display = true)
start_row, start_pos = Player.coord_string(start)
dest_row, dest_pos = Player.coord_string(destination)
board_array[start_row][start_pos] = " "
board_array[dest_row][dest_pos] = start_piece
start_piece.move(destination)
end
private
def gather_pieces
pieces = []
@board.rows.each do |row|
row.each do |obj|
if obj.is_a? Piece
if obj.team_color == @team_color
pieces << obj
end
end
end
end
pieces
end
def find_king
@board.rows.each do |row|
row.each do |obj|
if obj.is_a?(King)
if obj.team_color == @team_color
return obj
end
end
end
end
end
end