-
Notifications
You must be signed in to change notification settings - Fork 17
/
game.rb
53 lines (43 loc) · 948 Bytes
/
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
module TicTacToe
class Game
include TicTacToe::Rules
def initialize
@board = TicTacToe::Board.new
@players = [:X, :O].cycle
end
attr_reader :board, :players, :current_player
def play
catch(:finished) do
loop do
start_new_turn
show_board
check_move { |error_message| puts error_message }
check_win { puts "#{current_player} wins" }
check_draw { puts "It's a tie" }
end
end
end
def start_new_turn
@current_player = @players.next
end
def show_board
puts board
end
def game_over
throw :finished
end
def move_input
print "\n>> "
response = gets
case response
when /quit/i
puts "Wimp!"
throw :finished
else
row, col = response.chomp.split.map { |e| e.to_i }
puts
[row, col]
end
end
end
end