Skip to content

Instantly share code, notes, and snippets.

@sh4869
Created June 4, 2014 04:00
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 sh4869/8b88356aaeba26914358 to your computer and use it in GitHub Desktop.
Save sh4869/8b88356aaeba26914358 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//相手のカードの番号を決めるための関数
int EnRandom(int x[]){ //Enemy's card create
x[0]=rand()%10; //0~9の間の乱数をx[0](一枚目のカード)に代入
x[1]=x[0];
//x[0](一枚目のカード)とx[1](二枚目のカード)が違う数になるまでx[1]に0~9の乱数を代入
while(x[0]==x[1]){
x[1]=rand()%10;
}
x[2]=x[1];
//x[2](三枚目のカード)がx[0],x[1]と違う数になるまでx[2]に0~9の乱数を代入
while(x[2]==x[0]||x[2]==x[1]){
x[2]=rand()%10;
}
return 0;
}
//メイン関数
int main(void)
{
int i,j,eat,bite,turn=0,count_card = 0;
int enemy[3],you[3]; // enemy[]:敵のカード you[]:入力用の変数
int EnRandom(int x[]); //プロトタイプ宣言
srand((unsigned)time(NULL)); //これを使うことで毎回違う乱数が生まれる(詳しくはググれ)
EnRandom(enemy); //カード決定
printf("Game Start!!\n\n");
for(;;){
printf("\nYour turn\n"); //ずっと俺のターンなのは仕様
turn++;
for(;;){
/* 相手のカードを入力します */
printf("Put three Enemy's number\n");
while(count_card < 3){
printf("input number of card_%d: ",count_card+1);
scanf("%d",&you[count_card]); //入力された数をyou[]に代入する
count_card = count_card + 1;
}
count_card = 0; //もし下記のエラーが出た時0でないとループしてくれないため
//エラー処理
if( you[0]<0 || you[1]<0 || you[2]<0 || you[0]>9 || you[1]>9 || you[2]>9 ){ //数字がおおきすぎた時
printf("Error: New number is too big\n");
}else if( you[0]==you[1] || you[1]==you[2] || you[2]==you[0] ){ //数字が被っている時
printf("Error: New number is old number\n");
}else{
break; //抜けだして判定のところへ
}
}
eat=0;bite=0;
//判定
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(enemy[i]==you[j]){ //もし数字がかぶっていたら
if(i==j){ //同じ位置にカードがあるか確認
eat++;
}else{
bite++;
}
}
}
}
if(eat==3){ //ゲームクリア
printf("CLEAR!!!!\nEnemy's Card: %d %d %d\n Turn number: %d turn\n",enemy[0],enemy[1],enemy[2],turn);
break; //終了
}else{
printf("%d EAT %d BITE\n",eat,bite);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment