-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.rb
66 lines (57 loc) 路 1.5 KB
/
controller.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
class Controller
attr_reader :board, :view
def initialize(board, view)
@board = board
@view = view
@current_player = 1
@cursor_position = 0
end
def run
while true
column = get_move
drop_piece(column)
if winner?(board)
view.display_win_message(@current_player)
break
elsif stalemate?(board)
view.display_tie_message
break
else
get_next_player
end
end
end
def get_move
while true
view.display_game(board, @current_player, @cursor_position)
input = view.get_key
if input == :left && @cursor_position > 0
move_cursor(-1)
elsif input == :right && @cursor_position < (board.width - 1)
move_cursor(1)
elsif (input == :down || input == :enter) && board.valid_move?(@cursor_position)
return @cursor_position
end
end
end
def drop_piece(x)
view.animate_drop(board, @current_player, @cursor_position)
board.play_piece(@current_player, x)
end
def move_cursor(delta)
@cursor_position = @cursor_position + delta
end
def get_next_player
@current_player = (@current_player == 1) ? 2 : 1
end
def winner?(board)
lines = board.rows + board.columns + board.diagonals + board.antidiagonals
lines.any? do |line|
player_char = @current_player.to_s
!!line.map{ |sym| sym.to_s[-1] }.join("").match(/#{player_char}{4}/)
end
end
def stalemate?(board)
board.rows.flatten.all? { |cell| cell != :empty }
end
end