Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minesweeper game+Bug fixes #209

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Age-calculator/age_cal.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Age calculator</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="age_cal.css">
</head>
<body>
<div class="container">
Expand All @@ -17,6 +17,6 @@ <h1>Simple Javascript <br><span> age calculator</span></h1>
<p id="result"></p>
</div>
</div>
<script src="script.js"></script>
<script src="age_cal.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion Dice-game/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ document.querySelectorAll("img")[1].setAttribute("src", randomImageSource2);

//If player 1 wins
if (randomNumber1 > randomNumber2) {
document.querySelector("h1").innerHTML = "🚩 Play 1 Wins!";
document.querySelector("h1").innerHTML = "🚩 Player 1 Wins!";
}
else if (randomNumber2 > randomNumber1) {
document.querySelector("h1").innerHTML = "Player 2 Wins! 🚩";
Expand Down
23 changes: 23 additions & 0 deletions Minesweeper/Readme.md
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...?)
19 changes: 19 additions & 0 deletions Minesweeper/index.html
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>
83 changes: 83 additions & 0 deletions Minesweeper/script.js
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();
36 changes: 36 additions & 0 deletions Minesweeper/style.css
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;
}
2 changes: 1 addition & 1 deletion MovieApp/Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Movie-App
1 change: 1 addition & 0 deletions PokeDex/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ body{
background-color: white;
margin: 5px;
padding: 5px;
cursor: pointer;
}

#pokemon-info img{
Expand Down
5 changes: 5 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ const projects = [
discription: "This project can calculate factorial of the very large numbers.",
link: "Factorial-Calculator/index.html",
image: "https://github.com/shrey141102/Javascript-projects/assets/114330097/1c3e626e-40b3-4ee3-9f0c-ab92263d39c3"
},
{ title: "Minesweeper",
discription: "Simple minesweeper game made using HTML, CSS and JS.",
link: "Minesweeper/index.html",
image: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBIREhISEhEYGBISGhgaGBgYGBgYGBESGRwZHBgYGBgdIS4mHR4rIRgYJjgmKy8xNTY1HCU7QDtAPy40NTEBDAwMEA8QGBIRGDEdGCE0MTE0MTE0MTQxNDQxMTE0NDE0MTExNDQ0NDQxMTQ0MT8xNDExNDExMT80NDQ0NDE/Mf/AABEIAM8A9AMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAAAQIDBQYHBAj/xABAEAACAQIEBAMFBgUCBAcAAAABAgADEQQSITEFBkFRE2FxByIygZEUQlJicqEjM4KSwbGyU9Hw8RU0Q5OiwtL/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/xAAaEQEBAQADAQAAAAAAAAAAAAAAARExQVEC/9oADAMBAAIRAxEAPwDssREBERAREQEREBERAREQEREBERASDJkQEiIgJSYJkEyoGQZBMgmAMoMkmUkyiDKTJMgmUQZBgmRARIvEDJxETCkREBERAREQERLVGqrjMrBluRcEEXBsRcdQQRAuxEQERIgYTivHkw+JwtBgLVy2Y9aY2Q+hYEfKZucd5w4ga2Jq1E1yEZAdiKZ93X8LWv8A1GdI5Y4suKw6OGuQFBJ3ZWVWRjpuUZSbaBsw6S4jNSImA5l42MMmRD/Gce71yKdMxH1sD28oVnFqA3sQcpsbEGzaGx7GxH1k3mv8o+7RdSbsWDm+pu6qSSetyCfmZniYQJkEwTIJgCZSTBMpJlAmUkwTIJlAmUkwTIJgJSYMQESIgZWIiYUiIgIiICIkQJnM+I4+rw/H18jEI75yu6tnAckr11YjodDYidLnO/aNhrVqVS38xCvzptf9xUH9ssSts4Nx+niQo0Vz0vdW/Q3XcaGx8pm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NNBbO7flGg8zYDUzYeP06i1qlCihas9V6VJFtdmzMB5AAAkk6ADWdM5G5Up8Mw+T4sRVsa1T8bdFBOuRbm3zPWatRpnEPZh9mRDh+I10RnValwLKXGUPlQr9/IDfYG9/d15tzBhK2Gr1KFSs7lDuS3vA7GxY2n0/XorUR6bC6OCrA9VYWI+hnDfarw1kelVbVxek7fjZQGRz0uysG02uR0kGrcn8F+347D4Y38N2zVCNCKSDM+vS4GW/dhPoLg/KHD8GQ1DCU1cbOQXqD0d7sPkZoPsO4Rpicaw3IoofIWeoR5ElB/SZ1yRSa5xfllKovTAUjULsoYbFD907aWKnXQElpscQPmXmjlavw9yHUtSJ918uWxOyuoJCt21Kn7pOtu5ezjH/AGjheDfqieG1970iUufUKD85neIcPpYmm1OtTV0YEEEA6HeYnlHl3/w1K1BHzUGqF6YN81NWChkJ6gFbg/mMDYYiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiBgeG8u06OKr4tverVWbIbfyabEFgvmx1J7ADob56IgJontX4T42Aq1FW70grG2pshJB+hdf6vKb3KWUEEEXB3B1BEDD8ocJ+w4HDYe3vogL+dVrs5/uYzNREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBEiLwJiReLwJiReLwJiReLwJiReLwJiReLwJiReLwJiReLwJiReLwJiREBERA//9k="
}
];

Expand Down