본문 바로가기
Programming/C#

c - Baseball Game.

by NAMP 2009. 3. 20.

/* 단순한 세자리 숫자 맟추기 게임 입니다. */


#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main(){

                 

                  //set variables

                 

                  int i;

                  int min=1000;

                  int times=0;

                  int sel;

                  int com[4];

                  int usr[4];

                  int strk,ball;

                 

                  srand(time(NULL));rand();rand();rand();

                  srand(rand())              ;

                 

                  while(1){

                                  

                                   //start//

                                   sel=99;

                                   while(sel>3||sel<0){

                                                     printf("== Baseball Game ==\n");

                                                     printf(" 1. Game Start\n");

                                                     printf(" 2. Game Score\n");

                                                     printf(" 3. End Game\n");

                                                     printf(" >");

                                                     fflush(stdin);

                                                     scanf("%d",&sel);

                                   }

                                  

                                   switch(sel)

                                   {

                                   case 1:

                                                    

                                                    

                                                     times=0;

                                                     strk=0;

                                                    

                                                    

                                                     do

                                                     {

                                                                       com[0]=rand()%9+1;

                                                                       com[1]=rand()%9+1;

                                                                       com[2]=rand()%9+1;

                                                     } while(com[1]==com[0]||com[1]==com[2]||com[2]==com[0]);

                                                    

                                                     //printf("\n%d,%d,%d",com[0],com[1],com[2]);

                                                     printf("\n<< Game Start >>\n\n");

                                                    

                                                     while (strk!=3)

                                                     {

                                                                       strk=ball=0;

                                                                       sel=0;

                                                                       while (sel<100||sel>999)

                                                                       {

                                                                                        printf("Input 3 numbers (without 0):");

                                                                                        fflush(stdin);

                                                                                        scanf("%d",&sel);

                                                                                       

                                                                                        usr[2]=sel%10;

                                                                                        usr[1]=(sel%100)/10;

                                                                                        usr[0]=(sel%1000)/100;

                                                                                       

                                                                                        /*if (usr[0]==0||usr[1]==0||usr[2]==0)

                                                                                        {

                                                                                        continue;

                                                                                        }

                                                                                        if (usr[1]==usr[0]||usr[1]==usr[2]||usr[2]==usr[0]) {

                                                                                        printf("Input Each different numbers.\n");

                                                                                        sel=0;

                                                                                        continue;

                                                                       }*/                                               

                                                                       }

                                                                      

                                                                       for (i=0;i<3;i++)

                                                                       {

                                                                                        if (usr[i]==com[i]){

                                                                                                          strk++;

                                                                                        }

                                                                                        if (usr[i]==com[(i+1)%3]){

                                                                                                          ball++;

                                                                                        }

                                                                                        if (usr[i]==com[(i+2)%3]){

                                                                                                          ball++;

                                                                                        }

                                                                       }

                                                                      

                                                                       if (strk==3)

                                                                       {

                                                                                        printf("Bingo!!!\n\n");

                                                                                        system("pause");

                                                                                        system("cls");

 

                                                                       }else{

                                                                                       

                                                                                        printf("Strike:%d,  Ball:%d,  Now %d times\n\n",strk,ball,++times);

                                                                       }

                                                     }

                                                    

                                                     if (min>times)

                                                     {

                                                                       min=times;

                                                     }

                                                     break;

                                                    

                                   case 2:

                                                     if (min==1000)

                                                     {

                                                                       printf("It's First Time\n\n");

                                                     }else{

                                                                       printf("\n Minimum %d times\n\n",min);

                                                     }

                                                    

                                                     break;

                                   case 3:

                                                     printf("Good bye\n");

                                                     exit(0);

                                                     break;

                                                    

                                                    

                                                    

                                                    

                                   }

                                  

                  }

                 

                  return 0;

                 

}

'Programming > C#' 카테고리의 다른 글

C - 지렁이 & 지뢰 찾기 게임 만들기  (0) 2009.03.29
C - 구구단 파일 입출력  (0) 2009.03.27
Maximum value of each Radix ... ?  (0) 2009.03.25
c - Students' score management program.  (0) 2009.03.21
C - Dice Game  (0) 2009.03.14

댓글