-
Notifications
You must be signed in to change notification settings - Fork 0
/
play
executable file
·50 lines (41 loc) · 891 Bytes
/
play
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
#!/usr/bin/env ruby
require './tic_tac_toe'
def clear_screen
print "\e[H\e[2J"
end
clear_screen
ttt = TicTacToe.new
puts "You are playing '#{ttt.player_symbol}'"
if ttt.player_symbol == TicTacToe::FIRST_PLAYER_SYMBOL
puts 'You make the first move'
else
puts 'The computer makes the first move'
end
puts ttt.board_string
while ttt.winner.nil?
while true
print 'Enter your move as "row, column": '
input = gets.chomp
unless input =~ /\d\s*,\s*\d/
puts 'Invalid input, try again'
next
end
row, col = input.split(',').map(&:strip)
unless ttt.record_player_move(row, col)
puts 'Invalid move, try again'
next
end
break
end
ttt.make_move
clear_screen
puts ttt.board_string
end
case ttt.winner
when :draw
puts 'It was a draw!'
when :computer
puts 'The computer won :('
when :player
puts 'Aww yeah, you won!'
end