-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.rb
63 lines (53 loc) · 1.26 KB
/
tictactoe.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
require 'pry'
def draw_board(b)
system 'clear'
puts ""
puts " #{b[1]} | #{b[2]} | #{b[3]} "
puts "-----+-----+-----"
puts " #{b[4]} | #{b[5]} | #{b[6]} "
puts "-----+-----+-----"
puts " #{b[7]} | #{b[8]} | #{b[9]} "
end
def empty_positions(b)
b.select {|k, v| v == ' ' }.keys
end
def player_picks(b)
open = empty_positions(b)
puts "Pick a position #{open}"
pos = gets.chomp.to_i
b[pos] = 'X'
end
def computer_picks(b)
pos = empty_positions(b).sample
if b[5] == ' '
b[5] = 'O'
else
b[pos] = 'O'
end
end
def check_winner(b)
winning_lines = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]]
winning_lines.each do |line|
if b[line[0]] == 'O' && b[line[1]] == 'O' && b[line[2]] == 'O'
return 'Computer'
end
if b[line[0]] == 'X' && b[line[1]] == 'X' && b[line[2]] == 'X'
return 'Player'
end
end
return nil
end
board = {1 => ' ', 2 => ' ', 3 => ' ', 4 => ' ', 5 => ' ', 6 => ' ', 7 => ' ', 8 => ' ', 9 => ' '}
draw_board(board)
begin
player_picks(board)
computer_picks(board)
draw_board(board)
winner = check_winner(board)
#binding.pry
end until winner || empty_positions(board).empty?
if winner
puts "#{winner} won!"
else
puts "game ended in a tie"
end