Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP1521 21T3 — Assignment 1: minesweeper, MIPS minesweeper

Assignment 1: minesweeper, MIPS minesweeper
version: 1.6 last updated: 2021-10-18 20:00:00
Aims
to give you experience writing MIPS assembly code
to give you experience with data and control structures in MIPS
Getting Started
Create a new directory for this assignment called minesweeper, change to this directory, and fetch the provided code by running these
commands:
$ mkdir minesweeper
$ cd minesweeper
$ 1521 fetch minesweeper
If you're not working at CSE, you can download the provided files as a zip file or a tar file.
This will add the following files into the directory:
grid.h: some grid related constants in C, such as its size.
grid.s: the MIPS version of grid.h.
beginner.[hs]: more constants in C and MIPS.
intermediate.[hs]: more constants in C and MIPS.
expert.[hs]: more constants in C and MIPS.
minesweeper.c: a reference implementation of Minesweeper in C.
minesweeper.s: a stub assembly file to complete.
Minesweeper
How to play Minesweeper
minesweeper.c is an implementation of the classic game Minesweeper. The objective of the game is to clear a board that contains hidden
"mines" or bombs without detonating any of them. To do this, you will need to use the clues on the board, which indicate how many
bombs are in the adjacent cells.
At any point, you can perform 2 actions:
Marking a cell - marking or flagging a cell that you think might be a bomb,
Revealing a cell - revealing the cell. If it is an empty cell, then all of the surrounding empty cells will also be revealed. Revealing a
cell containing a bomb is game over.
Once you have revealed all cells that do not contain a bomb, you win the game.
Before starting the assignment, it is recommended that you understand how to play Minesweeper. A good place to start is Google's
built-in minesweeper game that can be found here.
minesweeper.c
The assignment version of Minesweeper is played on a grid represented by a 2D array of 8-bit ints (int8_t **grid). Each element in the
2D array represents a cell on the grid. For each element in the 2D array, 7 of the 8 bits are used to represent information regarding that
cell, as shown in the diagram below.
is_marked - 1 if cell is marked, 0 if not.
is_revealed - 1 if cell is revealed, 0 if not.
is_bomb - 1 if cell is a bomb, 0 if not.
value - number of bombs in adjacent cells, including diagonally adjacent cells. This will hold the value of 0 - 8 as there are a total
of 8 adjacent cells. The value 0 represents an empty cell.
You can use bitwise operations to extract or set relevant bits. The relevant masks are #defined at the top of the minesweeper.c file.
By default, the game is played on a 10x10 grid. However, you can change these settings to play on a different sized grid by changing the
#include provided. As a brief guidline, the standard levels of Minesweeper are:
Beginner: 9 x 9 grid, with max 10 bombs.
Intermediate: 16 x 16 grid, with max 40 bombs.
Expert: 16 x 30 grid, with max 90 bombs.
The assignment Minesweeper runs on an infinite loop, which means that you can play multiple rounds of Minesweeper. For each round,
your score will be calculated based on how many cells are left to be revealed. Once you have won or lost a game, you will be prompted
for input on whether you would like to play again, or if you would like to print out the user scores so far. The assignment
implementation keeps track of the last 10 rounds of Minesweeper.
Running minesweeper.c
First compile and run minesweeper.c by running these commands:
$ dcc -o minesweeper minesweeper.c
$ ./minesweeper
Once you run the program, you will be prompted for some user input.
Number of bombs - number of bombs on the grid. Input should be an integer from 1 to 91 inclusive.
Seed - number used to generate bombs on the grid. Different seeds will generate different grids.
Debug mode - allows the entire grid to be immediately revealed. Input should be 0 or 1.
Username - any string you would like to use to identify yourself. This will be used to keep track of your score, and the running high
score.
NOTE:
You cannot win a game in debug mode as all the cells are immediately revealed! Debug mode is only useful to make sure that
your board contains the correct values when given a particular seed.
As an example:
How many bombs on the grid? 10
Seed: 2
Debug Mode: 0
Enter your user name: selina
Reveal Any Cell to Begin...:
Total Bomb Count: 10
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
What's your first move? (action row col)
If debug mode is 1, then the output will look like this:
How many bombs on the grid? 20
Seed: 2
Debug Mode: 1
Enter your user name: selina
Reveal Any Cell to Begin...:
Total Bomb Count: 20
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
What's your first move? (action row col)
IMPORTANT: Note that the grid is initially always completely empty (all zeroes). This is done as the bombs on the grid are only
generated after you reveal the first cell. This is so that your first input will never cause you to immediately lose.
Playing minesweeper.c
To play the game you need to provide input in the form of action row col, where
action - either 0 for marking, 1 for revealing or -1 to exit the program.
row - row of cell you want to perform action on. This is zero indexed.
col - column of cell you want to perform action on. This is zero indexed.
As an example, providing the input
1
4
4
will reveal the cell at (4, 4). This input will generate the board, which will look something like this (note that this example follows on
from the above example, i.e. the seed used is 2):
Total Bomb Count: 10
0 0 0 0 1 - - - - -
0 0 0 0 1 - - - - -
0 0 0 0 1 1 2 - - -
0 0 0 0 0 0 2 - - -
0 0 0 0 0 0 2 - - -
0 0 0 1 1 1 2 - - -
0 0 1 2 - - - - - -
0 0 1 - 2 1 1 - - -
0 0 1 1 1 0 1 - - -
0 0 0 0 0 0 1 - - -
What's your next move? (action row col)
To mark a cell, you would do something like this:
0
7
3
This marks the cell at (7, 3) by placing an X on the cell. Marking a bomb decreases the total bomb count by 1. The total bomb count
can be negative if your number of marked cells is larger than the total number of bombs on the grid. Note that you can unmark a cell by
providing the same input again. This will increase the total bomb count.
What's your next move? (action row col)
0
7
3
Total Bomb Count: 9
0 0 0 0 1 - - - - -
0 0 0 0 1 - - - - -
0 0 0 0 1 1 2 - - -
0 0 0 0 0 0 2 - - -
0 0 0 0 0 0 2 - - -
0 0 0 1 1 1 2 - - -
0 0 1 2 - - - - - -
0 0 1 X 2 1 1 - - -
0 0 1 1 1 0 1 - - -
0 0 0 0 0 0 1 - - -
If you reveal a bomb, you will lose. If you reveal all cells that are not bombs, you win the game. Either way, your score will be calculated
for you, the high score printed out, and you will be asked whether you want to start a new game.
Boom! you lost :(
Your score was: 72 (28 cells remaining)
The highscore is now: 72 by you (selina)
New Game? (no: 0, yes: 1, scores: 2)
To keep playing, enter 1. To stop, enter 0. To print out the last 10 scores, enter 2. Printing out the scores will look something like this:
New Game? (no: 0, yes: 1, scores: 2)
2
-------------SCORES-----------
------------------------------
* USERNAME: selina
* SCORE: 63
------------------------------
* USERNAME: natalie
* SCORE: 54
------------------------------
To get a feel of how the assignment should work, try minesweeper.c on a terminal. You should also read through minesweeper.c before
starting the assignment to help you understand what the program is doing.
minesweeper.s: The Assignment
Your task in this assignment is to complete minesweeper.s in MIPS.
You have been provided with some assembly and some helpful information in minesweeper.s and some grid related constants in grid.s.
In order to change the grid size, you can change the constants N_ROWS, N_COLS and MAX_BOMBS that are defined in grid.s. You can also
change the file you're running the assignment with to the provided files beginner.s, intermediate.s or expert.s, which simulate
different levels of minesweeper.
Read through the provided code carefully, then add MIPS assembly so it executes exactly the same as minesweeper.c. To run your
assignment, run this command:
$ 1521 mipsy grid.s minesweeper.s
or to use the beginner.s file,
$ 1521 mipsy beginner.s minesweeper.s
If you're running your assignment with spim instead, then run:
$ cat grid.s minesweeper.s > game.s
$ 1521 spim -f game.s
or to use the beginner.s file,
$ cat beginner.s minesweeper.s > game.s
$ 1521 spim -f game.s
A handful of utility functions have already been completed in MIPS assembly for you. You only have to implement the following
unfinished functions in MIPS assembly.
NOTE:
If used mistakenly, cat may overwrite your file. Make sure to submit intermediate versions of your assignment so that your
files can be easily recovered if this happens. You can do this by copying your work to your CSE account and submitting it using
es ca be eas y eco e ed t s appe s. ou ca do t s by copy g you o to you CS accou t a d sub tt g t us g
the give command (check the Submission section). Only your final submission will be marked.
Subset 0 - Revealing the grid
For this subset, you will need to add code to complete the reveal_grid function in minesweeper.s. This function allows you to
immediately reveal all cells on the grid if debug mode is 1.
Remember that the grid is always initially empty and only gets filled up after you input your first reveal action. This is done so that the
first input never kills the player. This means that the grid will be filled with zeroes for this subset.
On completion of this subset, your program should match these behaviours:
How many bombs on the grid? 10
Seed: 2
Debug Mode: 1
Enter your user name: selina
Reveal Any Cell to Begin...:
Total Bomb Count: 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
What's your first move? (action row col)
Subset 1 - Placing bombs
For this subset, you will need to complete the place_bombs function in minesweeper.s. This function will place the bombs on the grid.
Different seed inputs will place bombs in different cells.
On completion of this subset, bombs (denoted by *) should be placed on the grid. Bombs are only placed on the grid after the user's
first reveal input. To check whether your bombs have been placed in the correct cells, set debug mode to be 1 so that all the cells of the
grid are revealed. On completion of this subset, your program should behave as such:
How many bombs on the grid? 20
Seed: 2
Debug Mode: 1
Enter your user name: natalie
Reveal Any Cell to Begin...:
Total Bomb Count: 20
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
What's your first move? (action row col)
1
2
3
Total Bomb Count: 20
0 0 1 * 2 1 1 1 1 1
0 0 1 1 2 * 2 3 * 2
0 0 0 0 1 1 2 * * 2
0 0 0 0 0 0 2 3 4 2
0 0 1 1 2 1 3 * 3 *
0 0 1 * 4 * 4 * 3 1
0 1 3 4 * * 3 1 1 0
0 1 * * 5 3 3 2 2 1
0 1 3 * 3 * 3 * * 2
0 0 1 1 2 1 3 * 4 *
What's your next move? (action row col)
HINT:
Make sure to follow correct MIPS calling conventions, which will most likely include using $s registers.
Subset 2 - Marking a cell
For this subset, you will need to complete the mark_cell function in minesweeper.s. This will allow you to mark and unmark cells on the
grid. mark_cell takes in two arguments: row and col, which are the row and column of the cell to mark.
Once you have completed this subset, inputting
0
row
col
should mark or unmark the cell at row, col. For example, to mark the cell at (9, 9), you would do:
What's your next move? (action row col)
0
9
9
Total Bomb Count: 9
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - X
To unmark the cell at (9,9), provide the same input again.
What's your next move? (action row col)
0
9
9
Total Bomb Count: 10
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -