Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Tic Tac Toe</h1>
<div id="game-board"></div>
<script src="script.js"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const server = require('./server.js');

const gameBoard = ['', '', '', '', '', '', '', '', ''];
let currentPlayer = 'X';

function checkWin() {
// Check rows
if (gameBoard[0] === currentPlayer && gameBoard[1] === currentPlayer && gameBoard[2] === currentPlayer) {
return true;
}
if (gameBoard[3] === currentPlayer && gameBoard[4] === currentPlayer && gameBoard[5] === currentPlayer) {
return true;
}
if (gameBoard[6] === currentPlayer && gameBoard[7] === currentPlayer && gameBoard[8] === currentPlayer) {
return true;
}

// Check columns
if (gameBoard[0] === currentPlayer && gameBoard[3] === currentPlayer && gameBoard[6] === currentPlayer) {
return true;
}
if (gameBoard[1] === currentPlayer && gameBoard[4] === currentPlayer && gameBoard[7] === currentPlayer) {
return true;
}
if (gameBoard[2] === currentPlayer && gameBoard[5] === currentPlayer && gameBoard[8] === currentPlayer) {
return true;
}

// Check diagonals
if (gameBoard[0] === currentPlayer && gameBoard[4] === currentPlayer && gameBoard[8] === currentPlayer) {
return true;
}
if (gameBoard[2] === currentPlayer && gameBoard[4] === currentPlayer && gameBoard[6] === currentPlayer) {
return true;
}

return false;
}

function resetGame() {
gameBoard.fill('');
currentPlayer = 'X';
}

server.start();
95 changes: 95 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIO(server);

const PORT = process.env.PORT || 3000;

let board = [
['', '', ''],
['', '', ''],
['', '', '']
];

let players = [];
let currentPlayer = null;

function checkWin() {
// Check rows
for (let i = 0; i < 3; i++) {
if (board[i][0] !== '' && board[i][0] === board[i][1] && board[i][0] === board[i][2]) {
return true;
}
}

// Check columns
for (let i = 0; i < 3; i++) {
if (board[0][i] !== '' && board[0][i] === board[1][i] && board[0][i] === board[2][i]) {
return true;
}
}

// Check diagonals
if (board[0][0] !== '' && board[0][0] === board[1][1] && board[0][0] === board[2][2]) {
return true;
}
if (board[0][2] !== '' && board[0][2] === board[1][1] && board[0][2] === board[2][0]) {
return true;
}

return false;
}

function resetGame() {
board = [
['', '', ''],
['', '', ''],
['', '', '']
];
players = [];
currentPlayer = null;
}

io.on('connection', (socket) => {
console.log('A user connected');

socket.on('join', (playerName) => {
if (players.length < 2) {
players.push(playerName);
socket.emit('playerJoined', playerName);
if (players.length === 2) {
currentPlayer = players[0];
io.emit('gameStart', currentPlayer);
}
} else {
socket.emit('gameFull');
}
});

socket.on('makeMove', (data) => {
const { row, col, player } = data;
if (player === currentPlayer && board[row][col] === '') {
board[row][col] = player;
io.emit('moveMade', { row, col, player });
if (checkWin()) {
io.emit('gameOver', currentPlayer);
resetGame();
} else {
currentPlayer = players.find(p => p !== currentPlayer);
io.emit('nextTurn', currentPlayer);
}
}
});

socket.on('disconnect', () => {
console.log('A user disconnected');
resetGame();
});
});

server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
30 changes: 30 additions & 0 deletions shared_dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Shared dependencies between the generated files:

1. index.html:
- style.css
- script.js

2. style.css:
- No shared dependencies

3. script.js:
- server.js
- gameBoard
- currentPlayer
- checkWin
- resetGame

4. server.js:
- script.js
- express
- http
- socket.io
- app
- server
- io
- PORT
- board
- players
- currentPlayer
- checkWin
- resetGame
65 changes: 65 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* style.css */

body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}

.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 10px;
background-color: #fff;
border-radius: 5px;
padding: 10px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.cell {
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
font-weight: bold;
color: #333;
background-color: #f2f2f2;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}

.cell:hover {
background-color: #e6e6e6;
}

.message {
margin-top: 20px;
font-size: 18px;
font-weight: bold;
color: #333;
}

.button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
font-weight: bold;
color: #fff;
background-color: #333;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}

.button:hover {
background-color: #555;
}