Skip to content

Instantly share code, notes, and snippets.

@baobao
Created May 3, 2020 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/98dc5cffb50727e5a7a7cdd31a17a8e7 to your computer and use it in GitHub Desktop.
Save baobao/98dc5cffb50727e5a7a7cdd31a17a8e7 to your computer and use it in GitHub Desktop.
Cでじゃんけんゲーム
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
// じゃんけんの手
char te[3][20] = {"グー", "チョキ", "パー"};
int main()
{
char c[32];
int npcResult, playerInput, k;
//char playerResult, pcResult;
printf("#######################\n");
printf("# じゃんけんゲーム\n");
printf("#\n");
printf("# 9キーで終了\n");
printf("#######################\n\n");
srand(time(NULL));
while(1)
{
npcResult = rand()%3 + 1;
// memo : sleepはunistd.hが必要
// 単位は秒
sleep(1);
printf("\n\nじゃんけん..... (1:グー, 2:チョキ, 3:パー)\n\n");
// 入力の待機
fflush(stdout);
gets(c);
sleep(1);
// int型に変換した数値。変換不能文字は0を返す。
playerInput = atoi(c);
if (playerInput < 1 || playerInput > 3)
{
printf("何を言ってるのかわからない...\n");
printf("もう一回いきますよ \n\n");
continue;
}
else
{
printf("NPCの手は 【%s】 / プレーヤーの手は 【%s】\n\n", te[npcResult - 1], te[playerInput - 1]);
if (npcResult == playerInput)
{
// あいこ
printf("あいこ\n");
continue;
}
else if (
(npcResult == 1 && playerInput == 2)
|| (npcResult == 2 && playerInput == 3)
|| (npcResult == 3 && playerInput == 1)
)
{
// NPCの勝ち
printf("NPCの勝利\n");
sleep(1);
printf("\n\n\n残念....\n\n");
}else
{
// プレーヤーの勝ち
printf("プレーヤーの勝利\n");
sleep(1);
printf("★★★★★★★★★★★★★★★\n");
printf("!!!おめでとう!!!\n");
printf("★★★★★★★★★★★★★★★\n");
sleep(1);
}
}
printf("\n\n\n----------------------------\n続ける場合は9キー以外を押してください\n");
fflush(stdout);
// 文字列入力
gets(c);
// 文字列を数字に変換
k = atoi(c);
if (k == 27)break; // Ecsで終了
}
printf("また挑戦待ってるぜ!!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment