Minesweeper Counter
Minesweeper is a popular single-player puzzle game. It is played on a 2D grid. When the player clicks on a square, it is revealed, and one of three possible outcomes occurs:
- The square is a mine. If this happens, the game ends.
- The square is not a mine, and has no mines next to it. If this happens, the game continues.
- The square is not a mine, but it has a mine in one of its adjacent 8 cells. If this happens, a number is displayed to the user indicating how many mines are adjacent to the square.
You will implement the logic for the last option: given a 2D grid of mine locations, and a target cell, return how many mines are adjacent to the given cell. You will also need to write a function to load a grid of mine locations from a file.
Assignment
Write a program that asks the user for the name of a file containing the mine locations, and a cell to query. The cell to query should take the form of two numbers, which are the indices into the grid. The program should print the number of mines adjacent to that cell (or the string "Boom!" if the cell is a mine).
This is the first assignment in which I will not tell you what functions to write---although there is no requirement to structure your program in terms of functions, I still strongly recommend that you do so. Figure out the large units of work that your program does (that could possibly be reused) and then figure out how break the program down into functions. I will suggest having at least one function to read in the file, and one function to count the number of adjacent mines.
Once you have written your program, make sure to test it! I have included two files for test cases at the end of this assignment.
File Format
The file that you are passed will have a single line at the top containing a number, $N$. This will then be followed
by $N$ lines, each with $N$ characters on it (forming an $N \times N$ grid). Each character will be either an
x
or an o
, with x
indicating a mine.
Here is an example file for a $4 \times 4$ grid:
4
x o o o
o o o o
o x x x
x x x x
(0,0)
will be the top-left of this grid.
You may assume that the file format is always correct.
File reading might not quite work the way you expect. The string methods strip()
and
split()
may be helpful, though they are not required to get a working program.
Examples
Examples using the above data in the file "mines.txt":
$ python3 Minesweeper.py
Name of mine file: mines.txt
Location to sweep: 0 0
Boom!
$ python3 Minesweeper.py
Name of mine file: mines.txt
Location to sweep: 0 1
1 mines adjacent to this location
$ python3 Minesweeper.py
Name of mine file: mines.txt
Location to sweep: 2 0
3 mines adjacent to this location
$ python3 Minesweeper.py
Name of mine file: mines.txt
Location to sweep: 4 2
This location does not exist!
$ python3 Minesweeper.py
Name of mine file: mynes.txt
This file does not exist!
Insight
How many squares do you have to check for a mine? What if you had to check all squares within two squares of the center? Three?
Is there a pattern or formula for the number of squares you need to check, as a function of the distance from the center? (Call it $k$).
If you can't think of a formula, that's fine, just note how many squares you would need to check for $k=1$ (the case you programmed in this assignment), $k=2$, and $k=3$.
Submission
Submit a single file named Minesweeper.py
on Canvas. Your file needs to compile and run. It should also
have a header with the following information (this goes in your source file, not in the program output):
# File: Minesweeper.py
# Student:
# Course: Intro to Programming
#
# Date:
# Description of Program:
# Pattern of number of squares that need to be checked:
The description should be a short (1-3 sentence) description of what the program does. Do not describe how it's written!
Test Case
In mines file:
8
x o o o o x x o
o x o o o o o o
x x x o o o o o
x x x o o o x o
x x x o o o x o
x x x o o o o o
x o x o o o x x
x x x o o o x o
Responses for cells:
0 0
: Boom!
1 0
: 4
3 7
: 2
0 7
: 1
7 7
: 3
6 0
: Boom!
6 1
: 8
5 3
: 3