본문 바로가기
프로그래밍/C 언어(코딩)

C언어] 슬라이딩 퍼즐-2

by 곰나나 2023. 12. 31.

[슬라이딩 퍼즐]

툴 버전 : Visual Studio 2022

 

슬라이딩 퍼즐 랜덤 숫자판, 입력 받아서 움직이고 서로 칸을 교체한다. 

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 슬라이딩 퍼즐
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
 
#define ROWS 4
#define COLS 5
 
//& 퍼즐 상태를 나타내는 구조체
typedef struct 
{
    int board[ROWS][COLS];
    int emptyRow, emptyCol;
} PuzzleState;
 
//& 퍼즐 상태를 출력하는 함수
void printPuzzle(const PuzzleState* state)
{
    printf("이동키 : ←:w, →:d, ↑:w, ↓:s, q:종료, r:다시 \n\n");
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLS; ++j)
        {
            printf("\t %2d ", state->board[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}//printPuzzle
 
//& 빈 칸을 이동시키는 함수
void moveEmptyCell(PuzzleState* state, int moveRow, int moveCol)
{
    int newRow = state->emptyRow + moveRow;
    int newCol = state->emptyCol + moveCol;
 
    //& 이동 가능한 위치인지 확인
    if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS)
    {
        //& 빈 칸과 인접한 칸을 교환
        state->board[state->emptyRow][state->emptyCol] = state->board[newRow][newCol];
        state->board[newRow][newCol] = 0;
        state->emptyRow = newRow;
        state->emptyCol = newCol;
    }
}//moveEmptyCell
 
//& 퍼즐을 초기화하는 함수
void initializePuzzle(PuzzleState* state)
{
    int numbers[ROWS * COLS - 1];
 
    //& 1부터 19까지의 숫자를 배열에 저장
    for (int i = 0; i < ROWS * COLS - 1++i)
    {
        numbers[i] = i + 1;
    }
 
    //& 배열을 섞기
    for (int i = ROWS * COLS - 2; i > 0--i)
    {
        int j = rand() % (i + 1);
        // 숫자 교환
        int temp = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp;
    }
 
    //& 퍼즐에 랜덤한 숫자 배치
    int count = 0;
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLS; ++j)
        {
            if (count == ROWS * COLS - 1)
            {
                state->board[i][j] = 0// 빈 칸
                state->emptyRow = i;
                state->emptyCol = j;
            }
            else
            {
                state->board[i][j] = numbers[count++];
            }
        }
    }
}// initializePuzzle
 
int main()
{
    //& 초기 퍼즐 상태
    PuzzleState currentState;
 
    srand((unsigned int)time(NULL)); // 난수 발생기 초기화
 
    //& 퍼즐 초기화
    initializePuzzle(&currentState);
 
    while (1)
    {
        //& 콘솔 화면 지우기 (Windows용)
        system("cls"); 
 
        printPuzzle(&currentState);
 
        char ch = _getch(); //& 키 입력 대기
 
        //& 키에 따라 빈 칸 이동
        switch (ch)
        {
            //& (위-1,0), (아래 1,0), (왼0,-1), (오 0,1), 퍼즐 재설정, 종료
        case 'w':
            moveEmptyCell(&currentState, -10); 
            break;
        case 's':
            moveEmptyCell(&currentState, 10); 
            break;
        case 'a':
            moveEmptyCell(&currentState, 0-1);
            break;
        case 'd':
            moveEmptyCell(&currentState, 01); 
            break;
        case 'r':
            initializePuzzle(&currentState); 
            break;
        case 'q':
            printf("게임 종료\n");
            return 0;
        }
    }
 
    return 0;
}//main
cs

728x90

'프로그래밍 > C 언어(코딩)' 카테고리의 다른 글

C언어] RPG게임만들기-1 (플레이어 설정)  (0) 2024.01.01
C언어] 슬라이딩 퍼즐-3  (1) 2023.12.31
C언어] 슬라이딩 퍼즐-1  (0) 2023.12.30
C언어] 성적표  (0) 2023.12.22
C언어] 시간 함수  (1) 2023.12.22