-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1063 from Aditi22Bansal/main
Solved #1062- Hexquest game
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |