C++   ゲーム

ホーム   C/C++チュートリアル


  1. このC++チュートリアルは、 wxWidgetsのコードを読むための必要最小限なC++を説明してきました。
  2. おもな説明は前回「定数」でだいたい終わりましたが、 wxWidgetsではGUIパーツからキーボードの入力を受け取るため、 通常のC++で説明されるべきコンソール(console、ターミナル、terminal) からのキーボード入力を説明していませんでした。
  3. この章では、コンソール(ターミナル)からキーボード入力を受け取る方法を説明して、 簡単なゲームを作ります。

std::cin

C++では、std::cin を利用してキーボードの入力を受け取ります。

cin.cpp


#include <iostream>

int main()
{
    std::string name;
    
    std::cout << "Enter your name: ";
    std::cin  >> name;
    std::cout << "Hello " << name << "." << std::endl;
    
    return 0;
}
    

実行結果


Enter your name: Hanako
Hello Hanako.
    

コード説明

  1. std::cin で、コンソール(ターミナル)に入力され、 エンターキーが押されるまでの文字列を、変数に代入できます。
  2. coutがコンソールアウトで、cinがコンソールインの略だと思われます。
  3. >> と << の方向が気になるところですが、 cin がコンソールから変数に向かっているのに対して、 cout が 文字列がコンソールに向かっていると思えば なんとなく分かるような気もします。

じゃんけんゲーム

では、cin と乱数を利用してじゃんけんゲームを作ってみます。

janken.cpp


#include <iostream>
//#include <stdio.h>    // 環境によって必要です
//#include <string>     // 環境によって必要です

int you(std::string hand);
int computer();
void janken(int you, int computer);

int main()
{
    srand(time(NULL));
    std::string hand;
    int opponent;
    int yourself;
    
    while (1)
    {
        std::cout << "グー:1  チョキ:2  パー:3  終了:4  ";
        std::cin  >> hand;
        std::cout << "--------------------" << std::endl;
        if (hand == "4")
        {
            std::cout << "Bye.." << std::endl;
            break;
        }
        yourself = you(hand);
        if (yourself)
        {
            opponent = computer();
            janken(yourself, opponent);
        }
    }
    return 0;
}

int you(std::string hand)
{
    int num = atoi(hand.c_str());
    switch (num)
    {
    case 1:
        std::cout << "あなたはグーです。" << std::endl;
        return num;
    case 2:
        std::cout << "あなたはチョキです。" << std::endl;
        return num;
    case 3:
        std::cout << "あなたはパーです。" << std::endl;
        return num;
    default:
        std::cout << "無効な値です。" << std::endl;
    }
    return 0;
}

int computer()
{
    int num = rand() % 3 + 1;
    switch (num)
    {
    case 1:
        std::cout << "あいてはグーです。" << std::endl;
        break;
    case 2:
        std::cout << "あいてはチョキです。" << std::endl;
        break;
    case 3:
        std::cout << "あいてはパーです。" << std::endl;
        break;
    }
    
    return num;
}

void janken(int you, int com)
{
    if (you == com) {
        std::cout << "あいこです。" << std::endl;
    } else if (you == 1 && com == 2) {
        std::cout << "あなたの勝ちです。" << std::endl;
    } else if (you == 2 && com == 3) {
        std::cout << "あなたの勝ちです。" << std::endl;
    } else if (you == 3 && com == 1) {
        std::cout << "あなたの勝ちです。" << std::endl;
    } else {
        std::cout << "あなたの負けです。" << std::endl;
    }
}
    

実行結果


グー:1  チョキ:2  パー:3  終了:4  0
--------------------
無効な値です。
グー:1  チョキ:2  パー:3  終了:4  1
--------------------
あなたはグーです。
あいてはチョキです。
あなたの勝ちです。
グー:1  チョキ:2  パー:3  終了:4  2
--------------------
あなたはチョキです。
あいてはチョキです。
あいこです。
グー:1  チョキ:2  パー:3  終了:4  3
--------------------
あなたはパーです。
あいてはグーです。
あなたの勝ちです。
グー:1  チョキ:2  パー:3  終了:4  4
--------------------
Bye..
    

コード説明

  1. int num = atoi(hand.c_str());
    atoi関数は引数のchar *型の文字列を数値に変換した値を返します。 c_str関数は、std::stringのメンバ関数で、std::string型を char *型に変換した値を返します。
  2. return num;
    you関数の中のswitch文を break; ではなく、return で値を返すことで終わっています。
  3. else if (you == 1 && com == 2)
    &&は条件演算子と呼ばれるもので、左辺の条件「かつ」右辺の条件が、true の場合 という意味になります。アンド(AND)とも呼ばれます。


46262 visits
Posted: Dec. 12, 2019
Update: Dec. 18, 2019

ホーム   C/C++チュートリアル   目次