24 game

2019PHP高薪工程师学习路线图....>>>

#!/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;
	}
}