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 #681

Merged
merged 2 commits into from
Apr 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "eae564ba-b4d9-4463-ae3f-f7f05f4e52ea",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
],
"foregone": [
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/minesweeper/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Hints for Bash Minesweeper Exercise

This exercise seems to require a two-dimensional array.
Bash only offers one-dimensional indexed or associative [arrays][array].

Multi-dimensional arrays can be simulated by using an associative arrays: encode the indices into a comma-separated string to use as the array key.

```bash
# store this matrix:
# a b
# c d
declare -A array2d
array2d["0,0"]="a"
array2d["0,1"]="b"
array2d["1,0"]="c"
array2d["1,1"]="d"
```

[array]: https://www.gnu.org/software/bash/manual/bash.html#Arrays
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)
15 changes: 15 additions & 0 deletions exercises/practice/minesweeper/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"authors": [],
"files": {
"solution": [
"minesweeper.sh"
],
"test": [
"minesweeper.bats"
],
"example": [
".meta/example.sh"
]
},
"blurb": "Add the numbers to a minesweeper board."
}
51 changes: 51 additions & 0 deletions exercises/practice/minesweeper/.meta/example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# some global vars
declare -A board
height=$#
width=${#1}

parse_input() {
local rownum=0 row col index char
for row in "$@"; do
for ((col = 0; col < "${#row}"; col++)); do
index="${rownum},${col}"
char=${row:col:1}
board[$index]=$char
done
(( rownum++ ))
done
}

count() {
local r=$1 c=$2
local count=0
for dr in -1 0 1; do
(( r + dr < 0 || r + dr == height )) && continue
for dc in -1 0 1; do
(( c + dc < 0 || c + dc == width )) && continue
(( dr == 0 && dc == 0 )) && continue
index="$((r + dr)),$((c + dc))"
[[ ${board[$index]} == "*" ]] && (( count++ ))
done
done
(( count == 0 )) && echo " " || echo "$count"
}

create_output() {
local row output col
for ((row = 0; row < height; row++)); do
output=""
for ((col = 0; col < width; col++)); do
if [[ ${board[$row,$col]} == "*" ]]; then
output+="*"
else
output+=$( count "$row" "$col" )
fi
done
echo "$output"
done
}

parse_input "$@"
create_output
47 changes: 47 additions & 0 deletions exercises/practice/minesweeper/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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"
include = false

[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"