-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
120 lines (98 loc) · 2.3 KB
/
game.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
require './board.rb'
require 'debugger'
ALPHAS = {
"a" => 0,
"b" => 1,
"c" => 2,
"d" => 3,
"e" => 4,
"f" => 5,
"g" => 6,
"h" => 7,
}
class Game
attr_reader :board, :players
def initialize
@board = Board.new
@players = {
red: HumanPlayer.new(:red, @board),
black: HumanPlayer.new(:black, @board)
}
@current_player = :black
end
def inspect
nil
end
def play
end
end
class HumanPlayer
attr_reader :color, :board
attr_accessor :pieces, :force_moves
def initialize(color, board)
@color, @board = color, board
update_pieces
end
def pick_piece
start_pos = nil
neighbors = []
piece_color = nil
@board.display
puts "#{@color.capitalize} player's turn,"
until start_pos != nil
puts "Please select a piece to move (Example: 2A)"
temp = gets.chomp.downcase.split("")
unless (0..7).include?(temp[0].to_i) && ALPHAS.include?(temp[1])
@board.display
puts "That's not even a proper coordinate"
start_pos = nil
next
end
start_pos = [temp[0].to_i, ALPHAS[temp[1]]]
unless @board.piece_at(start_pos) != nil # ===========> if selection is empty
@board.display
puts "There's no piece there"
start_pos = nil
next
else
piece_color = @board.piece_at(start_pos).color
if piece_color != @color # =========================> if selection is wrong color
@board.display
puts "That's not your piece"
start_pos = nil
next
end
end
nbrs = @board.find_neighbors(start_pos) # ============> finds surrounding pieces
if @board.piece_at(start_pos).can_move?
start_pos
else
@board.display
puts "You can't move that piece"
start_pos = nil
end
end
start_pos
end
def update_pieces
@pieces = []
@board.rows.each do |row|
row.each do |spot|
unless spot.nil?
@pieces << spot unless spot.color != color
end
end
end
end
def force_moves?
ans = false
@force_moves = []
self.pieces.each do |piece|
if piece.can_jump?
@force_moves << piece.pos
ans = true
end
end
ans
end
end #<===== HumanPlayerClass end