# Programming Challenge: Rock, Paper, Scissors

• SpooF
• ٩๏̯͡๏۶
• Joined: May 22, 2004
• Posts: 3415
• Loc: Richland, WA
• Status: Offline

December 3rd, 2010, 4:17 pm

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
#define NULL (::rand() % 2)
• Anonymous
• Bot
• Joined: 25 Feb 2008
• Posts: ?
• Loc: Ozzuland
• Status: Online

December 3rd, 2010, 4:17 pm

• Bogey
• Bogey
• Joined: Jul 14, 2005
• Posts: 8212
• Loc: USA
• Status: Offline

December 3rd, 2010, 4:59 pm

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.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
• SpooF
• ٩๏̯͡๏۶
• Joined: May 22, 2004
• Posts: 3415
• Loc: Richland, WA
• Status: Offline

December 3rd, 2010, 5:47 pm

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
#define NULL (::rand() % 2)
• Bogey
• Bogey
• Joined: Jul 14, 2005
• Posts: 8212
• Loc: USA
• Status: Offline

December 3rd, 2010, 9:43 pm

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. ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
• spork
• Brewmaster
• Joined: Sep 22, 2003
• Posts: 6134
• Loc: Seattle, WA
• Status: Offline

December 5th, 2010, 2:10 pm

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
The Beer Monocle. Classy.
• SpooF
• ٩๏̯͡๏۶
• Joined: May 22, 2004
• Posts: 3415
• Loc: Richland, WA
• Status: Offline

December 6th, 2010, 10:28 am

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
#define NULL (::rand() % 2)
• spork
• Brewmaster
• Joined: Sep 22, 2003
• Posts: 6134
• Loc: Seattle, WA
• Status: Offline

December 6th, 2010, 11:30 am

Oh, I read the question completely wrong. I took two player names and generated random values for the "throws".
The Beer Monocle. Classy.
• joebert
• Sledgehammer
• Joined: Feb 10, 2004
• Posts: 13458
• Loc: Florida
• Status: Offline

December 6th, 2010, 4:35 pm

That's pretty slick Spoof. I gave it a whirl with bitwise operators, but didn't get anywhere with them.
Strong with this one, the sudo is.

## Post Information

• Total Posts in this topic: 8 posts
• Users browsing this forum: No registered users and 131 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

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.