JS实现tic tac toe

JS实现tic tac toe

项目地址:https://github.com/Miever1/todo-list-js

基本效果:

具体实现:

html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./node_modules/normalize.css/normalize.css">
    <link rel="stylesheet" href="./css/index.css">
    <link rel="stylesheet" href="./css/style.css">
</head>

<body>
    <div class="game">
        <div class="board">
            <div class="board-row">
                <button class="sqare"></button>
                <button class="sqare
可以提供一些代码和思路。 好的,我可以为您提供一些关于使用 React 实现 Tic Tac Toe 游戏的建议。首先,您需要创建一个 React 组件来代表这个游戏。您可以使用以下代码作为参考: ``` import React, { useState } from 'react'; function Square(props) { return ( <button className="square" onClick={props.onClick}> {props.value} </button> ); } function Board(props) { const renderSquare = i => { return ( <Square value={props.squares[i]} onClick={() => props.onClick(i)} /> ); }; return ( <div> <div className="board-row"> {renderSquare(0)} {renderSquare(1)} {renderSquare(2)} </div> <div className="board-row"> {renderSquare(3)} {renderSquare(4)} {renderSquare(5)} </div> <div className="board-row"> {renderSquare(6)} {renderSquare(7)} {renderSquare(8)} </div> </div> ); } function Game() { const [history, setHistory] = useState([{ squares: Array(9).fill(null) }]); const [stepNumber, setStepNumber] = useState(0); const [xIsNext, setXIsNext] = useState(true); const handleClick = i => { const current = history.slice(0, stepNumber + 1); const squares = current[current.length - 1].squares.slice(); if (calculateWinner(squares) || squares[i]) { return; } squares[i] = xIsNext ? 'X' : 'O'; setHistory(current.concat([{ squares: squares }])); setStepNumber(current.length); setXIsNext(!xIsNext); }; const jumpTo = step => { setStepNumber(step); setXIsNext(step % 2 === 0); }; const current = history[stepNumber]; const winner = calculateWinner(current.squares); const moves = history.map((step, move) => { const desc = move ? 'Go to move #' + move : 'Go to game start'; return ( <li key={move}> <button onClick={() => jumpTo(move)}>{desc}</button> </li> ); }); let status; if (winner) { status = 'Winner: ' + winner; } else if (stepNumber === 9) { status = 'Draw!'; } else { status = 'Next player: ' + (xIsNext ? 'X' : 'O'); } return ( <div className="game"> <div className="game-board"> <Board squares={current.squares} onClick={handleClick} /> </div> <div className="game-info"> <div>{status}</div> <ol>{moves}</ol> </div> </div> ); } function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i]; if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { return squares[a]; } } return null; } export default Game; ``` 在这个代码中,我们首先定义了 `Square` 组件来表示格子。当一个格子被点击时,我们会传入一个 `onClick` 函数来处理点击事件。 接着,我们定义了 `Board` 组件,它会渲染一个 3x3 的游戏棋盘,并包含一个 `onClick` 函数来处理格子点击事件。通过 `renderSquare` 函数,我们可以方便地生成每一个格子。 最后,我们定义了 `Game` 组件,它包含了游戏的逻辑以及状态。在这个组件中,我们使用了 `useState` Hook 来管理游戏历史记录、当前步骤以及下一步是哪一个玩家。我们在 `handleClick` 函数中处理剪枝和状态更新。 `jumpTo` 函数可以让我们回到历史记录中的任意一步。 最后,在 `calculateWinner` 函数中,我们使用了一个数组来表示所有的获胜情况,并且使用了一个循环来判断是否有玩家获胜。 希望这些代码能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值