رایانشکده *** Computation Hub

رایانشکده *** Computation Hub

محاسبات ریاضی، حل مسئله های برنامه نویسی و پیاده سازی الگوریتم
رایانشکده *** Computation Hub

رایانشکده *** Computation Hub

محاسبات ریاضی، حل مسئله های برنامه نویسی و پیاده سازی الگوریتم

بازی سنگ، کاغذ، قیچی به زبان سی پلاس پلاس

به زبان سی پلاس پلاس برنامه‌ای بنویسید که کاربر بتواند با استفاده از آن با کامپیوتر پیوسته بازی سنگ، کاغذ، قیچی را انجام دهد. انتخاب کامپیوتر باید تصادفی باشد. پس از خروج کاربر از برنامه تعداد دفعات انجام بازی، برد، باخت و مساوی در اختیار او قرار می‌گیرد.

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 <iostream>

#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;

}

}


نظرات 2 + ارسال نظر
miraqa moradi دوشنبه 20 شهریور 1402 ساعت 22:47 http://Iran

cout << draw << " rounds ended in a

میرآغا دوشنبه 20 شهریور 1402 ساعت 22:44 http://Iran

kvvvjn vvb

برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد