Задача на С++ (Напишите игру Rock, paper, scissors, lizard, Spock.)
Здрасте,я написал игру,но теперь, нужно ее модернизировать, я так и не понял что от меня хотят, надеюсь поможете вот:
Реализуйте программу таким образом, чтобы пользователь мог повторить игру столько раз, сколько захочет. Выведите сообщение типа Try again? и варианты ответа: Yes ([y]) и No ([n]).
По завершению игры выведите статистику по игре. Количество побед user, computet и сколько раз сыграли в ничью.
Я так и непонял что от меня хотят. Ответьте мне пожалуйста или скиньте код с этим модификациями.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int number;
cout << "Enter number: ";
cin >> number;
int count = 0;
while (number>0 || number < 0) {
number /= 10;
count++;
}
cout << count << endl;*/
char exit;
int count = 0;
while (true)
{
cout << "Hello" << endl;
count++;
while (true)
{
cout << "Try again? [y] - yes; [n] - no: ";
cin >> exit;
if (exit == 'y' || exit == 'n')break;
else cout << "Select true option!" << endl;
}
if (exit == 'n')break;
}
cout << count << endl;
int number;
while (true) {
cout << "Enter number: ";
cin >> number;
if (number == 0)break;
if (number % 3 == 0 || number % 7 == 0)continue;
cout << number << endl;
}
int answer;
do {
cout << "2 + 2 = ?:";
cin >> answer;
} while (answer != 4);
char again = ' ';
do {
int number = 100 / 2;
int step = 100 / 2;
char answer;
int count = 0;
while (true) {
count++;
cout << "Number is [>][<][=] " << number << ": ";
cin >> answer;
if (answer == '>') {
step /= 2;
if (step == 0) step = 1;
number += step;
}
else if (answer == '<') {
step /= 2;
if (step == 0) step = 1;
number -= step;
}
else if (answer == '=') {
cout << "Your magic number " << number << endl;
break;
}
if (count == 10) { cout << "Error!!!" << endl; break; }
}
while (true)
{
cout << "Try again [y] - yes; [n] - no: ";
cin >> again;
if (again == 'y' || again == 'n')break;
else cout << "Select true option!" << endl;
}
} while (again != 'n');
int number = 123456; // 345612
int ofsset = 2;
int new_number = 0;
int start, end; // start = 3456; end = 12
cout << "Enter number: ";
cin >> number;
cout << "Set ofsset: ";
cin >> ofsset;
int count = 0,n=1, flag = number;
while (number > 0) {
number /= 10;
count++;
}
number = flag;
for (int i = 0; i < count - ofsset; i++) {
n *= 10;
}
start = number % n; // 10 * 10 * 10 * 10
end = number / n;
n = 1;
for (int i = 0; i < ofsset; i++) {
n *= 10;
}
cout << n << endl;
new_number = start * n + end;
3456 * 100 + 12// 10 * 10
new_number = start * 100 + end;
cout << number << " => " << new_number << endl;
srand(time(0));
cout << "Rock, scissors, paper" << endl;
cout << "Start Game" << endl;
int user_point = 0, comp_point = 0;
char user_select = ' ', comp_select = ' ';
for (int i = 1; i <= 3; i++) {
cout << "Round " << i << endl;
while (true) {
cout << "Select choice:\n\t\t[r] - rock;\n\t\t[s] - scissors;\n\t\t[p] - paper;\n\tInput: ";
cin >> user_select;
if (user_select == 'r' || user_select == 's' || user_select == 'p')break;
else cout << "Select true option!" << endl;
}
switch (1 + rand() % 3)
{
case 1: comp_select = 'r'; break;
case 2: comp_select = 's'; break;
case 3: comp_select = 'p'; break;
}
cout << "Computer select: " << comp_select << endl;
if (user_select == comp_select) {
cout << "Draw!!" << endl;
}
else if (user_select == 'r' && comp_select == 's') {
user_point++;
cout << "User win" << endl;
}
else if (user_select == 's' && comp_select == 'p') {
user_point++;
cout << "User win" << endl;
}
else if (user_select == 'p' && comp_select == 'r') {
user_point++;
cout << "User win" << endl;
}
else {
comp_point++;
cout << "Computer wins!" << endl;
}
}
if (user_point == comp_point) {
cout << "Draw" << endl;
}
else if (user_point > comp_point) {
cout << "Congratulation!!!! User win!" << endl;
}
else {
cout << "Sorry ...... Computer wins" << endl;
}
int min = 5;
int max = 10;
cout << min + rand() % (max - min + 1) << endl;
cout << rand() << endl;
}
Ответы (1 шт):
Например вы можете сделать так:
int main() {
setlocale(LC_ALL, "Russian");
int playerWins = 0, draw = 0, computerWins = 0;
char next = 'y';
cout << "Rock, scissors, paper" << endl;
cout << "Start Game" << endl;
while (next == 'y') {
cout << "Ваша игра!" << endl;
game_func(playerWins, draw, computerWins);
do {
cout << "Продолжить? (y/n): ";
cin >> next;
} while (next != 'y' && next != 'n');
}
cout << "Результаты: " << endl;
cout << "Вы победили " << playerWins << " раз." << endl;
cout << "Компьютер победил " << computerWins << " раз." << endl;
cout << "Ничьих " << draw << " раз." << endl;
return 0;
}
Желательно логику игры вынести в отдельную функцию, чтобы не нагромождать main.
В коде выше, мы по ссылке передаем в функцию игры 3 переменные, которые увеличиваются в зависимости от результата игры. И в конце когда пользователь введет n мы выводим результаты на экран.
Сама функция game_func почти не отличается от вашего кода, только добавлено увеличение переменных, которые отвечают за результат игры в конце:
void game_func(int& playerWins, int& draw, int& computerWins) {
int user_point = 0, comp_point = 0;
char user_select = ' ', comp_select = ' ';
for (int i = 1; i <= 3; i++) {
cout << "Round " << i << endl;
while (true) {
cout << "Select choice:\n\t\t[r] - rock;\n\t\t[s] - scissors;\n\t\t[p] - paper;\n\tInput: ";
cin >> user_select;
if (user_select == 'r' || user_select == 's' || user_select == 'p')break;
else cout << "Select true option!" << endl;
}
switch (1 + rand() % 3)
{
case 1: comp_select = 'r'; break;
case 2: comp_select = 's'; break;
case 3: comp_select = 'p'; break;
}
cout << "Computer select: " << comp_select << endl;
if (user_select == comp_select) {
cout << "Draw!!" << endl;
}
else if (user_select == 'r' && comp_select == 's') {
user_point++;
cout << "User win" << endl;
}
else if (user_select == 's' && comp_select == 'p') {
user_point++;
cout << "User win" << endl;
}
else if (user_select == 'p' && comp_select == 'r') {
user_point++;
cout << "User win" << endl;
}
else {
comp_point++;
cout << "Computer wins!" << endl;
}
}
if (user_point == comp_point) {
//Увеличиваем счетчик ничьих
++draw;
cout << "Draw" << endl;
}
else if (user_point > comp_point) {
//Увеличиваем счетчик побед игрока
++playerWins;
cout << "Congratulation!!!! User win!" << endl;
}
else {
//И так же компьютера.
++computerWins;
cout << "Sorry ...... Computer wins" << endl;
}
}
Так же вместе ссылок можно использовать глобальные переменные. Удачи :)