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

명품 C언어 프로젝트 8.8장 슬롯 머신 응용문제

꾸준함. 2017. 9. 12. 12:00

[3번 문제]

/*

규칙을 추가하여 직선 방향 외에 대각선 방향으로도 당첨 사례를 판정할 수 있도록 수정하시오

*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

#include <Windows.h>

 

void display_rule(void);

void draw_check02(int c, int r);

void reel_series(int r[][3]);

void gotoxy(int x, int y);

int game_progress(int money);

void display_reel(char rc[][3], int r[][3], int index);

void clear_text(void);

void game_control(char reel[][3], int reel_num[][3], int *money);

int return_money(int r[][3], int betting, int *case_n);

void clear_case(void);

 

int main(void)

{

        char reel[6][3] = { "", "", "", "", "", "" };

        int i, reel_num[3][3];

        int money = 10000;

        srand((unsigned)time(NULL));

        printf("\t슬롯머신 게임\n\n");

        display_rule();

        gotoxy(3, 5);

        printf("1 2 3");

        gotoxy(1, 6);

        draw_check02(3, 3);

        for (i = 0; i < 3; i++)

               reel_num[0][i] = rand() % 5;

        reel_series(reel_num);

 

        do

        {

               game_control(reel, reel_num, &money);

        } while (money);

        gotoxy(3, 20);

        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_rule

*/

void display_rule(void)

{

        gotoxy(30, 3);

        printf("\t1\t2\t3\t사례금\n");

        gotoxy(30, 4);

        printf("----------------------------------\n");

        gotoxy(30, 5);

        printf("1:\t\t\t\t*10\n");

        gotoxy(30, 6);

        printf("2:\t\t\t\t *7\n");

        gotoxy(30, 7);

        printf("3:\t\t\t\t *5\n");

        gotoxy(30, 8);

        printf("4:\t\t\t기타\t *4\n");

        gotoxy(30, 9);

        printf("5:\t\t\t기타\t *3\n");

        gotoxy(30, 10);

        printf("6:\t\t\t기타\t *3\n");

        gotoxy(30, 11);

        printf("7:\t\t\t기타\t *3\n");

        gotoxy(30, 12);

        printf("8:\t\t\t기타\t *2\n");

        gotoxy(30, 13);

        printf("9:\t\t\t기타\t *1\n");

}

 

/*

게임 진행 상황과 배팅금액을 입력받아 반환하는 함수 game_progress

*/

int game_progress(int money)

{

        int bet;

        gotoxy(43, 17);

        printf("현재 남은 금액: %d", money);

        gotoxy(1, 17);

        printf("배팅금액을 입력하고 Enter키를 누르면 ");

        gotoxy(1, 19);

        printf("0을 입력하면 종료합니다.");

        gotoxy(1, 18);

        printf("게임을 시작합니다. 금액 입력>");

        gotoxy(30, 18);

        scanf("%d", &bet);

        if (bet == 0)

               exit(0);

        gotoxy(1, 19);

        printf("아무키나 한번만 키를 누르면 릴이 멈춥니다.");

        return bet;

}

 

/*

화면에 표시된 지시어들을 지우는 함수 clear_text

*/

void clear_text(void)

{

        int i, j;

        for (i = 17; i < 20; i++)

        {

               gotoxy(1, i);

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

                       printf(" ");

        }

}

 

/*

화면에 표시된 사례 지우기 함수 clear case

*/

void clear_case(void)

{

        gotoxy(43, 18);

        printf("                                             ");

        gotoxy(43, 19);

        printf("                                             ");

}

 

/*

각 릴에 대한 기호의 순서를 결정하는 함수 reel_series

*/

void reel_series(int r[][3])

{

        int i, j;

        for (i = 0; i < 3; i++)

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

                       r[j][i] = (r[0][i] + j) % 6; //세개의 릴에 대한 기호 순서

}

 

/*

회전 순서에 따라 릴을 화면에 출력하는 함수 display_reel

*/

void display_reel(char rc[][3], int r[][3], int index)

{

        int i, j;

        for (i = 0; i < 3; i++)

               for (j = index; j < 3; j++)

               {

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

                       printf("%s", rc[r[i][j]]);

               }

}

 

/*

슬롯 머신 게임을 제어하는 함수 game_control

*/

void game_control(char reel[][3], int reel_num[][3], int *money)

{

        int i, j, bet, case_num = 0, thank, num[3][3];

        double pst;

        clock_t start, end;

 

        display_reel(reel, reel_num, 0);

        bet = game_progress(*money);

 

        for (i = 0; i < 3; i++)

        {

               start = clock();

               do

               {

                       for (j = i; j < 3; j++)

                              reel_num[0][j] = reel_num[1][j];

                       reel_series(reel_num);

                       display_reel(reel, reel_num, i);

                       end = clock();

                       pst = (double)(end - start) / CLK_TCK;

               } while (!kbhit() || (pst < 1)); //1초안에 혹은 키를 누를경우

        }

        for (i = 0; i < 3; i++)

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

                       num[i][j] = reel_num[i][j]; //복사

        getch();

        thank = return_money(num, bet, &case_num);

        if (thank == 0)

               *money -= bet;

        else

               *money += thank;

        if (case_num)

        {

               gotoxy(43, 18);

               printf("사례:%d, 베팅:%d, 사례금:%d", case_num, bet, thank);

        }

        else

        {

               clear_case();

        }

        clear_text();

}

 

/*

당첨 사례 따라 배팅 금액과 사례금을 계산하는 함수 return_money

*/

int return_money(int r[][3], int betting, int *case_num)

{

        int total = 0;

        //문양이 세개 동일할경우(, , )

        if (r[1][0] == r[1][1] && r[1][1] == r[1][2] && r[1][0] == r[1][2])

        {

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

               {

                       total = betting * 10;

                       *case_num = 1;

               }

               else if (r[1][1] == 1)

               {

                       total = betting * 7;

                       *case_num = 2;

               }

               else if (r[1][1] == 2)

               {

                       total = betting * 5;

                       *case_num = 3;

               }

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        //대각선 포함

        else if (r[0][0] == r[1][1] && r[1][1] == r[2][2] && r[0][0] == r[2][2])

        {

               if (r[1][1] == 0) //대각선과 다겹치는 곳은 r[1][1]이므로

               {

                       total = betting * 10;

                       *case_num = 1;

               }

               else if (r[1][1] == 1)

               {

                       total = betting * 7;

                       *case_num = 2;

               }

               else if (r[1][1] == 2)

               {

                       total = betting * 5;

                       *case_num = 3;

               }

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if (r[0][2] == r[1][1] && r[1][1] == r[2][0] && r[0][2] == r[2][0])

        {

               if (r[1][1] == 0) //대각선과 다겹치는 곳은 r[1][1]이므로

               {

                       total = betting * 10;

                       *case_num = 1;

               }

               else if (r[1][1] == 1)

               {

                       total = betting * 7;

                       *case_num = 2;

               }

               else if (r[1][1] == 2)

               {

                       total = betting * 5;

                       *case_num = 3;

               }

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        //그 외 두개가 동일할 경우, 대각선 포함

        else if ((r[1][0] == 0 && r[1][1] == 0) || (r[1][1] == 0 && r[1][2] == 0) || (r[1][0] == 0 && r[1][2] == 0))

        {

               total = betting * 4;

               *case_num = 4;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][0] == 0 && r[1][1] == 0) || (r[1][1] == 0 && r[2][2] == 0) || (r[0][0] == 0 && r[2][2] == 0))

        {

               total = betting * 4;

               *case_num = 4;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][2] == 0 && r[1][1] == 0) || (r[1][1] == 0 && r[2][0] == 0) || (r[0][2] == 0 && r[2][0] == 0))

        {

               total = betting * 4;

               *case_num = 4;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[1][0] == 1 && r[1][1] == 1) || (r[1][1] == 1 && r[1][2] == 1) || (r[1][0] == 1 && r[1][2] == 1))

        {

               total = betting * 3;

               *case_num = 5;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][0] == 1 && r[1][1] == 1) || (r[1][1] == 1 && r[2][2] == 1) || (r[0][0] == 1 && r[2][2] == 1))

        {

               total = betting * 3;

               *case_num = 5;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][2] == 1 && r[1][1] == 1) || (r[1][1] == 1 && r[2][0] == 1) || (r[0][2] == 1 && r[2][0] == 1))

        {

               total = betting * 3;

               *case_num = 5;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[1][0] == 2 && r[1][1] == 2) || (r[1][1] == 2 && r[1][2] == 2) || (r[1][0] == 2 && r[1][2] == 2))

        {

               total = betting * 3;

               *case_num = 6;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][0] == 2 && r[1][1] == 2) || (r[1][1] == 2 && r[2][2] == 2) || (r[0][0] == 2 && r[2][2] == 2))

        {

               total = betting * 3;

               *case_num = 6;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][2] == 2 && r[1][1] == 2) || (r[1][1] == 2 && r[2][0] == 2) || (r[0][2] == 2 && r[2][0] == 2))

        {

               total = betting * 3;

               *case_num = 6;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[1][0] == 3 && r[1][1] == 3) || (r[1][1] == 3 && r[1][2] == 3) || (r[1][0] == 3 && r[1][2] == 3))

        {

               total = betting * 3;

               *case_num = 7;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][0] == 3 && r[1][1] == 3) || (r[1][1] == 3 && r[2][2] == 3) || (r[0][0] == 3 && r[2][2] == 3))

        {

               total = betting * 3;

               *case_num = 7;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][2] == 3 && r[1][1] == 3) || (r[1][1] == 3 && r[2][0] == 3) || (r[0][2] == 3 && r[2][0] == 3))

        {

               total = betting * 3;

               *case_num = 7;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[1][0] == 4 && r[1][1] == 4) || (r[1][1] == 4 && r[1][2] == 4) || (r[1][0] == 4 && r[1][2] == 4))

        {

               total = betting * 2;

               *case_num = 8;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][0] == 4 && r[1][1] == 4) || (r[1][1] == 4 && r[2][2] == 4) || (r[0][0] == 4 && r[2][2] == 4))

        {

               total = betting * 2;

               *case_num = 8;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][2] == 4 && r[1][1] == 4) || (r[1][1] == 4 && r[2][0] == 4) || (r[0][2] == 4 && r[2][0] == 4))

        {

               total = betting * 2;

               *case_num = 8;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[1][0] == 5 && r[1][1] == 5) || (r[1][1] == 5 && r[1][2] == 5) || (r[1][0] == 5 && r[1][2] == 5))

        {

               total = betting * 1;

               *case_num = 9;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][0] == 5 && r[1][1] == 5) || (r[1][1] == 5 && r[2][2] == 5) || (r[0][0] == 5 && r[2][2] == 5))

        {

               total = betting * 1;

               *case_num = 9;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else if ((r[0][2] == 5 && r[1][1] == 5) || (r[1][1] == 5 && r[2][0] == 5) || (r[0][2] == 5 && r[2][0] == 5))

        {

               total = betting * 1;

               *case_num = 9;

               gotoxy(45, 19);

               printf("당첨 방향: ");

        }

        else //당첨 사례가 없을 경우

               *case_num = 0;

        return total;

}

 


[4번 문제]

/*

릴이 멈추는 순서를 난수에 의해 임의로 결정하도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

#include <Windows.h>

 

void display_rule(void);

void draw_check02(int c, int r);

void reel_series(int r[][3]);

void gotoxy(int x, int y);

int game_progress(int money);

void display_reel(char rc[][3], int r[][3]);

void clear_text(void);

void game_control(char reel[][3], int reel_num[][3], int *money);

int return_money(int r[], int betting, int *case_n);

void clear_case(void);

 

int main(void)

{

        char reel[6][3] = { "", "", "", "", "", "" };

        int i, reel_num[3][3];

        int money = 10000;

        srand((unsigned)time(NULL));

        printf("\t슬롯머신 게임\n\n");

        display_rule();

        gotoxy(3, 5);

        printf("1 2 3");

        gotoxy(1, 6);

        draw_check02(3, 3);

        for (i = 0; i < 3; i++)

               reel_num[0][i] = rand() % 5;

        reel_series(reel_num);

 

        do

        {

               game_control(reel, reel_num, &money);

        } while (money);

        gotoxy(3, 20);

        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_rule

*/

void display_rule(void)

{

        gotoxy(30, 3);

        printf("\t1\t2\t3\t사례금\n");

        gotoxy(30, 4);

        printf("----------------------------------\n");

        gotoxy(30, 5);

        printf("1:\t\t\t\t*10\n");

        gotoxy(30, 6);

        printf("2:\t\t\t\t *7\n");

        gotoxy(30, 7);

        printf("3:\t\t\t\t *5\n");

        gotoxy(30, 8);

        printf("4:\t\t\t기타\t *4\n");

        gotoxy(30, 9);

        printf("5:\t\t\t기타\t *3\n");

        gotoxy(30, 10);

        printf("6:\t\t\t기타\t *3\n");

        gotoxy(30, 11);

        printf("7:\t\t\t기타\t *3\n");

        gotoxy(30, 12);

        printf("8:\t\t\t기타\t *2\n");

        gotoxy(30, 13);

        printf("9:\t\t\t기타\t *1\n");

}

 

/*

게임 진행 상황과 배팅금액을 입력받아 반환하는 함수 game_progress

*/

int game_progress(int money)

{

        int bet;

        gotoxy(43, 17);

        printf("현재 남은 금액: %d", money);

        gotoxy(1, 17);

        printf("배팅금액을 입력하고 Enter키를 누르면 ");

        gotoxy(1, 19);

        printf("0을 입력하면 종료합니다.");

        gotoxy(1, 18);

        printf("게임을 시작합니다. 금액 입력>");

        gotoxy(30, 18);

        scanf("%d", &bet);

        if (bet == 0)

               exit(0);

        gotoxy(1, 19);

        printf("아무키나 한번만 키를 누르면 릴이 멈춥니다.");

        return bet;

}

 

/*

화면에 표시된 지시어들을 지우는 함수 clear_text

*/

void clear_text(void)

{

        int i, j;

        for (i = 17; i < 20; i++)

        {

               gotoxy(1, i);

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

                       printf(" ");

        }

}

 

/*

화면에 표시된 사례 지우기 함수 clear case

*/

void clear_case(void)

{

        gotoxy(43, 18);

        printf("                                             ");

}

 

/*

각 릴에 대한 기호의 순서를 결정하는 함수 reel_series

*/

void reel_series(int r[][3])

{

        int i, j;

        for (i = 0; i < 3; i++)

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

                       r[j][i] = (r[0][i] + j) % 6; //세개의 릴에 대한 기호 순서

}

 

/*

회전 순서에 따라 릴을 화면에 출력하는 함수 display_reel

*/

void display_reel(char rc[][3], int r[][3])

{

        int i, j;

        for (i = 0; i < 3; i++)

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

               {

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

                       printf("%s", rc[r[i][j]]);

               }

}

 

/*

슬롯 머신 게임을 제어하는 함수 game_control

*/

void game_control(char reel[][3], int reel_num[][3], int *money)

{

        int i, j, bet, case_num = 0, thank, num[3];

        double pst;

        int random[3]; //임의의 릴을 먼저 멈추게 하도록 난수 발생

        clock_t start, end;

 

        display_reel(reel, reel_num, 0);

        bet = game_progress(*money);

 

        //겹치지 않도록

        random[0] = rand() % 3;

        do

        {

               random[1] = rand() % 3;

        } while (random[1] == random[0]);

        do

        {

               random[2] = rand() % 3;

        } while (random[2] == random[1] || random[2] == random[0]);

 

        for (i = 0; i < 3; i++)

        {

               /*

               gotoxy(1, 24);

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

                       printf("%d ", random[j]);

                       */

               start = clock();

               do

               {

                       for (j = i; j < 3; j++) //릴을 돌리는 함수

                              reel_num[0][random[j]] = reel_num[1][random[j]];

                       reel_series(reel_num);

                       display_reel(reel, reel_num);

                       end = clock();

                       pst = (double)(end - start) / CLK_TCK;

               } while (!kbhit() || (pst < 1)); //1초안에 혹은 키를 누를경우

               num[i] = reel_num[1][i]; //가운데만 보기때문에

        }

        getch();

        thank = return_money(num, bet, &case_num);

        if (thank == 0)

               *money -= bet;

        else

               *money += thank;

        if (case_num)

        {

               gotoxy(43, 18);

               printf("사례:%d, 베팅:%d, 사례금:%d", case_num, bet, thank);

        }

        else

        {

               clear_case();

        }

        clear_text();

}

 

/*

당첨 사례 따라 배팅 금액과 사례금을 계산하는 함수 return_money

*/

int return_money(int r[], int betting, int *case_num)

{

        int total = 0;

        if (r[0] == r[1] && r[1] == r[2] && r[0] == r[2]) //문양이 세개 동일할경우(, , )

        {

               if (r[0] == 0)

               {

                       total = betting * 10;

                       *case_num = 1;

               }

               else if (r[0] == 1)

               {

                       total = betting * 7;

                       *case_num = 2;

               }

               else if (r[0] == 2)

               {

                       total = betting * 5;

                       *case_num = 3;

               }

        }

        //그 외 두개가 동일할 경우

        else if ((r[0] == 0 && r[1] == 0) || (r[1] == 0 && r[2] == 0) || (r[0] == 0 && r[2] == 0))

        {

               total = betting * 4;

               *case_num = 4;

        }

        else if ((r[0] == 1 && r[1] == 1) || (r[1] == 1 && r[2] == 1) || (r[0] == 1 && r[2] == 1))

        {

               total = betting * 3;

               *case_num = 5;

        }

        else if ((r[0] == 2 && r[1] == 2) || (r[1] == 2 && r[2] == 2) || (r[0] == 2 && r[2] == 2))

        {

               total = betting * 3;

               *case_num = 6;

        }

        else if ((r[0] == 3 && r[1] == 3) || (r[1] == 3 || r[2] == 3) || (r[0] == 3 && r[2] == 3))

        {

               total = betting * 3;

               *case_num = 7;

        }

        else if ((r[0] == 4 && r[1] == 4) || (r[1] == 4 && r[2] == 4) || (r[0] == 4 && r[2] == 4))

        {

               total = betting * 2;

               *case_num = 8;

        }

        else if ((r[0] == 5 && r[1] == 5) || (r[1] == 5 && r[2] == 5) || (r[0] == 5 && r[2] == 5))

        {

               total = betting * 1;

               *case_num = 9;

        }

        else //당첨 사례가 없을 경우

               *case_num = 0;

        return total;

}

 



개발환경:Visual Studio 2017


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


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


반응형