작은숲:위키노트/C 언어 예제/야구 게임 만들기

큰숲백과, 나무를 보지 말고 큰 숲을 보라.
Utolee90 (토론 | 기여)님의 2017년 7월 2일 (일) 17:59 판 (Utolee90님이 위키노트:C 언어 예제/야구 게임 만들기 문서를 C 언어 예제/야구 게임 만들기 문서로 이동했습니다: Move_Pages_by_removing_the_text_위키노트_via_pywikibot)
/**
 * 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);
}
이 작은숲 문서의 출처는 위키노트의 위키노트/C 언어 예제/야구 게임 만들기 문서입니다.