Cute Bow Tie Hearts Blinking Pink Pointer

프론트엔드/React

[React]틱택토게임 만들기

청포도 에이드 2022. 4. 14. 17:25
728x90

기본 설정 세팅

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
    <div id="root"></div>
    <script type="text/babel">


        class App extends React.Component{
            render(){
                return(
                    <div>
                       
                    </div>    
                )
            }
        }


        ReactDOM.render(
            <App />,
            document.querySelector('#root')
        )
        
    </script>
</body>
</html>

 

코드 구현

 

<body>
    <div id="root"></div>
    <script type="text/babel">
        // Square
        // Board
        // game

        class Square extends React.Component{
            state = {
                value:null,
            }
            render(){
                return(
                    <button className="square"
                            onClick={ ()=>{ this.props.onClick() } }
                    >
                        {this.props.value}
                    </button>
                )
            }
        }

        class Board extends React.Component{

            state = {
                squares:Array(9).fill(null),
                xIsNext:true,

            }
            handleClick = (i)=>{
                const squares = [...this.state.squares]
                squares[i] = this.state.xIsNext ? "X" : "O"

                this.setState({
                    squares,
                    xIsNext:!this.state.xIsNext
                })
            }

            renderSquare = i =>{
                return <Square
                            onClick={ ()=>{this.handleClick(i)}}
                            value = {this.state.squares[i]}
                                
                        />

            }

            render(){
                return(
                    <div>
                        <div className="status"></div>
                        <div className="board-rows">
                            {this.renderSquare(0)}
                            {this.renderSquare(1)}
                            {this.renderSquare(2)}
                        </div>
                        <div className="board-rows">
                            {this.renderSquare(3)}
                            {this.renderSquare(4)}
                            {this.renderSquare(5)}
                        </div>
                        <div className="board-rows">
                            {this.renderSquare(6)}
                            {this.renderSquare(7)}
                            {this.renderSquare(8)}
                        </div>
                    </div>
                )
            }
        }


        class Game extends React.Component{
            render(){
                return(
                    <div className="game">

                        <div className="game-board">
                            <Board/>
                        </div>
                       
                    </div>    
                )
            }
        }


        ReactDOM.render(
            <Game />,
            document.querySelector('#root')
        )
        
    </script>
</body>

css

 

        body {
        font: 14px "Century Gothic", Futura, sans-serif;
        margin: 20px;
        }

        ol, ul {
        padding-left: 30px;
        }

        .board-row:after {
        clear: both;
        content: "";
        display: table;
        }

        .status {
        margin-bottom: 10px;
        }

        .square {
        background: #fff;
        border: 1px solid #999;
        float: left;
        font-size: 24px;
        font-weight: bold;
        line-height: 34px;
        height: 34px;
        margin-right: -1px;
        margin-top: -1px;
        padding: 0;
        text-align: center;
        width: 34px;
        }

        .square:focus {
        outline: none;
        }

        .kbd-navigation .square:focus {
        background: #ddd;
        }

        .game {
        display: flex;
        flex-direction: row;
        }

        .game-info {
        margin-left: 20px;
        }

다음 게시물

 

바로가기: 틱택토 승자정하기

728x90