Skip to content

Add minesweeper exercise #117

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

Merged
merged 1 commit into from
May 9, 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 @@ -217,6 +217,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "5a1afe73-e34d-4331-8490-ac67ddf92b44",
"practices": [],
"prerequisites": [],
"difficulty": 6
}
]
},
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": [
"eNascimento178"
],
"files": {
"solution": [
"minesweeper.ijs"
],
"test": [
"test.ijs"
],
"example": [
".meta/example.ijs"
]
},
"blurb": "Add the numbers to a minesweeper board."
}
70 changes: 70 additions & 0 deletions exercises/practice/minesweeper/.meta/example.ijs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
annotate=: {{
if. *./ 1&>: $y do.
] y
elseif. 1 = {.$y do.
board_1_x_n y
elseif. 1 ={:$y do.
board_1_x_n&.|: y
elseif. 2 2 -: $ y do.
board_2_x_2 y
elseif. 3 2 -: $ y do.
board_ge_2_x_2&.|: y
else.
board_ge_2_x_2 y
end.
}}

board_ge_2_x_2=: {{
bomb =. '*'"_
bomb_next =. ":@{:
clear =. ' '"_
shape =. $ y
matrix =. ' *'&i. y
count =. +/^:_ every
is_bomb =. '*'"_^:(1 -: {.) each
are_bombs_next =. ":@{:^:(0 -: {.) each
no_bombs =. ' '"_^:(0 0&-:) each

x_center =. 1 1 ,: 3 3
x_corners =. (shape - 2) ,: 2 2
x_horiz_boundaries =. (1,~ ({. shape) - 2) ,: 2 3
x_vert_boundaries =. (1, ({: shape) - 2) ,: 3 2

center =. count x_center <;._3 matrix
corners =. count x_corners <;._3 matrix
horiz_boundaries =. count x_horiz_boundaries <;._3 matrix
vert_boundaries =. count x_vert_boundaries <;._3 matrix

fst_row =. ({.{.corners), ({.horiz_boundaries), ({:{.corners)
lst_row =. ({.{:corners), ({:horiz_boundaries), ({:{:corners)
'left_col right_col' =. ,. each <"1 |: vert_boundaries
middle_rows =. left_col ,. center ,. right_col

sectors_count =. fst_row , middle_rows , lst_row

board =. matrix <@,"0 sectors_count


;"1 is_bomb are_bombs_next no_bombs board
}}

board_2_x_2=:{{
bombs =. +/^:_ ' *'&i. y

;"1 ":@bombs"_^:([: -. '*'&-: +. ' '&-: *. bombs = 0"_) each y
}}

board_1_x_n=: {{
matrix =. ' *'&i. y
count =. +/^:_ every
is_bomb =. '*'"_^:(1 -: {.) each
are_bombs_next =. ":@{:^:(0 -: {.) each
no_bombs =. ' '"_^:(0 0&-:) each
center =. count (1 1,:1 3) <;._3 matrix
corners=. count ((1, ({:$y) - 2 ) ,: 1 2) <;._3 matrix

sectors_count=. ({.{.corners),. center,. ({:{.corners)
board =. matrix <@,"0 sectors_count

;"1 is_bomb are_bombs_next no_bombs board
}}
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"
3 changes: 3 additions & 0 deletions exercises/practice/minesweeper/minesweeper.ijs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'general/unittest'

annotate=: 'You need to implement this verb.'13!:8 (55)
130 changes: 130 additions & 0 deletions exercises/practice/minesweeper/test.ijs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
load 'minesweeper.ijs'


before_all=: monad define
(]Description =: (3 : 'descriptions=: i.0')`(3 : 'descriptions=: descriptions , < y'))@.0 ''
(]Order =: (3 : 'order=: i.0')`(3 : 'order=: order , < y'))@.0 ''
(]Task =: (3 : 'tasks=: i.0')`(3 : 'tasks=: tasks , < y'))@.0 ''
)


minesweeper_test_01_ignore=: 0
test_minesweeper_test_01 =: monad define
Description@.1 ('no rows')
Order@.1 (1)

NB. minefield=. ,:''
NB. expected =. ,:''
assert (,:'') -: annotate ,:''
)

minesweeper_test_02_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_02 =: monad define
Description@.1 ('no columns')
Order@.1 (2)

NB. minefield=. ,:a:
NB. expected =. ,:a:
assert (,:,:' ') -: annotate ,:,:' '
)

minesweeper_test_03_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_03 =: monad define
Description@.1 ('no mines')
Order@.1 (3)

NB. minefield=. ' ',' ',:' '
NB. expected =. ' ',' ',:' '
assert (' ',' ',:' ') -: annotate ' ',' ',:' '
)

minesweeper_test_04_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_04 =: monad define
Description@.1 ('minefield with only mines')
Order@.1 (4)

NB. minefield=. '***','***',:'***'
NB. expected =. '***','***',:'***'
assert ('***','***',:'***') -: annotate '***','***',:'***'
)

minesweeper_test_05_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_05 =: monad define
Description@.1 ('mine surrounded by spaces')
Order@.1 (5)

NB. minefield=. ' ',' * ',:' '
NB. expected =. '111','1*1',:'111'
assert ('111','1*1',:'111') -: annotate ' ',' * ',:' '
)

minesweeper_test_06_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_06 =: monad define
Description@.1 ('space surrounded by mines')
Order@.1 (6)

NB. minefield=. '***','* *',:'***'
NB. expected =. '***','*8*',:'***'
assert ('***','*8*',:'***') -: annotate '***','* *',:'***'
)

minesweeper_test_07_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_07 =: monad define
Description@.1 ('horizontal line')
Order@.1 (7)

NB. minefield=. ,:' * * '
NB. expected =. ,:'1*2*1'
assert (,:'1*2*1') -: annotate ,:' * * '
)

minesweeper_test_08_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_08 =: monad define
Description@.1 ('horizontal line, mines at edges')
Order@.1 (8)

NB. minefield=. '* *'
NB. expected =. '*1 1*'
assert (,:'*1 1*') -: annotate ,:'* *'
)

minesweeper_test_09_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_09 =: monad define
Description@.1 ('vertical line')
Order@.1 (9)

NB. minefield=. ' ','*',' ','*',:' '
NB. expected =. '1','*','2','*',:'1'
assert ('1','*','2','*',:'1') -: annotate ' ','*',' ','*',:' '
)

minesweeper_test_10_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_10 =: monad define
Description@.1 ('vertical line, mines at edges')
Order@.1 (10)

NB. minefield=. '*',' ',' ',' ',:'*'
NB. expected =. '*','1',' ','1',:'*'
assert ('*','1',' ','1',:'*') -: annotate '*',' ',' ',' ',:'*'
)

minesweeper_test_11_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_11 =: monad define
Description@.1 ('cross')
Order@.1 (11)

NB. minefield=. ' * ',' * ','*****',' * ',:' * '
NB. expected =. ' 2*2 ','25*52','*****','25*52',:' 2*2 '
assert (' 2*2 ','25*52','*****','25*52',:' 2*2 ') -: annotate ' * ',' * ','*****',' * ',:' * '
)

minesweeper_test_12_ignore=: 1 NB. Change this value to 0 to run this test
test_minesweeper_test_12 =: monad define
Description@.1 ('large minefield')
Order@.1 (12)

NB. minefield=. ' * * ',' * ',' * ',' * *',' * * ',:' '
NB. expected =. '1*22*1','12*322',' 123*2','112*4*','1*22*2',:'111111'
assert ('1*22*1','12*322',' 123*2','112*4*','1*22*2',:'111111') -: annotate ' * * ',' * ',' * ',' * *',' * * ',:' '
)