aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2021-08-21 13:09:35 +0200
committerLubos Kolouch <lubos@kolouch.net>2021-08-21 13:09:35 +0200
commit5eead37e15967a2a2ca677f799db881262c7f72b (patch)
tree38571604962a3b07262313f2610e425ff045967f
parentf9e755c4e4b0cde4d6424fe47191b2b713cc4c47 (diff)
downloadperlweeklychallenge-club-5eead37e15967a2a2ca677f799db881262c7f72b.tar.gz
perlweeklychallenge-club-5eead37e15967a2a2ca677f799db881262c7f72b.tar.bz2
perlweeklychallenge-club-5eead37e15967a2a2ca677f799db881262c7f72b.zip
Challenge 126 LK Perl
-rw-r--r--challenge-126/lubos-kolouch/perl/ch-1.pl44
-rw-r--r--challenge-126/lubos-kolouch/perl/ch-2.pl123
2 files changed, 167 insertions, 0 deletions
diff --git a/challenge-126/lubos-kolouch/perl/ch-1.pl b/challenge-126/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..b406a490f4
--- /dev/null
+++ b/challenge-126/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: The Weekly Challenge 126
+# Task 1 - Count Numbers
+#
+# AUTHOR: Lubos
+# VERSION: 1.0
+# CREATED: 08/21/2021 12:05:53
+#===============================================================================
+
+use strict;
+use warnings;
+
+# so, to be honest, there is probably an easy mathematical solution, based on
+# the fact there are:
+#
+# 9 numbers 1-9
+# 9 * 9 numbers 1-100
+# etc.
+# So I can imagine a solution dividing the number by 10 and counting the
+# numbers, but no time to play with it
+
+sub get_numbers_without_1 {
+ my $what = shift;
+
+ my $count = 0;
+ for (2..$what) {
+ $count++ unless /1/;
+ }
+
+ return $count;
+}
+
+use Test::More;
+
+is(get_numbers_without_1(15), 8, 'Test for 15');
+is(get_numbers_without_1(25), 13, 'Test for 25');
+done_testing;
+
diff --git a/challenge-126/lubos-kolouch/perl/ch-2.pl b/challenge-126/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..733b6749b0
--- /dev/null
+++ b/challenge-126/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,123 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-2.pl
+#
+# USAGE: ./ch-2.pl
+#
+# DESCRIPTION: The Weekly Challenge 126
+# Task 2 - Minesweeper Game
+#
+# AUTHOR: Lubos Kolouch
+# CREATED: 08/21/2021 12:09:52
+#===============================================================================
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+# just printing the field is booooring, let's implement the whole game
+# and yes, print the field at the end
+
+my @field = [];
+my $x = int(rand(0))+5;
+my $y = int(rand(0))+5;
+
+my $empty_fields = 0;
+my @complete_field;
+my @player_field;
+
+sub init_field {
+ for my $x_iter (0..$x) {
+ for my $y_iter (0..$y) {
+ $field[$x_iter][$y_iter] = "*";
+ $field[$x_iter][$y_iter] = "x" if (rand() > 0.7);
+ $player_field[$x_iter][$y_iter] = "*";
+ }
+ }
+}
+
+sub print_field {
+ my $what = shift;
+
+ print " ";
+ print "$_ " for (0..$y);
+
+ print "\n ";
+ print "-"x(($y+1)*2);
+ print "\n";
+ for my $x_iter (0..$x) {
+ print " ";
+ print $x_iter." | ";
+ for my $y_iter (0..$y) {
+ print $what->[$x_iter][$y_iter]." ";
+ }
+ print "\n";
+ }
+
+}
+sub calculate_field{
+ for my $x_iter (0..$x) {
+ for my $y_iter (0..$y) {
+ if ($field[$x_iter][$y_iter] eq "x") {
+ $complete_field[$x_iter][$y_iter] = "x";
+ } else {
+ my $counter = 0;
+
+ for my $x_dir (-1, 0, 1) {
+ next if $x_dir + $x_iter > $x;
+ next if $x_dir + $x_iter < 0;
+
+ for my $y_dir (-1, 0, 1) {
+ next if $y_dir + $y_iter > $y;
+ next if $y_dir + $y_iter < 0;
+
+ $counter++ if $field[$x_dir+$x_iter][$y_dir+$y_iter] eq "x";
+ }
+ }
+ $complete_field[$x_iter][$y_iter] = $counter;
+ $empty_fields++;
+ }
+ }
+ }
+}
+
+sub play_game {
+ print "Find all fields without bombs \n";
+
+ while ($empty_fields > 0) {
+ print_field(\@player_field);
+ print "Fields remain $empty_fields \n";
+ print "Enter coordinates separated by space (for example 0 0):\n";
+
+ my $input = <>;
+ my @coords = split / /, $input;
+ if ($field[$coords[0]][$coords[1]] eq "x") {
+ print "BOOOOM! \n";
+
+ print_field(\@complete_field);
+ return;
+ }
+
+ if ($player_field[$coords[0]][$coords[1]] ne "*") {
+ print "You tried that already \n";
+ next;
+ }
+
+ $player_field[$coords[0]][$coords[1]] = $complete_field[$coords[0]][$coords[1]];
+ $empty_fields--;
+
+
+ if ($empty_fields == 0) {
+ print "Congratulations, you won! \n";
+ print_field(\@complete_field);
+ return;
+ }
+ }
+}
+
+init_field;
+calculate_field;
+play_game;
+
+