메뉴 건너뛰기

창작에 관련된 질문이나 간단한 팁, 예제를 올리는 곳

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 5, 5로 설정.
playerx = 5
playery = 5

-- 박스의 위치를 저장하는 배열.
box = {}

-- 움직인 횟수 저장
moves = 0

-- 맵 데이터를 저장하는 8*8의 2차원 배열 Map 생성
map = {}
for a = 1, 8 do
    map[a] = {}
    for b = 1, 8 do
        map[a][b] = 0
    end
end

function makebox(x, y)
-- 새 박스를 만든다.
    box[#box + 1] = {}
    box[#box]["x"] = x
    box[#box]["y"] = y
end

function printmap()
-- 맵을 표시하는 printmap 함수
    tempstr = ""

    for a = 1, 8 do
        for b= 1, 8 do
            tempbuffer = ""
            if (map[a][b] == 0) then
                tempbuffer = " "
            elseif (map[a][b] == 1) then
                tempbuffer = "#"
            end
           
        for c = 1, #box do
            if (box[c]["x"] == b and box[c]["y"] == a) then
                tempbuffer = "o"
                break
            end
        end
           
        if (playerx == b and playery == a) then
            tempbuffer = "P"
        end
       
        tempstr = tempstr..tempbuffer
        end
        print(tempstr)
        tempstr = ""
    end
end

-- 맵 데이터 초기화
for a = 1, 8 do
    for b = 1, 8 do
        if (a == 1 or a == 8 or b == 1 or b == 8) then
            map[a][b] = 1
        end
    end
end

makebox(4,3)

makebox(4,6)

function boxexist(x, y)
    for a = 1, #box do
        if (box[a]["x"] == x and box[a]["y"] == y) then
            return a
        end
    end
    return 0
end

while(true) do
    -- 메인 무한 루프
    os.execute("clear") -- 화면을 지운다. OS가 아닌 Scite 환경에서는 제대로 동작하지 않음.
    printmap()
    print("")
    print("움직임: "..moves)
    print("어느 방향으로 이동할까요?")
    print("a: 왼쪽 d: 오른쪽 w: 위 s: 아래 q: 종료 :")
    choose = io.read()
    if (choose == "a") then
        if (map[playery][playerx - 1] == 0) then
            if (boxexist(playerx - 1, playery) ~= 0) then
                if (boxexist(playerx - 2, playery) == 0) then
                    if map[playerx - 2][playery] == 0 then
                        box[boxexist(playerx - 1, playery)]["x"] = box[boxexist(playerx - 1, playery)]["x"] - 1
                        playerx = playerx - 1
                        moves = moves + 1
                    end
                end
            else
                playerx = playerx - 1
                moves = moves + 1
            end
        end
    elseif (choose == "s") then
        if (map[playery + 1][playerx] == 0) then
            if (boxexist(playerx, playery + 1) ~= 0) then
                if (boxexist(playerx, playery + 2) == 0) then
                    if map[playerx][playery + 2] == 0 then
                        box[boxexist(playerx, playery + 1)]["y"] = box[boxexist(playerx, playery + 1)]["y"] + 1
                        playery = playery + 1
                        moves = moves + 1
                    end
                end
            else
                playery = playery + 1
                moves = moves + 1
            end
        end
    elseif (choose == "d") then
        if (map[playery][playerx + 1] == 0) then
            if (boxexist(playerx + 1, playery) ~= 0) then
                if (boxexist(playerx + 2, playery) == 0) then
                    if map[playerx + 2][playery] == 0 then
                        box[boxexist(playerx + 1, playery)]["x"] = box[boxexist(playerx + 1, playery)]["x"] + 1
                        playerx = playerx + 1
                        moves = moves + 1
                    end
                end
            else
                playerx = playerx + 1
                moves = moves + 1
            end
        end
    elseif (choose == "w") then
        if (map[playery - 1][playerx] == 0) then
            if (boxexist(playerx, playery - 1) ~= 0) then
                if (boxexist(playerx, playery - 2) == 0) then
                    if map[playerx][playery - 2] == 0 then
                        box[boxexist(playerx, playery - 1)]["y"] = box[boxexist(playerx, playery - 1)]["y"] - 1
                        playery = playery - 1
                        moves = moves + 1
                    end
                end
            else
                playery = playery - 1
                moves = moves + 1
            end
        end
    elseif (choose == "q") then
        os.execute("clear")
        print("그럼 안녕!")
        io.read()
        return
    end
end



제목이 꽤 길죠? 그만큼 많은 일을 했습니다. 이 부분이 소코반의 핵심 아니겠어요?


코딩은 안 어려운데 설명이 영... 하지만 까라면 까야죠, 뭐!

그냥 공장 견학하는 기분으로 설명합니다.


-- 박스의 위치를 저장하는 배열.
box = {}


테이블인데 주석은 배열이에요. 그냥 써요(...)

루아의 테이블은 숫자 안에 문자열로도 인덱스를 넣을 수 있어서

box[1]["x"] = 1 이런것도 가능해요.


function makebox(x, y)
-- 새 박스를 만든다.
    box[#box + 1] = {}
    box[#box]["x"] = x
    box[#box]["y"] = y
end


새 박스를 만드는 거.

#box 하면 지금 box가 몇개인지 알려줍니다.

그래서 새로운 박스를 하나 더 만들고 박스의 x값과 y값을 집어넣습니다.


        for c = 1, #box do
            if (box[c]["x"] == b and box[c]["y"] == a) then
                tempbuffer = "o"
                break
            end
        end


printmap 함수에서 발췌.

주인공 그리는것과 같은 방법으로 박스를 그립니다.


makebox(4,3)

makebox(4,6)


테스트용으로 박스를 만듭니다.


function boxexist(x, y)
    for a = 1, #box do
        if (box[a]["x"] == x and box[a]["y"] == y) then
            return a
        end
    end
    return 0
end


x, y 좌표에 박스가 존재하는지 돌려주는 함수.

박스가 존재한다면 0이 아닌, box 테이블에 저장된 인덱스를 돌려줍니다.

박스가 존재하지 않으면 0을 돌려줌.


if (map[playery][playerx - 1] == 0) then
            if (boxexist(playerx - 1, playery) ~= 0) then
                if (boxexist(playerx - 2, playery) == 0) then
                    if map[playerx - 2][playery] == 0 then
                        box[boxexist(playerx - 1, playery)]["x"] = box[boxexist(playerx - 1, playery)]["x"] - 1
                        playerx = playerx - 1
                        moves = moves + 1
                    end
                end
            else
                playerx = playerx - 1
                moves = moves + 1
            end
        end


움직임 처리에서의 조건문들이 좀 많이 복잡해졌는데..

일단 플레이어의 앞에 벽이 없는 경우에만 처리합니다.

플레이어의 앞에 박스가 없으면 그냥 움직입니다.

박스가 있다면 플레이어의 앞 앞에 박스 혹은 벽이 없다면 박스를 옮기고 플레이어도 움직입니다.

더 효율적으로 처리할 수 있을지도 모르겠습니다만 게을러서 이런 걸 4개나 만들어놓고 말았습니다.


다음엔 목표 지점을 만들고, 목표 지점에 모든 상자가 옮겨지면 앤딩을 보여주도록 하겠습니다.

대망의 앤딩입니다~ 끝을 보자!


+

    코드의 양과 작업시간은 비례하지 않습니다.
    높으신 분들이 하시는 착각 중 하나.
    저 코드도 사실 복붙해서 만들었습니다.

    저런 코드는 나중에 변경 사항이 생기면 4단 모두 변경해줘야 하는데 까먹고 하나를 안 변경한다면 충격과 공포... 버그가 생기는 겁니다. 그러므로 저런 비효율적인 코딩보다는 효율적으로 여차저차 하는게 더 좋은데 그건 나중에 시간되면 보여드리겠습니다.

조회 수 :
677
등록일 :
2013.09.17
08:32:36 (*.209.135.92)
엮인글 :
게시글 주소 :
https://hondoom.com/zbxe/index.php?mid=study&document_srl=703462
List of Articles
번호 제목 글쓴이 날짜 조회 수sort
185 자동화된 Lua 스크립트의 문서화 - LDoc 노루발 2021-01-11 43225
184 VC++ 2008 Express Edition에서 문D라이브 링크 [2] A.미스릴 2008-04-17 13090
183 cocos2d-x 터치와 업데이트 활성화 시키기 똥똥배 2011-10-27 10656
182 문D 질문 #2 [1] A.미스릴 2008-06-01 8797
181 MFC 더블 버퍼링 질문 [2] A.미스릴 2008-06-13 6602
180 문D라이브로 만드는 더블드래곤.. 질문입니다 [4] 하와이안 2009-01-15 5550
179 임의의 점이 다각형 내부에 있는지 검사하는 함수 똥똥배 2008-04-14 5358
178 문D라이브로 더블드래곤을 만들자(11) file 똥똥배 2008-05-17 4647
177 문D라이브로 더블드래곤을 만들자(1) [2] file 똥똥배 2008-04-16 4479
176 씨언어 질문 (내일 시험 ㄷㄷ) [1] 쿠로쇼우 2008-06-17 4331
175 문D라이브도 더블버퍼링이 필요한가요? [3] A.미스릴 2008-06-28 4223
174 문D라이브로 더블드래곤을 만들자(10) file 똥똥배 2008-05-17 4065
173 #define에 대해 [1] A.미스릴 2008-05-19 4061
172 클레스들을 담은 헤더들의 혼란 [4] A.미스릴 2008-06-21 3892
171 srand에 관해서 [4] A.미스릴 2008-07-15 3867
170 문D 질문 [5] A.미스릴 2008-05-26 3847
169 흥크립트에 궁금한점 [4] 상상악수 2008-08-21 3835
168 C++ 질문 [1] A.미스릴 2008-12-21 3818
167 TinyXML의 한계 [2] 똥똥배 2011-12-11 3753
166 문D라이브 2008년 5월 19일 버전 [2] 혼돈 2008-06-02 3747