-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff6688e
commit 32da065
Showing
1 changed file
with
56 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,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(); | ||
}); |