PDA

View Full Version : نوشتن بازی سنگ کاغذ قیچی



forogh68
پنج شنبه 05 فروردین 1395, 18:19 عصر
سلام.
دارم بازی معروف سنگ کاغذ قیچی رو با سی ++ می نویسم و می خوام که فقط هم با if نوشته بشه! این رو نوشتم واسه یه دونش ولی جواب نمیده ... چکار کنم؟

char a , b , paper , sang ;
cin >> a >> b;

if (a == 'paper' && b == 'sang')
cout << "paper ghalabe sang";

smokyshadow
پنج شنبه 05 فروردین 1395, 18:56 عصر
slm ...:لبخند::لبخند::لبخند:
moshkeletun ine ke char baraye negahdari yek charactere na ye string mese paper o sang ya char* esh konid ya string fek konam bayad syntax error gerefte bashid .agaram na ke asan
hich vaght tu if nemire ke bekhad chizi chap kone ye kode zadam goftam shayad komak kone

#include<iostream>
#include<cstring>
using namespace std;
int main() {
char* handone = " ";
char * handtwo = " ";
int countone = 0;
int counttwo = 0;
while (countone != 3 || counttwo != 3) {
cin >> handone >> handtwo;


if (handone == "paper" && handtwo == "stone" || handone == "scissor"&&handtwo == "paper" || handone == "stone"&&handtwo == "scissor") {
countone++;
}


if (handtwo == "paper" && handone == "stone" || handtwo == "scissor"&&handone == "paper" || handtwo == "stone"&&handone == "scissor") {
counttwo++;
}
}
if (countone == 3) {
cout << "handone wins!";
}
else {
cout << "handtwo wins";
}
}

forogh68
جمعه 06 فروردین 1395, 06:57 صبح
slm ...:لبخند::لبخند::لبخند:
moshkeletun ine ke char baraye negahdari yek charactere na ye string mese paper o sang ya char* esh konid ya string fek konam bayad syntax error gerefte bashid .agaram na ke asan
hich vaght tu if nemire ke bekhad chizi chap kone ye kode zadam goftam shayad komak kone

#include<iostream>
#include<cstring>
using namespace std;
int main() {
char* handone = " ";
char * handtwo = " ";
int countone = 0;
int counttwo = 0;
while (countone != 3 || counttwo != 3) {
cin >> handone >> handtwo;


if (handone == "paper" && handtwo == "stone" || handone == "scissor"&&handtwo == "paper" || handone == "stone"&&handtwo == "scissor") {
countone++;
}


if (handtwo == "paper" && handone == "stone" || handtwo == "scissor"&&handone == "paper" || handtwo == "stone"&&handone == "scissor") {
counttwo++;
}
}
if (countone == 3) {
cout << "handone wins!";
}
else {
cout << "handtwo wins";
}
}





















ممنونم از لطف شما.
ولی من فقط از if می خوام حل شه.

poker97
شنبه 07 فروردین 1395, 08:15 صبح
#include <iostream>
#include <string>
using namespace std;
int main()
{
string player1,player2, rock="rock",paper="paper",scissors="scissors";
getline (cin,player1);
getline (cin,player2);


//--------------------------------------------------------------------------------

if (player1==player2)
{
cout<<"Tie.";

}
else if (player1=="rock" && player2=="paper")
{
cout<<"Player 2 wins.";
}
else if (player1=="rock" && player2== "scissors")
{

cout<<"Player 1 wins.";
}
else if (player1=="paper" && player2=="rock")
{
cout<<"Player 1 wins.";
}
else if (player1=="paper" && player2 =="scissors")
{
cout<<"Player 2 wins.";
}
else if (player1=="scissors" && player2=="paper")
{
cout<<"Player 1 wins.";
}
else if( player1=="scissors" && player2=="rock")
{
cout<<"Player 2 wins.";
}
else if (player1!= "paper" || player1!="rock" || player1!="scissors")
{
cout<<"This is not a valid object selection.";
}
else if (player2!= "paper" || player2!="rock" || player2!="scissors")
{
cout<<"This is not a valid object selection.";
}



}

1-9-7-2
شنبه 07 فروردین 1395, 09:31 صبح
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define UNKNOWN -1
#define ROCK 0
#define PAPER 1
#define SCISSORS 2
#define RAND rand()%3
#define __ERASE__ eraser();
#define __ANTISTORM__ getchar();

void eraser (void);

int main()
{
srand (time(NULL));

char objects[3][10] = {"Rock","Paper","Scissors"};
int rock, paper, scissors, maxp, winner, wins, mode, player, c, t;

for (mode='?'; mode!='1' && mode!='2';) {
printf ("-> How'd you like this to go?\n1. Me vs Computer\n2. Computer vs Computer!\n\(1/2) ");
scanf ("%c", &mode);
__ERASE__
}

printf ("-> Max power of all: ");
scanf ("%d", &maxp);
if (maxp<1)
maxp=1;
__ERASE__ __ANTISTORM__

Start: rock=paper=scissors=maxp; winner=UNKNOWN; wins=0;
while (winner==UNKNOWN) {
printf ("Rock:%d Paper:%d Scissors:%d\n", rock, paper, scissors);
if (mode=='1') {
for (t=wins; t>0 && t<=30; --t)
putchar ('_');
printf ("Wins:%d\n", wins);
printf ("here you go with: 1(rock) 2(paper) 3(scissors) -> ");
scanf ("%d", &player);
--player;
__ERASE__
}
else
player=RAND;

switch (player) {
case ROCK:
if (rock>0) {
printf (" :::Rock vs ");
if (RAND==SCISSORS && scissors>0)
--scissors, ++wins, printf ("Scissors:::\n");
else if (RAND==PAPER && paper>0)
--rock, --wins, printf ("Paper:::\n");
else
printf ("Rock:::\n");
if (paper==0 && scissors==0)
winner=ROCK;
}
else
printf (" Error> No Rock's left!\n");
break;

case PAPER:
if (paper>0) {
printf (" :::Paper vs ");
if (RAND==ROCK && rock>0)
--rock, ++wins, printf ("Rock:::\n");
else if (RAND==SCISSORS && scissors>0)
--paper, --wins, printf ("Scissors:::\n");
else
printf ("Paper:::\n");
if (rock==0 && scissors==0)
winner=PAPER;
}
else
printf (" Error> No Paper's left!\n");
break;

case SCISSORS:
if (scissors>0) {
printf (" :::Scissors vs ");
if (RAND==PAPER && paper>0)
--paper, ++wins, printf ("Paper:::\n");
else if (RAND==ROCK && rock>0)
--scissors, --wins, printf ("Rock:::\n");
else
printf ("Scissors:::\n");
if (rock==0 && paper==0)
winner=SCISSORS;
}
else
printf (" Error> No Scissors' left!\n");
break;
}
}
if (mode=='1') {
if (wins>0)
printf ("You won the Computer!\n");
else if (wins==0)
printf ("You equal to Computer!\n");
else
printf ("You lost to Computer!\n");
}
printf ("\nThe winner object is: [%s]\n", objects[winner]);

printf ("\nTry again? (Y/N) ");
while ((c=getchar())!=EOF) {
if (c=='Y' || c=='y') {
__ERASE__
goto Start;
}
else if (c=='N' || c=='n')
break;
}

return (EXIT_SUCCESS);
}

void eraser (void) {
system ("cls");
}