발효홍삼
코딩하는 홍삼
발효홍삼
전체 방문자
오늘
어제
  • 분류 전체보기 (142)
    • PS (63)
      • 프로그래머스 (9)
      • 코드업 (10)
      • 백준 (43)
      • 알고스팟 (1)
    • Programming Language (11)
      • html_css (2)
      • java (0)
      • c,c++ (2)
      • vanillajs (2)
      • react (0)
      • vue.js (0)
      • angular.js (0)
      • electron (3)
      • 엄랭(Umjunsik-lang) (1)
      • F# (1)
      • Node.js (0)
      • Go (0)
    • knowledge (41)
      • algorithm (3)
      • data structure (1)
      • os (1)
      • ML (1)
      • math (31)
      • paper review (0)
      • IT-license (4)
    • Programming Guide (27)
      • React (1)
      • Electron (2)
      • CSS , SASS ( SCSS ) , Tailw.. (3)
      • Node.js (1)
      • Go (1)
      • Ruby on Rails (2)
      • R (1)
      • PHP (1)
      • Docker (1)
      • JSP (1)
      • C# (1)
      • Django (1)
      • Flask (1)
      • Dart (1)
      • Next.js (1)
      • Vue.js (1)
      • Unity (1)
      • React Native (0)
      • Flutter (3)
      • GraphQL (1)
      • MongoDB (1)
      • .NET (1)
      • RUST (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 수학
  • 파이썬
  • electron
  • js
  • C
  • 출력
  • c++
  • java
  • codeup
  • 기초100제
  • LV1
  • cpp
  • 정보처리기능사 필기
  • 이산수학
  • 미분
  • 구현
  • 적분
  • 기초백제
  • 알고리즘
  • CSS
  • 백준
  • Python
  • 코드업
  • 미적분학
  • 프로그래머스
  • 정보처리기능사
  • 자바
  • JavaScript
  • nodejs
  • HTML

최근 댓글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
발효홍삼

코딩하는 홍삼

Programming Language/c,c++

[C/C++] C++로 2048 구현하기

2023. 1. 3. 05:49
728x90
#include <iostream>
#include <Windows.h>
#include<conio.h>
#include <vector>

using namespace std;

/* 키보드 값 */
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define SUBMIT 4

/* 색상 열거형 정의*/
enum {
    black,
    blue,
    green,
    cyan,
    red,
    purple,
    brown,
    lightgray,
    darkgray,
    lightblue,
    lightgreen,
    lightcyan,
    lightred,
    lightpurple,
    yellow,
    white,
};

/* 함수 선언 */
void init();
void gotoxy(int, int);
void DrawTitle();
int DrawMenu();
int keyControl();
void DrawInfo();
void DrawMap();
void setColor(int, int);
void gameLoop();
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
void RandomNum();

int tempMap[6][6] = {
    {-1,-1,-1,-1,-1,-1},
    {-1,0,0,0,0,-1},
    {-1,0,0,0,0,-1},
    {-1,0,0,0,0,-1},
    {-1, 0,0,0,0,-1},
    {-1,-1,-1,-1,-1,-1}
};

int map[6][6] = {
    {-1,-1,-1,-1,-1,-1},
    {-1,0,0,0,0,-1},
    {-1,0,0,0,0,-1},
    {-1,0,0,0,0,-1},
    {-1, 0,0,0,0,-1},
    {-1,-1,-1,-1,-1,-1}
};

int limitNumber = 2;
vector <pair<int, int>> Pt;

int main(void) {
    init();
    
    while (true) {
        DrawTitle();
        int menuCode = DrawMenu();
        if (menuCode == 0) {
            gameLoop();
        }
        else if (menuCode == 1) {
            DrawInfo();
        }
        else if (menuCode == 2) {
            return 0;
        }
        system("cls");
        setColor(white, black);
    }


	return 0;
}

void init() {
    system("title 2048"); 
    CONSOLE_CURSOR_INFO c = { 0 };
    c.dwSize = 1; //두께 : 1
    c.bVisible = 0; //커서 숨기기
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &c);
}

void gotoxy(int x, int y) {
    COORD Pos;        //x, y를 가지고 있는 구조체
    Pos.X = 2 * x;
    Pos.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}

void DrawTitle() {
    gotoxy(24, -7);
    cout << "\n\n\n\n";
    cout << "                                   #####    ###   #        #####\n";
    cout << "                                  #     #  #   #  #    #  #     #\n";
    cout << "                                        #  #   #  #    #  #     #\n";
    cout << "                                   #####  #     # #    #   #####\n";
    cout << "                                  #       #     # ####### #     #\n";
    cout << "                                  #        #   #       #  #     #\n";
    cout << "                                  #        #   #       #  #     #\n";
    cout << "                                  #######   ###        #   #####\n";
    cout << "\n\n\n\n";
}

int DrawMenu() {
    int x = 24;
    int y = 12;
    gotoxy(24-2, 12);
    cout << ">   게임 시작";
    gotoxy(24, 13);
    cout << "게임 정보";
    gotoxy(24, 14);
    cout << "종료";

    while (true) {
        int n = keyControl();
        switch (n)
        {
            case UP: {
                if (y > 12) {
                    gotoxy(x - 2, y);
                    cout << " ";
                    gotoxy(x - 2, --y);
                    cout << ">";
                }
                break;
            }
           
            case DOWN: {
                if (y < 14) {
                    gotoxy(x - 2, y);
                    cout << " ";
                    gotoxy(x - 2, ++y);
                    cout << ">";
                }
                break;
            }
           
            case SUBMIT: {
                return y - 12;
            }
        }
    }
}

int keyControl() {
    int i = _getch();
    if (i == 224) {
        i = _getch();
        switch (i) 
        {
        case 72: return UP; break;
        case 80: return DOWN; break;
        case 75: return LEFT; break;
        case 77: return RIGHT; break;
        }
    }
    else if (i == 32) {
        return SUBMIT;
    }
}

void DrawInfo() {
    system("cls");
    cout << "How to play : \n";
    cout << "Use your arrow keys to move the tiles.\nTiles with the same number merge into one when they touch.\nAdd them up to reach 2048!\n";
    cout << "Development : balhyo-younjisang";

    while (1) {
        if (keyControl() == SUBMIT) {
            break;
        }
    }
}

void DrawMap() {
    system("cls");

    for (int h = 0; h < 6; h++) {
        for (int w = 0; w < 6; w++) {
            int t = tempMap[h][w];

            if (t == 0) {
                setColor(yellow, black);
                cout << "b";
            }
            else if (t == -1) {
                setColor(white, black);
                cout << "#";
            }
            else if (t == 1024) {
                setColor(white, black);
                system("cls");
                gotoxy(24, 12);
                cout << "You Win!";
                return;
            }
            else {
                setColor(cyan, white);
                Pt.push_back(make_pair(h, w));
                cout << t;
            }
        }
        cout << "\n";
    }
    //Sleep(2000);
}

void setColor(int forground, int background) {
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    int code = forground + background * 16;
    SetConsoleTextAttribute(consoleHandle, code);
}

void gameLoop() {

    srand((int)time(NULL));
    for (int i = 0; i < 2; i++) {
        int x = (rand() % 4) + 1;     
        int y = (rand() % 4) + 1;

        int t = (rand() % limitNumber) + 1;
        if (t % 2) t++;
        map[x][y] = t;
        Pt.push_back(make_pair(x, y));
    }

    while (true) {
        memcpy(tempMap, map, sizeof(tempMap));
        DrawMap();
        switch (keyControl())
        {
            case UP: {
                moveUp();
                break;
            }
            case DOWN: {
                moveDown();
                break;
            }
            case LEFT: {
                moveLeft();
                break;
            }
            case RIGHT: {
                moveRight();
                break;
            }
        }
        RandomNum();
    }
}

void RandomNum() {
    int x = 0, y = 0;
    while (map[x][y] != 0) {
        x = (rand() % 4) + 1;
        y = (rand() % 4) + 1;
    }

    int t = (rand() % limitNumber) + 1;
    if (t % 2) t++;
    map[x][y] = t;
    Pt.push_back(make_pair(x, y));
}


void moveUp() {
   for (auto u : Pt) {
        int x = u.first;
        int y = u.second;
        int dx = x;
        int value = map[x][y];
        gotoxy(x, y);

        while (map[dx - 1][y] == 0) 
            dx--;

        if (map[dx - 1][y] == map[dx][y]) {
            map[x][y] = 0;
            map[dx][y] = 0;
            map[dx - 1][y] = value * 2;
        }
        else {
            map[x][y] = 0;
            map[dx][y] = value;
        }
       
    }
}

void moveDown() {
    for (auto u : Pt) {
        int x = u.first;
        int y = u.second;
        int dx = x;
        int value = map[x][y];
        gotoxy(x, y);

        while (map[dx + 1][y] == 0)
            dx++;
        if (map[dx + 1][y] == map[dx][y]) {
            map[x][y] = 0;
            map[dx][y] = 0;
            map[dx + 1][y] = value * 2;
        }
        else {
            map[x][y] = 0;
            map[dx][y] = value;
        }
    }
}

void moveLeft() {
    for (auto u : Pt) {
        int x = u.first;
        int y = u.second;
        int dy = y;
        int value = map[x][y];
        gotoxy(x, y);

        while (map[x][dy-1] == 0)
            dy--;

        if (map[x][dy-1] == map[x][y]) {
            map[x][y] = 0;
            map[x][dy] = 0;
            map[x][dy-1] = value * 2;
        }
        else {
            map[x][y] = 0;
            map[x][dy] = value;
        }
    }
}

void moveRight() {
    for (auto u : Pt) {
        int x = u.first;
        int y = u.second;
        int dy = y;
        int value = map[x][y];
        gotoxy(x, y);

        while (map[x][dy + 1] == 0)
            dy++;
        if (map[x][dy + 1] == map[x][y]) {
            map[x][y] = 0;
            map[x][dy] = 0;
            map[x][dy + 1] = value * 2;
        }
        else {
            map[x][y] = 0;
            map[x][dy] = value;
        }
    }
}

처음 만들어보는 콘솔 게임이다보니 5시간 정도 걸렸다.

728x90
저작자표시 비영리 (새창열림)

'Programming Language > c,c++' 카테고리의 다른 글

[ c++ ] c++를 이용한 TCP 소켓 통신 구현  (0) 2022.08.03
    'Programming Language/c,c++' 카테고리의 다른 글
    • [ c++ ] c++를 이용한 TCP 소켓 통신 구현
    발효홍삼
    발효홍삼
    코딩하는 홍삼

    티스토리툴바