편집 기록

편집 기록
  • 프로필 알 수 없는 사용자님의 편집
    날짜2018.06.21

    소코반 게임에서 적을 만드는 코드 좀 도와주세요 ㅠㅠ


    http://www.soen.kr/

    여기 코드를 C++에 맞게 수정한 후, 분석하고나서 '적'이 등장하는 코드를 짜보았는데.. 오류는 안 나지만 제가 원하는 결과가 나오지 않네요.

    밑에 코드대로 하면, @(플레이어)가 움직이는대로 제가 코드에 짠대로 3(적)이 움직여집니다. 다만 문제가 3과 @가 만나면 게임이 끝나야 하는데, 끝나지 않네요 ㅠㅠㅠ 적군에게 먹혀도 플레이어는 잘만 움직여집니다.. 코드가 너무 기네요 죄송합니다 ㅠ

    밑에는 코드입니다.

    #include <Turbo.h>
    #include <iostream>
    using namespace std;
    
    #define LEFT 75 
    #define RIGHT 77
    #define UP 72
    #define DOWN 80
    
    void DrawScreen();
    bool TestEnd();
    void Move(int dir);
    char ns[18][21];
    char es[18][21];
    void putchxy(int x, int y, char ch);
    int nStage;
    int nx, ny;
    int nMove;
    int ex, ey;
    
    void putchxy(int x, int y, char ch)
    
    {
        gotoxy(x, y); _putch(ch);
    }
    const int MAXSTAGE = 3;
    
    char arStage[MAXSTAGE][18][21] = {
    
    
    
        {
            "####################",
            "####################",
            "####################",
            "#####  3############",
            "#####O  ############",
            "#####  O############",
            "###  O O ###########",
            "### # ## ###########",
            "#   # ## #####  ..##",
            "# O  O   @      ..##",
            "##### ### # ##  ..##",
            "#####     ##########",
            "####################",
            "####################",
            "####################",
            "####################",
            "####################",
            "####################"
    
        },
    
    
      {
          "####################",
          "####################",
          "####################",
          "####################",
          "####..  #     ######",
          "####..  # O  O  ####",
          "####..  #O####  ####",
          "####..    @ ##  ####",
          "####..  # #  O #####",
          "######### ##O O ####",
          "###### O  O O O ####",
          "######    #   3 ####",
          "####################",
          "####################",
          "####################",
          "####################",
          "####################",
          "####################"
      },
    
      {
          "####################",
          "####################",
          "####################",
          "####################",
          "##########3    @####",
          "########## O#O #####",
          "########## O  O#####",
          "###########O O #####",
          "########## O # #####",
          "##....  ## O  O  ###",
          "###...    O  O   ###",
          "##....  ############",
          "####################",
          "####################",
          "####################",
          "####################",
          "####################",
          "####################"
      },
    }; 
    
    int main()
    {
        int ch;
        int x, y;
    
        setcursortype(NOCURSOR); 
        nStage = 0;
    
        for (; 1;) {
    
            memcpy(ns, arStage[nStage], sizeof(ns));
            for (y = 0; y<18; y++) {
                for (x = 0; x<20; x++) {
                    if (ns[y][x] == '@') {
                        nx = x;
                        ny = y;
                        ns[y][x] = ' ';
                    }       
                }
            }
    
            for (y = 0; y<18; y++) {
                for (x = 0; x<20; x++) {
                    if (ns[y][x] == '3') {
                        ex = x;
                        ey = y;
                        ns[y][x] = ' ';
                    }
                }
            }
    
            clrscr();
            nMove = 0;
    
            for (; 2;) {
    
                DrawScreen();
                ch = _getch();
    
                if (ch == 0xE0 || ch == 0) {
                    ch = _getch();
                    switch (ch) {
                    case LEFT:
                    case RIGHT:
                    case UP:
                    case DOWN:
                        Move(ch);
                        break;
                    }
                }
    
                else {
                    ch = tolower(ch);
                    if (ch == 'r') {
                        break;
                    }
    
                    if (ch == 'n') {
                        if (nStage < MAXSTAGE - 1) {
                            nStage++;
                        }
                        break;
                    }
    
                    if (ch == 'p') {
                        if (nStage > 0) {
                            nStage--;
                        }
                        break;
                    }
                    if (ch == 'q') {
                        setcursortype(NORMALCURSOR);
                        exit(0);
                    }
                }
    
                if (TestEnd()) {
                    clrscr();
                    gotoxy(10, 10);
                    cout << nStage + 1 << "스테이지를 풀었습니다. 다음 스테이지로 이동합니다" << endl;
                    delay(2000);
    
                    if (nStage < MAXSTAGE - 1) {
                        nStage++;
                    }
                    break;
                }
            }
        }
    }
    
    void DrawScreen()
    {
        int x, y;
        for (y = 0; y<18; y++) {
            for (x = 0; x<20; x++) {
                putchxy(x, y, ns[y][x]);
            }
        }
    
        putchxy(nx, ny, '@');
        putchxy(ex, ey, '3');
    
        gotoxy(40, 2); puts("SOKOBAN");
        gotoxy(40, 4); puts("Q:종료, R:다시 시작");
        gotoxy(40, 6); puts("N:다음, P:이전");
        gotoxy(40, 8); printf("스테이지 : %d", nStage + 1);
        gotoxy(40, 10); printf("이동 회수 : %d", nMove);
    }
    
    bool TestEnd()
    {
        int x, y;
        for (y = 0; y<18; y++) {
            for (x = 0; x<20; x++) {
                if (arStage[nStage][y][x] == '.' && ns[y][x] != 'O') {
                    return false;
                }
    
                if (ns[y][x] == '3')
                {
                    return false;
                }
            }   
        }
        return true;
    }
    
    void Move(int dir)
    {
        int dx = 0, dy = 0;
        int bx = 0, by = 0;
    
        switch (dir) {
    
        case LEFT:
    
            dx = -1;
            bx = 1;
    
            break;
    
        case RIGHT:
    
            dx = 1;
            bx = -1;
    
            break;
    
        case UP:
    
            dy = -1;
            by = -1;
    
            break;
    
        case DOWN:
    
            dy = 1;
            by = 1;
    
            break;
        }
    
        if (ns[ny + dy][nx + dx] != '#') {
            if (ns[ny + dy][nx + dx] == 'O') {
                if (ns[ny + dy * 2][nx + dx * 2] == ' ' || ns[ny + dy * 2][nx + dx * 2] == '.') {
                    if (arStage[nStage][ny + dy][nx + dx] == '.') {
                        ns[ny + dy][nx + dx] = '.';
                    }
    
                    else {
                        ns[ny + dy][nx + dx] = ' ';
                    }
                    ns[ny + dy * 2][nx + dx * 2] = 'O';
                }
                else {
                    return;
                }
            }
    
            else if (ns[ny + dy][nx + bx] == '3')
            {
                gotoxy(40, 12); printf("Game Over");
                int g = 14;
                while (1)
                {
    
                    gotoxy(40, g); printf("재시작 하시겠습니까? ");
                    char select;
                    cin >> select;
    
                    if (select == 'Y' || select == 'y')
                    {
                        //아직 되돌아가는 코드는 미처 못짰습니다.
                    }
    
                    g += 2;
    
                }
            }
    
            nx += dx;
            ny += dy;
            nMove++;
    
        }
    
        if (ns[ey + by][ex + bx] != '#')
        {
    
            if (ns[ey + by][ex + bx] == 'O') {
                return;
            }
    
            else if (ns[ey + by][ex + bx] == '@')
            {
                gotoxy(40, 12); printf("Game Over");
    
                int g = 14;
                while (1)
                {
    
                    gotoxy(40, g); printf("재시작 하시겠습니까? ");
                    char select;
                    cin >> select;
    
                    if (select == 'Y' || select == 'y')
                    {
                    }
    
                    g += 2;
    
                }
            }
    
            ex += bx;
            ey += by;
    
    
        }
    
    
    }