您的位置:首页 > 游戏 > 手游 > 基于React 实现井字棋

基于React 实现井字棋

2024/12/23 5:04:43 来源:https://blog.csdn.net/qq_37011724/article/details/140275781  浏览:    关键词:基于React 实现井字棋

一、简介

      这篇文章会基于React 实现井字棋小游戏功能。

二、效果演示

三、技术实现


import {useEffect, useState} from "react";export default (props) => {return <Board/>
}const Board = () => {let initialState = [['', '', ''], ['', '', ''], ['', '', '']];const [squares, setSquares] = useState(initialState);const [times, setTimes] = useState(0);useEffect(()=>{// 判断每行是否相同for (let i = 0; i < 3; i++) {if (squares[i][0] === squares[i][1] && squares[i][1] === squares[i][2] && squares[i][0] !== '') {alert(squares[i][0] + ' win');setTimes(0)setSquares(initialState)return;}}// 判断每列是否相同for (let i = 0; i < 3; i++) {if (squares[0][i] === squares[1][i] && squares[1][i] === squares[2][i] && squares[0][i] !== '') {alert(squares[0][i] + ' win')setTimes(0)setSquares(initialState)return;}}// 判断对角线是否相同if (squares[0][0] === squares[1][1] && squares[1][1] === squares[2][2] && squares[0][0] !== '') {alert(squares[0][0] + ' win');setTimes(0)setSquares(initialState)return;}if (squares[0][2] === squares[1][1] && squares[1][1] === squares[2][0] && squares[0][2] !== ''){alert(squares[0][2] + ' win');setTimes(0)setSquares(initialState)return;}},[times])return <div style={{width:'130px', margin: '0 auto'}}><table style={{borderCollapse: 'collapse'}}>{squares.map((row, rowIdx) => {return <tr key={rowIdx}>{row.map((col, colIdx) => {return <td key={rowIdx + '-' + colIdx} style={{border: '1px solid #000', width: '40px', height: '40px'}}><div style={{width: '40px', height: '40px', textAlign: 'center', lineHeight:'40px'}} onClick={() => {const newSquares = [...squares];if (newSquares[rowIdx][colIdx] !== '') {return;}newSquares[rowIdx][colIdx] = times % 2 === 0 ? 'X' : 'O';setSquares(newSquares);setTimes(times + 1);}}>{col}</div></td>})}</tr>})}</table></div>
}

1.布局

        基于table实现,3行,3列。

2.表格元素点击

更新cell内容,更新次数。

     const newSquares = [...squares];if (newSquares[rowIdx][colIdx] !== '') {return;}newSquares[rowIdx][colIdx] = times % 2 === 0 ? 'X' : 'O';setSquares(newSquares);setTimes(times + 1);

3.判断游戏是否结束

判断每行,每列,斜线是否相等。

 useEffect(()=>{// 判断每行是否相同for (let i = 0; i < 3; i++) {if (squares[i][0] === squares[i][1] && squares[i][1] === squares[i][2] && squares[i][0] !== '') {alert(squares[i][0] + ' win');setTimes(0)setSquares(initialState)return;}}// 判断每列是否相同for (let i = 0; i < 3; i++) {if (squares[0][i] === squares[1][i] && squares[1][i] === squares[2][i] && squares[0][i] !== '') {alert(squares[0][i] + ' win')setTimes(0)setSquares(initialState)return;}}// 判断对角线是否相同if (squares[0][0] === squares[1][1] && squares[1][1] === squares[2][2] && squares[0][0] !== '') {alert(squares[0][0] + ' win');setTimes(0)setSquares(initialState)return;}if (squares[0][2] === squares[1][1] && squares[1][1] === squares[2][0] && squares[0][2] !== ''){alert(squares[0][2] + ' win');setTimes(0)setSquares(initialState)return;}},[times])

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com