React 实现五子棋

简介

         本文将会基于React 实现五子棋小游戏,游戏规则为先让5颗棋子连成1线的一方获胜。

实现效果

技术实现

页面布局

<div>
    <table style={
  {border: '1px solid #000', borderCollapse: 'collapse', backgroundColor: 'lightgray'}}>
      <tbody>
      {
        squares.map((row, rowIndex) => {
          return <tr key={rowIndex}>
            {
              row.map((col, colIndex) => {
                return <td key={colIndex}
                           style={
  {border: '1px solid #000', width: '30px', height: '30px'}}

                           onClick={(event) => {
                             const clickedElement = event.target as HTMLTableCellElement;

                             const rect = clickedElement.getBoundingClientRect();
                             const x = event.clientX - rect.left;
                             const y = event.clientY - rect.top;
                             const width = clickedElement.getBoundingClientRect().width;
                             const height = clickedElement.getBoundingClientRect().height;

                             const left = x / width;
                             const top = y / height;
                             if (left < 0.5 && top < 0.5){ // 左上
                               if (rowIndex > 0){
                                 rowIndex--;
                               }
                               if (colIndex > 0){
                                 colIndex--;
                               }
                             }
                             if (left < 0.5 && top > 0.5){ // 左下

                               if (colIndex > 0){
                                 colIndex--;
                               }
                             }
                             if (left > 0.5 && top < 0.5){ // 右上
                               if (rowIndex > 0){
                                 rowIndex--;
                               }

                             }

                             let item = squares[rowIndex][colIndex];
                             if (item !== '') {
                               return;
                             }
                             if (times % 2 === 0) {
                               item = 'black';
                             } else {
                               item = 'white';
                             }
                             const newSquares = [...squares];
                             newSquares[rowIndex][colIndex] = item;

                             setTimes(times + 1);
                             setSquares(newSquares);
                           }
                           }
                >
                  {col === 'white' ?
                    <div
                      style={
  {
                        width: '100%',
                        height: '100%',
                        backgroundColor: 'white',
                        borderRadius: '50%',
                        position: "relative",
                        right: "-50%",
                        bottom: "-50%"
                      }}></div>
                    : (col === 'black' ?
                      <div
                        style={
  {
                          width: '100%',
                          height: '100%',
                          backgroundColor: 'black',
                          borderRadius: '50%',
                          position: "relative",
                          right: "-50%",
                          bottom: "-50%"
                        }}&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值