Sure! Below is a simple yet functional implementation of a Tic Tac Toe game that supports both 1 Player (against the computer) and 2 Players modes. The GUI is minimal and clean, built using HTML, CSS, and JavaScript.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tic Tac Toe</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
margin: 0;
}
#game-container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
text-align: center;
}
#mode-selection {
margin-bottom: 20px;
}
#board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: 0 auto 20px;
}
.cell {
background-color: #e0e0e0;
font-size: 2em;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #d5d5d5;
}
#status {
margin-bottom: 20px;
font-size: 1.2em;
}
#restart-btn {
padding: 10px 20px;
font-size: 1em;
border: none;
background-color: #2196F3;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
#restart-btn:hover {
background-color: #1976D2;
}
</style>
</head>
<body>
<div id="game-container">
<div id="mode-selection">
<button id="single-player-btn">1 Player</button>
<button id="two-player-btn">2 Players</button>
</div>
<div id="status">Choose Game Mode</div>
<div id="board" style="display: none;"></div>
<button id="restart-btn" style="display: none;">Restart Game</button>
</div>
<script>
const singlePlayerBtn = document.getElementById('single-player-btn');
const twoPlayerBtn = document.getElementById('two-player-btn');
const board = document.getElementById('board');
const statusDiv = document.getElementById('status');
const restartBtn = document.getElementById('restart-btn');
let gameMode = null; // 'single' or 'two'
let boardState = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';
let gameActive = true;
singlePlayerBtn.addEventListener('click', () => startGame('single'));
twoPlayerBtn.addEventListener('click', () => startGame('two'));
restartBtn.addEventListener('click', () => resetGame());
function startGame(mode) {
gameMode = mode;
board.style.display = 'grid';
restartBtn.style.display = 'inline-block';
singlePlayerBtn.style.display = 'none';
twoPlayerBtn.style.display = 'none';
statusDiv.textContent = `Player ${currentPlayer}'s Turn`;
createBoard();
if (gameMode === 'single' && currentPlayer === 'O') {
computerMove();
}
}
function createBoard() {
board.innerHTML = '';
boardState = ['', '', '', '', '', '', '', '', ''];
for (let i = 0; i < 9; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.setAttribute('data-index', i);
cell.addEventListener('click', handleCellClick);
board.appendChild(cell);
}
gameActive = true;
currentPlayer = 'X';
}
function handleCellClick(e) {
const index = e.target.getAttribute('data-index');
if (boardState[index] !== '' || !gameActive) return;
boardState[index] = currentPlayer;
e.target.textContent = currentPlayer;
checkResult();
if (gameActive) {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
statusDiv.textContent = `Player ${currentPlayer}'s Turn`;
if (gameMode === 'single' && currentPlayer === 'O') {
setTimeout(computerMove, 500);
}
}
}
function computerMove() {
// Simple AI: choose a random available spot
const available = boardState.map((val, idx) => val === '' ? idx : null).filter(val => val !== null);
if (available.length === 0) return;
const move = available[Math.floor(Math.random() * available.length)];
boardState[move] = 'O';
document.querySelector(`.cell[data-index="${move}"]`).textContent = 'O';
checkResult();
if (gameActive) {
currentPlayer = 'X';
statusDiv.textContent = `Player ${currentPlayer}'s Turn`;
}
}
function checkResult() {
const winningConditions = [
[0,1,2],[3,4,5],[6,7,8], // rows
[0,3,6],[1,4,7],[2,5,8], // columns
[0,4,8],[2,4,6] // diagonals
];
for (let condition of winningConditions) {
const [a, b, c] = condition;
if (boardState[a] && boardState[a] === boardState[b] && boardState[a] === boardState[c]) {
statusDiv.textContent = `Player ${boardState[a]} Wins!`;
highlightCells(condition);
gameActive = false;
return;
}
}
if (!boardState.includes('')) {
statusDiv.textContent = `It's a Draw!`;
gameActive = false;
}
}
function highlightCells(condition) {
condition.forEach(index => {
document.querySelector(`.cell[data-index="${index}"]`).style.backgroundColor = '#90caf9';
});
}
function resetGame() {
gameMode = null;
board.style.display = 'none';
restartBtn.style.display = 'none';
singlePlayerBtn.style.display = 'inline-block';
twoPlayerBtn.style.display = 'inline-block';
statusDiv.textContent = 'Choose Game Mode';
board.innerHTML = '';
}
</script>
</body>
</html>
Feel free to customize and enhance the game as per your preferences. Enjoy playing Tic Tac Toe!