故障功能程序

时间:2013-11-29 23:55:15

标签: visual-c++

我的摇滚纸剪刀游戏代码遇到了麻烦,似乎无法缩小它的真实含义。我相信displayResults函数和getComputerchoice函数存在问题。对不起,我对这个班级很新,并没有找任何人为我编写代码,但我很欣赏任何指向正确方向的指针。

#include <iostream>
#include <string>
using namespace std;

int getComputerChoice ();
int getUserChoice ();
int determineWinner (int userchoice, int compchoice);
void displayResults (int &userchoice, int &compchoice, int &wincode);
string convertchoice (int userchoice, int compchoice);
string convertwin (int wincode);



int main ()
{
    int compchoice, userchoice, wincode;
    string compname, username, winname;

    cout << "Game Menu" << endl;
    cout << "---------" << endl;
    cout << "1) Rock" << endl;
    cout << "2) Paper" << endl;
    cout << "3) Scissors" << endl;
    cout << "4) Quit" << endl;
    cout << " " << endl;
    userchoice = getUserChoice ();
    compchoice = getComputerChoice ();
    while (userchoice != 4)
    {   
        wincode = determineWinner (userchoice, compchoice);

        displayResults (userchoice, compchoice, wincode);

        cout << " " << endl;
        cout << "=======================================" << endl;
        cout << "You selected: " << userchoice << endl;
        cout << "The computer selected: " << compchoice << endl;
        cout << "Game Winner: " << wincode << endl;
        cout << "=======================================" << endl;
        cout << "Game Menu" << endl;
        cout << "---------" << endl;
        cout << "1) Rock" << endl;
        cout << "2) Paper" << endl;
        cout << "3) Scissors" << endl;
        cout << "4) Quit" << endl;
        cout << " " << endl;
        cout << "Enter your choice: " << endl;
        cin >> userchoice;
        compchoice = getComputerChoice ();
    }

    return 0;
}


int getUserChoice ()
{
    int userchoice;
    cout << "Enter your choice: " << endl;
    cin >> userchoice;
    return userchoice;

}

int getComputerChoice ()
{
    srand (time(NULL));
    compchoice = rand() % 3 + 1;
    return compchoice;
}

int determineWinner (int userchoice, int compchoice)
{   if (compchoice == userchoice)
    {   return 0;
    }
    else if (compchoice == 1 && userchoice == 3)
    {   return 2;
    }
    else if (userchoice == 1 && compchoice == 3)
    {   return 1;
    }
    else if (compchoice == 3 && userchoice == 2)
    {   return 2;
    }
    else if (userchoice == 3 && compchoice == 2)
    {   return 1;
    }
    else if (compchoice == 2 && userchoice == 1)
    {   return 2;
    }
    else if (userchoice == 2 && compchoice == 1)
    {   return 1;
    }
}

void displayResults (int &userchoice, int &compchoice, int &wincode)
{
    username = string convertchoice (userchoice);
    compname = string convertchoice (compchoice);
    winname = string convertwin (wincode);
}

string convertchoice (int userchoice, int compchoice)
{
    int choice;
    userchoice = choice;
    compchoice = choice;
    if (choice == 1)
    {   return "Rock";
    }
    else if (choice == 2)
    {   return "Paper";
    }
    else if (choice == 3)
    {   return "Scissors";
    }
}

string convertwin (int wincode)
{
    if (wincode == 1)
    {
        return "User";
    }
    else if (wincode == 2)
    {
        return "Computer";
    }
    else if (wincode == 0)
    {
        return "Tie";
    }
}

0 个答案:

没有答案