r/badcode • • May 11 '22

python It works but.... NSFW

Post image
473 Upvotes

57 comments sorted by

View all comments

Show parent comment

3

u/[deleted] May 20 '22

By the way, I think there's a simpler way to do this

2

u/havens1515 May 20 '22

There are many ways to do it, but this is actually the simplest, especially without completely revamping the code from the ground up. Which is what I did in my tic tac toe game.

I can't remember exactly how it works, because I haven't looked at my code in years, but I created a "line" object, and then just check a method in each line object. It's a little more complex as a whole, but makes that main logic look simpler and easier to read.

2

u/mousepotatodoesstuff Aug 15 '22

Have you considered this?

{grid[a], grid[b], grid[c]} in ({1},{2})

This creates a set of unique numbers from the three spaces, which is then checked against two sets that correspond to winning states (all 1 or all 2 in line).