• 基于React 实现井字棋


    一、简介

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

    二、效果演示

    三、技术实现

    1. import {useEffect, useState} from "react";
    2. export default (props) => {
    3. return <Board/>
    4. }
    5. const Board = () => {
    6. let initialState = [['', '', ''], ['', '', ''], ['', '', '']];
    7. const [squares, setSquares] = useState(initialState);
    8. const [times, setTimes] = useState(0);
    9. useEffect(()=>{
    10. // 判断每行是否相同
    11. for (let i = 0; i < 3; i++) {
    12. if (squares[i][0] === squares[i][1] && squares[i][1] === squares[i][2] && squares[i][0] !== '') {
    13. alert(squares[i][0] + ' win');
    14. setTimes(0)
    15. setSquares(initialState)
    16. return;
    17. }
    18. }
    19. // 判断每列是否相同
    20. for (let i = 0; i < 3; i++) {
    21. if (squares[0][i] === squares[1][i] && squares[1][i] === squares[2][i] && squares[0][i] !== '') {
    22. alert(squares[0][i] + ' win')
    23. setTimes(0)
    24. setSquares(initialState)
    25. return;
    26. }
    27. }
    28. // 判断对角线是否相同
    29. if (squares[0][0] === squares[1][1] && squares[1][1] === squares[2][2] && squares[0][0] !== '') {
    30. alert(squares[0][0] + ' win');
    31. setTimes(0)
    32. setSquares(initialState)
    33. return;
    34. }
    35. if (squares[0][2] === squares[1][1] && squares[1][1] === squares[2][0] && squares[0][2] !== ''){
    36. alert(squares[0][2] + ' win');
    37. setTimes(0)
    38. setSquares(initialState)
    39. return;
    40. }
    41. },[times])
    42. return <div style={{width:'130px', margin: '0 auto'}}>
    43. <table style={{borderCollapse: 'collapse'}}>
    44. {squares.map((row, rowIdx) => {
    45. return <tr key={rowIdx}>
    46. {
    47. row.map((col, colIdx) => {
    48. return <td key={rowIdx + '-' + colIdx} style={{border: '1px solid #000', width: '40px', height: '40px'}}>
    49. <div style={{width: '40px', height: '40px', textAlign: 'center', lineHeight:'40px'}} onClick={
    50. () => {
    51. const newSquares = [...squares];
    52. if (newSquares[rowIdx][colIdx] !== '') {
    53. return;
    54. }
    55. newSquares[rowIdx][colIdx] = times % 2 === 0 ? 'X' : 'O';
    56. setSquares(newSquares);
    57. setTimes(times + 1);
    58. }
    59. }>{col}div>
    60. td>
    61. }
    62. )
    63. }
    64. tr>
    65. })}
    66. table>
    67. div>
    68. }

    1.布局

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

    2.表格元素点击

    更新cell内容,更新次数。

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

    3.判断游戏是否结束

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

    1. useEffect(()=>{
    2. // 判断每行是否相同
    3. for (let i = 0; i < 3; i++) {
    4. if (squares[i][0] === squares[i][1] && squares[i][1] === squares[i][2] && squares[i][0] !== '') {
    5. alert(squares[i][0] + ' win');
    6. setTimes(0)
    7. setSquares(initialState)
    8. return;
    9. }
    10. }
    11. // 判断每列是否相同
    12. for (let i = 0; i < 3; i++) {
    13. if (squares[0][i] === squares[1][i] && squares[1][i] === squares[2][i] && squares[0][i] !== '') {
    14. alert(squares[0][i] + ' win')
    15. setTimes(0)
    16. setSquares(initialState)
    17. return;
    18. }
    19. }
    20. // 判断对角线是否相同
    21. if (squares[0][0] === squares[1][1] && squares[1][1] === squares[2][2] && squares[0][0] !== '') {
    22. alert(squares[0][0] + ' win');
    23. setTimes(0)
    24. setSquares(initialState)
    25. return;
    26. }
    27. if (squares[0][2] === squares[1][1] && squares[1][1] === squares[2][0] && squares[0][2] !== ''){
    28. alert(squares[0][2] + ' win');
    29. setTimes(0)
    30. setSquares(initialState)
    31. return;
    32. }
    33. },[times])

  • 相关阅读:
    八股文第十九天
    vue--vuex 状态管理模式
    Euclidean Distance Transform - EDT
    STM32 JLINK感叹号问题解决
    Data Matrix码的使用
    工业智能网关BL110应用之三十五: 如何连接配置金鸽MQTT云服务器
    Linux线程间交互
    构建病毒宿主关系知识图谱
    KLEE简单使用
    面渣逆袭:微服务三十三问,两万字图文详解!速收藏!
  • 原文地址:https://blog.csdn.net/qq_37011724/article/details/140275781