• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

open 값이 반영이 안됩니다 ㅠㅠ

21.07.14 18:57 작성 조회수 102

0

강의에서 openCount라고 하신걸 저는 openedCell이라고 하여서 진행을 했는데요, 강의에서 말씀하신것 처럼 이미 열린칸에 대해서 또 카운트하는 것을 방지하기 위해 

if (tableData[row][cell] >= CODE.OPENED) {
          console.log("이미 열린칸", openedCell)
          return;
}

  위와 같은 코드를 작성해서 걸러주는 작업을 진행했습니다.

그런데 위 코드를 넣으니깐 갑자기 reducer에 openedCell값이 계속 0인 채로 업데이트가 안되는것 같습니다...

console.log로 찍어보니 직전까지 제대로 나오는 것 같고, 위 코드를 없애면 업데이트가 되지만 강의에서도 나온 문제점은 해결이 되지 않습니다.

저는 재귀가 아닌 queue에 넣어서 약간 알고리즘 문제에서 bfs를 하는 방식처럼 visited배열을 만들어 놓고 방문을 했는지 안했는지를 따져서 칸을 열었습니다.

값이 계속 0인 이유가 무엇일까요...? 도저히 감이 안잡혀서 질문드립니다 아래는 OPEN_CELL의 경우의 코드 전문입니다.

    

case OPEN_CELL: {
      const tableData = [...state.tableData];
      console.log(state.openedCell, "테스트")
      let visited = new Array(tableData.length);
      for (let i = 0; i < visited.length; i++) {
        visited[i] = new Array(tableData[i].length).fill(false);
      }

      let openedCell = state.openedCell;
      console.log("cellCount값 확인 : ", openedCell);
      const checkAround = (row, cell) => {
        if (row < 0 || row >= tableData.length || cell < 0 || cell >= tableData.length) {
          return;
        }
        if (visited[row][cell])
          return;
        if (tableData[row][cell] >= CODE.OPENED) {
          console.log("이미 열린칸", openedCell)
          return;
        }

        // if (tableData[row][cell] === CODE.NORMAL) {
        //   openedCell += 1;
        // } else {
        //   return;
        // }
        let around = [];

        if (tableData[row - 1]) {
          around = around.concat(
            tableData[row - 1][cell - 1],
            tableData[row - 1][cell],
            tableData[row - 1][cell + 1]
          )
        }
        around = around.concat(
          tableData[row][cell - 1],
          tableData[row][cell + 1]
        )
        if (tableData[row + 1]) {
          around = around.concat(
            tableData[row + 1][cell - 1],
            tableData[row + 1][cell],
            tableData[row + 1][cell + 1]
          )
        }

        const count = around.filter((v) => [CODE.MINE, CODE.FLAG_MINE, CODE.QUESTION_MINE].includes(v)).length;
        tableData[row][cell] = count;
        openedCell += 1;
        visited[row][cell] = true;

        return count;
      }

      let queue = [[action.row, action.cell]];

      while (queue.length !== 0) {
        console.log("test")
        const [row, cell] = queue.shift();
        const count = checkAround(row, cell)
        if (count === 0) {
          queue.push([row - 1, cell - 1], [row - 1, cell], [row - 1, cell + 1], [row, cell - 1]
            , [row, cell + 1], [row + 1, cell - 1], [row + 1, cell], [row + 1, cell + 1])
        }
      }

      console.log("cellCount값 처리후 확인 : ", openedCell);
      console.log(state.data.row * state.data.cell - state.data.mine, state.openedCell + openedCell, state.data.row * state.data.cell - state.data.mine === state.openedCell + openedCell)
      let halted = false;
      let result = ''
      if (state.data.row * state.data.cell - state.data.mine === openedCell) {
        halted = true;
        result = '승리하셨습니다!'
      }

      // console.log("값 갱신  ", state.data.row * state.data.cell - state.data.mine, state.openedCell, cellCount, state.openedCell + cellCount)
      console.log("openedCell값", openedCell)
      return {
        ...state,
        tableData,
        halted,
        result,
        openedCell,
      }
    }

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!