I need to create a method that returns “X” if X won, “O” if O won,
and ‘nil’ if no win.
board = [“O”, “X”, “O”, “X”, “X”, “O”, “X”, “O”, “X”]
WIN_COMBINATIONS =
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[2, 4, 6],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8]]
def won?(board)
combo = 0
while combo < WIN_COMBINATIONS.length
current_combo = WIN_COMBINATIONS[combo]
win1 = current_combo.all? { |position| board[position] == "X" }
win2 = current_combo.all? { |position| board[position] == "O" }
if win1 == true || win2 == true
return current_combo
else
false
end
combo += 1
end
end
def winner(board)
#need to figure out who won
#need to know if a game was won
#won?(board) will return the winning combo
#we can check the combo against the board and see if they’re X’s or
O’s
#we really need to only check if the first one is an X
won = won?(board)
type = won[0]
if won == true
return board[type]
else nil
end
endboard = [“O”, “X”, “O”, “X”, “X”, “O”, “X”, “O”, “X”]
WIN_COMBINATIONS =
[[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[2, 4, 6],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8]]
def won?(board)
combo = 0
while combo < WIN_COMBINATIONS.length
current_combo = WIN_COMBINATIONS[combo]
win1 = current_combo.all? { |position| board[position] == "X" }
win2 = current_combo.all? { |position| board[position] == "O" }
if win1 == true || win2 == true
return current_combo
else
false
end
combo += 1
end
end
def winner(board)
won = won?(board)
type = won[0]
if won == true
return board[type]
else nil
end
end
Basically, the method returns ‘nil’ when won?(board) is true. If I write
the method in the following manner, however…
def winner(board)
won = won?(board)
type = won[0]
return board[type]
end
The first 2 parts of the methods work. But it doesn’t return ‘nil’ when
it’s supposed to, obviously.
if won == true
return board[type]
else nil
end
I feel like it’s not understanding the first line of the above block,
but can’t figure out why.
Thank you ahead of time for you time!