RosettaCodeData/Task/24-game/Huginn/24-game.huginn

55 lines
1.3 KiB
Bash

#! /bin/sh
exec huginn --no-argv -E "${0}"
#! huginn
import Algorithms as algo;
import Mathematics as math;
import RegularExpressions as re;
make_game( rndGen_ ) {
board = "";
for ( i : algo.range( 4 ) ) {
board += ( " " + string( character( rndGen_.next() + integer( '1' ) ) ) );
}
return ( board.strip() );
}
main() {
rndGen = math.randomizer( 9 );
no = 0;
dd = re.compile( "\\d\\d" );
while ( true ) {
no += 1;
board = make_game( rndGen );
print( "Your four digits: {}\nExpression {}: ".format( board, no ) );
line = input();
if ( line == none ) {
print( "\n" );
break;
}
line = line.strip();
try {
if ( line == "q" ) {
break;
}
if ( ( pos = line.find_other_than( "{}+-*/() ".format( board ) ) ) >= 0 ) {
print( "Invalid input found at: {}, `{}`\n".format( pos, line ) );
continue;
}
if ( dd.match( line ).matched() ) {
print( "Digit concatenation is forbidden.\n" );
continue;
}
res = real( line );
if ( res == 24.0 ) {
print( "Thats right!\n" );
} else {
print( "Bad answer!\n" );
}
} catch ( Exception e ) {
print( "Not an expression: {}\n".format( e.what() ) );
}
}
return ( 0 );
}