Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to define a method that checks if a given space on the board is empty and address the scenarios that qualify as an empty space that isn't equal to the literal " ".

My tests failed to produce false/true, and instead provided nil.

What I have tried:

board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def position_taken?(board, index)
if board[index] == [" "] || nil
puts false
elsif board[index] != [" "]
puts true
end
end
end
Posted
Updated 2-Sep-18 4:39am

Quote:
My tests failed to produce false/true, and instead provided nil.

The reason is that your code do not cover all cases.
Try something like this:
VB
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def position_taken?(board, index)
    if board[index] == [" "] || nil # not sure what this is supposed to be
        puts false
    elsif board[index] != [" "]
        puts true
    else
        # put a warning message here and you will have surprise
    end
end
end

The reason is that board[index] == [" "] || nil and board[index] != [" "] are not complementary conditions.
In case second condition should be complement of first, the code should be simply:
VB
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def position_taken?(board, index)
    if board[index] == [" "] || nil
        puts false
    elsif board[index] != [" "]
    else
        puts true
    end
end
end

Nota: I have never used Ruby, I just know common usages in other languages.
 
Share this answer
 
v3
def position_taken?(board, index)
if board[index] == " " || board[index] == nil || board[index] == ""
return false
elsif board[index] != " "
return true
end
end

Thank you for your help, I ended up finding this solution worked and what the problem was by seeing how you interpreted my code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900