#!/usr/bin/env perl6
use v6;
my Str constant @totalElement =
prompt("輸入所有元素 ( ex: rock paper scissor ): \n")
.split(/\s/, :skip-empty)
.grep( * !=== "stop" ) ;
my Int constant $howManyRobot = {
loop {
$_ = prompt("幾個電腦 : ");
last if $_ ~~ /\d+/;
"需要輸入數字".say
}
$<>.Int
}.();
my Array constant @howManyCases = [X,] (loop { @totalElement })[^($howManyRobot + 1)];
my List @howManyCasesWillWin = gather loop {
once "輸入勝利的組合(一次一組),直到你打stop".say;
my @winCase = prompt("輸入: ")
.split(/\s/, :skip-empty);
last if "stop" ∈ @winCase;
"一次只能輸入一組".say and next if @winCase.elems != $howManyRobot + 1;
"包含先前沒輸入的元素".say and next if @winCase ⊈ @totalElement;
take @winCase
};
my Str @robot[$howManyRobot];
my Str $yourInput;
loop {
@robot = @totalElement.roll($howManyRobot);
$yourInput = {
loop {
$_ = prompt("你猜: ");
last if $_ ~~ /^ <@totalElement> $/;
"包含先前沒輸入的元素".say
}
$_
}.();
"你出 $yourInput 電腦出 { @robot} ".say;
if [||] @howManyCasesWillWin.map({ [&&] ( @$_ Zeq ($yourInput , |@robot) ) }) {
"你贏了".say;
last
}
else {
"繼續努力".say
}
}