지뢰 찾기

Study/C++ 2011. 9. 29. 20:22
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
int count_one(int mines[][5], int row, int col, int x, int y)
{
    int start_x = x - 1;
    int end_x = x + 1;
    int start_y = y - 1;
    int end_y = y + 1;
    int i, j, count = 0;
 
    if(start_x < 0) start_x = 0;
    if(end_x >= col) end_x = col - 1;
    if(start_y < 0) start_y = 0;
    if(end_y >= row) end_y = row - 1;
 
    for(i = start_x; i <= end_x; i++){
        for(j = start_y; j <= end_y; j++){
            count += mines[j][i];
        }
    }
 
    return count;
}
 
 
int main(void)
{
    srand(time(NULL));
 
    int mines[5][5], i, j, x, y;
 
    for(i = 0; i < 5; i++){
        for(j = 0; j < 5; j++){
            int temp = rand() % 2;
            cout << temp << " ";
            mines[i][j] = temp;
        }
        cout << endl;
    }
 
    do{
        // 사용자 입력
        // cin 을 사용해서 x, y에 위치 입력
        cin >> x >> y;
 
        if(x == -1 || y == -1) break;
 
        // 함수 호출
        cout << "1의 개수는 " << count_one(mines, 5, 5, x, y) << endl;
    }while(true);
 
    return 0;
}

'Study > C++' 카테고리의 다른 글

퍼즐게임  (0) 2011.09.29
tic tac toe 게임  (0) 2011.09.29
소수 확인하기  (0) 2011.09.29
빙고 게임  (0) 2011.09.29
동적 할당하기  (0) 2011.09.29
Posted by 코딩하는 야구쟁이
,