-
-
Notifications
You must be signed in to change notification settings - Fork 467
Add Minesweeper: A pretty Basic Minesweeper game written in Python fo... #505
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
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
Minesweeper/Readme.md
This file contains hidden or 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,10 @@ | ||
| ## minesweeper | ||
|
|
||
| A pretty Basic Minesweeper game written in Python for console. | ||
|
|
||
| This is a basic console version of Minesweeper in Python. | ||
| Features: | ||
| - 5x5 board with 3 random mines | ||
| - Player selects row/col | ||
| - Win if all safe cells are revealed | ||
| - Lose if a mine is hit |
63 changes: 63 additions & 0 deletions
Minesweeper/minesweeper.py
This file contains hidden or 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,63 @@ | ||
| import random | ||
|
|
||
| # Basic settings | ||
| rows = 5 | ||
| cols = 5 | ||
| mines = 3 | ||
|
|
||
| # Create hidden board | ||
| board = [[" " for _ in range(cols)] for _ in range(rows)] | ||
| visible = [["?" for _ in range(cols)] for _ in range(rows)] | ||
|
|
||
| # Place mines | ||
| placed_mines = 0 | ||
| while placed_mines < mines: | ||
| r = random.randint(0, rows - 1) | ||
| c = random.randint(0, cols - 1) | ||
| if board[r][c] != "X": | ||
| board[r][c] = "X" | ||
| placed_mines += 1 | ||
|
|
||
| # Count mines around a cell | ||
| def count_mines(r, c): | ||
| count = 0 | ||
| for i in range(r - 1, r + 2): | ||
| for j in range(c - 1, c + 2): | ||
| if 0 <= i < rows and 0 <= j < cols: | ||
| if board[i][j] == "X": | ||
| count += 1 | ||
| return count | ||
|
|
||
| # Fill board with numbers | ||
| for r in range(rows): | ||
| for c in range(cols): | ||
| if board[r][c] != "X": | ||
| board[r][c] = str(count_mines(r, c)) | ||
|
|
||
| # Show the visible board | ||
| def show(): | ||
| for row in visible: | ||
| print(" ".join(row)) | ||
| print() | ||
|
|
||
| # Game loop | ||
| safe_cells = rows * cols - mines | ||
| while True: | ||
| show() | ||
| r = int(input("Row (0-4): ")) | ||
| c = int(input("Col (0-4): ")) | ||
|
|
||
| if board[r][c] == "X": | ||
| print("💥 You hit a mine. Game Over!") | ||
| break | ||
| else: | ||
| visible[r][c] = board[r][c] | ||
| safe_cells -= 1 | ||
| if safe_cells == 0: | ||
| print("🏆 You won! No safe cells left.") | ||
| break | ||
|
|
||
| # Reveal the real board at the end | ||
| print("\nReal board:") | ||
| for row in board: | ||
| print(" ".join(row)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.