-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.rb
54 lines (46 loc) · 989 Bytes
/
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
class TicTacToe
X_MARKER = "x".downcase
O_MARKER = "o".downcase
EMPTY_SPACE = "-"
def initialize(file)
@file = file
@winner = winner
# @player_x = player_x
# @player_o = player_o
end
def play
open_file
check_rows_for_winner
check_columns_for_winner
end
private
def open_file
File.open(file, "r")
IO.foreach(file) do |line|
rows << line.chomp.split("") #adds each stripped line to the rows array
end
end
def check_rows_for_winner
rows.each do |row|
if row.count(X_MARKER) == 3
@winner = player_x
elsif row.count(O_MARKER) == 3
@winner = player_o
else
@winner = "No winner"
end
end
def check_columns_for_winner
row.transpose
check_rows_for_winner
end
def check_diaganol_for_winner
end
def display_winner
puts "#{@winner} is the winner!"
end
end
end
file = "board"
tictactoe = TicTacToe.new(file)
tictactoe.play