Carey Brown wrote:If you have the items 1-5 and if you consider a rule like
where value = (player1item * 10) + player2item
then you can create all the rules in a similar fashion and use a switch() statement for all the rules to determine if player 1 won. If there is no winner, then switch p1 and p2, i.e. value = player1item + (player2iitem * 10), and go through a second switch of rules. If a match is found then player 2 wins. If neither 1 or 2 wins then it is a tie.
Carey Brown wrote:You were close. You just had to put a statement that says IF ANY of these RULES match THEN player 1 wins.
Katherine Johnson wrote:Would this be required within each case statement like this:
I'm not sure what you're thinking here. I don't see any place where subtracting 10 would serve a purpose.Katherine Johnson wrote:I should code statements to subtract 10 from the global variable?
Katherine Johnson wrote:Would this be required within each case statement like this:
It is rather unusual for fallthrough to be useful, so all switch statements using fallthrough should have a comment saying where the fallthrough is wanted; something like “// fallthrough intended” is usually sufficient. Otherwise somebody reading your code will assume the fallthrough is a mistake and “correct” the code by adding break;Carey Brown wrote:. . . you can use "fall through" to your advantage in this case . . .
Carey Brown wrote:Enough time has passed so I don't think posting the answer should be an issue.
Carey Brown wrote:... but they would be more complicated if part of the requirements were to present the winning rule in textual format.
salvin francis wrote:Here's my variation using enums, all the magic of numbers, etc.. are encapsulated within the enum and only a single method is public:
Carey Brown wrote:I find these approaches academically interesting but they would be more complicated if part of the requirements were to present the winning rule in textual format.
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|