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 136 137 138 139 | #include <iostream> #include <stdio.h> #include <utility> #include <windows.h> #include <functional> void movecursor(int x, int y) { COORD pos = { x,y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } #define MAP_Y_SIZE 50 #define MAP_X_SIZE 25 #define STONE_NUM 2 bool map[STONE_NUM][MAP_Y_SIZE][MAP_X_SIZE] = { { {false,}, }, }; enum STONE_COLOR { WHITE, BLACK }; class exception { public: std::string msg; exception(std::string str) { msg = str; } }; bool put_stone(STONE_COLOR stoneKind, std::pair<int, int> loc) { if (map[not stoneKind][loc.first][loc.second]) { throw exception("wrong loc"); } map[stoneKind][loc.first][loc.second] = true; // 돌이 연속으로 5개인지 판정 { std::pair<int, int> curInvestigatedLoc; auto& CIL = curInvestigatedLoc; std::function<bool(std::pair<int, int> d, int continuity)> find = [&](std::pair<int, int> d, int continuity = 0) ->bool { CIL.first += d.first; CIL.second += d.second; movecursor(0, MAP_X_SIZE * 2 + 2); std::cout << CIL.first << " " << CIL.second << std::endl; if (continuity == 5 && not map[stoneKind][CIL.first][CIL.second]) { return true; } else if (continuity == 5 && map[stoneKind][CIL.first][CIL.second]) { return false; } else if (continuity < 5 && map[stoneKind][CIL.first][CIL.second]) { return find(d, ++continuity); } else if (continuity < 5 && not map[stoneKind][CIL.first][CIL.second]) { return false; } }; bool result[9] = { find({ 1, -1 }, 0), find({ 1, 0 }, 0), find({ 1, 1 }, 0), find({ 0, -1}, 0), find({ 0, 1 }, 0), find({ -1,-1}, 0), find({ -1, 0}, 0), find({ -1, 1}, 0) }; /* find({ 1, -1 }, 0); find({ 1, 0 }, 0); find({ 1, 1 }, 0); find({ 0, -1 }, 0); find({ 0, 1}, 0); find({ -1,-1 }, 0); find({ -1, 0 }, 0); find({ -1, 1 }, 0); */ for (int i = 0; i < 9; i++) { if (result[i]) { return true; } } } return false; } void renderMap() { for (int i = 0; i < MAP_Y_SIZE * 2; i++) { for (int j = 0; j < MAP_X_SIZE * 2; j++) { movecursor(i, j); printf("%s", "┼"); } } for (int i = 0; i < MAP_Y_SIZE; i++) { for (int j = 0; j < MAP_X_SIZE; j++) { movecursor(i * 2, j * 2); printf("%s", map[BLACK][i][j] ? "○" : map[WHITE][i][j] ? "●" : "┼"); } } } std::pair<int, int> input() { std::pair<int, int> loc; std::cin >> loc.first >> loc.second; return loc; } int main() { std::pair<int, int> loc_input; int curTurn = BLACK; while (1) { try { renderMap(); movecursor(0, MAP_X_SIZE * 2 + 1); if (int s = put_stone((STONE_COLOR)curTurn, input())) { movecursor(0, MAP_X_SIZE + 2); std::cout << (s ? "흑" : "백") << "이 이겼습니다"; } } catch (exception& exc) { movecursor(0, MAP_X_SIZE * 2 + 2); std::cout << exc.msg; } curTurn = not curTurn; Sleep(3000); system("cls"); } } | cs |
게임 책을 보다보니 생각났다.
일단 만들 걸 정하자고..
그래서 일단 오목부터 만들려고 하는 중이다.
일단 지금은 승리 판정이랑 MAP_X랑 MAP_Y가 어째선지 뒤바뀐 것 같아 골치를 썩는 중이다.
또한 system("cls") 때문에 로그도 못찍는다.
이거에 대해선 아이디어가 있는데 자식프로세스를 생성해서 거기다 찍으면 될 것 같다..
뇌를 자극하는 Windows 시스템 프로그래밍을 참고해야겠다.
잡지식은 많은데 ps를 잘 안하니 구현력이 떨어진다.
이 게임의 최종 목표는 다음과 같다.
AI를 지원하는 GUI tcp/ip 오목게임.
뭔가 거창한 것 같지만 내가 딱 구현할 수 있을 수준으로만 할 것이다.
책에서 읽은 게 있는데 실현하기 쉬운 목표를 설정해야 한다고 한다.
그러니 또 생각나는 게 있는데 판정하는 알고리즘이 도저히 작동을 안하면 직접 짜다 포기할 바에 그냥 인터넷에서 배끼는 것이 맞을 것 같다.
밑밥 아니다. ㅎㅎ
콘솔버전 완성하면 GUI로 갈아탈 것이다..
콘솔과 GUI 등 오목게임을 구현하는 그래픽은 세부사항이라 생각하고 의존하지 않게끔 코드를 재구조화시켜야겠다.