A sentimental robot

Baseball Game(computer vs user) 본문

Data Structure

Baseball Game(computer vs user)

GOD03219 2018. 1. 3. 13:10

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#pragma warning(disable:4996)
void random(int r[]);
void ansGuessing(int g[], int a);
void ansChecking(int g[], int r[]);
void main()
{
 int r[3];
 int g[3];
 int a = 0;

 do {
  random(r);
  ansGuessing(g, a);
  ansChecking(g, r);
 } while (1);
}
void random(int r[]) {
 int i;
 srand((unsigned int)time(NULL));
 for (i = 0; i < 3; i++)
 {
  r[i] = rand() % 10;

  while (r[0] == r[1] || r[1] == r[2] || r[0] == r[2]) {
   i = 0;      // 다시 for문으로 들어가게 만들기
   break;
  }

 }

 printf("%d\t%d\t%d\n", r[0], r[1], r[2]);


}
void ansGuessing(int g[], int a) {
 do {
  printf("Attack (3 num) :");
  rewind(stdin);
  scanf("%d", &a);


  g[0] =( a / 100);
  g[1] = (a / 10) % 10;
  g[2] = a % 10;


  if (g[0] == g[1] || g[1] == g[2] || g[0] == g[2]) printf("Re-input\n");
 } while (g[0]==g[1]||g[1]==g[2]||g[0]==g[2]);

}
void ansChecking(int g[], int r[]) {


 int ballCount=0;
 int strikeCount=0;
 int i, j = 0;

 

 for (i = 0; i < 3; i++) {
  if ( g[i] == r[i] ) { strikeCount++;continue; }


  for (j = 0; j < 3; j++) {
   if ( g[i] == r[j] )ballCount++;
  }
 }


 printf("ball:%d\t strike:%d\n", ballCount, strikeCount);
 if (strikeCount == 3)  printf("Win!!!!!!\n");


}

 

'Data Structure' 카테고리의 다른 글

Linear Queue  (0) 2018.01.03
Babygin  (0) 2018.01.03
Single LinkedList Exercise2  (0) 2018.01.03
Single LinkedList Exercise  (0) 2018.01.03
LinkedList  (0) 2018.01.03