Free Lines Arrow
본문 바로가기
Algorithm/번외

[Algorithm] 뿌요뿌요(퍼즐게임)

by skahn1215 2021. 10. 19.
728x90
반응형

뿌요뿌요(퍼즐게임)

  • 일단 이것을 구현한 것은 코딩테스트를 보다 보면 
    퍼즐문제가 나오는 경우가 있다..
  • 연습삼아 구현해 보았다.
  • 참고로 처음 구현해봐서 리팩토링이 많이 필요하다.

 

 

문제

  • 한칸에 하나씩 하나의 블록을 놓는다.
  • 이미 블록이 있으면 그위에 둔다.
  • 쌓다가 3개 이상의 블록이 연결되는 순간 삭제한다.

 

 

분석

일단 3개가 필요해 보인다.

  • 쌓는 로직.
  • 삭제하는 로직.
  • 리프레쉬 하는 로직.

 

 

구현

package algorithm;

import java.awt.*;
import java.util.ArrayList;

class CandidatePoint {

    public CandidatePoint(int x, int y) {
        this.x = x;
        this.y = y;
    }

    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

public class PyoPyo {

    private int [][]map = new int [5][5];
    private int []dirX = {-1, 0, 1, 0};
    private int []dirY = {0, 1, 0, -1};


    public boolean checkMap(int dx, int dy) {
        if (dx < 0 || dx >= map.length || dy < 0 || dy >= map.length) {
            return false;
        } else {
            return true;
        }
    }

    public boolean checkDeleteItem() {
        boolean ret = false;

        boolean[][] visit = new boolean[5][5];
        ArrayList<CandidatePoint> deletePoint = new ArrayList<>();

        for(int i = map.length-1; i > 0; i--) {
           for(int j = 0; j < map.length; j++) {
               if (map[i][j] != 0) {
                   deletePoint.clear();
                   searchMap(i, j, map[i][j], visit, deletePoint);
                   if (deletePoint.size() >= 3) {
                       for (int k = 0; k < deletePoint.size(); k++) {
                           map[deletePoint.get(k).getX()][deletePoint.get(k).getY()] = 0;
                           ret = true;
                       }
                   }
               }
           }
        }

        return ret;
    }

    public void searchMap(int x, int y, int color, boolean [][]visit, ArrayList<CandidatePoint> deletePoint) {
        visit[x][y] = true;
        deletePoint.add(new CandidatePoint(x,y));

        for (int i = 0; i < 4; i++) {
            int dx = x + dirX[i];
            int dy = y + dirY[i];
            if (checkMap(dx, dy) && map[dx][dy] == color && visit[dx][dy] == false) {
                searchMap(dx, dy, color, visit, deletePoint);
            }
        }
    }

    public boolean refresh() {
        boolean ret = false;
        for(int i = map.length-2; i > 0; i--) {
            for(int j = 0; j < map.length; j++) {
                if (map[i][j] != 0) {
                    int row = i;
                    boolean flag = true;
                    while (flag) {
                        if (row < map.length-1) {
                            if (map[row + 1][j] == 0) {
                                ret = true;
                                map[row + 1][j] = map[row][j];
                                map[row][j] = 0;
                                flag = true;
                                row++;
                            } else {
                                flag = false;
                            }
                        } else {
                            flag = false;
                        }
                    }
                }
            }
        }
        return ret;
    }

    public void putItem(int col, int color) {
        boolean isPutItem = false;
        int row = 4;
        while (!isPutItem) {
            if (map[row][col] == 0) {
                map[row][col] = color;
                isPutItem = true;
            }
            row--;
        }
    }

    public void printMap() {
        System.out.println();
        for(int i = 0; i < map.length; i++) {
            for(int j = 0; j < map.length; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println("");
        }
    }

    public void solution(int [][]pyoPyoArray) {
        for(int i = 0; i < pyoPyoArray.length; i++) {
            putItem(pyoPyoArray[i][0], pyoPyoArray[i][1]);
            boolean refresh = true;
            while (refresh) {
                printMap();
                checkDeleteItem();
                refresh = refresh();

            }
        }
    }

    public static void main(String[] args) {
        int [][]pyoPyoArray = new int [18][2];
        pyoPyoArray[0][0] = 0;
        pyoPyoArray[0][1] = 1;

        pyoPyoArray[1][0] = 1;
        pyoPyoArray[1][1] = 2;

        pyoPyoArray[2][0] = 2;
        pyoPyoArray[2][1] = 1;

        pyoPyoArray[3][0] = 3;
        pyoPyoArray[3][1] = 3;

        pyoPyoArray[4][0] = 4;
        pyoPyoArray[4][1] = 5;

        pyoPyoArray[5][0] = 0;
        pyoPyoArray[5][1] = 1;


        pyoPyoArray[6][0] = 1;
        pyoPyoArray[6][1] = 3;


        pyoPyoArray[7][0] = 2;
        pyoPyoArray[7][1] = 1;


        pyoPyoArray[8][0] = 3;
        pyoPyoArray[8][1] = 3;


        pyoPyoArray[9][0] = 4;
        pyoPyoArray[9][1] = 4;


        pyoPyoArray[10][0] = 0;
        pyoPyoArray[10][1] = 1;

        pyoPyoArray[11][0] = 1;
        pyoPyoArray[11][1] = 2;


        pyoPyoArray[12][0] = 3;
        pyoPyoArray[12][1] = 1;


        pyoPyoArray[13][0] = 4;
        pyoPyoArray[13][1] = 4;

        pyoPyoArray[14][0] = 2;
        pyoPyoArray[14][1] = 1;

        pyoPyoArray[15][0] = 0;
        pyoPyoArray[15][1] = 2;

        pyoPyoArray[16][0] = 2;
        pyoPyoArray[16][1] = 3;

        pyoPyoArray[17][0] = 4;
        pyoPyoArray[17][1] = 4;

        PyoPyo pyoPyo = new PyoPyo();
        pyoPyo.solution(pyoPyoArray);
    }
}

 

 

 

결과

step1
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 0 0 0 0 

step2
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 2 0 0 0 

step3
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 2 1 0 0 

step4
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 2 1 3 0 

step5
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 2 1 3 5 

step6
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 0 0 0 0 
1 2 1 3 5 

step7
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 3 0 0 0 
1 2 1 3 5 

step8
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 3 1 0 0 
1 2 1 3 5 

step9
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 3 1 3 0 
1 2 1 3 5 

step10
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
1 3 1 3 4 
1 2 1 3 5 

step11
Delete!!!!!!
0 0 0 0 0 
0 0 0 0 0 
1 0 0 0 0 
1 3 1 3 4 
1 2 1 3 5 

step12
0 0 0 0 0 
0 0 0 0 0 
0 2 0 0 0 
0 3 1 3 4 
0 2 1 3 5 

step13
0 0 0 0 0 
0 0 0 0 0 
0 2 0 1 0 
0 3 1 3 4 
0 2 1 3 5 

step14
0 0 0 0 0 
0 0 0 0 0 
0 2 0 1 4 
0 3 1 3 4 
0 2 1 3 5 

step15
0 0 0 0 0 
0 0 0 0 0 
0 2 1 1 4 
0 3 1 3 4 
0 2 1 3 5 

step16
Delete!!!
0 0 0 0 0 
0 0 0 0 0 
0 2 0 0 4 
0 3 0 3 4 
2 2 0 3 5 

step17
0 0 0 0 0 
0 0 0 0 0 
0 2 0 0 4 
0 3 0 3 4 
2 2 3 3 5 

step18
Delete!!!
0 0 0 0 0 
0 0 0 0 4 
0 2 0 0 4 
0 3 0 0 4 
2 2 0 0 5

 

 

일단 동작은 잘되는것 같지만

버그가 있을 것이다.

더 많은 테스트를 통해 추후 개선해봐야겠다.

728x90
반응형

'Algorithm > 번외' 카테고리의 다른 글

[Algorithm] version max 값 찾기  (0) 2023.01.23
[Algorithm] 벌집 만들기  (0) 2021.10.05

댓글