提问者:小点点

如何让这个石头,纸,剪刀游戏从电脑上给出正确的输出?


我是C++新手,写了这段代码。 它是一个石头,纸和剪刀的游戏。 但问题是,例如,当计算机和玩家都得到“摇滚”时,计算机会说1-0或0-1。 有人知道怎么修吗?

我还想在我做更多的回合时添加记分板,但我不知道在这段代码中如何做到那一点。 有人能帮我吗?

谢啦!

#include <stdio.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int random ((rand() % 3) + 1);


string steen;
string papier;

int main(int argc, char **argv)
{
    srand(time(0)); 
    cout << "Welkom bij steen papier schaar: druk op enter om verder te gaan.";
    cin.get();
    cout <<"You are playing against the computer. your choices are 'rock' 'paper' and 'scissors'. good luck!\n\n";
    cout << "Round 1\n";
    cout << "rock paper or scissors: ";
    cin >> steen;  


    if((rand() % 3) + 1 == 1) { cout << "computer: steen";  }
    else if ((rand() % 3) + 1 == 2) { cout << "computer: papier"; }
    else if ((rand() % 3) + 1 == 3) { cout << "computer: schaar";} 

         if (random == 1 and steen == "steen") { cout << "\n1 - 1\n"; } 
    else if (random == 2 and steen == "papier") { cout << "\n1 - 1\n"; } 
    else if (random == 3 and steen == "schaar") { cout << "\n1 - 1\n"; } 
    else if (random == 1 and steen == "papier") { cout << "\n 1 - 0 \n"; } 
    else if (random == 1 and steen == "schaar") { cout << "\n0 - 1\n"; } 
    else if (random == 2 and steen == "steen") { cout << "\n0 - 1\n"; } 
    else if (random == 2 and steen == "schaar") { cout << "\n1 - 0\n"; } 
    else if (random == 3 and steen == "steen") { cout << "\n1 - 0\n"; } 
    else if (random == 3 and steen == "papier") { cout << "\n0 - 1\n"; } 
    else { cout << "syntax error\n\n" ; return 0; }



    return 0;
}

共1个答案

匿名用户

这里

 if((rand() % 3) + 1 == 1) { cout << "computer: steen";  }
    else if ((rand() % 3) + 1 == 2) { cout << "computer: papier"; }
    else if ((rand() % 3) + 1 == 3) { cout << "computer: schaar";} 

你滚动3个随机数,实际上它应该是1:

int roll = (rand() % 3)+1;

if(roll == 1) { cout << "computer: steen";  }
    else if (roll == 2) { cout << "computer: papier"; }
    else if (roll == 3) { cout << "computer: schaar";} 

然后这里:

if (random == 1 and steen == "steen") { //....
else if (random == 2 and steen == "papier") { //...
else if (random == 3 and steen == "schaar") { //...
else if (random == 2 and steen == "steen") { //...

这里您使用的是另一个随机数,但它应该与上面的相同。