inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

웹 게임을 만들며 배우는 React

8-7. 승리 조건 체크와 타이머

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

168

김은규

작성한 질문수 1

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,
      }
    }

react

답변 0

npm run dev 실행 시 포트가 안뜨는 문제

0

236

2

timeouts.current를 useEffect 의 input값으로 넣었을때

0

97

2

렌더링 테스트 코드 (Hooks)

0

93

1

Cannot find package 'react-refesh' 이런 에러 뜨시는 분들 보세요.

0

165

1

해당 에러 뜨는 분들 보세요. "Uncaught TypeError: ReactDom.createRoot is not a function"

1

207

1

강사님 레포지토리에서 코드 복사 시 master 브랜치 말고 react18 브랜치꺼 복붙하세요ㅠㅠ

0

105

1

useMemo와 useCallback 사용 시기

0

218

2

onRightClickTd가 작동을 하지 않습니다.

0

236

1

action.type 불러오는 방식

0

225

2

onClickRedo 질문

0

176

1

const Try = require(./Try) 빨간줄

0

259

1

npx webpack 실행시

0

324

1

지뢰찾기 강좌에서 빈칸들 한번에 열기 파트에서 여쭤보고싶은부분이 있어서 글 올립니다.

0

241

1

강좌에서 다루지 않은 기능들은 어떻게 학습하면 좋을까요?

0

318

1

react devtool이 enable 않됩니다.

0

544

2

React 랜더링이 되지 않습니다.

0

421

2

비동기로 동작하는 setState에 대해서

0

337

1

npm run dev 할 때 에러발생

0

494

2

memo, PureComponenet, shouldComponentUpdate 관련 질문

0

210

1

devMiddleware의 필요성

0

358

1

리액트에서 화살표 함수를 사용해야하는 이유

0

946

2

path.join관련질문

0

287

2

2-9. 웹팩 데브 서버와 핫 리로딩 설치과정 시 에러

0

384

1

next.js 에서 이와 비슷한 예제를 돌리고있는데 react랑 달라서 질문 드립니

1

502

4