mercredi 24 juin 2020

Number baseball game(C++)

I'm new to C, and is trying to make a number baseball game program that shows 3 choices after the player inputs their numbers, and get their results. z to continue the game, x to end it, m to open help. I think I've coded it well, but when i try debugging it, the first result appears twice, making the entire program weird. Could you please help?

#include <stdio.h>
#include <windows.h>
#include <time.h>
int main()
{
    int computerBall[4];
    int i, j = 1;
    int userBall[4];
    int count = 1;
    int strike;
    int ball;
    int out;
    char x, m, z;
    char k;
    int flag = 1;
    int o,q;
    q = 1;
    
    
    
        srand(time(NULL));

        computerBall[0] = rand() % 31;

        while (computerBall[0] == 0)
            computerBall[0] = rand() % 31;

        for (i = 1; i < 4; i++)
        {
            computerBall[i] = rand() % 31;

            while (q == 1)
            {
                flag = 0;
                for (j = 0; j < i; j++)
                {
                    if (computerBall[j] == computerBall[i])
                    {
                        computerBall[i] = rand() % 31;
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                    break;
            }
        }
        while (q == 1)
        {
            printf("숫자야구 게임");
            printf("1~30사이의 4개의 숫자를 선택\n");
            printf("%d회차 숫자야구\n", count);
            while (q == 1)

            {
                printf("숫자를 입력하세요:");
                scanf_s("%d %d %d %d", &userBall[0], &userBall[1], &userBall[2], &userBall[3]);


                if (userBall[0] < 1 || userBall[0]>30 || userBall[1] < 1 || userBall[1]>30 || userBall[2] < 1 || userBall[2]>30 || userBall[3] < 1 || userBall[3]>30)
                {
                    printf("범위 외의 숫자를 입력하시면 안됩니다.\n");
                    continue;
                }
                else if (userBall[0] == userBall[1] || userBall[0] == userBall[2] || userBall[0] == userBall[3] || userBall[1] == userBall[2] || userBall[1] == userBall[3] || userBall[2] == userBall[3])
                {
                    printf("중복된 숫자를 입력하시면 안됨니다.\n");
                    continue;
                }
                break;

            }
            ball = 0;
            strike = 0;
            out = 0;
            for (i = 0; i < 4; i++)
            {
                for (j = 0; j < 4; j++)
                {
                    if (userBall[i] == computerBall[j])
                    {
                        if (i == j)
                            strike++;
                        else
                            ball++;
                    }
                }
            }

            if (strike == 0)

            {
                if (ball == 0)
                {
                    out++;
                    printf("%d아웃\n\n", out);            

                }
                else
                
                    printf("%d볼\n\n", ball);                    
                


            }
            else if (ball == 0)
            
                printf("%d스트라이크\n\n", strike);
                



            else
            
                printf("%d스트라이크 %d볼\n\n", strike, ball);
                
            

            {
                printf("게임 진행은 z, 도움말은 m, 게임 종료는 x를 눌러주세요:");
                scanf_s("%c\n",&k);




                if (k == 'm')
                {
                    printf("도움말\n\n");
                    printf("1~30 사이의 자연수중 4개를 선택해서 입력\n");
                    printf("자리와 숫자가 맞으면 strike\n");
                    printf("숫자만 맞으면 ball\n");
                    printf("ball또는 strike가 하나도 없으면 out\n");
                    continue;
                }
                else if (k == 'x')
                {
                    printf("\n 게임종료\n");
                    printf("정답은 : ");
                    for (i = 0; i < 4; i++)
                        printf("%d ", computerBall[i]);
                    printf("입니다.\n");
                    printf("1을 누르면 재시작");
                    scanf_s("%d", &o);
                    if (o == 1)
                        continue;
                    else
                        q = 0;

                }
                else if (k == 'z')
                    continue;
            }




            if (strike == 4)
            {
                if (count < 6)
                    printf("성공입니다!");
                printf("1을 누르면 재시작");
                scanf_s("%d", &o);
                if (o == 1)
                    continue;
                else
                    q = 0;
                if (count > 5 || count < 8)
                {
                    printf("분발하세요\n");
                    printf("1을 누르면 재시작");
                    scanf_s("%d", &o);
                    if (o == 1)
                        continue;
                    else
                        q = 0;

                }


            }
            if (count > 10)
            {
                printf("게임에 집중하세요\n");
                strike = 4;
                printf("1을 누르면 재시작");
                scanf_s("%d", &o);
                if (o == 1)
                    continue;
                else
                    q = 0;
            }
            count++;

        
    }
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire