본문 바로가기

BE/C++

[C++][C] 미로찾기

출력화면

 

 

 

 

소스코드

#include < iostream > 
#include < fstream > 
#include < cstdlib > 
#include < Windows.h > 
#include < conio.h > 
using namespace std;
const int INDOOR = 2;
const int OUTDOOR = 3;
const int WALL1 = 1;
const int WALL2 = 9;
const int WAY = 0;
const int BLOCK = 4;
const int ITEM = 5;
const int EASY_SIZE = 12;
const int NORMAL_SIZE = 17;
const int HARD_SIZE = 15;
const int UP = 72;
const int DOWN = 80;
const int LEFT = 75;
const int RIGHT = 77;
const int animate_x = 12;
const int animate_y = 6;
const int id_size = 10;
void print_init(int ** (& map), int size);
void easy_mode(char id[id_size]);
void normal_mode(char id[id_size]);
void gotoxy(int x, int y);
void setColor(int color, int bgcolor);
void SetCursor(bool bVisible);
COORD getCursor(void);
void setCursor(int x, int y);
void easy_mode_play(int ** (& map), int(& score), char id[id_size]);
void normal_mode_play(int ** (& map), int(& score), char id[id_size]);
void show_rank();
// //////사용자 클래스
class User {
    public : char m_id[id_size] = {};
    int m_score = 0;
    int m_index;
    int m_rank;
    void insert(char id[id_size], int score, int count);
    void index(int rank);
};
void User::index(int rank) {
    m_index = rank;
}
void User::insert(char id[id_size], int score, int count) {
    strcpy(m_id, id);
    m_score = score;
    m_index = count;
}
void sort_rank(User user[100], int & count);
int main() {
    int level;
    char id[id_size];
    while (1) {
        system("mode con:cols=90 lines=43");
        gotoxy(10, 10);
        setColor(13, 0);
        cout << "\t\t" << endl;
        cout << endl;
        cout << "\t             !! 미 로 찾 기 !!  \t" << endl;
        cout << endl;
        setColor(15, 0);
        cout << "\t       1. 1단계 모드 ( 10 ×  10 )\t" << endl;
        cout << endl;
        cout << "\t       2. 2단계 모드 ( 15 × 15 )\t" << endl;
        cout << endl;
        cout << "\t       3. 랭킹 보기 \t" << endl;
        cout << endl;
        cout << "\t       4. 종료 하기\t" << endl;
        cout << endl;
        setColor(13, 0);
        cout << "\t       원하는 모드를 선택하세요 : ";
        cin >> level;
        if (level == 1 || level == 2) {
            cout << endl;
            cout << "\t       아이디를 입력하세요 : ";
            cin >> id;
        }
        setColor(15, 0);
        system("cls");
        if (level == 4) 
            break;
        
        switch (level) {
            case 1: easy_mode(id);
                break;
            case 2: normal_mode(id);
                break;
            case 3: show_rank();
                break;
        }
    }
    return 0;
}
void show_rank() {
    ifstream file("rank.txt");
    char name[id_size];
    int score;
    int count = 0;
    int temp = 9;
    User user[100];
    gotoxy(10, temp);
    cout << "===========================================" << endl;
    gotoxy(10, ++ temp);
    cout << "===========================================" << endl;
    gotoxy(10, ++ temp);
    cout << "                  점 수" << endl;
    gotoxy(10, ++ temp);
    cout << "===========================================" << endl;
    gotoxy(10, ++ temp);
    cout << "============ id ============ score ========" << endl;
    gotoxy(10, ++ temp);
    while (1) {
        if (file.is_open()) {
            while (!file.eof()) {
                gotoxy(15, ++ temp);
                file >> name >> score;
                user[count].insert(name, score, count);
                // cout <<"  "<<  name << "   \t|        " << score;
                count ++;
            }
            sort_rank(user, count);
            cout << endl;
            cout << "\t     메뉴로 돌아가려면 엔터키를 누르세요" << endl;
            SetCursor(false);
            char ch = _getch();
            if (ch == 13)  // 엔터키를 치면 메뉴로 돌아감
                break;
            
        } else {
            cout << "check file" << endl;
        }
    }
}
void sort_rank(User user[100], int & count) {
    int temp = 0;
    int min;
    for (int i = 0; i < count - 1; i ++) {
        for (int j = i + 1; j < count; j ++) {
            if (user[j].m_score > user[i].m_score) {
                int temp = user[i].m_index;
                int temp2 = user[j].m_index;
                user[j].index(temp);
                user[i].index(temp2);
            }
        }
    }
    int m_temp = 16;
    for (int i = 0; i < count; i ++) {
        m_temp = m_temp + 2;
        gotoxy(10, m_temp);
        for (int j = 0; j < count; j ++) {
            if (user[j].m_index == i) 
                cout << "\t" << user[j].m_index + 1 << " 등\t" << user[j].m_id << "\t\t" << user[j].m_score << endl;
            
        }
    }
}
void print_init(int ** (& map), int size) {
    int temp = animate_y - 1;
    gotoxy(animate_x - 2, temp);
    for (int i = 0; i < size; i ++) {
        gotoxy(animate_x - 2, temp);
        for (int j = 0; j < size; j ++) {
            if (map[i][j] == INDOOR || map[i][j] == OUTDOOR) {
                setColor(12, 0);
                cout << "◎";
                setColor(15, 0);
            } else if (map[i][j] == WALL1) {
                setColor(8, 0);
                cout << "■";
                setColor(15, 0);
            } else if (map[i][j] == WAY) {
                cout << "  ";
            } else if (map[i][j] == BLOCK) {
                setColor(11, 0);
                cout << "※";
                setColor(15, 0);
            } else if (map[i][j] == ITEM) {
                setColor(14, 0);
                cout << "★";
                setColor(15, 0);
            } else {
                setColor(7, 0);
                cout << "■";
                setColor(15, 0);
            }
        }
        temp ++;
    }
}
void easy_mode(char id[id_size]) {
    int ** map;
    int easy_score = 5;
    ifstream file("easy.txt");
    if (file.is_open()) {
        map = new int * [EASY_SIZE];
        for (int i = 0; i < EASY_SIZE; i ++) 
            map[i] = new int[EASY_SIZE];
        
        for (int i = 0; i < EASY_SIZE; i ++) {
            for (int j = 0; j < EASY_SIZE; j ++) {
                file >> map[i][j];
            }
        }
        print_init(map, EASY_SIZE);
        easy_mode_play(map, easy_score, id);
        for (int i = 0; i < EASY_SIZE; i ++) 
            delete map[i];    
        delete map;
    } else {
        cout << "check file" << endl;
    }
}
void easy_mode_play(int ** (& map), int(& score), char id[id_size]) {
    int m_row;
    int m_col;
    int x = animate_x;
    int y = animate_y;
    int temp_x = 0;
    int temp_y = 0;
    bool end = false;
    bool block = false;
    gotoxy(x, y);
    while (1) {
        while (!_kbhit()) {
            if (!end) {
                gotoxy(50, 8);
                cout << " [ 기본 점수 5점 ]";
                gotoxy(50, 10);
                cout << " [ 점수: " << score << "       ] ";
                gotoxy(50, 12);
                cout << " [        기본 설명        ] " << endl;
                gotoxy(50, 14);
                cout << "   ※ : 가시  ★ : 아이템 ";
                gotoxy(x, y);
                COORD cur = getCursor();
                cout << "♡";
                setCursor(cur.X, cur.Y);
                SetCursor(false);
                Sleep(100);
            }
        }
        int ch = _getch();
        if (ch == 224) { // 방향키 조절
            m_row = y -(animate_y - 1);;
            m_col = (x - animate_x) / 2 + 1;
            ch = _getch();
            switch (ch) {
                case UP:
                    { // 위로이동
                        if (map[m_row - 1][m_col] != WALL1 && map[m_row - 1][m_col] != WALL2) {
                            if (map[m_row - 1][m_col] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row - 1][m_col] = 0;
                            }
                            if (map[m_row - 1][m_col] == ITEM) {
                                score += 2;
                                map[m_row - 1][m_col] = 0;
                            }
                            if (map[m_row][m_col] == OUTDOOR) {
                                end = true;
                            }
                            y -= 1;
                        }
                        break;
                    }
                case DOWN:
                    { // 아래로 이동
                        if (map[m_row + 1][m_col] != WALL1 && map[m_row + 1][m_col] != WALL2) {
                            if (map[m_row + 1][m_col] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row + 1][m_col] = 0;
                            }
                            if (map[m_row + 1][m_col] == ITEM) {
                                score += 2;
                                map[m_row + 1][m_col] = 0;
                            }
                            if (map[m_row + 1][m_col] == OUTDOOR) {
                                end = true;
                            }
                            y += 1;
                        }
                        break;
                    }
                case LEFT:
                    { // 왼쪽으로 이동
                        if (map[m_row][m_col - 1] != WALL1 && map[m_row][m_col - 1] != WALL2) {
                            if (map[m_row][m_col - 1] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row][m_col - 1] = 0;
                            }
                            if (map[m_row][m_col - 1] == ITEM) {
                                score += 2;
                                map[m_row][m_col - 1] = 0;
                            }
                            if (map[m_row][m_col - 1] == OUTDOOR) {
                                end = true;
                            }
                            x -= 2;
                        }
                        break;
                    }
                case RIGHT:
                    { // 오른쪽으로 이동
                        if (map[m_row][m_col + 1] != WALL1 && map[m_row][m_col + 1] != WALL2) {
                            if (map[m_row][m_col + 1] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row][m_col + 1] = 0;
                            }
                            if (map[m_row][m_col + 1] == ITEM) {
                                score += 2;
                                map[m_row][m_col + 1] = 0;
                            }
                            if (map[m_row][m_col + 1] == OUTDOOR) {
                                end = true;
                            }
                            x += 2;
                        }
                        break;
                    }
            }
            printf(" ");
            if (block) {
                gotoxy(50, 18);
                cout << "앗 가시를 밟았다! 점수 -1 ";
                Sleep(1000);
                block = false;
            }
            gotoxy(50, 18);
            cout << "                                ";
            gotoxy(x, y);
        }
        if (end) {
            system("cls");
            gotoxy(12, 10);
            cout << "===========================================" << endl;
            gotoxy(12, 11);
            cout << "             게임이 끝났습니다." << endl;
            gotoxy(12, 12);
            cout << "          " << id << "님의 점수는 " << score << " 점 입니다" << endl;
            gotoxy(12, 13);
            cout << "             메뉴로 돌아갑니다." << endl;
            gotoxy(12, 14);
            cout << "===========================================" << endl;
            Sleep(3000);
            ofstream file("rank.txt", ios::app); // 이어쓰기 file <<'\n' << id << " " << score; break;
        }
    }
}
void normal_mode(char id[id_size]) {
    int ** map;
    int easy_score = 5;
    ifstream file("normal.txt");
    if (file.is_open()) {
        map = new int * [NORMAL_SIZE];
        for (int i = 0; i < NORMAL_SIZE; i ++) 
            map[i] = new int[NORMAL_SIZE];
        
        for (int i = 0; i < NORMAL_SIZE; i ++) {
            for (int j = 0; j < NORMAL_SIZE; j ++) {
                file >> map[i][j];
            }
        }
        print_init(map, NORMAL_SIZE);
        gotoxy(2, 1);
        normal_mode_play(map, easy_score, id);
        for (int i = 0; i < NORMAL_SIZE; i ++) 
            delete map[i];
        
        delete map;
    } else {
        cout << "check file" << endl;
    }
}
void normal_mode_play(int ** (& map), int(& score), char id[id_size]) {
    int m_row;
    int m_col;
    int x = animate_x;
    int y = animate_y;
    bool end = false;
    bool block = false;
    gotoxy(x, y);
    while (1) {
        while (!_kbhit()) {
            if (!end) {
                gotoxy(50, 8);
                cout << " [ 기본 점수 5점 ]";
                gotoxy(50, 10);
                cout << " [ 점수: " << score << "       ] ";
                gotoxy(50, 12);
                cout << " [        기본 설명        ] " << endl;
                gotoxy(50, 14);
                cout << "   ※ : 가시  ★ : 아이템 ";
                gotoxy(x, y);
                COORD cur = getCursor();
                cout << "♡";
                setCursor(cur.X, cur.Y);
                SetCursor(false);
                Sleep(100);
            }
        }
        int ch = _getch();
        if (ch == 224) { // 방향키 조절
            m_row = y -(animate_y - 1);;
            m_col = (x - animate_x) / 2 + 1;
            ch = _getch();
            switch (ch) {
                case UP:
                    { // 위로이동
                        if (map[m_row - 1][m_col] != WALL1 && map[m_row - 1][m_col] != WALL2) {
                            if (map[m_row - 1][m_col] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row - 1][m_col] = 0;
                            }
                            if (map[m_row - 1][m_col] == ITEM) {
                                score += 2;
                                map[m_row - 1][m_col] = 0;
                            }
                            if (map[m_row][m_col] == OUTDOOR) {
                                end = true;
                            }
                            y -= 1;
                        }
                        break;
                    }
                case DOWN:
                    { // 아래로 이동
                        if (map[m_row + 1][m_col] != WALL1 && map[m_row + 1][m_col] != WALL2) {
                            if (map[m_row + 1][m_col] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row + 1][m_col] = 0;
                            }
                            if (map[m_row + 1][m_col] == ITEM) {
                                score += 2;
                                map[m_row + 1][m_col] = 0;
                            }
                            if (map[m_row + 1][m_col] == OUTDOOR) {
                                end = true;
                            }
                            y += 1;
                        }
                        break;
                    }
                case LEFT:
                    { // 왼쪽으로 이동
                        if (map[m_row][m_col - 1] != WALL1 && map[m_row][m_col - 1] != WALL2) {
                            if (map[m_row][m_col - 1] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row][m_col - 1] = 0;
                            }
                            if (map[m_row][m_col - 1] == ITEM) {
                                score += 2;
                                map[m_row][m_col - 1] = 0;
                            }
                            if (map[m_row][m_col - 1] == OUTDOOR) {
                                end = true;
                            }
                            x -= 2;
                        }
                        break;
                    }
                case RIGHT:
                    { // 오른쪽으로 이동
                        if (map[m_row][m_col + 1] != WALL1 && map[m_row][m_col + 1] != WALL2) {
                            if (map[m_row][m_col + 1] == BLOCK) {
                                block = true;
                                score -= 1;
                                map[m_row][m_col + 1] = 0;
                            }
                            if (map[m_row][m_col + 1] == ITEM) {
                                score += 2;
                                map[m_row][m_col + 1] = 0;
                            }
                            if (map[m_row][m_col + 1] == OUTDOOR) {
                                end = true;
                            }
                            x += 2;
                        }
                        break;
                    }
            }
            printf(" ");
            if (block) {
                gotoxy(50, 18);
                cout << "앗 가시를 밟았다! 점수 -1";
                Sleep(1000);
                block = false;
            }
            gotoxy(50, 18);
            cout << "                                 ";
            gotoxy(x, y);
        }
        if (end) {
            system("cls");
            gotoxy(12, 10);
            cout << "===========================================" << endl;
            gotoxy(12, 11);
            cout << "             게임이 끝났습니다." << endl;
            gotoxy(12, 12);
            cout << "          " << id << "님의 점수는 " << score << " 점 입니다" << endl;
            gotoxy(12, 13);
            cout << "             메뉴로 돌아갑니다." << endl;
            gotoxy(12, 14);
            cout << "===========================================" << endl;
            Sleep(3000);
            ofstream file("rank.txt", ios::app); // 이어쓰기
            file << '\n' << id << " " << score;
            break;
        }
    }
}
void SetCursor(bool bVisible) {
    CONSOLE_CURSOR_INFO ConsoleCursor;
    ConsoleCursor.bVisible = bVisible;
    ConsoleCursor.dwSize = 1;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), & ConsoleCursor); // 설정
}
COORD getCursor(void) {
    COORD curPoint;
    CONSOLE_SCREEN_BUFFER_INFO pos;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), & pos);
    curPoint.X = pos.dwCursorPosition.X;
    curPoint.Y = pos.dwCursorPosition.Y;
    return curPoint;
}
void setCursor(int x, int y) {
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void gotoxy(int x, int y) {
    COORD Pos = {
        x,
        y
    };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void setColor(int color, int bgcolor) {
    color &= 0xf;
    bgcolor &= 0xf;
    static HANDLE std_output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(std_output_handle, bgcolor << 4 | color);
}