Recent Posts
Recent Comments
03-29 00:00
관리 메뉴

동글동글 라이프

C언어로 구현한 빙고게임 본문

개발자 이야기/C 언어로 게임 만들기

C언어로 구현한 빙고게임

동글동글라이프 2011. 10. 9. 22:31

어릴때 친구들과 열심히 했던 빙고게임을 C언어로 만들어 봤습니다.


국민적인 게임이라 빙고게임을 대부분 알고 계실 것이라 생각됩니다.

하지만 빙고게임의 방식이 각 나라나 지역에 따라 다르기 때문에
저는 제가 알고 있는 빙고 게임대로 만들어 보도록 하겠습니다 ^^


빙고게임을 만들기 위해서는 크게 3가지를 주의해서 만드시면 됩니다.


1. 숫자 섞기

5x5 판에서 1 ~ 25까지 순서대로만 숫자를 나열하면 재미가 없으니
두 숫자를 뽑아서 서로 바꿔주는 식으로 숫자를 섞으면 됩니다.


위와 같이 랜덤으로 좌표를 2개를 설정 한다음 두 값을 바꿔주는 형태로

사용자가 원하는 만큼 또는 적절한 양만큼 섞어주면 랜덤된 빙고 판이 완성되게 됩니다.


2. 숫자 설정


선택한 숫자를 임의의 숫자( 0 또는 다른 수) 로 바꿔줌으로써
출력도 다른 문자로 출력 되게 합니다.

이것은 내가 숫자를 부른 부분에 대해서 색칠을 하는 효과와 동일 합니다.

이미 숫자가 설정되어 있다면 다시 설정하게 하는 부분도 예외사항으로 잘 설정해야 합니다.



3. 빙고 개수 체크



빙고 개수 체크는 총 4가지 입니다.
행,열, 각 대각선으로 빙고 개수를 체크하면 됩니다.
( 2중 for 문 단 한번으로 이 빙고 개수를 모두 체크 할 수 있습니다.)

5x5 빙고판일 경우 5번이 먼저 Check 될 경우에
게임을 끝나게 설정 합니다.


아래는 컴퓨터와 플레이어가 직접 번호를 주고받으며
빙고게임을 할 수 있도록 만들어 보았습니다.

즐겜 하세요~~


원문 링크 : http://pastebin.com/NwmhyPtC

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5.  
  6. #define MAX 5
  7. #define WHITE 15
  8. #define YELLOW 14
  9.  
  10.  
  11. void textcolor(int color_number); // 텍스트 칼라 출력
  12. void gotoxy(int x, int y); // 좌표 이동
  13. int baserand(int x, int y); // 랜덤 범위 지정
  14.  
  15. // 초기값 설정
  16. void InitCount(int Player[MAX][MAX] , int Com[MAX][MAX]);
  17.  
  18. // MAP 설정
  19. void MixMAP(int Player[MAX][MAX]);
  20. int SearchMAP(int Player[MAX][MAX] ,int Num);
  21. void printMAP(int Player[MAX][MAX]);
  22. int CheckMAP(int Player[MAX][MAX]);
  23.  
  24. // 승리조건
  25. void Winner(int flag , int Player[MAX][MAX],int Com[MAX][MAX]);
  26.  
  27.  
  28.  
  29.  
  30. int main(void){
  31.  
  32.         int Player[MAX][MAX];
  33.         int Com[MAX][MAX];
  34.         int playerChk,comChk;
  35.         int Num;       
  36.  
  37.         InitCount(Player,Com);
  38.        
  39.         MixMAP(Player);
  40.  
  41.         while(1){
  42.                 gotoxy(0,0);
  43.  
  44.                 // 컴퓨터들 출력
  45.                 textcolor(WHITE);
  46.                 printf(" ====== Player ====== \n");
  47.                 printMAP(Player);
  48.                 textcolor(WHITE);
  49.                 printf(" ===== Computer ===== \n");
  50.                 printMAP(Com);         
  51.  
  52.                 textcolor(WHITE);
  53.                 printf(" > ");
  54.                 scanf("%d",&Num);
  55.  
  56.                 if( SearchMAP(Player,Num) == 0){
  57.                         printf("잘못입력하셨습니다. \n");                     
  58.                         system("pause");
  59.                         system("cls");
  60.                         continue;
  61.                 }
  62.  
  63.                 SearchMAP(Com,Num);
  64.  
  65.                 // 컴퓨터 턴
  66.                 while( 1 ){
  67.                         Num = baserand(1,MAX*MAX);
  68.                         if(SearchMAP(Com, Num) ==1 ){
  69.                                 SearchMAP(Player, Num);
  70.                                 break;
  71.                         }
  72.                 }
  73.  
  74.                 playerChk = CheckMAP(Player);
  75.                 comChk = CheckMAP(Com);
  76.  
  77.                 printf("Player Check = %d \n",playerChk);
  78.                 printf("Com Check = %d \n",comChk);
  79.  
  80.                 if(playerChk >= MAX && comChk >= MAX){
  81.                         if( playerChk > comChk){
  82.                                 Winner(0,Player,Com); // 내가 이겼을 때
  83.                         }else if( playerChk < comChk) {
  84.                                 Winner(1,Player,Com); // 내가 졌을 때
  85.                         }else{
  86.                                 Winner(2,Player,Com); // 배겼을 때
  87.                         }
  88.                 }else  if(playerChk >= MAX){
  89.                         Winner(0,Player,Com); // 내가 이겼을 때
  90.                 }else  if(comChk >= MAX){
  91.                         Winner(1,Player,Com); // 내가 졌을 때
  92.                 }
  93.  
  94.                 system("pause");
  95.                 system("cls");
  96.         }
  97.         return 0;
  98. }
  99.  
  100. // 텍스트 칼라 출력
  101. void textcolor(int color_number)
  102. {
  103.  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_number);
  104. };
  105. // 좌표 이동
  106. void gotoxy(int x, int y)
  107. {
  108.      COORD Cur;
  109.      Cur.X=x;
  110.      Cur.Y=y;
  111.      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Cur);
  112. }
  113. // 랜덤 범위 지정
  114. int baserand(int x, int y){
  115.  
  116.         static int z = 0;
  117.         int tmp;
  118.         if(z==0){
  119.                 srand((int)time(NULL));
  120.                 rand();rand();rand();rand();
  121.                 srand(rand());
  122.                 z=1;
  123.         }
  124.  
  125.         tmp = rand()%(y-x+1)+x;
  126.         return tmp;
  127. }
  128.  
  129. void printMAP(int Player[MAX][MAX]){
  130.         int i,j;
  131.         for(i=0;i<MAX;i++){
  132.                 for(j=0;j<MAX;j++){
  133.                         if(Player[i][j] == 0){
  134.                                 textcolor(YELLOW);
  135.                                 printf("%4s","♥");
  136.  
  137.                         }else{
  138.                                 textcolor(WHITE);
  139.                                 printf("%4d",Player[i][j]);
  140.                         }
  141.                 }
  142.                 printf("\n");
  143.         }
  144. }
  145.  
  146.  
  147. void MixMAP(int Player[MAX][MAX]){
  148.  
  149.         int i;
  150.         int x1,y1;
  151.         int x2,y2;
  152.         int tmp;
  153.  
  154.         printMAP(Player);
  155.  
  156.         for(i=0;i< 10*MAX ;i++){
  157.  
  158.                 x1 = baserand(0,MAX-1);
  159.                 y1 = baserand(0,MAX-1);
  160.  
  161.                 x2 = baserand(0,MAX-1);
  162.                 y2 = baserand(0,MAX-1);
  163.  
  164.                 // 두 값을 서로 바꾸는 코드
  165.                 tmp = Player[x1][y1];
  166.                 Player[x1][y1] = Player[x2][y2];
  167.                 Player[x2][y2] = tmp;
  168.  
  169.                 gotoxy(0,0);
  170.                 printMAP(Player);              
  171.        
  172.                 Sleep(10);
  173.         }
  174.         system("pause");
  175.         system("cls");
  176.        
  177. }
  178.  
  179. int SearchMAP(int Player[MAX][MAX] ,int Num){
  180.  
  181.         int i,j;
  182.         int flag = 0;
  183.  
  184.         for(i=0;i<MAX;i++){
  185.                 for(j=0;j<MAX;j++){
  186.                         if(Player[i][j] == Num){
  187.                                 flag = 1;
  188.                                 Player[i][j] = 0;
  189.                         }
  190.                 }
  191.         }
  192.  
  193.         return flag;
  194. }
  195.  
  196. int CheckMAP(int Player[MAX][MAX]){
  197.         int i,j;
  198.         int rowsFlag = 0;
  199.         int columnFlag = 0;
  200.         int crossleftFlag = 0;
  201.         int crossrightFlag = 0;
  202.         int check = 0;         
  203.  
  204.         for(i=0;i<MAX;i++){
  205.                 rowsFlag = 0;
  206.                 columnFlag = 0;
  207.  
  208.                 for(j=0;j<MAX;j++){
  209.                         if(Player[i][j] == 0){
  210.                                 rowsFlag++;
  211.                         }
  212.                         if(Player[j][i] == 0){
  213.                                 columnFlag++;
  214.                         }
  215.                 }
  216.                 // 가로체크
  217.                 if(rowsFlag == MAX){
  218.                         check++;
  219.                 }
  220.                 // 세로체크
  221.                 if(columnFlag == MAX){
  222.                         check++;
  223.                 }
  224.                 // 대각선 왼쪽에서 오른쪽
  225.                 if(Player[i][i] == 0){
  226.                         crossleftFlag++;
  227.                 }
  228.                 // 대각선 오른쪽에서 왼쪽
  229.                 if(Player[MAX-1-i][i] == 0){
  230.                         crossrightFlag++;
  231.                 }
  232.         }
  233.  
  234.         if(crossleftFlag == MAX){
  235.                 check++;
  236.         }
  237.  
  238.         if(crossrightFlag == MAX){
  239.                 check++;
  240.         }
  241.  
  242.         return check;
  243. }
  244.  
  245. void Winner(int flag , int Player[MAX][MAX],int Com[MAX][MAX]){
  246.         gotoxy(0,0);
  247.         textcolor(WHITE);
  248.         printf(" ====== Player ====== \n");
  249.         printMAP(Player);
  250.         textcolor(WHITE);
  251.         printf(" ===== Computer ===== \n");
  252.         printMAP(Com);         
  253.         gotoxy(0,MAX*2+5);
  254.         switch(flag){
  255.                 case 0:
  256.                         printf("당신이 이겼습니다. \n");
  257.                         break;
  258.                 case 1:
  259.                         printf("당신이 졌습니다. \n");
  260.                         break;
  261.                 case 2:
  262.                         printf("비겼습니다. \n");
  263.                         break;
  264.         }
  265.         exit(0);
  266.  
  267. }
  268.  
  269. void InitCount(int Player[MAX][MAX] , int Com[MAX][MAX]){
  270.  
  271.         int i,j;
  272.         int count =1 ;
  273.         for(i=0;i<MAX;i++){
  274.                 for(j=0;j<MAX;j++){
  275.                         Player[i][j] = count;
  276.                         Com[i][j] = count;
  277.                         count++;
  278.                 }
  279.         }
  280. }
  281.  


앞으로 잔잔한 게임들을 소스공개 및 설명과 함께 올리도록 하겠습니다.
 
 


Comments