가위바위보 게임을 만들어보았습니다.

 

함수도 활용하여 간단하게 제작해보았습니다.

 

나중에 SOLID에 대해 배우게 되시면, 왜 꼭! 함수를 잘 쓰셔야하는지 알게 되실 겁니다.

 

추후에 SOLID 내용을 다루는 게시글을 올리겠습니다 :D 즐코하세요~

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

void getLine();
void welcome();
void askGame();
void result_lose();
void result_win();
void result_draw();
void wrong_num_error();

void initial();
void printFront();
int ongame(int user_selected_num, int system_selected_num);
void printResult(int count_game, int count_win, int count_draw, int count_lose);

int main()
{
    int count_game = 0;
    int count_win = 0;
    int count_lose = 0;
    int count_draw = 0;
    int user_selected_num;
    int system_selected_num;

    srand(time(NULL));

    initial();

    while (TRUE)
    {
        scanf("%d", &user_selected_num);
        system_selected_num = (rand() % 3 + 1);

        if (user_selected_num == -1)
        {
            break;
        }

        if (user_selected_num > 0 && user_selected_num < 4)
        {
            // 올바른 입력
            int round_result = ongame(user_selected_num, system_selected_num);
            count_game++;
            if (round_result == 1)
            { // user win
                count_win++;
            }
            if (round_result == 0)
            { // user draw
                count_draw++;
            }
            if (round_result == -1)
            { // user lose
                count_lose++;
            }
        }
        else
        {
            // 잘못된 입력
            wrong_num_error();
            continue;
        }
    }
    // system("cls"); VSCODE 터미널에서는 cls 명령어를 일부러 막아놓았다고 합니다. by MS github
    getLine();
    printResult(count_game, count_win, count_draw, count_lose);
    return 0;
}

void getLine()
{
    static char *liner = "+========================================================+\n";
    printf("\t%s", liner);
}

void welcome()
{
    static char *welcome = "WELCOME TO RCS GAME!\n";
    printf("\t\t\t  %s", welcome);
}
void askGame()
{
    static char *askGame = "WHAT DO YOU WANT?\n";
    static char *askRock = "1. ROCK\n";
    static char *askScissors = "2. Scissors\n";
    static char *askPapers = "3. PAPER\n";
    static char *choose = "Choose what you want!\n";

    printf("\t\t%s", askGame);
    printf("\t\t\t%s", askRock);
    printf("\t\t\t%s", askScissors);
    printf("\t\t\t%s", askPapers);
    printf("\t\t%s", choose);
}

void result_lose()
{
    static char *result_lose = "[[[ YOU LOSE !!! ]]]\n";
    printf("\t\t  %s", result_lose);
}
void result_win()
{
    static char *result_win = "[[[ YOU WIN !!! ]]]\n";
    printf("\t\t  %s", result_win);
}

void result_draw()
{
    static char *result_draw = "[[[ YOU DRAW !!! ]]]\n";
    printf("\t\t  %s", result_draw);
}

void wrong_num_error()
{
    static char *message = "!!! You selected wrong number. Try Again !!!   If you want to end, Enter -1.\n";
    printf("\t%s", message);
}

void initial()
{
    getLine();
    welcome();
    getLine();
    askGame();
}
void printFront()
{
    system("clear");
    getLine();
    askGame();
}

void printResult(int count_game, int count_win, int count_draw, int count_lose)
{
    printf("\t\tWIN : %d, DRAW : %d, LOSE : %d\n", count_win, count_draw, count_lose);
    printf("\t\tWin Rate : %.1f %%", (float)count_win / (count_game)*100);
}

int ongame(int user_selected_num, int system_selected_num)
{
    if (user_selected_num == 1)
    { // USER SELECTED ROCK
        if (system_selected_num == 1)
        { // SYSTEM SELECTED ROCK
            result_draw();
            return 0;
        }
        if (system_selected_num == 2)
        { // SYSTEM SELECTED SCISSORS
            result_win();
            return 1;
        }

        if (system_selected_num == 3)
        { // SYSTEM SELECTED PAPER
            result_lose();
            return -1;
        }
    }

    if (user_selected_num == 2)
    { // USER SELECTED SCISSORS
        if (system_selected_num == 1)
        { // SYSTEM SELECTED ROCK
            result_lose();
            return -1;
        }
        if (system_selected_num == 2)
        { // SYSTEM SELECTED SCISSORS
            result_draw();
            return 0;
        }

        if (system_selected_num == 3)
        { // SYSTEM SELECTED PAPER
            result_win();
            return 1;
        }
    }

    if (user_selected_num == 3)
    { // USER SELECTED PAPER
        if (system_selected_num == 1)
        { // SYSTEM SELECTED ROCK
            result_win();
            return 1;
        }
        if (system_selected_num == 2)
        { // SYSTEM SELECTED SCISSORS
            result_lose();
            return -1;
        }

        if (system_selected_num == 3)
        { // SYSTEM SELECTED PAPER
            result_draw();
            return 0;
        }
    }

    return -2; // ERROR
}
 

 

        +========================================================+
                          WELCOME TO RCS GAME!
        +========================================================+
                WHAT DO YOU WANT?
                        1. ROCK
                        2. Scissors
                        3. PAPER
                Choose what you want!
1
                  [[[ YOU WIN !!! ]]]
2
                  [[[ YOU LOSE !!! ]]]
3
                  [[[ YOU WIN !!! ]]]
4
        !!! You selected wrong number. Try Again !!!   If you want to end, Enter -1.
1
                  [[[ YOU LOSE !!! ]]]
1
                  [[[ YOU DRAW !!! ]]]
2
                  [[[ YOU WIN !!! ]]]
-1
        +========================================================+
                WIN : 3, DRAW : 1, LOSE : 2
                Win Rate : 50.0 %
 

 

감사합니다.

+ Recent posts