PDA

View Full Version : Rock Paper Scissors in C++



Moonbat
09-27-2006, 05:53 PM
It's basically Rock Paper Scissors:D


//Rock Paper Scissors game by Moonbat
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned)time(0));
int choice;
int compchoice = (rand()%2)+1;
cout << "Welcome to ~ Rock Paper Scissors ~!! I assume you know how to play,";
cout << " so lets begin. You are playing against the computer. Type 0 for";
cout << " rock, 1 for paper, and 2 for scissors\n";
cin >> choice;

if (choice == 0)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == 1)
cout << "Paper beats rock! Sorry, you lose!\n\n\n\n";
else if (compchoice == 2)
cout << "Rock beats scissors! You win!\n\n\n\n";
}

if (choice == 1)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == 1)
cout << "Paper beats rock! You win!\n\n\n\n";
else if (compchoice == 2)
cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n";
}

if (choice == 2)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == 1)
cout << "Scissors beat paper! You win!\n\n\n\n";
else if (compchoice == 2)
cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n";
}
return main();
}

SyntaXmasteR
09-27-2006, 07:03 PM
I rewrote your program in 6 lines of code:


$class = array("0"=>'rock', "1"=> 'paper', "2"=> 'scissors');
$stats = array("00"=>'0', "01"=>'1', "02"=>'2', "10"=>'2', "11"=>'0', "12"=>'1', "20"=>'1', "21"=>'2', "22"=>'0');
$message=array("It's a tie!","You loose!","You Win!");

$player1=rand(0,2);
$player2=rand(0,2);

echo $class[$player1] . " VS " . $class[$player2] . "<br>" . $message[$stats[$player1 . $player2]];


My Program (http://boninroad.com/syntaxmaster/rockpaperscissors.php) - Hope its not to intense!

SyntaXmasteR
09-27-2006, 07:48 PM
Ohhhh Here is Rock - Paper - Scissors : V1.0 (http://boninroad.com/syntaxmaster/games/rockpaperscissors.php)

This was a little more than 6 lines but its mixed with html:


if(isset($_GET['weapon']))
{
if($_GET['weapon'] >= 0 && $_GET['weapon'] <= 2)
{
$class = array("0"=>'rock', "1"=> 'paper', "2"=> 'scissors');
$stats = array("00"=>'0', "01"=>'1', "02"=>'2', "10"=>'2', "11"=>'0', "12"=>'1', "20"=>'1', "21"=>'2', "22"=>'0');
$message=array("It's a tie!","You loose!","You Win!");

$player1=$_GET['weapon'];
$player2=rand(0,2);
?>
<img src="images\<? echo $player1; ?>.jpg" />&nbsp;&nbsp;<b>VS</b>&nbsp;&nbsp;<img src="images\<? echo $player2; ?>.jpg" />&nbsp;&nbsp;<font face="Verdana, Arial, Helvetica, sans-serif" size="+1" color="#FF0000"><? echo $message[$stats[$player1 . $player2]] ?></font>

<?
}
else echo "<h1>INVALID WEAPON BIOTCH!</h1>";
}
?>
<p>
<font face="Verdana, Arial, Helvetica, sans-serif" size="+1" color="#333333">Choose Your Weapon:</font><br />
<a href="rockpaperscissors.php?weapon=0" target="_self"><img src="images\0.jpg" /></a> &nbsp;&nbsp;
<a href="rockpaperscissors.php?weapon=1" target="_self"><img src="images\1.jpg" /></a> &nbsp;&nbsp;
<a href="rockpaperscissors.php?weapon=2" target="_self"><img src="images\2.jpg" /></a>
</p>

Moonbat
09-28-2006, 12:09 AM
But I wrote it in C++, so it took a few more lines:rolleyes:

Maybe you could add a "Code" section to the forums?

SyntaXmasteR
09-28-2006, 12:25 AM
I was thinking the same thing after I saw this post in the "Internet Privacy" section... I will request a Code section and hopefully we can get it added to the forum soon.

Moonbat
09-28-2006, 06:20 PM
Oh, ok, thanks:)