0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【C++】じゃんけんゲーム2

Last updated at Posted at 2023-10-16

【C++】じゃんけんゲームpart2

以前じゃんけんゲームを作ったのですが、それをもとにさらに改良してみたので、新しい記事にしました。

以前の記事に沢山のコメントありがとうございます!とても為になり勉強になります:blush:
なかなかレベルが低い私なので、技量が追い付かず、今回も自分の作ったコードを元に改良する形となってしまったの申し訳ございません。
これからコメントを参考に勉強していきたいと思います!

違う数値や文字を入れると、再度入力できるように改良。

前回は、breakでプログラムから抜けてしまっていたので、再入力できるように出来たらな~と思い作り直しました。
ランダムのところを、数値ではなく文字にするのにどうしたらいいか、解決するのに苦労しました。。。

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;           // stdという記述を省略可能とする

enum {
    ROCK = '1',
    SCISSORS = '2',
    PAPER = '3'
};                           // マジックナンバー避ける為数字に名前付ける。

enum { YES = '1', NO = '2' };

class Aite {                  // 相手のクラス
   public:
    char aitenote(char aihand);  
};

char Aite::aitenote(char aihand) {
    cout << "相手が出した手は・・・";
    if (aihand == ROCK)
        cout << "グー" << endl;
    else if (aihand == SCISSORS)
        cout << "チョキ" << endl;
    else if (aihand == PAPER)
        cout << "パー" << endl;
    return aihand;                    // 呼び出し元へ返す。
}

int main() {
    char tugi;
    do {                        // 1だったら、再度じゃんけん開始
        srand((unsigned)time(NULL));           // 現在の時刻で初期化
        char myhand;

        do {
            cout << "あなたは何を出す? 1:グー、 2:チョキ、 3:パー >> ";
            cin >> myhand;
            cout << "あなたが出した手は・・・";

            if (myhand == ROCK) {                  // 1だったらグーと表示
                cout << "グー" << endl;
            } else if (myhand == SCISSORS) {        // 2だったらチョキと表示
                cout << "チョキ" << endl;
            } else if (myhand == PAPER) {             // 3だったパーと表示
                cout << "パー" << endl;
            } else {
                cout << "出してる手が範囲外です。1~3を入力して下さい。"
                     << endl;
            }
        } while (myhand != ROCK && myhand != SCISSORS && myhand != PAPER);

        char aihand;
        Aite note;
        aihand = note.aitenote(rand() % 3 + 1 + '0');   // 1~3(数値ではなく文字を)ランダムに出す

        if (myhand == aihand)                            // あいこの条件
            cout << "あいこ" << endl;
        else if (myhand == ROCK && aihand == SCISSORS ||    // 自分の勝ちの条件
                 myhand == SCISSORS && aihand == PAPER ||
                 myhand == PAPER && aihand == ROCK)
            cout << "あなたの勝ち" << endl;
        else                                              // 他の条件は自分は負け
            cout << "あなたの負け" << endl;

        do {
            cout << "まだじゃんけんしますか?1:yes, 2:no >> ";
            cin >> tugi;
            if (tugi == NO) {                     // もし、2だったらじゃんけん終了
                cout << "終わります";
            } else if (tugi != YES && tugi != NO) {       // 1・2じゃなかったら
                cout << "1か2を入力して下さい。" << endl;
            }

        } while (
            tugi != NO && tugi != YES);  // 1・2じゃなかったら、まだじゃんけんしますか?に戻る。

    } while (tugi == YES);  // 1だったら再度じゃんけん開始
}
0
0
10

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?