Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- 
  Bổ xung thêm this.data là mảng 2 chiều
  và hàm create2DArray
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #canvas {
        position: absolute;
        top: 5;
        bottom: 5;
        left: 5;
        right: 5;
        margin: auto;
      }

      #btn-clear {
        position: absolute;
        top: 20px;
        left: 20px;
        z-index: 100;
      }
    </style>
  </head>

  <body>
    <button id="btn-clear">Clear</button>
    <canvas id="canvas" style="background-color: #dddddd;"> </canvas>

    <script>
      const canvas = document.getElementById("canvas");
      const btnClear = document.getElementById("btn-clear");
      const c = canvas.getContext("2d");

      canvas.width = innerWidth;
      canvas.height = innerHeight;

      canvas.addEventListener(
        "click",
        (e) => {
          board.handleClick(e);
        },
        false
      );

      btnClear.addEventListener("click", () => {
        board.data = [];
        board.win = {};
        board.xTurn = true;
        board.create2DArray();
        canvas.style.pointerEvents = "unset";
      });

      class Board {
        constructor(cellWidth, row, col) {
          this.cellWidth = cellWidth;
          this.row = row;
          this.col = col;
          //Bổ xung margin cho tổng quát hơn
          this.marginTop = 100;
          this.marginLeft = 5;
          this.marginBottom = 5;
          this.marginRight = 5;
          canvas.width =
            this.col * this.cellWidth + this.marginLeft + this.marginRight;
          canvas.height =
            this.row * this.cellWidth + this.marginTop + this.marginBottom;

          //
          this.data = [];
          this.create2DArray();
          this.win = {};
          this.xTurn = true;
        }

        create2DArray() {
          // Loop to create 2D array using 1D array
          for (let i = 0; i < this.row; i++) {
            this.data[i] = new Array(this.col);
            this.data[i].fill(0, 0);
          }
        }

        //Vẽ lại
        drawGrid() {
          c.lineWidth = 1; // set line color
          // Vẽ kẻ ngang
          for (let i = 0; i < this.row + 1; i++) {
            c.beginPath();
            if (i === 0 || i === this.row) {
              c.strokeStyle = "#000";
            } else {
              c.strokeStyle = "#777";
            }
            c.moveTo(this.marginLeft, this.marginTop + i * this.cellWidth);
            c.lineTo(
              this.marginLeft + this.col * this.cellWidth,
              this.marginTop + i * this.cellWidth
            );
            c.stroke();
            c.closePath();
          }

          //Vẽ kẻ dọc
          for (let j = 0; j < this.col + 1; j++) {
            c.beginPath();
            if (j === 0 || j === this.col) {
              c.strokeStyle = "#000000";
            } else {
              c.strokeStyle = "#888";
            }
            c.moveTo(this.marginLeft + j * this.cellWidth, this.marginTop);
            c.lineTo(
              this.marginLeft + j * this.cellWidth,
              this.marginTop + this.row * this.cellWidth
            );
            c.stroke();
            c.closePath();
          }
        }

        handleClick(e) {
          let x = e.layerX;
          let y = e.layerY;

          let i = Math.floor((y - this.marginTop) / this.cellWidth);
          let j = Math.floor((x - this.marginLeft) / this.cellWidth);
          if (i >= 0 && j >= 0 && this.data[i][j] === 0) {
            if (this.xTurn) {
              this.data[i][j] = 1;
            } else {
              this.data[i][j] = 2;
            }
            this.win = this.checkWin(i, j);
            this.xTurn = !this.xTurn;
          }
        }

        drawXO() {
          for (let i in this.data) {
            for (let j in this.data[i]) {
              if (this.data[i][j] === 1) {
                this.drawX(i, j);
              } else if (this.data[i][j] === 2) {
                this.drawO(i, j);
              }
            }
          }
        }

        drawX(i, j) {
          let x = j * this.cellWidth + this.marginLeft;
          let y = i * this.cellWidth + this.marginTop;

          c.strokeStyle = "#123456";
          c.lineWidth = 3;
          c.beginPath();
          c.moveTo(x + this.cellWidth * 0.2, y + this.cellWidth * 0.2);
          c.lineTo(x + this.cellWidth * 0.8, y + this.cellWidth * 0.8);
          c.stroke();
          c.closePath();

          c.beginPath();
          c.moveTo(x + this.cellWidth * 0.2, y + this.cellWidth * 0.8);
          c.lineTo(x + this.cellWidth * 0.8, y + this.cellWidth * 0.2);
          c.stroke();
          c.closePath();
        }

        drawO(i, j) {
          let x = j * this.cellWidth + this.marginLeft;
          let y = i * this.cellWidth + this.marginTop;

          c.beginPath();
          c.strokeStyle = "red";
          c.lineWidth = 3;
          c.arc(
            x + this.cellWidth / 2,
            y + this.cellWidth / 2,
            this.cellWidth * 0.3,
            0,
            2 * Math.PI
          );
          c.stroke();
          c.closePath();
        }

        checkWin(i, j) {
          let value = this.data[i][j];

          let countHozizontal = this.checkHozizontal(i, j, value);
          let countVertical = this.checkVertical(i, j, value);
          let countDiagonal1 = this.checkDiagonalTopLeft(i, j, value);
          let countDiagonal2 = this.checkDiagonalTopRight(i, j, value);

          if (countHozizontal.count >= 5) {
            return {
              isWin: true,
              start: countHozizontal.start,
              end: countHozizontal.end,
            };
          } else if (countVertical.count >= 5) {
            return {
              isWin: true,
              start: countVertical.start,
              end: countVertical.end,
            };
          } else if (countDiagonal1.count >= 5) {
            return {
              isWin: true,
              start: countDiagonal1.start,
              end: countDiagonal1.end,
            };
          } else if (countDiagonal2.count >= 5) {
            return {
              isWin: true,
              start: countDiagonal2.start,
              end: countDiagonal2.end,
            };
          }
          return { isWin: false };
        }

        checkHozizontal(row, col, value) {
          let count = 1;
          let startI = row;
          let endI = row;
          // Check bên trái value
          for (let i = row - 1; i >= 0; i--) {
            if (value === this.data[i][col]) {
              startI = i;
              count++;
            } else {
              break;
            }
          }

          // Check bên phải value
          for (let i = row + 1; i < this.data.length; i++) {
            if (value === this.data[i][col]) {
              endI = i;
              count++;
            } else {
              break;
            }
          }

          return {
            count,
            start: { startI, startJ: col },
            end: { endI, endJ: col },
          };
        }

        checkVertical(row, col, value) {
          let count = 1;
          let startJ = col;
          let endJ = col;
          // Check lên trên value
          for (let i = col - 1; i >= 0; i--) {
            if (value === this.data[row][i]) {
              startJ = i;
              count++;
            } else {
              break;
            }
          }

          // Check xuống dưới value
          for (let i = col + 1; i < this.data.length; i++) {
            if (value === this.data[row][i]) {
              endJ = i;
              count++;
            } else {
              break;
            }
          }

          return {
            count,
            start: { startI: row, startJ },
            end: { endI: row, endJ },
          };
        }

        checkDiagonalTopLeft(row, col, value) {
          let count = 1;
          let startI = row;
          let startJ = col;
          let endI = row;
          let endJ = col;

          // Check đường chéo -> Top left
          for (let i = 1; i < this.data.length; i++) {
            if (row - i < 0 || col - i < 0) {
              break;
            }
            if (value === this.data[row - i][col - i]) {
              startI = row - i;
              startJ = col - i;
              count++;
            } else {
              break;
            }
          }

          // Check đường chéo -> Bot Right
          for (let i = 1; i < this.data.length; i++) {
            if (
              row + i > this.data.length - 1 ||
              col + i > this.data[row].length - 1
            ) {
              break;
            }
            if (value === this.data[row + i][col + i]) {
              endI = row + i;
              endJ = col + i;
              count++;
            } else {
              break;
            }
          }

          return { count, start: { startI, startJ }, end: { endI, endJ } };
        }

        checkDiagonalTopRight(row, col, value) {
          let count = 1;
          let startI = row;
          let startJ = col;
          let endI = row;
          let endJ = col;

          // Check đường chéo -> Top Right
          for (let i = 1; i < this.data.length; i++) {
            if (row - i < 0 || col + i > this.data[i].length) {
              break;
            }
            if (value === this.data[row - i][col + i]) {
              startI = row - i;
              startJ = col + i;
              count++;
            } else {
              break;
            }
          }

          // Check đường chéo -> Bot left
          for (let i = 1; i < this.data.length; i++) {
            if (row + i > this.data.length - 1 || col - i < 0) {
              break;
            }
            if (value === this.data[row + i][col - i]) {
              endI = row + i;
              endJ = col - i;
              count++;
            } else {
              break;
            }
          }

          return { count, start: { startI, startJ }, end: { endI, endJ } };
        }

        drawWin() {
          if (this.win.isWin) {
            let startX =
              this.win.start.startJ * this.cellWidth + this.marginLeft;
            let startY =
              this.win.start.startI * this.cellWidth + this.marginTop;
            let endX = this.win.end.endJ * this.cellWidth + this.marginLeft;
            let endY = this.win.end.endI * this.cellWidth + this.marginTop;

            c.strokeStyle = "#000";
            c.beginPath();
            c.save();
            c.translate(this.cellWidth / 2, this.cellWidth / 2);
            c.moveTo(startX, startY);
            c.lineTo(endX, endY);
            c.stroke();
            c.restore();
            c.closePath();
            canvas.style.pointerEvents = "none";
          }
        }
      }

      let board = new Board(40, 50, 50);
      board.drawGrid();

      function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);
        board.drawGrid();
        board.drawXO();
        board.drawWin();
      }

      animate();
    </script>
  </body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console