Skip to content
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

Add minesweeper exercise #126

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "8db919e4-52a1-44e4-b93a-02df342a4396",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
Expand Down
12 changes: 12 additions & 0 deletions exercises/practice/minesweeper/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

## Minefield format

The minefield is represented as a string, with a newline character at the end of each row.

An example would be `" \n * \n \n"`

## Reserved Memory

The buffer for the input string uses bytes 64-319 of linear memory.

The input string can be modified in place if desired.
26 changes: 26 additions & 0 deletions exercises/practice/minesweeper/.docs/instructions.md
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·
```
5 changes: 5 additions & 0 deletions exercises/practice/minesweeper/.docs/introduction.md
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)
18 changes: 18 additions & 0 deletions exercises/practice/minesweeper/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [
"*.spec.js"
],
"excludedFiles": [
"custom.spec.js"
],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
26 changes: 26 additions & 0 deletions exercises/practice/minesweeper/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"minesweeper.wat"
],
"test": [
"minesweeper.spec.js"
],
"example": [
".meta/proof.ci.wat"
],
"invalidator": [
"package.json"
]
},
"blurb": "Add the numbers to a minesweeper board.",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
116 changes: 116 additions & 0 deletions exercises/practice/minesweeper/.meta/proof.ci.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
(module
(memory (export "mem") 1)

(global $NEWLINE i32 (i32.const 10))

(global $MINE i32 (i32.const 42))

(global $ZERO i32 (i32.const 48))

;;
;; Adds numbers to a minesweeper board.
;;
;; @param {i32} offset - The offset of the input string in linear memory
;; @param {i32} length - The length of the input string in linear memory
;;
;; @returns {(i32,i32)} - The offset and length of the output string in linear memory
;;
(func (export "annotate") (param $offset i32) (param $length i32) (result i32 i32)

(local $stop i32)
(local $lineLength i32) ;; including newline character
(local $index i32)
(local $char i32)
(local $mineCount i32)

(local $previousRow i32)
(local $currentRow i32)
(local $nextRow i32)
(local $adjacentRow i32)

(local $previousColumn i32)
(local $currentColumn i32)
(local $nextColumn i32)
(local $adjacentColumn i32)

(if (i32.eq (local.get $length) (i32.const 0)) (then
;; no rows
(return (local.get $offset) (local.get $length))
))

(local.set $stop (i32.add (local.get $offset) (local.get $length)))
(local.set $index (local.get $offset))
(loop
(local.set $char (i32.load8_u (local.get $index)))
(local.set $index (i32.add (local.get $index) (i32.const 1)))
(br_if 0 (i32.ne (local.get $char) (global.get $NEWLINE)))
)
(local.set $lineLength (i32.sub (local.get $index) (local.get $offset)))
(if (i32.eq (local.get $lineLength) (i32.const 1)) (then
;; each row has newline only
(return (local.get $offset) (local.get $length))
))

(local.set $currentRow (local.get $offset))
(local.set $nextRow (local.get $offset)) ;; start of first row

(loop $eachCurrentRow
(local.set $previousRow (local.get $currentRow)) ;; current row becomes previous row
(local.set $currentRow (local.get $nextRow)) ;; next row becomes current row
(local.set $nextRow (i32.add (local.get $currentRow) (local.get $lineLength)))
(if (i32.eq (local.get $nextRow) (local.get $stop)) (then
(local.set $nextRow (local.get $currentRow)) ;; last row
))

(local.set $currentColumn (i32.const 0))
(local.set $nextColumn (i32.const 0)) ;; first column

(loop $eachCurrentColumn
(local.set $previousColumn (local.get $currentColumn)) ;; current column becomes previous column
(local.set $currentColumn (local.get $nextColumn)) ;; next column becomes current column
(if (i32.ne (i32.add (local.get $nextColumn) (i32.const 2))
(local.get $lineLength)) (then
(local.set $nextColumn (i32.add (local.get $currentColumn) (i32.const 1)))
))

(local.set $index (i32.add (local.get $currentRow) (local.get $currentColumn))) ;; address of minefield square
(local.set $char (i32.load8_u (local.get $index)))
(if (i32.ne (local.get $char) (global.get $MINE)) (then
(local.set $mineCount (i32.const 0)) ;; number of adjacent mines

(local.set $adjacentRow (i32.sub (local.get $previousRow) (local.get $lineLength)))
(loop $eachAdjacentRow
;; address of adjacent row: $previousRow, $previousRow+$lineLength, $nextRow
(local.set $adjacentRow (i32.add (local.get $adjacentRow) (local.get $lineLength)))

(local.set $adjacentColumn (i32.sub (local.get $previousColumn) (i32.const 1)))
(loop $eachAdjacentColumn
;; index of adjacent column in row: $previousColumn, $previousColumn+1, $nextColumn
(local.set $adjacentColumn (i32.add (local.get $adjacentColumn) (i32.const 1)))

;; content of adjacent square
(local.set $char (i32.load8_u (i32.add (local.get $adjacentRow)
(local.get $adjacentColumn))))
;; increment mineCount if square contains mine
(local.set $mineCount (i32.add (local.get $mineCount)
(i32.eq (local.get $char) (global.get $MINE))))
(br_if $eachAdjacentColumn (i32.ne (local.get $adjacentColumn) (local.get $nextColumn)))
)

(br_if $eachAdjacentRow (i32.ne (local.get $adjacentRow) (local.get $nextRow)))
)

(if (local.get $mineCount) (then
(i32.store8 (local.get $index) (i32.add (global.get $ZERO) (local.get $mineCount)))
))
))

(br_if $eachCurrentColumn (i32.ne (local.get $currentColumn) (local.get $nextColumn)))
)

(br_if $eachCurrentRow (i32.ne (local.get $currentRow) (local.get $nextRow)))
)

(return (local.get $offset) (local.get $length))
)
)
46 changes: 46 additions & 0 deletions exercises/practice/minesweeper/.meta/tests.toml
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"
1 change: 1 addition & 0 deletions exercises/practice/minesweeper/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/minesweeper/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/minesweeper/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
presets: ["@exercism/babel-preset-javascript"],
plugins: [],
};
Loading
Loading