0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++でしりとりを作ってみよう

Posted at

まず初めに

自分はC++4か月目でQiitaも触れるのは初なので拙いところもあったりしますがミス等があったら指摘してくれると幸いです。

コード

#include<iostream>

int main()
{
	//定義
	std::wstring oldword= L"しりとり" ;
	wchar_t oldendword = L'り';
	std::wstring newword;
	wchar_t newendword;

	//日本語を出したいためロケールを渡す
	std::wcout.imbue(std::locale(""));
	std::wcin.imbue(std::locale(""));
	//ループ
	while (true)
	{
		std::wcout << L"現在は「" << oldword << L"」です。入力↓" << std::endl;
		std::wcin >> newword;
		//今入力した文字の一文字目と前回の最後の文字が一緒なら
		if (newword[0]==oldendword)
		{
			//何文字かを抽出
			int len = newword.length();
			newendword = newword[len-1];
			//今入力した文字の最後の文字が「ん」なら
			if (newendword == L'ん')
			{
				std::wcout << L"最後が「ん」なのでしりとりを終了します" << std::endl;
				//ループを抜ける
				break;
			}
			//違うなら
			else
			{
				//今入力した文字を上書き
				oldword = newword;
				oldendword = newendword;
			}
		}
		//違うなら
		else
		{
			std::wcout << L"先頭が「" << oldendword << L"」ではありません。入力↓" << std::endl;
		}
	}
	return 0;
}

ちょこっと解説

wstring

wchar_t

↑日本語の一文字一文字を入れてくれる。stringやcharだと英数字一文字一文字しか入ってくれないためこれを使用。

imbue(std::locale(""))

↑これ無いと正しく日本語が適用されない。

break

↑これでループを抜けている。

最後に

このコードはまだ改良点がたくさんある(例,同じ単語の判定 ターンの交代)と思うので皆さん色々改造しちゃってください!!

0
1
1

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?