Share your repls and programming experiences

← Back to all posts
Tic Tac Toe Validation in One Line
IMayBeMe (455)

One Line Tic Tac Toe Validation

Clean code always looks like it was written by someone who cares - Robert C. Martin

Well in that case it looks like I don't care :(

What more is there to say, this program checks a tic tac toe board(3 x 3 array) and returns True if someone won, or False if it is a draw. The only twist is that it is designed to be one unreadable line with an unhealthy amount of lambdas and list comps. Hope you find my beautiful code oops a typo, meant to say ugly cool.

You have no idea how long this took. Debugging is immposible
DynamicSquid (4891)

Nice lol! I'm trying to come up with a solution that would do it in less chars. Right now you have 563 characters.

IMayBeMe (455)

@DynamicSquid ooh nice, I would really like to see it if you come up with a shorter program!

DynamicSquid (4891)

@IMayBeMe here's how far I got:

I basically store the numbers of a row/column/diagonal in a set, and check if the set's length is equal to one.

This doesn't work too well because it still returns True for rows/columns/diagonals that have all 0's.

I'm thinking of adding something like this: {}.pop() == 1 so it will only for rows that have all 1's, but I'm not sure how to add it to my current code.

I'm also not that good with Python, so I don't know how to fit it all in one line!

I might revisit this another time :)

IMayBeMe (455)

@DynamicSquid nice! As far as returning true with zeros goes, it should as I just used zero to represent “o” which would still be a win for the player using zeros