Skip to content

Create Minesweeper game using python #217

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions M/Mine_Sweaper_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import random

# Define the game board
board_size = 10
num_mines = 10
board = [[0 for _ in range(board_size)] for _ in range(board_size)]

# Place the mines randomly on the board
mines = random.sample(range(board_size * board_size), num_mines)
for mine in mines:
row = mine // board_size
col = mine % board_size
board[row][col] = -1

# Calculate the number of adjacent mines for each cell
for row in range(board_size):
for col in range(board_size):
if board[row][col] == -1:
continue
for i in range(max(0, row - 1), min(board_size, row + 2)):
for j in range(max(0, col - 1), min(board_size, col + 2)):
if board[i][j] == -1:
board[row][col] += 1

# Define the game loop
while True:
# Display the current board
for row in board:
print(' '.join(str(cell) for cell in row))

# Prompt the player for input
row = int(input('Enter row: '))
col = int(input('Enter column: '))

# Check if the player hit a mine
if board[row][col] == -1:
print('Game over!')
break

# Reveal the cell and check if the player won
board[row][col] = -2
if all(all(cell in (-1, -2) for cell in row) for row in board):
print('You win!')
break
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ We appreciate the contributions from the following community members:
- [Ezhill Ragesh](https://github.com/ezhillragesh)
- [Brunda Bharadwaj](https://github.com/brundabharadwaj/)
- [Hemanth Singh](https://github.com/Hemanth11011)

- [Udeesha Rukshan](https://github.com/UdeeshaRukshan)
---

Happy coding! 🚀