C++で数当てゲームを作る。プログラムが用意した1~99の乱数を当てるゲーム。
ヒントの出し方を工夫するともっと面白いかも。
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 数当てゲーム 1~99の数を入力 24 それより大きいです 85 それより小さいです 65 それより大きいです 75 それより小さいです 70 それより大きいです 72 6回でクリア! まだやる?[Y/N] n |
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <iostream> #include <ctime> #include <cstdlib> int main( int argc, char const * argv[]) { int num = rand () % 100; int getnum = 0; int count = 0; std::string str; while (1) { std:: srand ( time (NULL)); std::cout << "数当てゲーム" << std::endl; std::cout << "1~99の数を入力" << std::endl; std::cout << std::endl; num = rand () % 100; getnum = 0; count = 0; while (1) { count++; std::cin >> getnum; if (num == getnum) { break ; } else if (num < getnum) { std::cout << "それより小さいです" << std::endl; } else { std::cout << "それより大きいです" << std::endl; } } std::cout << count << "回でクリア!" << std::endl; std::cout << std::endl << "まだやる?[Y/N]" << std::endl; std::cin >> str; if (str == "N" || str == "n" ) { break ; } } return 0; } |
コメント