React 實(shí)現(xiàn)井字棋的示例代碼
一、簡介
這篇文章會(huì)基于React 實(shí)現(xiàn)井字棋小游戲功能。
二、效果演示

三、技術(shù)實(shí)現(xiàn)
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;
}
}
// 判斷對(duì)角線是否相同
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實(shí)現(xiàn),3行,3列。
2.表格元素點(diǎn)擊
更新cell內(nèi)容,更新次數(shù)。
const newSquares = [...squares];
if (newSquares[rowIdx][colIdx] !== '') {
return;
}
newSquares[rowIdx][colIdx] = times % 2 === 0 ? 'X' : 'O';
setSquares(newSquares);
setTimes(times + 1);3.判斷游戲是否結(jié)束
判斷每行,每列,斜線是否相等。
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;
}
}
// 判斷對(duì)角線是否相同
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])到此這篇關(guān)于React 實(shí)現(xiàn)井字棋的文章就介紹到這了,更多相關(guān)React 井字棋內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React虛擬渲染實(shí)現(xiàn)50個(gè)或者一百個(gè)圖表渲染
這篇文章主要為大家介紹了React虛擬渲染實(shí)現(xiàn)50個(gè)或者100個(gè)圖表渲染的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
基于React.js實(shí)現(xiàn)簡單的文字跑馬燈效果
剛好手上有一個(gè)要實(shí)現(xiàn)文字跑馬燈的react項(xiàng)目,然后ant-design上面沒有這個(gè)組件,于是只能自己手?jǐn)]一個(gè),文中的實(shí)現(xiàn)方法講解詳細(xì),希望對(duì)大家有所幫助2023-01-01
React搭配TypeScript使用教程及實(shí)戰(zhàn)案例
本文主要介紹了React搭配TypeScript使用教程及實(shí)戰(zhàn)案例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-02-02
深入理解React Native原生模塊與JS模塊通信的幾種方式
本篇文章主要介紹了深入理解React Native原生模塊與JS模塊通信的幾種方式,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
React為什么需要Scheduler調(diào)度器原理詳解
這篇文章主要為大家介紹了React為什么需要Scheduler調(diào)度器原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
react中的useImperativeHandle()和forwardRef()用法
這篇文章主要介紹了react中的useImperativeHandle()和forwardRef()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

