Skip to content
Snippets Groups Projects
Commit 756d28b2 authored by Ulysse Durand's avatar Ulysse Durand
Browse files

bien avancé

parent b4d6515d
No related branches found
No related tags found
No related merge requests found
{"test2":1,"aaa":1,"bbb":2,"fff":1}
\ No newline at end of file
...@@ -32,11 +32,10 @@ ...@@ -32,11 +32,10 @@
const CELL_SIZE = 100; const CELL_SIZE = 100;
const RADIUS = CELL_SIZE / 2 - 10; const RADIUS = CELL_SIZE / 2 - 10;
// Initialize game board
function drawBoard(board) { function drawBoard(board) {
console.log(board);
// Draw Connect4 grid based on board state // Draw Connect4 grid based on board state
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'blue';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let row = 0; row < ROWS; row++) { for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) { for (let col = 0; col < COLS; col++) {
let x = col * CELL_SIZE + CELL_SIZE / 2; let x = col * CELL_SIZE + CELL_SIZE / 2;
...@@ -49,15 +48,15 @@ ...@@ -49,15 +48,15 @@
ctx.fill(); ctx.fill();
// Pion // Pion
if (board[row][col] !== null) {
ctx.beginPath(); ctx.beginPath();
ctx.arc(x, y, RADIUS, 0, Math.PI * 2); ctx.arc(x, y, RADIUS, 0, Math.PI * 2);
ctx.fillStyle = board[row][col] === 0 ? 'red' : 'yellow'; ctx.fillStyle = 'white';
if (board[row][col] === 0) {ctx.fillStyle='red';}
if (board[row][col] === 1) {ctx.fillStyle='yellow';}
ctx.fill(); ctx.fill();
} }
} }
} }
}
canvas.addEventListener('click', (e) => { canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect(); const rect = canvas.getBoundingClientRect();
...@@ -80,18 +79,18 @@ ...@@ -80,18 +79,18 @@
}); });
socket.on('stopGame', () => { socket.on('stopGame', ({board, winner}) => {
alert(winner === 0 ? 'Player 1 wins!' : 'Player 2 wins!');
const messages = document.getElementById('messages'); const messages = document.getElementById('messages');
messages.innerHTML += `<div><strong>Game Finish</strong></div>`; messages.innerHTML += `<div><strong>Game Finish</strong></div>`;
}); });
socket.on('updateGame', ({ board, currentPlayer, winner }) => { socket.on('updateGame', ({ board, currentPlayer}) => {
drawBoard(board); drawBoard(board);
if (winner !== null) alert(winner === 0 ? 'Player 1 wins!' : 'Player 2 wins!');
}); });
canvas.addEventListener('click', (e) => { canvas.addEventListener('click', (e) => {
const col = Math.floor(e.offsetX / 100); const col = Math.floor(e.offsetX / CELL_SIZE);
socket.emit('move', col); socket.emit('move', col);
}); });
...@@ -107,8 +106,6 @@ ...@@ -107,8 +106,6 @@
const messages = document.getElementById('messages'); const messages = document.getElementById('messages');
messages.innerHTML += `<div><strong>${name}:</strong> ${message}</div>`; messages.innerHTML += `<div><strong>${name}:</strong> ${message}</div>`;
}); });
drawBoard(); // initial
</script> </script>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -5,8 +5,6 @@ const { ...@@ -5,8 +5,6 @@ const {
} = require('uuid'); } = require('uuid');
const fs = require('fs'); const fs = require('fs');
const app = express(); const app = express();
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
...@@ -50,11 +48,12 @@ io.on('connection', (socket) => { ...@@ -50,11 +48,12 @@ io.on('connection', (socket) => {
let stopGame = (room, roomId) => { let stopGame = (room, roomId) => {
console.log("Not enough players in room " + roomId + ", game stops."); console.log("Not enough players in room " + roomId + ", game stops.");
room["gameState"] = Array(6).fill().map(() => Array(7).fill(null));
room["currentPlayer"] = 0;
}; };
let startGame = (room, roomId) => { let startGame = (room, roomId) => {
console.log("Two players on room " + roomId + ", game starting."); console.log("Two players on room " + roomId + ", game starting.");
console.log(room);
io.to(roomId).emit('startGame', { io.to(roomId).emit('startGame', {
board: room.gameState, board: room.gameState,
currentPlayer: room.currentPlayer currentPlayer: room.currentPlayer
...@@ -112,11 +111,12 @@ io.on('connection', (socket) => { ...@@ -112,11 +111,12 @@ io.on('connection', (socket) => {
return; return;
}; };
console.log("On room " + currentRoom + ", " + playerName + " played on column " + col);
const row = findAvailableRow(currentRoom.gameState, col); const row = findAvailableRow(currentRoom.gameState, col);
if (row === -1) return; if (row === -1) return;
console.log("On room " + currentRoom.id + ", " + playerName + " played on column " + col + " at height" + row);
currentRoom.gameState[row][col] = playerIndex; currentRoom.gameState[row][col] = playerIndex;
currentRoom.moves.push({ currentRoom.moves.push({
player: playerIndex, player: playerIndex,
...@@ -125,17 +125,24 @@ io.on('connection', (socket) => { ...@@ -125,17 +125,24 @@ io.on('connection', (socket) => {
const winner = checkWin(currentRoom.gameState, row, col); const winner = checkWin(currentRoom.gameState, row, col);
if (winner !== null) { if (winner !== null) {
console.log("In room "+currentRoom.id+", player "+playerName+" won");
hallOfFame[playerName] = (hallOfFame[playerName] || 0) + 1; hallOfFame[playerName] = (hallOfFame[playerName] || 0) + 1;
fs.writeFileSync('halloffame.json', JSON.stringify(hallOfFame)); fs.writeFileSync('halloffame.json', JSON.stringify(hallOfFame));
stopGame(currentRoom, currentRoom.id);
socket.join(currentRoom.id);
io.to(currentRoom.id).emit('stopGame', {
board: currentRoom.gameState,
winner: winner
});
} }
else {
currentRoom.currentPlayer = 1 - currentRoom.currentPlayer; currentRoom.currentPlayer = 1 - currentRoom.currentPlayer;
socket.join(currentRoom.id) socket.join(currentRoom.id);
io.to(currentRoom.id).emit('updateGame', { io.to(currentRoom.id).emit('updateGame', {
board: currentRoom.gameState, board: currentRoom.gameState,
currentPlayer: currentRoom.currentPlayer, currentPlayer: currentRoom.currentPlayer
winner
}); });
}
}); });
socket.on('chatMessage', (message) => { socket.on('chatMessage', (message) => {
...@@ -155,7 +162,7 @@ io.on('connection', (socket) => { ...@@ -155,7 +162,7 @@ io.on('connection', (socket) => {
function findAvailableRow(board, col) { function findAvailableRow(board, col) {
for (let row = 5; row >= 0; row--) { for (let row = 5; row >= 0; row--) {
if (!board[row][col]) return row; if (board[row][col] == null) return row;
} }
return -1; return -1;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment