/**
* Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org>
* http://mooo.org
* ----------------------------------------------------------------------
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#define FOREVER 1
#define NUM 4
int random(int from, int to);
void main()
{ int i = 0, j;
int strike, ball;
int num[NUM], user[NUM];
char temp[81];
srand((unsigned)time(NULL));
while (i < NUM) {
num[i] = random(1, 9);
for (j = 0; j < i; j++)
if (num[j] == num[i])
break;
if (i == j)
i++;
}
while (FOREVER) {
do {
printf("Enter 4 digits [1-9]: ");
gets(temp);
if (temp[0] == 'q' || temp[0] == '0')
exit(0);
for (i = 0; i < NUM; i++)
if (temp[i] >= '1' && temp[i] <= '9')
user[i] = temp[i] - '0';
else
break;
if (i != NUM)
continue;
} while (strlen(temp) != 4);
for (i = 0, strike = 0, ball = 0; i < NUM; i++) {
for (j = 0; j < NUM; j++) {
if (num[i] == user[j]) {
if (i == j)
strike++;
else
ball++;
}
}
}
printf("%d strike %d ball\n", strike, ball);
if (strike >= 4)
break;
}
}int random(int from, int to)
{ if (from > to)
return 0;
return (rand() % (to - from + 1) + from);
}