-
Notifications
You must be signed in to change notification settings - Fork 46
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 #209 from praveen-p09/main
Added minesweeper game+Bug fixes
- Loading branch information
Showing
9 changed files
with
171 additions
and
4 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
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
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,23 @@ | ||
# Minesweeper | ||
|
||
Minesweeper... yep, that old classic game you remember playing on good ol' Windows '95 back in the day (in fact it was implemented in Microsoft's software updates until Windows 8). Minesweeper has its origins in the earliest mainframe games of the 1960s and 1970s. The earliest ancestor of Minesweeper was Jerimac Ratliff's Cube. The basic gameplay style became a popular segment of the puzzle game genre during the 1980s. | ||
|
||
Brush up on your Minesweeper history [here](https://en.wikipedia.org/wiki/Minesweeper_(video_game)). | ||
|
||
Do you know what an actual Minesweeper is? I didn't either until I read [this](https://en.wikipedia.org/wiki/Minesweeper). | ||
|
||
### Basic Gameplay | ||
1. Select your difficulty level. | ||
* Easy = 9x9, 10 mines | ||
* Medium = 16x16, 40 mines | ||
* Hard = 30x30, 160 mines | ||
2. Click anywhere on the board to begin and start the timer. | ||
*The numbers depict how many mines are adjacent to any given cell. | ||
3. Use **"Shift + Click"** to add flags to a cell if you think it's a mine. | ||
|
||
|
||
### Winning/Losing | ||
* If you hit a mine... game over, homie. | ||
* Win by uncovering all the cells without mines! | ||
|
||
(get it... MINE... SWEEPER...?) |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Minesweeper</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
|
||
<div style="display:flex; justify-content: center; align-items: center; flex-direction: column;"> | ||
<h1>Minesweeper Game</h2> | ||
<div id="board"></div> | ||
</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,83 @@ | ||
const boardSize = 10; | ||
const totalMines = 20; | ||
let mines = []; | ||
let revealedCells = 0; | ||
|
||
function initializeBoard() { | ||
const board = document.getElementById('board'); | ||
for (let i = 0; i < boardSize * boardSize; i++) { | ||
const cell = document.createElement('div'); | ||
cell.classList.add('cell'); | ||
cell.setAttribute('data-index', i); | ||
cell.addEventListener('click', handleCellClick); | ||
board.appendChild(cell); | ||
} | ||
|
||
generateMines(); | ||
} | ||
|
||
function generateMines() { | ||
mines = Array.from({ length: totalMines }, () => Math.floor(Math.random() * (boardSize * boardSize))); | ||
console.log(mines); | ||
} | ||
|
||
function handleCellClick(event) { | ||
const clickedIndex = parseInt(event.target.getAttribute('data-index')); | ||
if (mines.includes(clickedIndex)) { | ||
revealMines(); | ||
alert('Game Over! You hit a mine.'); | ||
} else { | ||
revealCell(clickedIndex); | ||
if (revealedCells === boardSize * boardSize - totalMines) { | ||
alert('Congratulations! You won!'); | ||
} | ||
} | ||
} | ||
|
||
function revealMines() { | ||
mines.forEach(index => { | ||
const cell = document.querySelector(`.cell[data-index="${index}"]`); | ||
cell.classList.add('mine'); | ||
}); | ||
} | ||
|
||
function revealCell(index) { | ||
const cell = document.querySelector(`.cell[data-index="${index}"]`); | ||
if (!cell.classList.contains('revealed')) { | ||
cell.classList.add('revealed'); | ||
revealedCells++; | ||
|
||
const mineCount = countAdjacentMines(index); | ||
if (mineCount > 0) { | ||
cell.textContent = mineCount; | ||
} else { | ||
const neighbors = getNeighbors(index); | ||
neighbors.forEach(neighborIndex => revealCell(neighborIndex)); | ||
} | ||
} | ||
} | ||
|
||
function countAdjacentMines(index) { | ||
const neighbors = getNeighbors(index); | ||
return neighbors.filter(neighborIndex => mines.includes(neighborIndex)).length; | ||
} | ||
|
||
function getNeighbors(index) { | ||
const neighbors = []; | ||
const row = Math.floor(index / boardSize); | ||
const col = index % boardSize; | ||
|
||
for (let i = -1; i <= 1; i++) { | ||
for (let j = -1; j <= 1; j++) { | ||
const neighborRow = row + i; | ||
const neighborCol = col + j; | ||
if (neighborRow >= 0 && neighborRow < boardSize && neighborCol >= 0 && neighborCol < boardSize) { | ||
neighbors.push(neighborRow * boardSize + neighborCol); | ||
} | ||
} | ||
} | ||
|
||
return neighbors; | ||
} | ||
|
||
initializeBoard(); |
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,36 @@ | ||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
} | ||
|
||
#board { | ||
display: grid; | ||
gap: 2px; | ||
grid-template-columns: repeat(10, 30px); | ||
border: 1px solid #ccc; | ||
} | ||
|
||
.cell { | ||
width: 30px; | ||
height: 30px; | ||
background-color: #ddd; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
font-weight: bold; | ||
font-size: 14px; | ||
user-select: none; | ||
} | ||
|
||
.cell:hover { | ||
background-color: #eee; | ||
} | ||
|
||
.mine { | ||
background-color: #f00; | ||
color: #fff; | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
# Movie-App |
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ body{ | |
background-color: white; | ||
margin: 5px; | ||
padding: 5px; | ||
cursor: pointer; | ||
} | ||
|
||
#pokemon-info img{ | ||
|
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