From 6b635ca397613f1664371abc8da64f439ba2f81a Mon Sep 17 00:00:00 2001 From: haerong22 Date: Mon, 22 Mar 2021 21:53:39 +0900 Subject: [PATCH] react web game : useContext --- react_webgame/main/Form.jsx | 9 ++---- react_webgame/main/MineSearch.jsx | 52 +++++++++++++++++++++++++++---- react_webgame/main/Table.jsx | 15 +++++++-- react_webgame/main/Td.jsx | 41 ++++++++++++++++++++++-- react_webgame/main/Tr.jsx | 16 ++++++++-- react_webgame/main/client.jsx | 5 ++- react_webgame/main/index.html | 2 +- 7 files changed, 116 insertions(+), 24 deletions(-) diff --git a/react_webgame/main/Form.jsx b/react_webgame/main/Form.jsx index 03657786..c56c39cc 100644 --- a/react_webgame/main/Form.jsx +++ b/react_webgame/main/Form.jsx @@ -16,12 +16,9 @@ const Form = () => { const onChangeMine = useCallback((e) => { setMine(e.target.value); }, []); - const onClickBtn = useCallback( - (e) => { - dispatch({ type: START_GAME, row, cell, mine }); - }, - [row, cell, mine] - ); + const onClickBtn = useCallback(() => { + dispatch({ type: START_GAME, row, cell, mine }); + }, [row, cell, mine]); return (
diff --git a/react_webgame/main/MineSearch.jsx b/react_webgame/main/MineSearch.jsx index 690f80b1..f626051a 100644 --- a/react_webgame/main/MineSearch.jsx +++ b/react_webgame/main/MineSearch.jsx @@ -27,6 +27,46 @@ const initialState = { result: "", }; +// 지뢰 심는 함수 +const plantMine = (row, cell, mine) => { + console.log(row, cell, mine); + const candidate = Array(row * cell) + .fill() + .map((arr, i) => { + // 0 ~ 가로*세로 만큼 숫자 배열 생성 + return i; + }); + const shuffle = []; + // 지뢰 갯수 만큼 지뢰 생성 + while (candidate.length > row * cell - mine) { + const chosen = candidate.splice( + Math.floor(Math.random() * candidate.length), + 1 + )[0]; + shuffle.push(chosen); + } + + // 가로 * 세로 만큼의 2차원 배열 생성 + const data = []; + for (let i = 0; i < row; i++) { + const rowData = []; + data.push(rowData); + for (let j = 0; j < cell; j++) { + rowData.push(CODE.NORMAL); + } + } + + // 지뢰 심기 + for (let k = 0; k < shuffle.length; k++) { + const ver = Math.floor(shuffle[k] / cell); + const hor = shuffle[k] % cell; + data[ver][hor] = CODE.MINE; + } + + console.log(data); + return data; +}; + // action 명 export const START_GAME = "START_GAME"; @@ -36,7 +76,7 @@ const reducer = (state, action) => { case START_GAME: return { ...state, - tableDate: plantMine(action.row, action.cell, action.mine), + tableData: plantMine(action.row, action.cell, action.mine), }; default: return state; @@ -46,16 +86,16 @@ const reducer = (state, action) => { const MineSearch = () => { const [state, dispatch] = useReducer(reducer, initialState); - const value = useMemo(() => { - tableData: state.tableData, dispatch; - }, [state.tableData]); + const value = useMemo(() => ({ tableData: state.tableData, dispatch }), [ + state.tableData, + ]); return (
-
{timer}
+
{state.timer}
-
{result}
+
{state.result}
); }; diff --git a/react_webgame/main/Table.jsx b/react_webgame/main/Table.jsx index 45e6833c..f3f169fe 100644 --- a/react_webgame/main/Table.jsx +++ b/react_webgame/main/Table.jsx @@ -1,7 +1,18 @@ -import React from "react"; +import React, { useContext } from "react"; +import { TableContext } from "./MineSearch"; +import Tr from "./Tr"; const Table = () => { - return
; + const { tableData } = useContext(TableContext); + return ( +
+ {Array(tableData.length) + .fill() + .map((tr, i) => ( + + ))} +
+ ); }; export default Table; diff --git a/react_webgame/main/Td.jsx b/react_webgame/main/Td.jsx index f40141f0..17cf890d 100644 --- a/react_webgame/main/Td.jsx +++ b/react_webgame/main/Td.jsx @@ -1,7 +1,42 @@ -import React from "react"; +import React, { useContext } from "react"; +import { CODE, TableContext } from "./MineSearch"; -const Td = () => { - return
; +const getTdStyle = (code) => { + switch (code) { + case CODE.NORMAL: + case CODE.MINE: + return { + background: "#444", + }; + case CODE.OPENED: + return { + background: "white", + }; + default: + return { + background: "white", + }; + } +}; + +const getTdText = (code) => { + switch (code) { + case CODE.NORMAL: + return ""; + case CODE.MINE: + return "X"; + default: + return ""; + } +}; + +const Td = ({ rowIndex, cellIndex }) => { + const { tableData } = useContext(TableContext); + return ( + + {getTdText(tableData[rowIndex][cellIndex])} + + ); }; export default Td; diff --git a/react_webgame/main/Tr.jsx b/react_webgame/main/Tr.jsx index 46f26364..31deff0e 100644 --- a/react_webgame/main/Tr.jsx +++ b/react_webgame/main/Tr.jsx @@ -1,7 +1,17 @@ -import React from "react"; +import React, { useContext } from "react"; +import { TableContext } from "./MineSearch"; +import Td from "./Td"; -const Tr = () => { - return
; +const Tr = ({ rowIndex }) => { + const { tableData } = useContext(TableContext); + return ( + + {tableData[0] && + Array(tableData[0].length) + .fill() + .map((td, i) => )} + + ); }; export default Tr; diff --git a/react_webgame/main/client.jsx b/react_webgame/main/client.jsx index 7ffc6a03..a8b1eeb2 100644 --- a/react_webgame/main/client.jsx +++ b/react_webgame/main/client.jsx @@ -1,6 +1,5 @@ import React from "react"; import ReactDom from "react-dom"; +import MineSearch from "./MineSearch"; -import TicTacToe from "./TicTacToe"; - -ReactDom.render(, document.querySelector("#root")); +ReactDom.render(, document.querySelector("#root")); diff --git a/react_webgame/main/index.html b/react_webgame/main/index.html index e74ccd6f..3257650c 100644 --- a/react_webgame/main/index.html +++ b/react_webgame/main/index.html @@ -1,7 +1,7 @@ - 틱택토 + 지뢰찾기