Skip to content

Instantly share code, notes, and snippets.

@danielbonnell
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbonnell/6769fb41e2f5f4603fd9 to your computer and use it in GitHub Desktop.
Save danielbonnell/6769fb41e2f5f4603fd9 to your computer and use it in GitHub Desktop.
tic-tac-to challenge
def winner?(board)
@match = false
def check(type)
@match = true if type.uniq.length == 1 && !type.include?(" ")
end
board.each do |row|
check(row)
end
board.transpose.each do |col|
check(col)
end
diag1, diag2 = board.map.with_index { |row, i| [row[i], row[- i - 1]] }.transpose
check(diag1)
check(diag2)
@match
end
@HeroicEric
Copy link

Line 8:

(horizontal == "xxx" || horizontal == "ooo") ? h_match = true : false

This is the same as:

h_match = horizontal == "xxx" || horizontal == "ooo"

horizontal == "xxx" || horizontal == "ooo" is already going to return true or false.

@HeroicEric
Copy link

Line 18:

return true if h_match || v_match || d_match || d_match_2 else false

is the same as

return h_match || v_match || d_match || d_match_2

The result of that is already going to be true or false.

Which is also going to be returned implicitly since it's the last return value in the method Which means you don't have to explicitly write return

@danielbonnell
Copy link
Author

I wasn't able to get your line 8 suggestion implemented. It just won't work no matter how I play with it in the editor. Thanks for the other tips though. I simplified the code just a bit more, although launchcop is still giving is 8/7 for complexity (down from 11/7).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment