-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5c059c
commit c53a94c
Showing
10 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,6 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- The `$t0-9` registers can be used to temporarily store values | ||
- The instructions specify which registers are used as input and output |
15 changes: 15 additions & 0 deletions
15
exercises/practice/minesweeper/.docs/instructions.append.md
This file contains 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,15 @@ | ||
# Instructions append | ||
|
||
## Minefield format | ||
|
||
The minefield is represented as a null-terminated string, with a newline character at the end of each row. | ||
|
||
An example would be `" \n * \n \n"` | ||
|
||
## Registers | ||
|
||
| Register | Usage | Type | Description | | ||
| -------- | ------------ | ------- | ----------------------------- | | ||
| `$a0` | input | address | null-terminated input string | | ||
| `$a1` | input/output | address | null-terminated output string | | ||
| `$t0-9` | temporary | any | for temporary storage | |
This file contains 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,26 @@ | ||
# Instructions | ||
|
||
Your task is to add the mine counts to empty squares in a completed Minesweeper board. | ||
The board itself is a rectangle composed of squares that are either empty (`' '`) or a mine (`'*'`). | ||
|
||
For each empty square, count the number of mines adjacent to it (horizontally, vertically, diagonally). | ||
If the empty square has no adjacent mines, leave it empty. | ||
Otherwise replace it with the adjacent mines count. | ||
|
||
For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen): | ||
|
||
```text | ||
·*·*· | ||
··*·· | ||
··*·· | ||
····· | ||
``` | ||
|
||
Which your code should transform into this: | ||
|
||
```text | ||
1*3*1 | ||
13*31 | ||
·2*2· | ||
·111· | ||
``` |
This file contains 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,5 @@ | ||
# Introduction | ||
|
||
[Minesweeper][wikipedia] is a popular game where the user has to find the mines using numeric hints that indicate how many mines are directly adjacent (horizontally, vertically, diagonally) to a square. | ||
|
||
[wikipedia]: https://en.wikipedia.org/wiki/Minesweeper_(video_game) |
This file contains 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,17 @@ | ||
{ | ||
"authors": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"impl.mips" | ||
], | ||
"test": [ | ||
"runner.mips" | ||
], | ||
"example": [ | ||
".meta/example.mips" | ||
] | ||
}, | ||
"blurb": "Add the numbers to a minesweeper board." | ||
} |
This file contains 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,110 @@ | ||
# | Register | Usage | Type | Description | | ||
# | -------- | ------------ | ------- | --------------------------------------------- | | ||
# | `$a0` | input | address | null-terminated input string | | ||
# | `$a1` | input/output | address | null-terminated output string | | ||
# | `$t0` | temporary | address | pointer into input | | ||
# | `$t1` | temporary | byte | input character | | ||
# | `$t2` | temporary | address | line length (including newline character) | | ||
# | `$t3` | temporary | address | location of input's null terminator | | ||
# | `$t4` | temporary | address | previous row (or current for first row) | | ||
# | `$t5` | temporary | address | current row | | ||
# | `$t6` | temporary | address | next row (or current for last row) | | ||
# | `$t7` | temporary | integer | previous column (or current for first column) | | ||
# | `$t8` | temporary | integer | current column | | ||
# | `$t9` | temporary | integer | next column (or current for last column) | | ||
# | `$a2` | temporary | address | row of adjacent square | | ||
# | `$a3` | temporary | integer | column of adjacent square | | ||
# | `$v0` | temporary | byte | newline character | | ||
# | `$v1` | temporary | integer | number of adjacent mines | | ||
# | `$s0` | temporary | byte | mine character '*' | | ||
|
||
.globl annotate | ||
|
||
annotate: | ||
move $t0, $a0 | ||
lb $t1, 0($t0) # read first byte of minefield | ||
beqz $t1, return | ||
|
||
subi $sp, $sp, 4 # preserve original $s0 value on stack | ||
sw $s0, 0($sp) | ||
li $s0, '*' # mine character | ||
li $v0, '\n' | ||
|
||
find_newline: | ||
lb $t1, 0($t0) | ||
addi $t0, $t0, 1 | ||
bne $t1, $v0, find_newline | ||
sub $t2, $t0, $a0 # line length (including newline character) | ||
move $t0, $a0 | ||
|
||
find_null: | ||
add $t0, $t0, $t2 # jump ahead by line length | ||
lb $t1, 0($t0) | ||
bnez $t1, find_null | ||
move $t3, $t0 # location of input's null terminator | ||
|
||
move $t5, $a0 | ||
move $t6, $a0 # start of first row | ||
|
||
next_row: | ||
move $t8, $zero # first column | ||
move $t9, $zero | ||
move $t4, $t5 # current row becomes previous row | ||
move $t5, $t6 # next row becomes current row | ||
|
||
add $t6, $t5, $t2 # next row | ||
bne $t6, $t3, first_column | ||
move $t6, $t5 # last row | ||
|
||
first_column: | ||
beq $t2, 1, write_newline # jump ahead if rows contain no squares | ||
|
||
next_column: | ||
move $t7, $t8 # current column becomes previous column | ||
move $t8, $t9 # next column becomes current column | ||
addi $t9, $t8, 2 | ||
sne $t9, $t9, $t2 # Set $t9 to 1 if there are more columns, otherwise 0 | ||
add $t9, $t8, $t9 # next column | ||
|
||
add $t0, $t5, $t8 # address of minefield square | ||
lb $t1, 0($t0) | ||
beq $t1, $s0, write_square # jump ahead if we have reached a mine | ||
|
||
move $v1, $zero # number of adjacent mines | ||
subu $a2, $t4, $t2 | ||
|
||
adjacent_row: | ||
addu $a2, $a2, $t2 # address of adjacent row: $t4, $t4+$t2, $t6 | ||
subiu $a3, $t7, 1 | ||
|
||
adjacent_column: | ||
addiu $a3, $a3, 1 # index of adjacent column: $t7, $t7+1, $t9 | ||
|
||
add $t0, $a2, $a3 # address of adjacent square | ||
lb $t1, 0($t0) | ||
seq $t0, $t1, $s0 # 1 if square contains mine, 0 otherwise | ||
add $v1, $v1, $t0 # update mine count | ||
|
||
bne $a3, $t9, adjacent_column | ||
bne $a2, $t6, adjacent_row | ||
|
||
li $t1, ' ' | ||
beqz $v1, write_square | ||
addi $t1, $v1, '0' # mine count, as ASCII digit | ||
|
||
write_square: | ||
sb $t1, 0($a1) | ||
addi $a1, $a1, 1 # increment output pointer | ||
bne $t8, $t9, next_column | ||
|
||
write_newline: | ||
sb $v0, 0($a1) # write '\n' | ||
addi $a1, $a1, 1 | ||
bne $t5, $t6, next_row | ||
|
||
lw $s0, 0($sp) # restore original $s0 value | ||
addi $sp, $sp, 4 | ||
|
||
return: | ||
sb $zero, 0($a1) # null terminator | ||
jr $ra |
This file contains 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,46 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[0c5ec4bd-dea7-4138-8651-1203e1cb9f44] | ||
description = "no rows" | ||
|
||
[650ac4c0-ad6b-4b41-acde-e4ea5852c3b8] | ||
description = "no columns" | ||
|
||
[6fbf8f6d-a03b-42c9-9a58-b489e9235478] | ||
description = "no mines" | ||
|
||
[61aff1c4-fb31-4078-acad-cd5f1e635655] | ||
description = "minefield with only mines" | ||
|
||
[84167147-c504-4896-85d7-246b01dea7c5] | ||
description = "mine surrounded by spaces" | ||
|
||
[cb878f35-43e3-4c9d-93d9-139012cccc4a] | ||
description = "space surrounded by mines" | ||
|
||
[7037f483-ddb4-4b35-b005-0d0f4ef4606f] | ||
description = "horizontal line" | ||
|
||
[e359820f-bb8b-4eda-8762-47b64dba30a6] | ||
description = "horizontal line, mines at edges" | ||
|
||
[c5198b50-804f-47e9-ae02-c3b42f7ce3ab] | ||
description = "vertical line" | ||
|
||
[0c79a64d-703d-4660-9e90-5adfa5408939] | ||
description = "vertical line, mines at edges" | ||
|
||
[4b098563-b7f3-401c-97c6-79dd1b708f34] | ||
description = "cross" | ||
|
||
[04a260f1-b40a-4e89-839e-8dd8525abe0e] | ||
description = "large minefield" |
This file contains 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 @@ | ||
# | Register | Usage | Type | Description | | ||
# | -------- | ------------ | ------- | ----------------------------- | | ||
# | `$a0` | input | address | null-terminated input string | | ||
# | `$a1` | input/output | address | null-terminated output string | | ||
# | `$t0-9` | temporary | any | for temporary storage | | ||
|
||
.globl annotate | ||
|
||
annotate: | ||
jr $ra |
Oops, something went wrong.