返回顶部

收藏

24 game

更多
#!/usr/bin/env perl6

my @nums;

grammar InfixNotation {
    rule TOP {
        ^ <expr> $
    }
    token expr {
        <left>+ % <op>
    }
    token left {
        '(' <expr> ')' | <@nums>
    }
    token op {
        < + - * / >
    }
}

my $str;

while True {
    @nums = (1 .. 9).roll(4)>>.Str;
    say "Generate 4 digit : " ~ @nums;

    print "Now enter your expression:";
    $str = ~$*IN.get;
    if InfixNotation.parse($str) {
        my $value = $/.EVAL;

        say "$str = $value";

        say $value == 24 ?? "Good job!" !! "Don't lose heart. Come on!";
    } else {
        say "Expression check failed.";
    }

    say "Try again Y/N?";
    $str = $*IN.get;
    if $str.lc !~~ "y" {
        last;
    }
}

标签:lua

收藏

0人收藏

支持

0

反对

0

发表评论