diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-03-21 21:43:42 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-03-21 21:43:42 +0800 |
| commit | 651d1d0e52cbd32f09c03e1e8a2342432ade0833 (patch) | |
| tree | a91b0029fbca614a5afc849f20fb98aa9c75e57a | |
| parent | e9ea0d8d7f3b84d7a6c204e8fa79f91cd1df7627 (diff) | |
| download | perlweeklychallenge-club-651d1d0e52cbd32f09c03e1e8a2342432ade0833.tar.gz perlweeklychallenge-club-651d1d0e52cbd32f09c03e1e8a2342432ade0833.tar.bz2 perlweeklychallenge-club-651d1d0e52cbd32f09c03e1e8a2342432ade0833.zip | |
2 Perl scripts
| -rw-r--r-- | challenge-104/cheok-yin-fung/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-104/cheok-yin-fung/perl/ch-2.pl | 104 |
2 files changed, 121 insertions, 0 deletions
diff --git a/challenge-104/cheok-yin-fung/perl/ch-1.pl b/challenge-104/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..111f6a72e4 --- /dev/null +++ b/challenge-104/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +# The Weekly Challenge 104 +# FUSC Sequence + +use strict; +use warnings; + +my @a = (0,1); + +for my $i (3..50) { + $a[$i] = $i % 2 == 0 ? $a[$i/2] : $a[($i-1)/2]+$a[($i+1)/2]; +} + +for my $i (0..50) { + printf "%3d %2d\n", $i, $a[$i]; +} + diff --git a/challenge-104/cheok-yin-fung/perl/ch-2.pl b/challenge-104/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..b55f1ba0f2 --- /dev/null +++ b/challenge-104/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,104 @@ +#!/usr/bin/perl +# The Weekly Challenge 104 +# NIM Game +use strict; +use warnings; + +my $remain = 12; + +my $player = int rand(2); + +print " NIM Game\n "; +print " |" x 12, "\n"; + +#from task statment + +print +'For the purpose of this task, let assume you play against the machine. + +There are 3 simple rules to follow: + +a) You have 12 tokens +b) Each player can pick 1, 2 or 3 tokens at a time +c) The player who picks the last token wins the game + + +For simplicity, we have already randomly assigned the player: +'; + +if ($player == 1) { + print " First Player: YOU;\nSecond Player: COMPUTER;\n"; + your_decision(); +} +else +{ + print " First Player: COMPUTER;\nSecond Player: YOU;\n"; + machine_decision(); +} + +# ========================================================= +# ================ Subroutines ============================ +# ========================================================= + + +sub machine_decision { + my $m_take; + if ($remain % 4 != 0) { + $m_take = $remain % 4; + } + else { + $m_take = 1 + int rand(3); + } + print "The computer decides to take $m_take token"; + print ($m_take == 1 ? "." : "s." ); + print "\n"; + pick($m_take, ($player+1) % 2); +} + +sub your_decision { + print "Please input how many tokens you want to take: "; + my $inpt = <STDIN>; + chomp($inpt); + if (($inpt >= 4 or $inpt <= 0) || ($remain < $inpt)) { + print "WARN: invalid input; Please input again\n"; + your_decision(); + } + else { + pick(int $inpt, $player); + } +} + +sub pick { + my $num = $_[0]; + my $side = $_[1]; + $remain -= $num; + die "Invalid operation!" if $remain < 0 || $num >= 4 || $num <= 0 ; + + if ($remain == 0) { + endgame($side); + } + + print "\n ", "| " x $remain; + print "\nNumber of tokens remain: $remain\n"; + + if ($side == $player) { + machine_decision(); + } + else { + your_decision(); + } + +} + +sub endgame { + my $winner = $_[0]; + print "The winner is "; + if ($winner == $player) { + print "YOU. \n" + } + else { + print "the computer. \n" + } + print "Good game. \n"; + exit; +} |
