메뉴 건너뛰기

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

-- 플레이어 위치를 저장하는 변수
-- 초기 시작지점은 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 express.js 세션 적용 후 리다이렉트 시 세션 적용이 제대로 안 되는 문제점 노루발 4   2024-02-07 2024-02-07 14:23
문제: logout.js router.get('/', (req, res) => { req.session.destroy(); res.redirect('/login'); }); login.js if (result[0] !== undefined) { // 로그인에 성공하였으므로 세션을 할당 req.session.uid = result[0].uid; req.session.us...  
184 Bootstrap4 container class가 적용된 div의 양 옆에 설정하지 않은 margin이 생김 노루발 5   2024-02-07 2024-02-07 14:25
문제: <div class="container"> TEST </div> 위와 같은 페이지를 브라우저에서 렌더링 시 div의 양 옆에 설정하지 않은 빈 margin이 생김. 해결: <div class="container-fluid"> TEST </div> margin을 0으로 주고 width를 100%로 주고 등등 별걸 다 해봤는데 ...  
183 리캡챠 적용 [1] 노루발 11   2021-01-08 2021-01-11 12:15
XE 회원가입 시 구글 리캡챠 인증 추가하기 : 네이버 블로그 (naver.com)  
182 certbot을 이용한 HTTPS 인증서 발급 및 적용 노루발 18   2021-01-12 2021-01-12 16:57
snap 설치 및 업데이트 sudo snap install core; sudo snap refresh core certbot 설치 sudo snap install --classic certbot 심볼릭 링크 생성 sudo ln -s /snap/bin/certbot /usr/bin/certbot nginx에 맞춰 자동 설정 sudo certbot --nginx 알아서 다 해주기...  
181 love2d에서 안드로이드 터치 제스처 인식하기 노루발 20   2020-11-12 2020-11-12 00:38
-- 안드로이드/iOS 등등의 터치스크린 입력을 받는 기기에서는 총 3가지의 콜백 함수를 사용한다: -- love.touchpressed / love.touchmoved / love.touchreleased -- 다만 위 3가지 함수는 마우스 클릭으로는 발생하지 않으므로 추가적인 처리가 필요하다. -- ...  
180 Lua-love2d TCP 통신 [1] 노루발 22   2023-07-14 2023-07-22 16:19
서버: Lua 클라이언트: Love2d(Lua) 서버 구동에는 luasocket 라이브러리가 필요하며, luarocks로 설치할 수 있음. 별도 패키지 관리자가 있는 리눅스 시스템에서는 apt-get install lua-socket 등의 패키지 관리자 명령어로 설치 가능하며 Windows에서 구동시...  
179 illegal character 방지 [3] 노루발 22   2023-07-17 2023-07-19 16:14
문제점: 클라이언트 <-> 서버 통신을 하면서 다음과 같이 메세지를 주고받기로 함 패킷종류|파라미터1|파라미터2 예) MOTDREQ -> 서버에 MOTD를 요청 (파라미터 없음) MOTD|공지사항입니다 -> MOTD는 "공지사항입니다" 임 (파라미터 1개) MSG|김덕배|안녕하세...  
178 Lua 클래스 만들고 활용하기 노루발 24   2020-11-06 2020-11-06 22:43
------------------------ stairs.lua ------------------------ Stairs = {} function Stairs:new(x, y, floor, direction, locked) local newStairs = {x = x, y = y, floor = floor, direction = direction, locked = locked} self.__index = self retur...  
177 Lua 테이블 안에 함수 저장하기 노루발 28   2020-11-06 2020-11-06 21:07
테이블 안에 함수를 저장할 수 있다. function move(object, direction) print(object .. " moved to ".. direction) end scheduler = {} scheduler.queue = {} function scheduler.newentry(action, args) scheduler.queue[#scheduler.queue + 1] = {} schedu...  
176 루아 스타일 가이드 노루발 34   2020-11-19 2020-11-19 00:58
http://lua-users.org/wiki/LuaStyleGuide https://github.com/Olivine-Labs/lua-style-guide  
175 Lua 인수로 nil값이 들어왔을 경우 처리하기 노루발 36   2020-11-06 2020-11-06 20:29
or 연산자를 사용한다. 예제 코드: function moveplayer(direction) direction = direction or "nowhere" print("player moved to ".. direction) end moveplayer() --> "player moved to nowhere" moveplayer("north") -- "player moved to north" 이런 코드...  
174 이쁜 눈나가 유니티 개발 알려주는 재생목록 노루발 37   2020-11-12 2020-11-12 05:59
https://www.youtube.com/watch?v=Ur2jN6_si6c&list=PLi-ukGVOag_1lNphWV5S-xxWDe3XANpyE https://www.youtube.com/watch?v=sJClf9S7AMA&list=PLi-ukGVOag_0HR09oTs966Wt81IYYXlFH 유니티를 배우고 있는 건 아닌데 만들어진 라이브러리를 다루는게 아...  
173 Love2d 이미지 하얗게 그리기 노루발 42   2020-11-23 2020-11-23 04:11
아래와 같은 코드를 사용해 이미지에 색상을 적용할 수 있다. hamster = love.graphics.newImage("hamster.png") love.graphics.setColor(1, 0, 0) -- 빨간색으로 그리기 love.graphics.draw(hamster) love.graphics.setColor(1, 1, 1) 하지만 이미지를 하얗게...  
172 Love2d 게임 안드로이드로 패키징하기 노루발 48   2021-01-11 2021-02-21 01:45
http://hondoom.com/zbxe/index.php?mid=study&document_srl=797993 버전이 바뀌면서 빌드 방법이 바뀌었기에 다시 정리한다. 1. Android studio 설치 https://developer.android.com/studio/index.html SDK 플랫폼 - Android 11.0 [API 30] SDK 버전 - An...  
171 특정좌표를 기준으로 zoom in/zoom out하기 노루발 59   2020-11-11 2020-11-11 01:22
-- x,y - 줌인/줌아웃시 기준점 (좌표 이동이나 scale에 영향받지 않는 순수한 화면 좌표) -- scale - 현재 scale(1:그대로 0.5:1/2 사이즈 2:2배 사이즈) -- scaleinc - 얼마나 scale을 변화시킬것인가 (I usually use 0.1 or -0.1) -- camx - 카메라의 x좌표...  
170 Windows To Go와 R-Studio를 이용한 손실된 데이터 복구하기 노루발 89   2020-01-30 2020-01-30 19:52
하기의 복구 방법으로 모든 자료를 100% 복구할 수는 없으므로 자료의 손실 이전에 신뢰성 있고 주기적인 백업이 선행되어야 한다. 준비물: - 부팅 USB 혹은 외장 HDD/SSD - 복구한 자료를 저장할 외부 저장장치 (옵션) - R-Studio https://www.r-studio.com/ ...  
169 Oracle cloud에 Nginx/MariaDB 설치하기 노루발 93   2020-12-06 2020-12-06 20:19
https://itreport.tistory.com/624  
168 Love2d DPI 이슈 해결 [3] 노루발 97   2019-06-29 2019-07-01 06:34
이런 love 프로젝트가 있다고 하자. (conf.lua) function love.conf(t) t.window.width = 640 t.window.height = 360 end 창 크기를 640*480으로 설정한 뒤 실행하면 어떻게 보일까? 당연히 창 크기가 640*480 크기로 보여야겠지만 내 컴퓨터에서는 이렇게 보...  
167 Love2d로 만든 로그라이크 예제 노루발 253   2020-11-30 2020-11-30 22:54
https://gitlab.com/Jalexander39/roguelikedev-does-the-complete-roguelike-tutorial 이걸 왜 여태 몰랐지...  
166 love.update(dt) 에서 버벅이는 현상. 노루발 256   2013-09-17 2013-09-17 08:15
윈도우를 잡고 흔들거나 윈도우 사이로 잠시 전환하거나.. 등으로 원치 않는 렉이 발생할 시 dt의 값이 평소보다 크게 들어갑니다. (예를 들면, 평소에 0.25가 들어간다면 이번에는 3.1이 들어갑니다.) 이건 평소보다 더 많이 크기 때문에, dt를 가지고 타이...