簡単なはさみ石布ゲーム(C++実現)

29341 ワード

まず、次のC++コードを見てみましょう.それはハサミの石布のゲームを実現し、ゲームを終了するとゲームの結果を印刷します.ロックをpaperとscissorsをそれぞれ0,1,2に初期化した後(C++11の強タイプ列挙enumで実現され、かつ、三者が出現する前後順が後の判断条件に影響を与える)は、プログラムはrand()srand()を用いてランダムに応答する.プログラムはChoiceというnamespaceも導入されており、これは後の使い勝手のためだけでなく、後のプログラム拡張時に名前の衝突を引き起こすことも予防できる
#include 
#include 
#include 
#include 

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace { enum class Choice { rock, paper, scissors }; };

//         

Choice player_choice; 
Choice computer_choice; 

//             

int player_wins = 0;  
int computer_wins = 0;

string words[3] = { "rock", "paper", "scissors" };

Choice get_computer_choice();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);

int main(int argc, char* argv[])
{
    srand(time(NULL)); //       
    string input_str;
    int c;

    while (true) 
    {
        cout << "Enter Rock, Paper, Scissors, or Exit: ";
        getline(cin, input_str);

        //        

        if (input_str.size() < 1) 
        {
            cout << "Invalid input." << endl;
            continue;
        }
        c = input_str[0];
        if (c == 'R' || c == 'r')
            player_choice = Choice::rock;
        else if (c == 'P' || c == 'p')
            player_choice = Choice::paper;
        else if (c == 'S' || c == 's')
            player_choice = Choice::scissors;
        else if (c == 'E' || c == 'e')
            break;
        else 
        {
            cout << "Invalid input." << endl;
            continue;
        }

        //         ,       

        computer_choice = get_computer_choice();
        int p = (int)player_choice;
        int c = (int)computer_choice;
        cout << "You chose " << words[p];
        cout << "," << endl;
        cout << "I chose " << words[c];
        cout << "," << endl;
        decide_winner();
    }

    //      ,            

    cout << "player wins:   " << player_wins << endl;
    cout << "computer wins: " << computer_wins << endl;
    return EXIT_SUCCESS;
}

//           

Choice get_computer_choice()
{
    int n = rand0toN1(3);
    if (n == 0)
        return Choice::rock;
    if (n == 1)
        return Choice::paper;
    return Choice::scissors;
}

//        

void decide_winner()
{
    if (player_choice == computer_choice) 
    {
        cout << "Result is a tie." << endl << endl;
        return;
    }

    int p = (int)player_choice;
    int c = (int)computer_choice;
    if (p - c == 1 || p - c == -2) 
    {
        cout << get_msg(player_choice);
        cout << "YOU WIN!" << endl;
        player_wins++;
    } 
    else 
    {
        cout << get_msg(computer_choice);
        cout << "I WIN!" << endl;
        computer_wins++;
    }
    cout << endl;
}

//          

string get_msg(Choice winner)
{
    if (winner == Choice::rock)
        return string("Rock smashes scissors...  ");
    else if (winner == Choice::paper)
        return string("Paper covers rock...  ");
    else
        return string("Scissors cuts paper...  ");
}

//      0~n-1      

int rand0toN1(int n)
{
    return rand() % n;
}

上のコードでは、コンピュータは毎回rock、paper、scissorsから選択することができます.プログラムをより面白くするために、get_computer_choice()関数を修正し、プログラムが実行するたびに好みをロードし、それぞれの好みが現れる確率が異なります.
#include 
#include 
#include 
#include 

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace { enum class Choice { rock, paper, scissors }; };

//         

Choice player_choice;
Choice computer_choice;

//        

Choice comp_first_choice = Choice::rock;
Choice comp_second_choice = Choice::scissors;
Choice comp_third_choice = Choice::paper;

//             

int player_wins = 0;
int computer_wins = 0;

string words[3] = { "rock", "paper", "scissors" };

Choice get_computer_choice();
void assign_computer_prefs();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);

int main(int argc, char* argv[])
{
    srand(time(NULL)); //       
    assign_computer_prefs(); //        
    string input_str;
    int c;
    while (true) 
    {
        cout << "Enter Rock, Paper, Scissors, or Exit: ";
        getline(cin, input_str);

        //        

        if (input_str.size() < 1) 
        {
            cout << "Invalid input." << endl;
            continue;
        }
        c = input_str[0];
        if (c == 'R' || c == 'r')
            player_choice = Choice::rock;
        else if (c == 'P' || c == 'p')
            player_choice = Choice::paper;
        else if (c == 'S' || c == 's')
            player_choice = Choice::scissors;
        else if (c == 'E' || c == 'e')
            break;
        else 
        {
            cout << "Invalid input." << endl;
            continue;
        }

        //         ,       

        computer_choice = get_computer_choice();
        int p = (int)player_choice;
        int c = (int)computer_choice;
        cout << "You chose " << words[p];
        cout << "," << endl;
        cout << "I chose " << words[c];
        cout << "," << endl;
        decide_winner();
    }

    //      ,            

    cout << "player wins:   " << player_wins << endl;
    cout << "computer wins: " << computer_wins << endl;
    return EXIT_SUCCESS;
}

//          

void assign_computer_prefs()
{
    int n = rand0toN1(3);
    if (n == 0) 
    {
        comp_first_choice = Choice::rock;
        comp_second_choice = Choice::scissors;
        comp_third_choice = Choice::paper;
    } 
    else if (n == 1) 
    {
        comp_first_choice = Choice::scissors;
        comp_second_choice = Choice::paper;
        comp_third_choice = Choice::rock;
    } 
    else 
    {
        comp_first_choice = Choice::paper;
        comp_second_choice = Choice::rock;
        comp_third_choice = Choice::scissors;
    }
}


Choice get_computer_choice()
{
    int n = rand0toN1(10);
    if (n <= 4) // 50%     comp_first_choice
        return comp_first_choice;
    else if (n <= 7) // 30%     comp_second_choice
        return comp_second_choice;
    return comp_third_choice; //20%     comp_third_choice
}

//        

void decide_winner()
{
    if (player_choice == computer_choice) 
    {
        cout << "Result is a tie." << endl
             << endl;
        return;
    }

    int p = (int)player_choice;
    int c = (int)computer_choice;
    if (p - c == 1 || p - c == -2) 
    {
        cout << get_msg(player_choice);
        cout << "YOU WIN!" << endl;
        player_wins++;
    } 
    else 
    {
        cout << get_msg(computer_choice);
        cout << "I WIN!" << endl;
        computer_wins++;
    }
    cout << endl;
}

//          

string get_msg(Choice winner)
{
    if (winner == Choice::rock)
        return string("Rock smashes scissors...  ");
    else if (winner == Choice::paper)
        return string("Paper covers rock...  ");
    else
        return string("Scissors cuts paper...  ");
}

//      0~n-1      

int rand0toN1(int n)
{
    return rand() % n;
}

さらに上に加えて、プログラムを実行するたびにユーザーの選択を記録させ、数ラウンド後に統計に基づいてユーザーの好みを得た後、自分の好みを再調整することで、プレイヤーにもっと挑戦的だと感じさせる.
#include 
#include 
#include 
#include 

using std::cin;
using std::cout;
using std::endl;
using std::string;

namespace { enum class Choice { rock, paper, scissors }; };

//         

Choice player_choice;   
Choice computer_choice; 

//        

Choice comp_first_choice = Choice::rock;   
Choice comp_second_choice = Choice::scissors; 
Choice comp_third_choice = Choice::paper;

//             

int player_wins = 0;   
int computer_wins = 0; 

//           

int player_n_rock = 0; 
int player_n_scissors = 0;
int player_n_paper = 0;

string words[3] = { "rock", "paper", "scissors" };

Choice get_computer_choice();
void assign_computer_prefs();
void reset_prefs();
void decide_winner();
string get_msg(Choice winner);
int rand0toN1(int n);

int main(int argc, char* argv[])
{
    srand(time(NULL)); //       
    assign_computer_prefs(); //        
    string input_str;
    int c;
    while (true) 
    {
        int m = rand0toN1(10); 

        // 10    ,      
        if (m == 0) 
            reset_prefs();

        cout << "Enter Rock, Paper, Scissors, or Exit: ";
        getline(cin, input_str);

        //        

        if (input_str.size() < 1)
        {
            cout << "Invalid input." << endl;
            continue;
        }

        //        

        c = input_str[0];
        if (c == 'R' || c == 'r') 
        {
            player_choice = Choice::rock;
            player_n_rock++;
        } 
        else if (c == 'P' || c == 'p') 
        {
            player_choice = Choice::paper;
            player_n_paper++;
        } 
        else if (c == 'S' || c == 's') 
        {
            player_choice = Choice::scissors;
            player_n_scissors++;
        } 
        else if (c == 'E' || c == 'e') 
        {
            break;
        } 
        else 
        {
            cout << "Invalid input." << endl;
            continue;
        }

        //         ,       

        computer_choice = get_computer_choice();
        int p = (int)player_choice;
        int c = (int)computer_choice;
        cout << "You chose " << words[p];
        cout << "," << endl;
        cout << "I chose " << words[c];
        cout << "," << endl;
        decide_winner();
    }

    //      ,            

    cout << "player wins:   " << player_wins << endl;
    cout << "computer wins: " << computer_wins << endl;
    return EXIT_SUCCESS;
}

//          

void assign_computer_prefs()
{
    int n = rand0toN1(3);
    if (n == 0) {
        comp_first_choice = Choice::rock;
        comp_second_choice = Choice::scissors;
        comp_third_choice = Choice::paper;
    } else if (n == 1) {
        comp_first_choice = Choice::scissors;
        comp_second_choice = Choice::paper;
        comp_third_choice = Choice::rock;
    } else {
        comp_first_choice = Choice::paper;
        comp_second_choice = Choice::rock;
        comp_third_choice = Choice::scissors;
    }
}

//          

void reset_prefs()
{
    if (player_n_rock > player_n_paper && player_n_rock > player_n_scissors) 
    {
        comp_first_choice = Choice::paper;
        comp_second_choice = Choice::rock;
        comp_third_choice = Choice::scissors;
    } 
    else if (player_n_paper > player_n_scissors)
     {
        comp_first_choice = Choice::scissors;
        comp_second_choice = Choice::paper;
        comp_third_choice = Choice::rock;
    } 
    else 
    {
        comp_first_choice = Choice::rock;
        comp_second_choice = Choice::scissors;
        comp_third_choice = Choice::paper;
    }
}


Choice get_computer_choice()
{
    int n = rand0toN1(10);
    if (n <= 4) // 50%     comp_first_choice
        return comp_first_choice;
    else if (n <= 7) // 30%     comp_second_choice
        return comp_second_choice;
    return comp_third_choice; //20%     comp_third_choice
}

//        

void decide_winner()
{
    if (player_choice == computer_choice) 
    {
        cout << "Result is a tie." << endl
             << endl;
        return;
    }

    int p = (int)player_choice;
    int c = (int)computer_choice;
    if (p - c == 1 || p - c == -2) 
    {
        cout << get_msg(player_choice);
        cout << "YOU WIN!" << endl;
        player_wins++;
    } 
    else 
    {
        cout << get_msg(computer_choice);
        cout << "I WIN!" << endl;
        computer_wins++;
    }
    cout << endl;
}

//          

string get_msg(Choice winner)
{
    if (winner == Choice::rock)
        return string("Rock smashes scissors...  ");
    else if (winner == Choice::paper)
        return string("Paper covers rock...  ");
    else
        return string("Scissors cuts paper...  ");
}

//      0~n-1      

int rand0toN1(int n)
{
    return rand() % n;
}

参考资料:《C++Without Fear 2 nd Edtion》,中文译名为《好学的C++》