complex JavaScript (Web UI using HTML Canvas)
complex JavaScript (Web UI using HTML Canvas)
DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<style>
canvas { background: #eee; margin-top: 20px; display: block; margin: auto; }
</style>
</head>
<body>
<canvas id="game" width="300" height="300"></canvas>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const size = 100;
let board = [["", "", ""], ["", "", ""], ["", "", ""]];
let current = "X";
function drawBoard() {
ctx.clearRect(0, 0, 300, 300);
for (let i = 1; i < 3; i++) {
ctx.beginPath();
ctx.moveTo(i * size, 0);
ctx.lineTo(i * size, 300);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * size);
ctx.lineTo(300, i * size);
ctx.stroke();
}
for (let r = 0; r < 3; r++)
for (let c = 0; c < 3; c++) {
let val = board[r][c];
if (val) {
ctx.font = "60px Arial";
ctx.fillText(val, c * size + 30, r * size + 70);
}
}
}
function checkWinner() {
for (let i = 0; i < 3; i++) {
if (board[i][0] && board[i][0] == board[i][1] && board[i][1] == board[i][2])
return board[i][0];
if (board