Sure! Below is the complete HTML code for a Tic-Tac-Toe game on a 10x10 grid, where the objective is to get 5 in a row horizontally, vertically, or diagonally to win. The CSS and JavaScript are included within the HTML code for convenience.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tic-Tac-Toe 10x10 - 5 in a Row</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#board {
display: grid;
grid-template-columns: repeat(10, 40px);
grid-template-rows: repeat(10, 40px);
justify-content: center;
margin: 20px auto;
width: 400px;
}
.cell {
border: 1px solid #333;
width: 40px;
height: 40px;
line-height: 40px;
font-size: 24px;
cursor: pointer;
user-select: none;
}
#message {
margin-top: 20px;
font-size: 24px;
height: 30px;
}
#reset {
margin-top: 20px;
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Tic-Tac-Toe 10x10 - 5 in a Row</h1>
<div id="board"></div>
<div id="message"></div>
<button id="reset">Reset Game</button>
<script>
const boardSize = 10; // Size of the board (10x10)
const winCondition = 5; // Number of marks in a row needed to win
let board = []; // 2D array to represent the board state
let currentPlayer = 'X'; // Current player ('X' or 'O')
let gameActive = true; // Game state
const boardElement = document.getElementById('board');
const messageElement = document.getElementById('message');
const resetButton = document.getElementById('reset');
// Initialize the board and set up event listeners
function initBoard() {
board = [];
boardElement.innerHTML = '';
gameActive = true;
currentPlayer = 'X';
messageElement.textContent = "";
for (let i = 0; i < boardSize; i++) {
board[i] = [];
for (let j = 0; j < boardSize; j++) {
board[i][j] = '';
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', cellClicked);
boardElement.appendChild(cell);
}
}
}
// Handle a cell click event
function cellClicked() {
if (!gameActive) return;
const row = parseInt(this.dataset.row);
const col = parseInt(this.dataset.col);
if (board[row][col] !== '') {
// Cell already occupied
return;
}
board[row][col] = currentPlayer;
this.textContent = currentPlayer;
if (checkWin(row, col)) {
messageElement.textContent = `Player ${currentPlayer} wins!`;
gameActive = false;
} else if (isBoardFull()) {
messageElement.textContent = "It's a draw!";
gameActive = false;
} else {
// Switch players
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
// Check for a win condition from the last move
function checkWin(row, col) {
return (
checkDirection(row, col, 1, 0) || // Horizontal
checkDirection(row, col, 0, 1) || // Vertical
checkDirection(row, col, 1, 1) || // Diagonal down-right
checkDirection(row, col, 1, -1) // Diagonal down-left
);
}
// Check a specific direction for the win condition
function checkDirection(row, col, deltaRow, deltaCol) {
let count = 1; // Start with the current mark
// Check in the positive direction
count += countMarks(row, col, deltaRow, deltaCol);
// Check in the negative direction
count += countMarks(row, col, -deltaRow, -deltaCol);
// Return true if win condition is met
return count >= winCondition;
}
// Count matching marks in a given direction
function countMarks(row, col, deltaRow, deltaCol) {
let count = 0;
let r = row + deltaRow;
let c = col + deltaCol;
while (
r >= 0 && r < boardSize &&
c >= 0 && c < boardSize &&
board[r][c] === currentPlayer
) {
count++;
r += deltaRow;
c += deltaCol;
}
return count;
}
// Check if the board is completely filled
function isBoardFull() {
for (let i = 0; i < boardSize; i++) {
if (board[i].includes('')) {
return false;
}
}
return true;
}
// Reset the game when the reset button is clicked
resetButton.addEventListener('click', initBoard);
// Initialize the game on page load
initBoard();
</script>
</body>
</html>
How to Use:
-
Open the Game:
- Save the above code into a file with an
.html extension, for example, tic-tac-toe.html.
- Open the file in a modern web browser (e.g., Chrome, Firefox, Edge).
-
Playing the Game:
- The game is for two players: 'X' and 'O'.
- Players take turns clicking on empty cells to place their mark.
- The first player to get 5 of their marks in a row horizontally, vertically, or diagonally wins.
- The game will display a message declaring the winner or if there's a draw.
-
Resetting the Game:
- Click the "Reset Game" button to start a new game at any time.
Explanation of Key Components:
-
HTML Structure:
- The
#board div contains the game grid.
- The
#message div displays game status messages.
- The
#reset button allows players to reset the game.
-
CSS Styles:
- Uses Grid Layout to create a 10x10 grid.
.cell class defines the appearance of each cell.
-
JavaScript Logic:
- Initialization (
initBoard):
- Sets up the 2D
board array and creates clickable cells.
- Event Handling (
cellClicked):
- Places the player's mark in a cell and checks for a win or draw.
- Win Checking (
checkWin, checkDirection, countMarks):
- Checks all directions from the last move to see if the player has 5 in a row.
- Game State Management:
- Tracks the current player and whether the game is active.
- Updates messages and switches players accordingly.
- Reset Functionality:
- Resets the game state and clears the board when the reset button is clicked.
Note:
- This game is designed for two players playing on the same device.
- Ensure that JavaScript is enabled in your browser to play the game.
Enjoy your game of extended Tic-Tac-Toe!