Skip to content
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 @@ -642,6 +642,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "fe341c5e-0292-455f-93be-1ffb8f0b4b3b",
"practices": [],
"prerequisites": [],
"difficulty": 8
}
]
},
Expand Down
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)
17 changes: 17 additions & 0 deletions exercises/practice/minesweeper/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"minesweeper.coffee"
],
"test": [
"minesweeper.spec.coffee"
],
"example": [
".meta/example.coffee"
]
},
"blurb": "Add the numbers to a minesweeper board."
}
25 changes: 25 additions & 0 deletions exercises/practice/minesweeper/.meta/example.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Minesweeper
@annotate: (minefield) ->
if minefield.length < 1
return minefield
if minefield[0].length < 1
return minefield

board = minefield.map (row) -> row.split ''

board.map (row, x) ->
r = row.map (cell, y) ->
if cell == '*'
return cell
count = 0
for i in [-1..1]
for j in [-1..1]
if i + x >= 0 && i + x < board.length && j + y >= 0 && j + y < row.length
count += 1 if board[i + x][j + y] == '*'
if count == 0
' '
else
count.toString()
r.join ''

module.exports = Minesweeper
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"
4 changes: 4 additions & 0 deletions exercises/practice/minesweeper/minesweeper.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Minesweeper
@annotate: (minefield) ->

module.exports = Minesweeper
132 changes: 132 additions & 0 deletions exercises/practice/minesweeper/minesweeper.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
Minesweeper = require './minesweeper'

describe 'Minesweeper', ->
it 'no rows', ->
minefield = []
expect(Minesweeper.annotate minefield).toEqual []

xit 'no columns', ->
minefield = ['']
expect(Minesweeper.annotate minefield).toEqual ['']

xit 'no mines', ->
minefield = [
' '
' '
' '
]
expect(Minesweeper.annotate minefield).toEqual [
' '
' '
' '
]

xit 'minefield with only mines', ->
minefield = [
'***'
'***'
'***'
]
expect(Minesweeper.annotate minefield).toEqual [
'***'
'***'
'***'
]

xit 'mine surrounded by spaces', ->
minefield = [
' '
' * '
' '
]
expect(Minesweeper.annotate minefield).toEqual [
'111'
'1*1'
'111'
]

xit 'space surrounded by mines', ->
minefield = [
'***'
'* *'
'***'
]
expect(Minesweeper.annotate minefield).toEqual [
'***'
'*8*'
'***'
]

xit 'horizontal line', ->
minefield = [' * * ']
expect(Minesweeper.annotate minefield).toEqual ['1*2*1']

xit 'horizontal line, mines at edges', ->
minefield = ['* *']
expect(Minesweeper.annotate minefield).toEqual ['*1 1*']

xit 'vertical line', ->
minefield = [
' '
'*'
' '
'*'
' '
]
expect(Minesweeper.annotate minefield).toEqual [
'1'
'*'
'2'
'*'
'1'
]

xit 'vertical line, mines at edges', ->
minefield = [
'*'
' '
' '
' '
'*'
]
expect(Minesweeper.annotate minefield).toEqual [
'*'
'1'
' '
'1'
'*'
]

xit 'cross', ->
minefield = [
' * '
' * '
'*****'
' * '
' * '
]
expect(Minesweeper.annotate minefield).toEqual [
' 2*2 '
'25*52'
'*****'
'25*52'
' 2*2 '
]

xit 'large minefield', ->
minefield = [
' * * '
' * '
' * '
' * *'
' * * '
' '
]
expect(Minesweeper.annotate minefield).toEqual [
'1*22*1'
'12*322'
' 123*2'
'112*4*'
'1*22*2'
'111111'
]