What's the right way to handle "One, Both, or None" logic?

Posted by Stephen on Stack Overflow See other posts from Stack Overflow or by Stephen
Published on 2010-12-23T23:40:38Z Indexed on 2010/12/23 23:54 UTC
Read the original article Hit count: 224

I have a logic situation that is best described as two "Teams" trying to win a task. The outcome of this task could be a single winner, a tie (draw), or no winner (stalemate).

Currently, I'm using a nested if/else statement like so:

// using PHP, but the concept seems language agnostic.
if ($team_a->win()) {
    if ($team_b->win()) {
        //  this is a draw
    } else {
        //  team_a is the winner
    }
} else {
    if ($team_b->win()) { 
        //  team_b is the winner
    } else {
        //  This is a stalemate, no winner.
    }
}

This seems rather spaghetti-like and repetitive. Is there a more logical, DRY pattern I could use?

© Stack Overflow or respective owner

Related posts about php

Related posts about language-agnostic