Skip to content

Instantly share code, notes, and snippets.

@BMuscle
Last active August 29, 2019 13:26
Show Gist options
  • Select an option

  • Save BMuscle/57f623bd7ce58f9766dcaaa9805a5be7 to your computer and use it in GitHub Desktop.

Select an option

Save BMuscle/57f623bd7ce58f9766dcaaa9805a5be7 to your computer and use it in GitHub Desktop.
じゃんけんソース⑥
#include <stdio.h>
int jankenJudge(int p1, int p2) {//プレイヤーの手を引数に
int judge;//返す為のjudgeを宣言
//勝ち負けの判定
if (p1 == p2) {
judge = 3;//あいこの場合
}
else {
p1 = p1 % 3;//相手との差を求める
if (p1 + 1 == p2) {
judge = 1;//P1が勝ち
}
else {
judge = 2;//あいこは条件から外れているのでP2の勝ち
}
}
return judge;
}
void result(int judge) {//求められたjudgeを使う
//結果の出力
switch (judge) {
case 1://プレイヤー1の勝ち
printf("プレイヤー1の勝ち!!\n");
break;
case 2://プレイヤー2の勝ち
printf("プレイヤー2の勝ち!!\n");
break;
case 3://あいこ
printf("あいこです\nもう一度入力してください\n");
break;
default:
break;
}
}
int main(void) {
int player1;//プレイヤー1の手
int player2;//プレイヤー2の手
int judge;
do {
scanf("%d", &player1);//プレイヤー1の手を格納
scanf("%d", &player2);//プレイヤー2の手を格納
judge = jankenJudge(player1, player2);//じゃんけんの判定
result(judge);//結果の出力
} while (judge == 3);//あいこで
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment