Skip to content

Commit

Permalink
fix game winner and add tie result
Browse files Browse the repository at this point in the history
  • Loading branch information
nil1729 committed Sep 14, 2023
1 parent c1a9b6f commit 2671279
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
10 changes: 8 additions & 2 deletions frontend/src/components/GamePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,17 @@ const GamePage = ({
</Row>
)}
{current_game.playing &&
current_game.winner &&
(current_game.winner || current_game.tie) &&
current_game.users[client_id]?.user_type === "player" && (
<Row>
<div className="text-center mt-5">
<h3>Player {current_game.winner.user_icon} win the game</h3>
{current_game.winner ? (
<h3>Player {current_game.winner.user_icon} win the game</h3>
) : current_game.tie ? (
<h3>Match Tied</h3>
) : (
<h3>Something went wrong</h3>
)}
<br />
<Button
color="info"
Expand Down
73 changes: 59 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const GAMES = {
// board_state: [0, 0, 0, 0, 0, 0, 0, 0, 0]
// current_turn: user_object
// playing: false
// winner: null
// winner: null,
// tie: false
// }
};

Expand All @@ -32,65 +33,109 @@ const winner_check = (board_state) => {

if (Object.keys(board_state_map).length >= 4) {
if (
board_state_map[0] &&
board_state_map[1] &&
board_state_map[2] &&
board_state_map[0] === board_state_map[1] &&
board_state_map[1] === board_state_map[2] &&
board_state_map[2] === board_state_map[0]
)
) {
return user_icon_player[board_state_map[0]];
}

if (
board_state_map[0] &&
board_state_map[3] &&
board_state_map[6] &&
board_state_map[0] === board_state_map[3] &&
board_state_map[3] === board_state_map[6] &&
board_state_map[6] === board_state_map[0]
)
) {
return user_icon_player[board_state_map[0]];
}

if (
board_state_map[0] &&
board_state_map[4] &&
board_state_map[8] &&
board_state_map[0] === board_state_map[4] &&
board_state_map[4] === board_state_map[8] &&
board_state_map[8] === board_state_map[0]
)
) {
return user_icon_player[board_state_map[0]];
}

if (
board_state_map[1] &&
board_state_map[4] &&
board_state_map[7] &&
board_state_map[1] === board_state_map[4] &&
board_state_map[4] === board_state_map[7] &&
board_state_map[7] === board_state_map[1]
)
) {
return user_icon_player[board_state_map[1]];
}

if (
board_state_map[2] &&
board_state_map[4] &&
board_state_map[6] &&
board_state_map[2] === board_state_map[4] &&
board_state_map[4] === board_state_map[6] &&
board_state_map[6] === board_state_map[2]
)
) {
return user_icon_player[board_state_map[2]];
}

if (
board_state_map[2] &&
board_state_map[5] &&
board_state_map[8] &&
board_state_map[2] === board_state_map[5] &&
board_state_map[5] === board_state_map[8] &&
board_state_map[8] === board_state_map[2]
)
) {
return user_icon_player[board_state_map[2]];
}

if (
board_state_map[3] &&
board_state_map[4] &&
board_state_map[5] &&
board_state_map[3] === board_state_map[4] &&
board_state_map[4] === board_state_map[5] &&
board_state_map[5] === board_state_map[3]
)
) {
return user_icon_player[board_state_map[3]];
}

if (
board_state_map[6] &&
board_state_map[7] &&
board_state_map[8] &&
board_state_map[6] === board_state_map[7] &&
board_state_map[7] === board_state_map[8] &&
board_state_map[8] === board_state_map[6]
)
) {
return user_icon_player[board_state_map[6]];

return null;
}
} else return null;
};

const tie_check = (board_state) => {
let board_state_map = {};

board_state.forEach((item, index) => {
if (item !== 0) {
board_state_map[index] = item.user_icon;
}
});

if (Object.keys(board_state_map).length === 9) {
return true;
} else return false;
};

// Initialize App
const app = express();
const httpServer = createServer(app);
Expand Down Expand Up @@ -243,6 +288,7 @@ io.on("connection", (socket) => {

// Winner Check
game_object.winner = winner_check(game_object.board_state);
game_object.tie = tie_check(game_object.board_state);

io.to(game_id).emit("my_current_game", game_object);
});
Expand All @@ -266,6 +312,7 @@ io.on("connection", (socket) => {
game_object.playing = true;
game_object.current_turn = game_object.users[socket.id];
game_object.winner = null;
game_object.tie = false;
game_object.board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0];

io.to(game_id).emit("my_current_game", game_object);
Expand Down Expand Up @@ -299,9 +346,7 @@ io.on("connection", (socket) => {
delete GAMES[it].users[socket.id];

Object.keys(GAMES[it].users).forEach((user_socket_id) => {
socket
.to(user_socket_id)
.emit("my_current_game", JSON.stringify(GAMES[it]));
socket.to(user_socket_id).emit("my_current_game", GAMES[it]);
});
}

Expand Down

0 comments on commit 2671279

Please sign in to comment.