# Programming Challenge: Rock, Paper, Scissors

• ٩๏̯͡๏۶
• Posts: 3423
• Loc: Richland, WA

3+ Months Ago

This might be a dud but I'm wondering how many ways can you write a function to tell you the outcome of who would win in a game of Rock Paper, Scissors.

Argument choose is yours, the function just has to return some sort of answer that would tell you who won the game, aka player 1 or player 2 or a draw.

* The function must take two player inputs in some fashion and return who won.

And no, Rock can't bust through paper

I have a solution that I'll post at a later date (Its quite a *simple* solution). Come up with as many ways as you can, any language can be submitted, once a solution has been posted, try to solve it in a different way.

If your not familiar with the game: http://en.wikipedia.org/wiki/Rock-paper-scissors
• Genius
• Posts: 8485
• Loc: USA

3+ Months Ago

PHP Code: [ Select ]
<?php
\$solutions = array('1' => 'Player 1', '2' => 'Player 2', '3' => 'Draw');
\$id = rand('1','3');
echo \$solutions[\$id];
?>
1. <?php
2. \$solutions = array('1' => 'Player 1', '2' => 'Player 2', '3' => 'Draw');
3. \$id = rand('1','3');
4. echo \$solutions[\$id];
5. ?>
There you go Took me 5 seconds.
• ٩๏̯͡๏۶
• Posts: 3423
• Loc: Richland, WA

3+ Months Ago

Maybe I should clarify it actually has to take two player inputs as arguments and return the winner.

Whether you pass those values by reference in an array, class, struct or anything else is up to you. Besides Bogey, that's not a function
• Genius
• Posts: 8485
• Loc: USA

3+ Months Ago

Fine, here is something I've created that took me 2 minutes
PHP Code: [ Select ]
<?php
function ropasci(\$pl1, \$pl2)
{
if(\$pl1 == 'rock' && \$pl2 == 'paper')
{
return 'Player 2 wins: paper over rock';
}
elseif(\$pl1 == 'rock' && \$pl2 == 'scissors')
{
return 'Player 1 wins: Rock over scissors';
}
elseif(\$pl1 == 'paper' && \$pl2 == 'rock')
{
return 'Player 1 wins: paper over rock';
}
elseif(\$pl1 == 'paper' && \$pl2 == 'scissors')
{
return 'Player 2 wins: Scissors over paper';
}
elseif(\$pl1 == 'scissors' && \$pl2 == 'rock')
{
return 'Player 2 wins: Rock over scissors';
}
elseif(\$pl1 == 'scissors' && \$pl2 == 'paper')
{
return 'Player 1 wins: Scissors over paper';
}
elseif(\$pl1 == \$pl2)
{
return "Draw: \$pl1 equals \$pl2";
}
else
{
return false;
}
}
?>
1. <?php
2. function ropasci(\$pl1, \$pl2)
3. {
4.     if(\$pl1 == 'rock' && \$pl2 == 'paper')
5.     {
6.         return 'Player 2 wins: paper over rock';
7.     }
8.     elseif(\$pl1 == 'rock' && \$pl2 == 'scissors')
9.     {
10.         return 'Player 1 wins: Rock over scissors';
11.     }
12.     elseif(\$pl1 == 'paper' && \$pl2 == 'rock')
13.     {
14.         return 'Player 1 wins: paper over rock';
15.     }
16.     elseif(\$pl1 == 'paper' && \$pl2 == 'scissors')
17.     {
18.         return 'Player 2 wins: Scissors over paper';
19.     }
20.     elseif(\$pl1 == 'scissors' && \$pl2 == 'rock')
21.     {
22.         return 'Player 2 wins: Rock over scissors';
23.     }
24.     elseif(\$pl1 == 'scissors' && \$pl2 == 'paper')
25.     {
26.         return 'Player 1 wins: Scissors over paper';
27.     }
28.     elseif(\$pl1 == \$pl2)
29.     {
30.         return "Draw: \$pl1 equals \$pl2";
31.     }
32.     else
33.     {
34.         return false;
35.     }
36. }
37. ?>
• Brewmaster
• Posts: 6297
• Loc: Seattle, WA

3+ Months Ago

winner :: String -> String -> String
winner p1 p2
| p1c - p2c == 1 || p1c - p2c == -2 = p1
| p1c == p2c = "Tie"
| otherwise p2
where
p1c = rand 2
p2c = rand 2
1. winner :: String -> String -> String
2. winner p1 p2
3.   | p1c - p2c == 1 || p1c - p2c == -2 = p1
4.   | p1c == p2c = "Tie"
5.   | otherwise p2
6.   where
7.     p1c = rand 2
8.     p2c = rand 2
• ٩๏̯͡๏۶
• Posts: 3423
• Loc: Richland, WA

3+ Months Ago

This is what I came up with after a while

Code: [ Select ]
function name(p1,p2) {
return (3+p1-p2) % 3;
}
1. function name(p1,p2) {
2.   return (3+p1-p2) % 3;
3. }

Returns '1' - Player one has won.
Returns '2' - Player two has won.
Returns '3' - Tie between players.

Where p1, p2 are:
0 - Rock
1 - Paper
2 - Scissors
• Brewmaster
• Posts: 6297
• Loc: Seattle, WA

3+ Months Ago

Oh, I read the question completely wrong. I took two player names and generated random values for the "throws".
• Genius
• Posts: 13511
• Loc: Florida

3+ Months Ago

That's pretty slick Spoof. I gave it a whirl with bitwise operators, but didn't get anywhere with them.

## Post Information

• Total Posts in this topic: 8 posts
• Users browsing this forum: No registered users and 39 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum