From 06c8a2dfcefeb01ec49aeea23024338654811f4d Mon Sep 17 00:00:00 2001 From: "e2b-for-github[bot]" <134465507+e2b-for-github[bot]@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:12:59 +0000 Subject: [PATCH 1/2] Initial commit From 47e58c84d6f24b376ce42ca73726796ed68e7022 Mon Sep 17 00:00:00 2001 From: "e2b-for-github[bot]" <337977+e2b-for-github[bot]@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:13:47 +0000 Subject: [PATCH 2/2] Add code based on the PR instructions --- README.md | 1 - index.html | 12 ++++++ script.js | 45 ++++++++++++++++++++ server.js | 95 ++++++++++++++++++++++++++++++++++++++++++ shared_dependencies.md | 30 +++++++++++++ style.css | 65 +++++++++++++++++++++++++++++ 6 files changed, 247 insertions(+), 1 deletion(-) delete mode 100644 README.md create mode 100755 index.html create mode 100755 script.js create mode 100755 server.js create mode 100755 shared_dependencies.md create mode 100755 style.css diff --git a/README.md b/README.md deleted file mode 100644 index adf7c2b..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# smolAI \ No newline at end of file diff --git a/index.html b/index.html new file mode 100755 index 0000000..3766876 --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + Tic Tac Toe + + + +

Tic Tac Toe

+
+ + + \ No newline at end of file diff --git a/script.js b/script.js new file mode 100755 index 0000000..d61a209 --- /dev/null +++ b/script.js @@ -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(); \ No newline at end of file diff --git a/server.js b/server.js new file mode 100755 index 0000000..5ac89e5 --- /dev/null +++ b/server.js @@ -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}`); +}); \ No newline at end of file diff --git a/shared_dependencies.md b/shared_dependencies.md new file mode 100755 index 0000000..a186cc5 --- /dev/null +++ b/shared_dependencies.md @@ -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 \ No newline at end of file diff --git a/style.css b/style.css new file mode 100755 index 0000000..9fab260 --- /dev/null +++ b/style.css @@ -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; +} \ No newline at end of file