C언어

(C/C++) 폭탄게임(지뢰찾기) 구현해보기!

혀래이 2021. 6. 29. 14:53
  • 지뢰가 나올 확률을 50% -> 25%로 조정했습니다. 
  • 코드 복붙하셔서 콘솔창에서 플레이해보면 생각보다 재미있습니다!
  • 주석으로 명세 붙여놓았으니 구현해보실 분들은 주석을 보세요.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 10 x 10 이차원 배열
// 1, 0 으로 랜덤한 값이 저장되어 있음
// 1 : 폭탄 0 : 안전지대
// 플레이어 -> 정수를 두 개 입력 ( 2, 3 ) 
// 컴퓨터 -> 랜덤하게 두 자리 지정 ( 1, 4 )
// 폭탄을 밟으면 게임 오버 
// 게임 오버까지 계속 진행
// 플레이어 win! / 컴퓨터 win!
// 1,0 표시하다가 완성하면 x로 덮어주기
// 건드린 공간은 0,1로 재표시
 
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
 
void main() {
    srand(time(NULL));
    int map[10][10];
    char x_map[10][10];
    int player_x, player_y, com_x, com_y;
 
    // 폭탄게임 초기화
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            map[i][j] = rand() % 4;
            x_map[i][j] = 'X';
        }
    }
 
 
    while (1) {
 
        // 컴퓨터의 랜덤선택
        com_x = rand() % 10 + 1;
        com_y = rand() % 10 + 1;
 
        if (x_map[com_x - 1][com_y - 1== '1' || x_map[com_x - 1][com_y - 1== '0') {
            continue;
        }
 
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                printf(" %c l", x_map[i][j]);
            }
            printf("\n");
            printf("----------------------------------------");
            printf("\n");
        }
 
        printf("x좌표와 y좌표를 입력하세요! : ");
        scanf("%d %d"&player_x, &player_y);
 
 
        if (player_x < 1 || player_x > 10 || player_y < 1 || player_y > 10) {
            printf("잘못된 범위입니다. 다시 입력하세요!\n");
            continue;
        }
        else if (x_map[player_x - 1][player_y - 1== '1' || x_map[player_x - 1][player_y - 1== '0') {
            printf("이미 선택된 자리입니다. 다른 자리를 입력하세요!\n");
            continue;
        }
 
        if (map[player_x - 1][player_y - 1== 1) {
            x_map[player_x - 1][player_y - 1= '*';
            printf("컴퓨터 win!\n");
            break;
        }
        else {
            x_map[player_x - 1][player_y - 1= '0';
        }
        Sleep(1000);
        printf("컴퓨터의 선택 : %d %d\n", com_x, com_y);
 
        if (map[com_x - 1][com_y - 1== 1) {
            x_map[com_x - 1][com_y - 1= '*';
            printf("플레이어 win!\n");
            break;
        }
        else {
            x_map[com_x - 1][com_y - 1= '0';
        }
 
        Sleep(1000);
        system("cls");
 
 
    }
    Sleep(1000);
    system("cls");
    printf("결과를 공개합니다! \n\n");
 
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            printf(" %c l", x_map[i][j]);
        }
        printf("\n");
        printf("----------------------------------------");
        printf("\n");
    }
 
}
cs