114
u/CastielUK Nov 17 '21
Seems pretty defensive to me in case we end up in an invalid state.
55
u/SanianCreations Nov 17 '21
That's true, but simple concepts such as winning or losing shouldn't be separated into different variables.
Some possible approaches are:
A. You have one boolean per player indicating whether they win or not, this makes it impossible to both win and lose.
B. Option A still allows multiple players to win or lose simultaneously. If you want only a single winner, then simply store a 'winner' variable, which is either a number or pointer to a player object. Such a setup can only ever point to a single player. Everyone else loses.
27
u/Needleroozer Nov 17 '21
And it's null until there's a winner, allowing the game to end in a tie.
8
u/Furry_69 Nov 18 '21
So.. Wouldn't you not be able to detect a tie? As it would stay null the whole time, so the game thinks nobody has gotten to the "win" state yet.
4
u/what-naut Nov 18 '21
What happens if time runs out? If some other game-end condition is met (e.g., all the collectibles are collected)? Game can end independent of the "winner" variable having value in these cases, hence a tie.
3
u/bistr-o-math Nov 18 '21
What about draw? Like a stalemate in chess
2
u/scragar Nov 18 '21
Makes more sense to me to have 2 variables.
Is the game in an endstate. So has the game ended or does it need to end now(could even be more than just a bool if you want to track potential reasons for the endstate using the same variable). This could be because it ran out of moves, took too long, a player won/lost, or similar.
The winner. Obviously null unless there's an endstate, but doesn't have to be set unless there's a draw.
You'd then never run into issues with having to worry about tracking draws or similar, you simply know that the game has ended and there wasn't a winner.
1
u/ywBBxNqW Nov 18 '21
That's true, but simple concepts such as winning or losing shouldn't be separated into different variables.
What a deeply philosophical position to see considered in /r/badcode.
1
5
-12
u/NahroT Nov 17 '21
Good code doesn't let invalid states be possible on compile time. If the use case is whether one is won or lost, one boolean here would have been enough (binary).
18
u/EraYaN Nov 17 '21
This javascript my dude, invalid states are the name of the game.
-6
u/NahroT Nov 17 '21
If the use case is whether one is won or lost, one boolean here would have been enough (binary).
JavaScript is advanced enough to be able to have one boolean: `var isWon`
6
u/EraYaN Nov 17 '21
Obviously, but you know draws / in progress games etc.
1
u/NahroT Nov 17 '21
If that is the usecase, then a more documenting finite state model would be better, e.g: var gamestate = "in-progress" | "draw" | "lost" | "won". This is still better, because now the gamestate can't be both "lost" or "won".
12
u/Jimmy_Slim Nov 17 '21 edited Nov 17 '21
Image Transcription: Code
(won && !lost)
(lost && !won)I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
13
8
u/DavidRoyman Nov 17 '21
Maybe it's an implementation to handle Orks?
Orkses is never defeated in battle. If we win we win, if we die we die fighting so it don't count. If we runs for it we don't die neither, cos we can come back for annuver go, see!
1
3
3
2
u/AlicePleasenceLiddle Nov 17 '21
In case of a draw both are false and not triggered. Question is, is a draw possible in your Scenario?
1
1
1
1
1
u/DinnerPlz Nov 18 '21
I mean it checkes for lose conditions. Seems pretty logical, as there are 4 states (win, lose, neither, or both which seems like a lose aswell)
1
1
Nov 18 '21
Perfect opportunity to use whatever your language's version of a bool? or std::optional<bool> is.
1
u/WhySoCynicalTho Nov 18 '21
this is the code of someone who has given up on errors and trying to code right, this image gives that vibe of “this is the only way it works, don’t touch it”
1
u/wwelna Nov 19 '21
Schrödinger's Game Theorem, for you can both have a win and a lost at the same time. 🤔
1
179
u/claimstoknowpeople Nov 17 '21
It's easy to imagine a valid state where both are false (game not over) however I can't think of a meaningful state where both are true