Yeah, this may be a patch for something where a structure was decided early and this was used to add in some functionality without needing to change the structure.
I can see why this was done, people forget that code is done from the top to the bottom, left to right and most people forget the power of an else statement
In Magic: the Gathering, it was possible to win and lose in some very specific scenarios, though I don't remember any. If an effect that says you win the game triggers some other effect that says you lose, that can happen. Maybe that's the point here?
If this is a game with more than two players, it's possible for you to beat one player and a third player defeat you. Thus, you have both won and lost. Perhaps it's checking if you've defeated someone and are still playing. The second one might be if you've lost and qualify for consolation prizes.
IMO the biggest problem here is using booleans (won and lost) at all. Instead, it would probably be better to have a single variable gameState which represents the current state, and then we can do different things depending on its value. This variable can be a String (with values like 'won' | 'lost' | 'draw') or maybe some kind of number scheme.
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.
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.
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.
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).
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".
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!
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”
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
114
u/CastielUK Nov 17 '21
Seems pretty defensive to me in case we end up in an invalid state.
12
u/Jimmy_Slim Nov 17 '21 edited Nov 17 '21
Image Transcription: Code
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
u/knoam Nov 17 '21
Heads I
won, tails I!lost.8
u/DavidRoyman Nov 17 '21
Maybe it's an implementation to handle Orks?
3
u/Xebrasaur Nov 17 '21
Maybe you end up getting True for both win and lose when the game is a tie?
3
u/xigxag457 Nov 17 '21
Reminds me of PDDL.
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
u/carterpape Nov 17 '21
The “else” is a tie. Are we missing something?
1
u/Noorff Nov 17 '21
yes
1
u/V0K0S06 Nov 17 '21
true && !false
1
u/Drakethos Nov 17 '21
Could be a stalemate
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
u/rleonr Nov 18 '21
Participation price when you don't lose but you don't win either? Lol
1
u/[deleted] Nov 18 '21
Perfect opportunity to use whatever your language's version of a
bool?orstd::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. 🤔