こんにちは。
今回は、時間を持て余したのでそれを使って、数当てゲームを作ってみました。
遊び方。
このゲームは隠された数字をヒントをもとに探し出すゲームです。
デフォルトでは。
・aキーを押すと現在のカウントをマイナスします。
・dキーを押すと現在のカウントをプラスします。
・スペースキーを押すと回答を答え合わせします。
・AキーやDキーを押すとちょっと多く操作できます。
・q、w、eキーではワープができます。
最大値に大きな値を入れると迷子になる可能性があります。
開発環境
Visual Studio2019デス。130行位あります。
免責
デバッグはしていますが、大体2時間で書いたコードなので抜けはあるかもしれません。
コード
Main.cpp
# include <iostream>
# include <cstdint>
# include <tuple>
# include <random>
# include <conio.h>//not standard.
//C++14 needed.
std::intmax_t GetInput() {
std::intmax_t R = 0;
std::cin >> R;
std::cin.clear();
return R;
}
std::tuple<bool,std::intmax_t, std::uintmax_t, std::uintmax_t> Game(std::uintmax_t N) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<std::intmax_t> ui(-static_cast<std::intmax_t>(N), static_cast<std::intmax_t>(N));
std::intmax_t V = 0;
std::intmax_t A = ui(mt);
std::uintmax_t CA = 0;
std::uintmax_t CM = 0;
while (true) {
std::cout << V<<" "<< "\r";
int Key = _getch();
//std::cout << '\a';
CM++;
if (Key == 'd') {
V++;
}
if (Key == 'a') {
V--;
}
if (Key == 'D') {
V+=7;
}
if (Key == 'A') {
V-=7;
}
if (Key == 'w') {
V = 0;
}
if (Key == 'e') {
V = N;
}
if (Key == 'q') {
V= -static_cast<std::intmax_t>(N);
}
if (Key == 27) {
return { false,A,CA,CM };
}
std::cout <<" " <<'\r';
if (Key == ' ') {
if (V > A) {
std::cout <<V <<":L" <<'\r';
}
if (V < A) {
std::cout <<V << ":H" << '\r';
}
if (V == A) {
std::cout <<V << ":GET IT!!" << '\r';
break;
}
CA++;
}
V = std::max<std::intmax_t>(-static_cast<std::intmax_t>(N), std::min<std::intmax_t>(V, static_cast<std::intmax_t>(N)));
//V = std::clamp<std::intmax_t>(V, -static_cast<std::intmax_t>(N), static_cast<std::intmax_t>(N));
}
std::cout << std::endl;
std::cout << std::endl;
return { true,A,CA,CM };
}
int main() {
std::cout << "Wellcome!!" << std::endl;
std::cout << "I am Number Hitter!!" << std::endl;
std::cout << std::endl;
std::cout << "***You will Find a Number!!***" << std::endl;
std::cout << std::endl;
std::cout << "Input Max Number!" << std::endl;
std::cout << ">>";
std::intmax_t N = GetInput();
std::cout << std::endl;
if (N <= 0) { N = 16; }
std::cout << "You choise Max is " << N << std::endl;
std::cout << "Let's Start Game!!" << std::endl;
std::cout << "a key is move Minus. " << std::endl;
std::cout << "d key is move Plus. " << std::endl;
std::cout << "Space key is Auth Anther. " << std::endl;
std::cout << std::endl;
bool F = false;
std::intmax_t A = 0;
std::uintmax_t CA = 0;
std::uintmax_t CM = 0;
std::tie(F,A,CA,CM)=Game(N);
if (F) {
std::cout << "You Find Anther! " << A << std::endl;
std::cout << "The Move Count is " << CM << std::endl;
std::cout << "The Auth Anther Count is " << CA << std::endl;
if (CA == 1) { std::cout << "You are Wonder!!!" << std::endl; }
}
else {
std::cout << "You Fail!" << std::endl;
std::cout << "Anther is " << A << std::endl;
std::cout << "The Move Count is " << CM << std::endl;
std::cout << "The Auth Antheris " << CA << std::endl;
}
return 0;
}