PDA

View Full Version : problem in c++



kramer
10-02-2007, 08:22 PM
can anyone help me turn this 2 player game of paper, rock and scissor into a player versus computer type?please..i badly need it..


# include <iostream>
using namespace std;
int main () {
char a,b;
int d,e;
char ans;
do {
cout << "This is a game called Jack en Poy\n";
cout <<"Player *, please select your move:" << endl
<< "*. Rock" << endl
<< "2. Scissors" << endl
<< "*. Paper";
cout << "What is your move?\n";
cin >> d;
if (d==*) {
cout << "Player *'s move is rock.";
}
else if (d==2){
cout << "Player *'s move is scissors.";
}
else if (d==*){
cout << "Player *'s move is paper.";
}
cout << "\nPlayer 2, please select your move:"<< endl
<< "*. Rock" << endl
<< "2. Scissors" << endl
<< "*. Paper";
cout << "\nWhat is your move?\n";
cin >> e;
if (e==*) {
cout << "Player *'s move is rock.";
}
else if (e==2){
cout << "Player *'s move is scissors.";
}
else if (e==*){
cout << "Player 2's move is paper.";
}
cout << "\nThe result is: \n";
if (d==* && e==*){
cout << "It is a tie!";
}
else if (d==2 && e==2) {
cout << "It is a tie!";
}
else if (d==* && e==*) {
cout << "It is a tie!";
}
else if (d==* && e==2){
cout << "Player * wins!";
}
else if (d==* && e==*) {
cout << "Player 2 wins!";
}
else if (d==2 && e==*) {
cout << "Player * wins!";
}
else if (d==2 && e==*){
cout << "Player 2 wins!";
}
else if (d==* && e==*) {
cout << "Player * wins!";
}
else if (d==* && e==2) {
cout << "Player 2 wins!";
}
cout << "\nRun the program again?(y/n)\n";
cin >> ans;
} while (ans!='n');
return 0;
}
:confused:

Moonbat
10-02-2007, 10:32 PM
Err, what errors are you getting exactly?

BTW, I made the exact same thing long time ago, using if-else loops. I didn't know about the swtich() statement then, it'll make your project much easier, and it would've made mine easier as well. Anyway, here's my old code:


//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()&#*7;2)+*;
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, * 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 == *)
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 == *)
{
if (compchoice == 0)
cout << "It's a tie!\n\n\n\n";
else if (compchoice == *)
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 == *)
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();
}

P.S. - Like I said, this is old code and has a lot of bad programming habits in it, like using return main. Also, I wouldn't do what you are doing (declaring two variables in one statement) it's better to just declare them one at a time, or use an array.

SyntaXmasteR
10-02-2007, 10:49 PM
Tell her c++ is a lame language to create a paper rock scissors game with. Then send her here: Paper Rock Scissors (http://boninroad.com/syntax******/games/rockpaperscissors.php)