#๐ Need help with a minesweeper project
7 messages ยท Page 1 of 1 (latest)
@lethal seal
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
So for example, my code is supposed to return with this as a win condition:
4[ ][ ][ ][ ][ ]
3[ ][ ][ ][ ][ ]
2[ ][ ][ ][ ][ ]
1[2][2][1][ ][ ]
0[ ][1][0][ ][ ]
a b c d e
4[ ][ ][ ][ ][ ]
3[ ][3][1][0][ ]
2[ ][ ][1][1][ ]
1[2][2][1][2][ ]
0[ ][1][0][ ][ ]
a b c d e
4[ ][ ][ ][0][0]
3[ ][3][1][0][0]
2[ ][ ][1][1][ ]
1[2][2][1][2][ ]
0[ ][1][0][ ][ ]
a b c d e
4[ ][2][0][0][0]
3[ ][3][1][0][0]
2[ ][ ][1][1][ ]
1[2][2][1][2][ ]
0[ ][1][0][ ][ ]
a b c d e
4[ ][2][0][0][0]
3[ ][3][1][0][0]
2[ ][ ][1][1][1]
1[2][2][1][2][ ]
0[ ][1][0][ ][ ]
a b c d e
4[ ][2][0][0][0]
3[ ][3][1][0][0]
2[2][ ][1][1][1]
1[2][2][1][2][ ]
0[ ][1][0][ ][ ]
a b c d e
4[ ][2][0][0][0]
3[ ][3][1][0][0]
2[2][ ][1][1][1]
1[2][2][1][2][ ]
0[ ][1][0][2][ ]
a b c d e
However, what I get in a 'win' condition, would be this
4 [ ] [ ] [ ] [ ] [ ]
3 [ ] [ ] [ ] [ ] [ ]
2 [ ] [ ] [ ] [ ] [ ]
1 [1] [0] [0] [ ] [ ]
0 [1] [0] [0] [ ] [ ]
a b c d e
4 [ ] [ ] [ ] [ ] [ ]
3 [ ] [0] [0] [0] [ ]
2 [ ] [1] [0] [0] [ ]
1 [1] [0] [0] [0] [ ]
0 [1] [0] [0] [ ] [ ]
a b c d e
4 [ ] [ ] [ ] [0] [1]
3 [ ] [0] [0] [0] [1]
2 [ ] [1] [0] [0] [ ]
1 [1] [0] [0] [0] [ ]
0 [1] [0] [0] [ ] [ ]
a b c d e
4 [ ] [0] [0] [0] [1]
3 [ ] [0] [0] [0] [1]
2 [ ] [1] [0] [0] [ ]
1 [1] [0] [0] [0] [ ]
0 [1] [0] [0] [ ] [ ]
a b c d e
4 [ ] [0] [0] [0] [1]
3 [ ] [0] [0] [0] [1]
2 [ ] [1] [0] [0] [0]
1 [1] [0] [0] [0] [0]
0 [1] [0] [0] [ ] [ ]
a b c d e
4 [ ] [0] [0] [0] [1]
3 [0] [0] [0] [0] [1]
2 [0] [1] [0] [0] [0]
1 [1] [0] [0] [0] [0]
0 [1] [0] [0] [ ] [ ]
a b c d e
4 [ ] [0] [0] [0] [1]
3 [0] [0] [0] [0] [1]
2 [0] [1] [0] [0] [0]
1 [1] [0] [0] [0] [0]
0 [1] [0] [0] [0] [0]
a b c d e
And this is the code provided as well as the given main
def read_file(file_name):
"""
Reads the file and returns a 2D list representing the grid with 'X' and '0's.
"""
list = []
f = open(file_name, 'r')
w = int(f.readline())
h = int(f.readline())
for i in range(h):
line = f.readline().strip().split(',')
line = [char.strip() for char in line] # Remove extra spaces
list.append(line)
f.close()
return list
def make_empty_grid(grid):
"""
Returns an empty grid with the same dimensions as the input grid.
"""
grid_list = []
for r in grid:
row = [' '] * len(r)
grid_list.append(row)
return grid_list
def update_grid(grid):
"""
Modifies the grid by replacing '0's with the count of adjacent mines.
"""
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == '0':
number = 0
# Check adjacent squares
for x in range(max(0, i - 1), min(len(grid), i + 2)):
for y in range(max(0, j - 1), min(len(grid[i]), j + 2)):
if grid[x][y] == 'X':
number += 1
grid[i][j] = str(number)
def dig(answer_key, coordinate, user_view):
"""
Reveals the square at the given coordinate and updates the user view grid.
"""
# Convert coordinate to indices
x = ord(coordinate[0]) - 97 # Convert letter to column index
y = int(coordinate[1]) # Convert number to row index
# Update user view grid
user_view[y][x] = answer_key[y][x]
# Check adjacent squares and update user view
for i in range(max(0, y - 1), min(len(answer_key), y + 2)):
for j in range(max(0, x - 1), min(len(answer_key[i]), x + 2)):
if user_view[i][j] == ' ':
user_view[i][j] = answer_key[i][j]
There is a part two of this, but I cannot fill every message here
def count_total_moves(user_view):
"""
Counts the number of spots on the grid that have not been revealed by the player
and do not contain a mine.
"""
count = 0
for i in user_view:
for j in i:
if j == ' ':
count += 1
return count
def print_grid(grid):
"""
Prints the grid to the standard output.
"""
for i in range(len(grid) - 1, -1, -1):
row = f"{i} "
for j in range(len(grid[i])):
row += f"[{grid[i][j]}] "
print(row.rstrip())
print(" ", end="")
# Define a list of lowercase letters
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
# Map index to lowercase letter
for k in range(len(grid[0])):
letter = letters[k]
print(f" {letter} ", end="")
print()
def determine_game_status(grid, user_view):
"""
Determines the game status based on the grid and the user view.
Returns:
bool: True if the game should continue, False if the game is over.
"""
for i in range(len(grid)):
for j in range(len(grid[i])):
# Check if a mine has been revealed
if grid[i][j] == 'X' and user_view[i][j] != ' ':
return False
# Check if there are unrevealed spots without mines
if grid[i][j] != 'X' and user_view[i][j] == ' ':
return True
# If all non-mine spots have been revealed
return False
if __name__ == '__main__':
grid = read_file("fiveByFive.txt")
user_view = make_empty_grid(grid)
update_grid(grid)
moves = ["b0", "c2", "e4", "c4", "e2", "a2", "d0"]
index = 0
while determine_game_status(grid, user_view) and index < len(moves):
dig(grid, moves[index], user_view)
index += 1
print_grid(user_view)
And that is part 2
I've been modifying the update_grid to see if it would create any impact due to the int value representing it wrong on the cells but it caused a few problems
@lethal seal
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.