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

三、技術(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;
}
}
// 判斷對角線是否相同
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實現(xiàn),3行,3列。
2.表格元素點擊
更新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;
}
}
// 判斷對角線是否相同
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 實現(xiàn)井字棋的文章就介紹到這了,更多相關(guān)React 井字棋內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React視頻播放控制組件Video Controls的實現(xiàn)
本文主要介紹了React視頻播放控制組件Video Controls的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
react-three/postprocessing庫的參數(shù)中文含義使用解析
這篇文章主要介紹了react-three/postprocessing庫的參數(shù)中文含義使用總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Vite+React搭建開發(fā)構(gòu)建環(huán)境實踐記錄
這篇文章主要介紹了Vite+React搭建開發(fā)構(gòu)建環(huán)境實踐,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09
從零搭建Webpack5-react腳手架的實現(xiàn)步驟(附源碼)
本文主要介紹了從零搭建Webpack5-react腳手架的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
使用React實現(xiàn)一個簡單的待辦事項列表的示例代碼
這篇文章我們將詳細(xì)講解如何建立一個這樣簡單的列表,文章通過代碼示例介紹的非常詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08

