Initial upload
This commit is contained in:
1
julia/minesweeper/.exercism/metadata.json
Normal file
1
julia/minesweeper/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"minesweeper","id":"a839588890684ea89e338552c20ab03a","url":"https://exercism.io/my/solutions/a839588890684ea89e338552c20ab03a","handle":"halfdan","is_requester":true,"auto_approve":false}
|
43
julia/minesweeper/README.md
Normal file
43
julia/minesweeper/README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Minesweeper
|
||||
|
||||
Add the mine counts to a completed Minesweeper board.
|
||||
|
||||
Minesweeper 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.
|
||||
|
||||
In this exercise you have to create some code that counts the number of
|
||||
mines adjacent to a given empty square and replaces that square with the
|
||||
count.
|
||||
|
||||
The board is a rectangle composed of blank space (' ') characters. A mine
|
||||
is represented by an asterisk ('\*') character.
|
||||
|
||||
If a given space has no adjacent mines at all, leave that square blank.
|
||||
|
||||
## Examples
|
||||
|
||||
For example you may receive a 5 x 4 board like this (empty spaces are
|
||||
represented here with the '·' character for display on screen):
|
||||
|
||||
```
|
||||
·*·*·
|
||||
··*··
|
||||
··*··
|
||||
·····
|
||||
```
|
||||
|
||||
And your code will transform it into this:
|
||||
|
||||
```
|
||||
1*3*1
|
||||
13*31
|
||||
·2*2·
|
||||
·111·
|
||||
```
|
||||
|
||||
## Version compatibility
|
||||
This exercise has been tested on Julia versions >=1.0.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
68
julia/minesweeper/minesweeper.ipynb
Normal file
68
julia/minesweeper/minesweeper.ipynb
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": ["# Minesweeper\n", "\n", "Add the mine counts to a completed Minesweeper board.\n", "\n", "Minesweeper is a popular game where the user has to find the mines using\n", "numeric hints that indicate how many mines are directly adjacent\n", "(horizontally, vertically, diagonally) to a square.\n", "\n", "In this exercise you have to create some code that counts the number of\n", "mines adjacent to a given empty square and replaces that square with the\n", "count.\n", "\n", "The board is a rectangle composed of blank space (' ') characters. A mine\n", "is represented by an asterisk ('\\*') character.\n", "\n", "If a given space has no adjacent mines at all, leave that square blank.\n", "\n", "## Examples\n", "\n", "For example you may receive a 5 x 4 board like this (empty spaces are\n", "represented here with the '·' character for display on screen):\n", "\n", "```\n", "·*·*·\n", "··*··\n", "··*··\n", "·····\n", "```\n", "\n", "And your code will transform it into this:\n", "\n", "```\n", "1*3*1\n", "13*31\n", "·2*2·\n", "·111·\n", "```\n", "\n", "## Version compatibility\n", "This exercise has been tested on Julia versions >=1.0.\n", "\n", "## Submitting Incomplete Solutions\n", "It's possible to submit an incomplete solution so you can see how others have completed the exercise."]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": ["## Your solution"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": ["# submit"]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": ["## Test suite"]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": ["using Test\n", "\n", "# include(\"minesweeper.jl\")\n", "\n", "@testset \"no rows\" begin\n", " @test annotate([]) == []\n", "end\n", "\n", "@testset \"no columns\" begin\n", " @test annotate([\"\"]) == [\"\"]\n", "end\n", "\n", "@testset \"no mines\" begin\n", " minefield = [\" \",\n", " \" \",\n", " \" \"]\n", " out = [\" \",\n", " \" \",\n", " \" \"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"minefield only mines\" begin\n", " minefield = [\"***\",\n", " \"***\",\n", " \"***\"]\n", " out = [\"***\",\n", " \"***\",\n", " \"***\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"mine surrounded by spaces\" begin\n", " minefield = [\" \",\n", " \" * \",\n", " \" \"]\n", " out = [\"111\",\n", " \"1*1\",\n", " \"111\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"space surrounded by mines\" begin\n", " minefield = [\"***\",\n", " \"* *\",\n", " \"***\"]\n", " out = [\"***\",\n", " \"*8*\",\n", " \"***\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"horizontal line\" begin\n", " minefield = [\" * * \"]\n", " out = [\"1*2*1\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"horizontal line mines at edges\" begin\n", " minefield = [\"* *\"]\n", " out = [\"*1 1*\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"vertical line\" begin\n", " minefield = [\" \",\n", " \"*\",\n", " \" \",\n", " \"*\",\n", " \" \"]\n", " out = [\"1\",\n", " \"*\",\n", " \"2\",\n", " \"*\",\n", " \"1\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"vertical line mines at edges\" begin\n", " minefield = [\"*\",\n", " \" \",\n", " \" \",\n", " \" \",\n", " \"*\"]\n", " out = [\"*\",\n", " \"1\",\n", " \" \",\n", " \"1\",\n", " \"*\"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"cross\" begin\n", " minefield = [\" * \",\n", " \" * \",\n", " \"*****\",\n", " \" * \",\n", " \" * \"]\n", " out = [\" 2*2 \",\n", " \"25*52\",\n", " \"*****\",\n", " \"25*52\",\n", " \" 2*2 \"]\n", " @test annotate(minefield) == out\n", "end\n", "\n", "@testset \"large minefield\" begin\n", " minefield = [\" * * \",\n", " \" * \",\n", " \" * \",\n", " \" * *\",\n", " \" * * \",\n", " \" \"]\n", " out = [\"1*22*1\",\n", " \"12*322\",\n", " \" 123*2\",\n", " \"112*4*\",\n", " \"1*22*2\",\n", " \"111111\"]\n", " @test annotate(minefield) == out\n", "end"]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Prepare submission\n",
|
||||
"To submit your exercise, you need to save your solution in a file called `minesweeper.jl` before using the CLI.\n",
|
||||
"You can either create it manually or use the following functions, which will automatically write every notebook cell that starts with `# submit` to the file `minesweeper.jl`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# using Pkg; Pkg.add(\"Exercism\")\n",
|
||||
"# using Exercism\n",
|
||||
"# Exercism.create_submission(\"minesweeper\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.3.0",
|
||||
"language": "julia",
|
||||
"name": "julia-1.3"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.3.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
25
julia/minesweeper/minesweeper.jl
Normal file
25
julia/minesweeper/minesweeper.jl
Normal file
@@ -0,0 +1,25 @@
|
||||
function annotate(field)
|
||||
afield = []
|
||||
for x ∈ 1:length(field)
|
||||
line = ""
|
||||
for y ∈ 1:length(field[x])
|
||||
el = field[x][y]
|
||||
if el == '*'
|
||||
line = "$line$el"
|
||||
continue
|
||||
end
|
||||
stars = 0
|
||||
if x > 1
|
||||
stars += count(c -> c == '*', field[x - 1][max(y - 1, 1):min(y + 1, length(field[x]))])
|
||||
end
|
||||
stars += count(c -> c == '*', field[x][max(y - 1, 1):min(y + 1, length(field[x]))])
|
||||
if x < length(field)
|
||||
stars += count(c -> c == '*', field[x + 1][max(y - 1, 1):min(y + 1, length(field[x]))])
|
||||
end
|
||||
ch = stars == 0 ? " " : "$stars"
|
||||
line = "$line$ch"
|
||||
end
|
||||
append!(afield, [line])
|
||||
end
|
||||
afield
|
||||
end
|
121
julia/minesweeper/runtests.jl
Normal file
121
julia/minesweeper/runtests.jl
Normal file
@@ -0,0 +1,121 @@
|
||||
using Test
|
||||
|
||||
include("minesweeper.jl")
|
||||
|
||||
@testset "no rows" begin
|
||||
@test annotate([]) == []
|
||||
end
|
||||
|
||||
# @testset "no columns" begin
|
||||
# @test annotate([""]) == [""]
|
||||
# end
|
||||
|
||||
@testset "no mines" begin
|
||||
minefield = [" ",
|
||||
" ",
|
||||
" "]
|
||||
out = [" ",
|
||||
" ",
|
||||
" "]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "minefield only mines" begin
|
||||
minefield = ["***",
|
||||
"***",
|
||||
"***"]
|
||||
out = ["***",
|
||||
"***",
|
||||
"***"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "mine surrounded by spaces" begin
|
||||
minefield = [" ",
|
||||
" * ",
|
||||
" "]
|
||||
out = ["111",
|
||||
"1*1",
|
||||
"111"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "space surrounded by mines" begin
|
||||
minefield = ["***",
|
||||
"* *",
|
||||
"***"]
|
||||
out = ["***",
|
||||
"*8*",
|
||||
"***"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "horizontal line" begin
|
||||
minefield = [" * * "]
|
||||
out = ["1*2*1"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "horizontal line mines at edges" begin
|
||||
minefield = ["* *"]
|
||||
out = ["*1 1*"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "vertical line" begin
|
||||
minefield = [" ",
|
||||
"*",
|
||||
" ",
|
||||
"*",
|
||||
" "]
|
||||
out = ["1",
|
||||
"*",
|
||||
"2",
|
||||
"*",
|
||||
"1"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "vertical line mines at edges" begin
|
||||
minefield = ["*",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
"*"]
|
||||
out = ["*",
|
||||
"1",
|
||||
" ",
|
||||
"1",
|
||||
"*"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "cross" begin
|
||||
minefield = [" * ",
|
||||
" * ",
|
||||
"*****",
|
||||
" * ",
|
||||
" * "]
|
||||
out = [" 2*2 ",
|
||||
"25*52",
|
||||
"*****",
|
||||
"25*52",
|
||||
" 2*2 "]
|
||||
@test annotate(minefield) == out
|
||||
end
|
||||
|
||||
@testset "large minefield" begin
|
||||
minefield = [" * * ",
|
||||
" * ",
|
||||
" * ",
|
||||
" * *",
|
||||
" * * ",
|
||||
" "]
|
||||
out = ["1*22*1",
|
||||
"12*322",
|
||||
" 123*2",
|
||||
"112*4*",
|
||||
"1*22*2",
|
||||
"111111"]
|
||||
@test annotate(minefield) == out
|
||||
end
|
Reference in New Issue
Block a user