ش | ی | د | س | چ | پ | ج |
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
به زبان سی پلاس پلاس برنامهای بنویسید که کاربر بتواند با استفاده از آن با کامپیوتر پیوسته بازی سنگ، کاغذ، قیچی را انجام دهد. انتخاب کامپیوتر باید تصادفی باشد. پس از خروج کاربر از برنامه تعداد دفعات انجام بازی، برد، باخت و مساوی در اختیار او قرار میگیرد.
Write a program in C++ through which the user can play Rock-Paper-Scissors with the computer. In this program, the choices of the computer are random and on exit, the user can see the number of rounds he has played, won, lost and tied.
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
cout << "Rock Paper Scissors \n\n";
cout << "Programmer: Mohammad Rajabpur \t rajabpur.blogfa.com \n\n";
cout << "R = Rock; P = Paper; S = Scissors; \n\n";
cout << "Type any other character to exit the program. \n\n";
int counter = 0;
int win = 0;
int lose = 0;
int draw = 0;
while(true)
{
string input;
cout << "Rock! Paper! Scissors! \n";
cout << " You >> ";
cin >> input;
int n = rand() % 3 + 1;
if (n == 1)
{
cout << "Computer >> R \n";
}
else if (n == 2)
{
cout << "Computer >> P \n";
}
else if (n == 3)
{
cout << "Computer >> S \n";
}
else
{
cout << "Randomization Error!!! \n";
break;
}
if (input == "r" || input == "R")
{
if (n == 1)
{
cout << "A Draw! \n";
counter += 1;
draw += 1;
}
else if (n == 2)
{
cout << "You lose! Paper covers rock. \n";
counter += 1;
lose += 1;
}
else if (n == 3)
{
cout << "You win! Rock crushes scissors. \n";
counter += 1;
win += 1;
}
}
else if (input == "p" || input == "P")
{
if (n == 1)
{
cout << "You win! Paper covers rock. \n";
counter += 1;
win += 1;
}
else if (n == 2)
{
cout << "A Draw! \n";
counter += 1;
draw += 1;
}
else if (n == 3)
{
cout << "You lose! Scissors cuts paper. \n";
counter += 1;
lose += 1;
}
}
else if (input == "s" || input == "S")
{
if (n == 1)
{
cout << "You lose! Rock crushes scissors. \n";
counter += 1;
lose += 1;
}
else if (n == 2)
{
cout << "You win! Scissors cuts paper. \n";
counter += 1;
win += 1;
}
else if (n == 3)
{
cout << "A Draw! \n";
counter += 1;
draw += 1;
}
}
else
{
cout << "The program is terminated. \n";
cout << "You played " << counter << " rounds \n";
cout << "You won " << win << " rounds \n";
cout << "You lost " << lose << " rounds \n";
cout << draw << " rounds ended in a draw. \n";
break;
}
cout << endl;
}
}
cout << draw << " rounds ended in a
kvvvjn vvb