C/명품 C언어 프로젝트(안기수 저)

명품 C언어 프로젝트 8.2장 예제

꾸준함. 2017. 8. 31. 12:49

/*

야구 스코어의 득점판 표시 프로그램

*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

#include <string.h>

#include <Windows.h>

 

void control_scoreboard(int st, int end, int r[], int h[], int e[], int b[]);

void display_scoreboard(int r[], int h[], int e[], int b[]);

void draw_check02(int c, int r);

void gotoxy(int x, int y);

 

int main(void)

{

        int baseball[12][2] = { 0 }, r[2] = { 0 }, h[2] = { 0 }, e[2] = { 0 }, b[2] = { 0 };

        srand((unsigned)time(NULL));

        display_scoreboard(r, h, e, b);

        control_scoreboard(1, 12, r, h, e, b);

        gotoxy(1, 15);

        printf("무승부입니다. 아무키나 누르시오\n");

        getch();

        return 0;

}

 

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

확장된 바둑판 그리기 함수 draw_check02

*/

void draw_check02(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[12];

        for (i = 1; i < 12; i++)

               b[i] = 0xa0 + i;

        printf("%c%c", a, b[3]);

        for (i = 0; i < c - 1; i++)

        {

               printf("%c%c", a, b[1]);

               printf("%c%c", a, b[8]);

        }

        printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

        for (i = 0; i < r - 1; i++)

        {

               printf("%c%c", a, b[2]);;

               for (j = 0; j < c; j++)

               {

                       printf("  ");

                       printf("%c%c", a, b[2]);

               }

               printf("\n");

               printf("%c%c", a, b[7]);

               for (j = 0; j < c - 1; j++)

               {

                       printf("%c%c", a, b[1]);

                       printf("%c%c", a, b[11]);

               }

               printf("%c%c", a, b[1]);

               printf("%c%c", a, b[9]);

               printf("\n");

        }

        printf("%c%c", a, b[2]);

        for (j = 0; j < c; j++)

        {

               printf("  ");

               printf("%c%c", a, b[2]);

        }

        printf("\n");

        printf("%c%c", a, b[6]);

        for (i = 0; i < c - 1; i++)

        {

               printf("%c%c", a, b[1]);

               printf("%c%c", a, b[10]);

        }

        printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 

/*

전광판을 표시하는 함수 display_scoreboard

*/

void display_scoreboard(int r[], int h[], int e[], int b[])

{

        int i, j;

        printf("스코어보드 \n");

        draw_check02(17, 3);

        for (i = 1; i <= 12; i++)

        {

               gotoxy(i * 4 + 3, 3);

               printf("%2d", i);

        }

        gotoxy(13 * 4 + 4, 3);

        printf("R");

        gotoxy(14 * 4 + 4, 3);

        printf("H");

        gotoxy(15 * 4 + 4, 3);

        printf("E");

        gotoxy(16 * 4 + 4, 3);

        printf("B");

        gotoxy(3, 5);

        printf("CL");

        gotoxy(3, 7);

        printf("TG");

        for (j = 0; j <= 1; j++)

        {

               gotoxy(13 * 4 + 3, 5 + 2 * j);

               printf("%2d", r[j]);

               gotoxy(14 * 4 + 3, 5 + 2 * j);

               printf("%2d", h[j]);

               gotoxy(15 * 4 + 3, 5 + 2 * j);

               printf("%2d", e[j]);

               gotoxy(16 * 4 + 3, 5 + 2 * j);

               printf("%2d", b[j]);

        }

}

 

/*

각 회차별 정보를 득점판에 출력하는 함수 control_scoreboard

*/

void control_scoreboard(int st, int end, int r[], int h[], int e[], int b[])

{

        int i, j, k, run, hit, error, fourBall;

        char state[3];

        for (i = st; i <= end; i++)

        {

               for (j = 0; j <= 1; j++)

               {

                       if (j == 0)

                              strcpy(state, "");

                       else

                              strcpy(state, "");

                       gotoxy(i * 4 + 3, 5 + 2 * j);

                       printf("%2d", 0);

                       gotoxy(1, 10);

                       printf("%d%s 결과 ", i, state);

                       gotoxy(1, 11);

 

                       printf("안타수 : ");

                       hit = rand() % 5;

                       h[j] += hit;

                       printf("%d", hit);

                       gotoxy(14 * 4 + 3, 5 + 2 * j);

                       printf("%2d", h[j]);

                       gotoxy(1, 12);

 

                       printf("실책수: ");

                       error = rand() % 2;

                       e[j] += error;

                       printf("%d", error);

                       gotoxy(15 * 4 + 3, 5 + 2 * (1 - j));

                       printf("%2d", e[j]);

 

                       gotoxy(1, 13);

                       printf("포볼수: ");

                       fourBall = rand() % 3;

                       b[j] += fourBall;

                       printf("%d", fourBall);

                       gotoxy(16 * 4 + 3, 5 + 2 * j);

                       printf("%2d", b[j]);

                       gotoxy(1, 14);

                       printf("점수: ");

                       run = hit / 2; //점수는 안타의 반으로 가정한다

                       r[j] += run;

                       printf("%d", run);

                       gotoxy(13 * 4 + 3, 5 + 2 * j);

                       printf("%2d", r[j]);

 

                       gotoxy(i * 4 + 3, 5 + 2 * j);

                       printf("%2d", run);

 

                       gotoxy(1, 15);

                       printf("아무키나 누르시오....");

                       getch();

                       for (k = 10; k <= 15; k++)

                       {

                              gotoxy(1, k);

                              printf("                                        "); //출력문 지우기

                       }

                       if (i == 9 && j == 1 && r[0] == r[1]) //9회말에도 동점인 경우

                       {

                              gotoxy(1, 15);

                              printf("연장전에 돌입합니다\n");

                              Sleep(1000);

                              gotoxy(1, 15);

                              printf("                                      ");

                       }

                       if (9 <= i && ((j == 0 && r[0] < r[1]) || (j == 1 && r[0] != r[1]))) //9회에 점수가 다를 경우

                       {

                              gotoxy(1, 15);

                              if (r[0] > r[1])

                                      printf("CL 팀이 이겼습니다. 아무키나 누르시오\n");

                              else

                                      printf("TG 팀이 이겼습니다. 아무키나 누르시오\n");

                              getch();

                              exit(0);

                       }

               }

        }

}




개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 명품 C언어 프로젝트 안기수 저

반응형