Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable: 4996)
- #include "DxLib.h"
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <random>
- #include <vector>
- char String[256];
- int InputHandle;
- int mozicount = 0;
- int gimonnlock = 0;
- int gimon = 0;
- char my_str2(const char* s1, const char* s2)//ここで入力した文字列と用意された文字列を引数として扱う。
- {
- //s1, s2を比較する関数を使うためだけにs2の文字列のサイズが必要なので、変数aに用意した文字列の情報s2を文字列の長さを測るための関数strlenに引数として渡す。
- const size_t a = strlen(s2);
- //無限ループする。
- for (;;) {
- //関数memcmpの返り値が0の時は一致した時なので、==0とする。
- if (memcmp(s1, s2, a) == 0)
- return 1;//入力した文字列にい指定された文字列が入っていた場合は1を返すように設定した。
- //入力した文字列が最後の文字まで到達した場合は一致する文字列がないということなので0を返すようにした。
- else if (*s1 == '\0')
- return 0;//入っていなかった
- //文字列が一致した場合でも一致する文字列がない場合でも入力した文字列の一文字分の文字コードのバイト数?が繰り上がるようにした。
- else
- ++s1;
- }
- }
- int memory = 0;
- std::string result;
- char buffer[256];//★InputHandleに入ったのは文字のデータなので、文字のデータが受け取れる変数の型にする。
- char buffer2[256];
- char input[256];
- char* p;
- FILE* outputfile; // 出力ストリーム
- int hyouzi = 0;
- std::string find_word(const std::string& file, const std::string& buffer) {
- std::string result;
- std::ifstream stream(file);
- if (stream.is_open()) {
- std::vector<std::string> lines;
- std::string line;
- // bufferを含む文字列の集合をlinesに求める
- while (std::getline(stream, line)) {
- if (line.find(buffer) != std::string::npos) {
- lines.push_back(line);
- }
- }
- // linesが空でなければ、そのうちいずれかをデタラメに返す
- if (!lines.empty()) {
- std::random_device gen;
- std::uniform_int_distribution<int> dist(0, lines.size() - 1);
- result = lines.at(dist(gen));
- }
- }
- return result;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- SetGraphMode(1500, 780, 32); // ウィンドウの大きさを指定
- ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
- // DXライブラリの初期化
- if (DxLib_Init() == -1) return -1;
- SetFontSize(42); //サイズを42に変更
- // 描画先を裏にする
- SetDrawScreen(DX_SCREEN_BACK);
- InputHandle = MakeKeyInput(150, FALSE, FALSE, FALSE); //これで総計150バイトの文字データを保持できる。
- // 作成したキー入力ハンドルをアクティブにする
- SetActiveKeyInput(InputHandle);
- while (ProcessMessage() == 0)
- {
- // 画面の初期化
- ClearDrawScreen();
- // 入力モードを描画
- DrawKeyInputModeString(640, 480);
- // 入力途中の文字列を描画
- DrawKeyInputString(0, 0, InputHandle);//InputHandleはint型とリファレンスに書いてあったんで
- //エンターキーが押された時の部分。
- if (CheckKeyInput(InputHandle) != 0) {
- hyouzi = 0;
- gimon = 0;
- ++mozicount;
- // 入力された文字列を取得、その文字列を数値に変換
- GetKeyInputString(buffer, InputHandle);
- if (memory == 0) {
- outputfile = fopen("da.txt", "r"); // ファイルを読み込み用にオープン(開く)
- if (outputfile == NULL) { // オープンに失敗した場合
- printf("cannot open\n"); // エラーメッセージを出して
- exit(1); // 異常終了
- while ((p = fgets(buffer2, 256, outputfile)) != NULL){
- // 文字の入力の入るバッファと、メモからの文字が入るバッファ2とで一致する文字が出てきた場合
- if (my_str2(buffer, buffer2) == 0) {
- std::string result = find_word("da.txt", buffer);
- break;
- }
- // 文字の入力の入るバッファと、メモからの文字が入るバッファ2とで一致しない文字が出てきた場合
- if (hyouzi == 0) {
- if (strcmp(buffer, buffer2) != NULL) {
- gimon = 1;
- }
- }
- }
- fclose(outputfile); // ファイルをクローズ(閉じる)
- }
- //できればエンターキーを押して、次の文章を記憶できるようにしたかったが、文章には必ずと言っていいほど「は」や「とは」
- //が入るし、定義のように覚えさせたいので「覚えて」の後に下のような条件で文章を記憶するようにさせた。
- if (memory == 1 && my_str2(buffer, "とは") or memory == 1 && my_str2(buffer, "は") or memory == 1 && my_str2(buffer, "が")) {
- ++mozicount;
- memory = 2;
- }
- //duration = 1;
- // 再度インプットハンドルをアクティブにする
- SetActiveKeyInput(InputHandle);
- // 入力文字列を初期化する
- SetKeyInputString("", InputHandle);
- }
- //エンターキーが押されていないときでの処理
- if (memory == 2) {
- outputfile = fopen("da.txt", "a+"); // ファイルを書き込み用にオープン(開く)
- if (outputfile == NULL) { // オープンに失敗した場合
- printf("cannot open\n"); // エラーメッセージを出して
- exit(1); // 異常終了
- }
- fprintf(outputfile, "%s\n", buffer); // ファイルに書く
- fclose(outputfile); // ファイルをクローズ(閉じる)
- memory = 0;
- }
- if (p != NULL or hyouzi == 1) {
- DrawFormatString(100, 500, GetColor(5, 105, 19), " buffer2は%s,resultは%s", buffer2, result.c_str());
- }
- if (hyouzi == 0 && gimon == 1) {
- DrawFormatString(100, 600, GetColor(5, 105, 0), "申し訳ありません%sとは何ですか?", buffer);
- }
- // 裏画面の内容を表画面に反映させる
- ScreenFlip();
- }
- // 用済みのインプットハンドルを削除する
- DeleteKeyInput(InputHandle);
- // 画面の初期化
- ClearDrawScreen();
- // 裏画面の内容を表画面に反映させる
- ScreenFlip();
- // DXライブラリの使用終了
- DxLib_End();
- // 終了
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement