'야구게임'에 해당되는 글 1건

  1. 2009.03.24 야구게임
C/C++2009. 3. 24. 20:45

#include <stdio.h>   //야구게임
#include <time.h>
#include <stdlib.h>

int main(){
 int Player[3];  //플레이어 숫자
 int CPU[3];  //컴퓨터 숫자
 int strike=0,ball=0,count=0;
 int i,j;
 int tmp;

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

 CPU[0]=rand()%9+1;  //랜덤수 생성
 CPU[1]=rand()%9+1;
 CPU[2]=rand()%9+1;


 while((CPU[0]==CPU[1])||(CPU[1]==CPU[2])||(CPU[0]==CPU[2])) //랜덤수가 중복 되지 않기 위해
 {
  CPU[0]=rand()%9+1;
  CPU[1]=rand()%9+1;
  CPU[2]=rand()%9+1;
 }


 printf("== 게임을 시작 합니다 ==\n"); 

 while(1){
  printf("= 숫자를 세개 입력하세요 = :");
  scanf("%d",&tmp);

  strike=0;
  ball=0;

  Player[0] = tmp/100;
  Player[1] = tmp%100/10;
  Player[2] = tmp%100%10;


  for(i=0;i<3;i++)  
   for(j=0;j<3;j++)
    if(CPU[i]==Player[j]&&i==j){
     strike++;      //스트라이크 맞춘갯수
    }else if(CPU[i]==Player[j]){
     ball++;       //자리는 안맞고 숫자만 맞춘갯수
    }


    count++;

    printf("%d번째  Strike: %d  Ball: %d\n",count,strike,ball);
    printf("\n");


    if(strike==3){        // 3개 모두 자리수 맞추면 끝
     printf("숫자는 %d, %d, %d 입니다\n",CPU[0],CPU[1],CPU[2]);
     printf("%d 번만에 맞추셨습니다!! \n",count);
     break;
    }

 }
}


Posted by 샤키