exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

commit 35c2cdcf8842ea1df2e6bc1d0189f1c74babe9e4
parent 47debcf9033cd0ef22b472bfde47e3f6511fa33c
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon, 14 Feb 2022 23:40:24 +0000

initial commit for minesweeper

Diffstat:
Aminesweeper/HELP.md | 36++++++++++++++++++++++++++++++++++++
Aminesweeper/Minesweeper.pm | 25+++++++++++++++++++++++++
Aminesweeper/README.md | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aminesweeper/minesweeper.t | 214+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 331 insertions(+), 0 deletions(-)

diff --git a/minesweeper/HELP.md b/minesweeper/HELP.md @@ -0,0 +1,35 @@ +# Help + +## Running the tests + +There is a Perl 5 script with the extension `.t`, which will be used to test +your solution. You can run through the tests by using the command: + +```bash +`prove .` +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit Minesweeper.pm` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Perl 5 track's documentation](https://exercism.org/docs/tracks/perl5) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [/r/perl5](https://www.reddit.com/r/perl5) is the perl5 subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/perl5) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +\ No newline at end of file diff --git a/minesweeper/Minesweeper.pm b/minesweeper/Minesweeper.pm @@ -0,0 +1,25 @@ +package Minesweeper; +use strict; +use warnings; +use Exporter qw<import>; +our @EXPORT_OK = qw<annotate>; + +sub annotate { + my ($minefield) = @_; + print "start of minefield....\n"; + print "$_\n" foreach (@{ $minefield }); + print "end of minefield...\n"; +# my @field; +# foreach my $row (@{ $minefield }) { +# push @field => [split ( //, $row ), 'X']; +# } + my @field = map { [split ( //, $_ ), 'X'] } @{ $minefield }; + print "----\n"; + foreach my $row (@field) { + print @{ $row }, "\n"; + } + print "----\n"; + return undef; +} + +1; diff --git a/minesweeper/README.md b/minesweeper/README.md @@ -0,0 +1,55 @@ +# Minesweeper + +Welcome to Minesweeper on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +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· +``` + +## Source + +### Created by + +- @autark + +### Contributed to by + +- @bistik +- @kytrinyx +- @m-dango +- @rfilipo +\ No newline at end of file diff --git a/minesweeper/minesweeper.t b/minesweeper/minesweeper.t @@ -0,0 +1,214 @@ +#!/usr/bin/env perl +use Test2::V0; +use JSON::PP; +use constant JSON => JSON::PP->new; + +use FindBin qw<$Bin>; +use lib $Bin, "$Bin/local/lib/perl5"; + +use Minesweeper qw<annotate>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<annotate> or bail_out; + +for my $case (@test_cases) { + is( annotate( $case->{input}{minefield} ), + $case->{expected}, $case->{description}, ); +} + +done_testing; + +__DATA__ +[ + { + "description": "no rows", + "expected": [], + "input": { + "minefield": [] + }, + "property": "annotate" + }, + { + "description": "no columns", + "expected": [ + "" + ], + "input": { + "minefield": [ + "" + ] + }, + "property": "annotate" + }, + { + "description": "no mines", + "expected": [ + " ", + " ", + " " + ], + "input": { + "minefield": [ + " ", + " ", + " " + ] + }, + "property": "annotate" + }, + { + "description": "minefield with only mines", + "expected": [ + "***", + "***", + "***" + ], + "input": { + "minefield": [ + "***", + "***", + "***" + ] + }, + "property": "annotate" + }, + { + "description": "mine surrounded by spaces", + "expected": [ + "111", + "1*1", + "111" + ], + "input": { + "minefield": [ + " ", + " * ", + " " + ] + }, + "property": "annotate" + }, + { + "description": "space surrounded by mines", + "expected": [ + "***", + "*8*", + "***" + ], + "input": { + "minefield": [ + "***", + "* *", + "***" + ] + }, + "property": "annotate" + }, + { + "description": "horizontal line", + "expected": [ + "1*2*1" + ], + "input": { + "minefield": [ + " * * " + ] + }, + "property": "annotate" + }, + { + "description": "horizontal line, mines at edges", + "expected": [ + "*1 1*" + ], + "input": { + "minefield": [ + "* *" + ] + }, + "property": "annotate" + }, + { + "description": "vertical line", + "expected": [ + "1", + "*", + "2", + "*", + "1" + ], + "input": { + "minefield": [ + " ", + "*", + " ", + "*", + " " + ] + }, + "property": "annotate" + }, + { + "description": "vertical line, mines at edges", + "expected": [ + "*", + "1", + " ", + "1", + "*" + ], + "input": { + "minefield": [ + "*", + " ", + " ", + " ", + "*" + ] + }, + "property": "annotate" + }, + { + "description": "cross", + "expected": [ + " 2*2 ", + "25*52", + "*****", + "25*52", + " 2*2 " + ], + "input": { + "minefield": [ + " * ", + " * ", + "*****", + " * ", + " * " + ] + }, + "property": "annotate" + }, + { + "description": "large minefield", + "expected": [ + "1*22*1", + "12*322", + " 123*2", + "112*4*", + "1*22*2", + "111111" + ], + "input": { + "minefield": [ + " * * ", + " * ", + " * ", + " * *", + " * * ", + " " + ] + }, + "property": "annotate" + } +]