Skip to content

Commit

Permalink
Merge pull request #1063 from Aditi22Bansal/main
Browse files Browse the repository at this point in the history
Solved #1062- Hexquest game
  • Loading branch information
Durgesh4993 authored Jul 21, 2024
2 parents 7eb1bd7 + 32da065 commit 9575bee
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
18 changes: 18 additions & 0 deletions SinglePlayer - Games/HexQuest_game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HexQuest</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>HexQuest</h1>
<div id="game-board"></div>
<div id="info">
<p id="message"></p>
<button id="restart">Restart Game</button>
</div>
<script src="script.js"></script>
</body>
</html>
56 changes: 56 additions & 0 deletions SinglePlayer - Games/HexQuest_game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

document.addEventListener("DOMContentLoaded", () => {
const gameBoard = document.getElementById("game-board");
const message = document.getElementById("message");
const restartButton = document.getElementById("restart");
const rows = 10;
const cols = 10;
let characterPosition = 0;
let goalPosition = 99;

// Initialize game board
function initBoard() {
gameBoard.innerHTML = "";
characterPosition = 0;
goalPosition = 99;
for (let i = 0; i < rows * cols; i++) {
const hex = document.createElement("div");
hex.classList.add("hex");
if (i === characterPosition) hex.classList.add("character");
if (i === goalPosition) hex.classList.add("goal");
if (Math.random() < 0.1 && i !== characterPosition && i !== goalPosition) hex.classList.add("obstacle");
hex.addEventListener("click", () => moveCharacter(i));
gameBoard.appendChild(hex);
}
updateMessage("Find the goal!");
}

// Move character
function moveCharacter(position) {
const distance = Math.abs(position - characterPosition);
if (distance === 1 || distance === cols) {
const hexes = document.querySelectorAll(".hex");
hexes[characterPosition].classList.remove("character");
characterPosition = position;
hexes[characterPosition].classList.add("character");

if (characterPosition === goalPosition) {
updateMessage("You found the goal! Click Restart to play again.");
} else if (hexes[characterPosition].classList.contains("obstacle")) {
updateMessage("You hit an obstacle! Click Restart to try again.");
} else {
updateMessage("Keep going!");
}
}
}

// Update message
function updateMessage(text) {
message.textContent = text;
}

// Restart game
restartButton.addEventListener("click", initBoard);

initBoard();
});
57 changes: 57 additions & 0 deletions SinglePlayer - Games/HexQuest_game/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

h1 {
text-align: center;
margin-bottom: 20px;
}

#game-board {
display: grid;
grid-template-columns: repeat(10, 50px);
gap: 5px;
justify-content: center;
}

.hex {
width: 50px;
height: 50px;
background-color: #fff;
border: 1px solid #ccc;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
display: flex;
justify-content: center;
align-items: center;
font-size: 14px;
cursor: pointer;
}

.hex.character {
background-color: #4caf50;
}

.hex.obstacle {
background-color: #f44336;
}

.hex.goal {
background-color: #ffeb3b;
}

#info {
margin-top: 20px;
text-align: center;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

0 comments on commit 9575bee

Please sign in to comment.