Hacker News new | ask | show | jobs
by lostcolony 1722 days ago
It's funny. The older I've gotten, the more code I've written, and the slower I am to write code.

When I was fresh out of college the 'obvious' approach just appeared, and I was off to write it.

Now, any problem I am slow to peel apart in my mind, decide what the best way to approach it is, based on the language I'm using, what I'm feeling right now, readability, maintainability, etc.

Tic Tac Toe? Interesting; should I store board state as a 2d array, or a single array? The obvious approach would be to try and play every possible game with backtracking, but perhaps instead it would be simpler to just generate every possible permutation of 5 Xs and 4 Os? Would that work; on the one hand a badly playing O might lose after just 3 Xs, but we could still fill out the board if we 'kept playing', so it maps one to one, so maybe! Of course, then you risk situations where both X and O wins, so maybe not? Also, what about rotations; we could reduce our work by ensuring we didn't try to solve for cases that are just rotations of one another, but is the book keeping of that more work than just brute forcing it, given the constrained nature of the problem? Etc etc.

Writing working code matters, yes. But if you're looking for people who think just a few seconds before typing anything, and then seem frustrated they can't type fast enough, you're optimizing for juniors.

I hope it's all satire.

3 comments

> every possible permutation of 5 Xs and 4 Os

This would not include all possible end game states. A value is either X or O or empty. It's possible to end with empties.

Which is touched on; you don't need to evaluate game states, you merely need to count them. A game that ended with X or O before the board filled out could still have additional Xs and Os written on it; while that would "lose" the winning state, it's immaterial since you aren't interested in the state, just the outcomes (or, possibly, the ways to get there).
> every possible permutation of 5 Xs and 4 Os? Would that work

xox --- ---

corresponds to two different games depending on where you placed the first x, so no

I wasn't actually asking or pondering over it (the answer would in part be determined by what is meant by a "valid game"); I was just presenting a train of thought akin to what I would have in such a situation, examining the question in various lights as to ways to solve it, and generating clarifying questions to ask or ideas to explore.
Board state can also be stored as as sets of X positions and O positions.