Skip to content

Commit fbc6fe9

Browse files
authored
Add minesweeper (#323)
1 parent 6701212 commit fbc6fe9

File tree

8 files changed

+263
-0
lines changed

8 files changed

+263
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@
642642
"practices": [],
643643
"prerequisites": [],
644644
"difficulty": 2
645+
},
646+
{
647+
"slug": "minesweeper",
648+
"name": "Minesweeper",
649+
"uuid": "fe341c5e-0292-455f-93be-1ffb8f0b4b3b",
650+
"practices": [],
651+
"prerequisites": [],
652+
"difficulty": 8
645653
}
646654
]
647655
},
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Instructions
2+
3+
Your task is to add the mine counts to empty squares in a completed Minesweeper board.
4+
The board itself is a rectangle composed of squares that are either empty (`' '`) or a mine (`'*'`).
5+
6+
For each empty square, count the number of mines adjacent to it (horizontally, vertically, diagonally).
7+
If the empty square has no adjacent mines, leave it empty.
8+
Otherwise replace it with the adjacent mines count.
9+
10+
For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):
11+
12+
```text
13+
·*·*·
14+
··*··
15+
··*··
16+
·····
17+
```
18+
19+
Which your code should transform into this:
20+
21+
```text
22+
1*3*1
23+
13*31
24+
·2*2·
25+
·111·
26+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
[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.
4+
5+
[wikipedia]: https://en.wikipedia.org/wiki/Minesweeper_(video_game)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"minesweeper.coffee"
8+
],
9+
"test": [
10+
"minesweeper.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Add the numbers to a minesweeper board."
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Minesweeper
2+
@annotate: (minefield) ->
3+
if minefield.length < 1
4+
return minefield
5+
if minefield[0].length < 1
6+
return minefield
7+
8+
board = minefield.map (row) -> row.split ''
9+
10+
board.map (row, x) ->
11+
r = row.map (cell, y) ->
12+
if cell == '*'
13+
return cell
14+
count = 0
15+
for i in [-1..1]
16+
for j in [-1..1]
17+
if i + x >= 0 && i + x < board.length && j + y >= 0 && j + y < row.length
18+
count += 1 if board[i + x][j + y] == '*'
19+
if count == 0
20+
' '
21+
else
22+
count.toString()
23+
r.join ''
24+
25+
module.exports = Minesweeper
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[0c5ec4bd-dea7-4138-8651-1203e1cb9f44]
13+
description = "no rows"
14+
15+
[650ac4c0-ad6b-4b41-acde-e4ea5852c3b8]
16+
description = "no columns"
17+
18+
[6fbf8f6d-a03b-42c9-9a58-b489e9235478]
19+
description = "no mines"
20+
21+
[61aff1c4-fb31-4078-acad-cd5f1e635655]
22+
description = "minefield with only mines"
23+
24+
[84167147-c504-4896-85d7-246b01dea7c5]
25+
description = "mine surrounded by spaces"
26+
27+
[cb878f35-43e3-4c9d-93d9-139012cccc4a]
28+
description = "space surrounded by mines"
29+
30+
[7037f483-ddb4-4b35-b005-0d0f4ef4606f]
31+
description = "horizontal line"
32+
33+
[e359820f-bb8b-4eda-8762-47b64dba30a6]
34+
description = "horizontal line, mines at edges"
35+
36+
[c5198b50-804f-47e9-ae02-c3b42f7ce3ab]
37+
description = "vertical line"
38+
39+
[0c79a64d-703d-4660-9e90-5adfa5408939]
40+
description = "vertical line, mines at edges"
41+
42+
[4b098563-b7f3-401c-97c6-79dd1b708f34]
43+
description = "cross"
44+
45+
[04a260f1-b40a-4e89-839e-8dd8525abe0e]
46+
description = "large minefield"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Minesweeper
2+
@annotate: (minefield) ->
3+
4+
module.exports = Minesweeper
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
Minesweeper = require './minesweeper'
2+
3+
describe 'Minesweeper', ->
4+
it 'no rows', ->
5+
minefield = []
6+
expect(Minesweeper.annotate minefield).toEqual []
7+
8+
xit 'no columns', ->
9+
minefield = ['']
10+
expect(Minesweeper.annotate minefield).toEqual ['']
11+
12+
xit 'no mines', ->
13+
minefield = [
14+
' '
15+
' '
16+
' '
17+
]
18+
expect(Minesweeper.annotate minefield).toEqual [
19+
' '
20+
' '
21+
' '
22+
]
23+
24+
xit 'minefield with only mines', ->
25+
minefield = [
26+
'***'
27+
'***'
28+
'***'
29+
]
30+
expect(Minesweeper.annotate minefield).toEqual [
31+
'***'
32+
'***'
33+
'***'
34+
]
35+
36+
xit 'mine surrounded by spaces', ->
37+
minefield = [
38+
' '
39+
' * '
40+
' '
41+
]
42+
expect(Minesweeper.annotate minefield).toEqual [
43+
'111'
44+
'1*1'
45+
'111'
46+
]
47+
48+
xit 'space surrounded by mines', ->
49+
minefield = [
50+
'***'
51+
'* *'
52+
'***'
53+
]
54+
expect(Minesweeper.annotate minefield).toEqual [
55+
'***'
56+
'*8*'
57+
'***'
58+
]
59+
60+
xit 'horizontal line', ->
61+
minefield = [' * * ']
62+
expect(Minesweeper.annotate minefield).toEqual ['1*2*1']
63+
64+
xit 'horizontal line, mines at edges', ->
65+
minefield = ['* *']
66+
expect(Minesweeper.annotate minefield).toEqual ['*1 1*']
67+
68+
xit 'vertical line', ->
69+
minefield = [
70+
' '
71+
'*'
72+
' '
73+
'*'
74+
' '
75+
]
76+
expect(Minesweeper.annotate minefield).toEqual [
77+
'1'
78+
'*'
79+
'2'
80+
'*'
81+
'1'
82+
]
83+
84+
xit 'vertical line, mines at edges', ->
85+
minefield = [
86+
'*'
87+
' '
88+
' '
89+
' '
90+
'*'
91+
]
92+
expect(Minesweeper.annotate minefield).toEqual [
93+
'*'
94+
'1'
95+
' '
96+
'1'
97+
'*'
98+
]
99+
100+
xit 'cross', ->
101+
minefield = [
102+
' * '
103+
' * '
104+
'*****'
105+
' * '
106+
' * '
107+
]
108+
expect(Minesweeper.annotate minefield).toEqual [
109+
' 2*2 '
110+
'25*52'
111+
'*****'
112+
'25*52'
113+
' 2*2 '
114+
]
115+
116+
xit 'large minefield', ->
117+
minefield = [
118+
' * * '
119+
' * '
120+
' * '
121+
' * *'
122+
' * * '
123+
' '
124+
]
125+
expect(Minesweeper.annotate minefield).toEqual [
126+
'1*22*1'
127+
'12*322'
128+
' 123*2'
129+
'112*4*'
130+
'1*22*2'
131+
'111111'
132+
]

0 commit comments

Comments
 (0)