Guest User

Untitled

a guest
Apr 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.04 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict "vars";
  3. use warnings;
  4. system('clear');
  5. our @tape;
  6. our $pos = 15;
  7. our @sa= qw(1>b 1>~);
  8. our @sb= qw(0>c 9>b);
  9. our @sc= qw(1<c 9<a);
  10. our %instruction = (
  11.     '1' =>  sub { $tape[$pos] = '1' },
  12.     '0' =>  sub { $tape[$pos] = '0' },
  13.     '9' =>  sub { $tape[$pos] = &randbit },
  14.     '<' =>  sub { --$pos },
  15.     '>' =>  sub { ++$pos },
  16.     '~' =>  sub { print "END OF CALCULATION\n" ; exit 0 },
  17. );
  18. for ('a' .. 'z') { $instruction{$_} = sub { init("s".$_) } }
  19. { push @tape,'0' ; redo unless scalar @tape == 31 }
  20. sub exec {
  21. for ( unpack("(A1)*", $_[0]) ) {
  22.         &{$instruction{$_}};
  23.         select(undef, undef, undef, 0.025);
  24.         print &print_tape . "\n";
  25. } # the game.
  26. }
  27. sub init { ($_) = @_; ($tape[$pos] == 0) ? {&exec(${$_}[0])} : { &exec(${$_}[1]) } }
  28. sub print_tape {
  29.     my $t = join '', @tape;
  30.     $t =~ s/(^.{$pos})(.)(.*$)/\033[0m${1}\033[1;37m${2}\033[0m${3}/;
  31.     print $t;
  32.     return "";
  33. }
  34. sub randbit {
  35.     open(RAND,"/dev/urandom") or die;
  36.     ord getc RAND > 127 ? 1 : 0;
  37.     close(RAND)
  38. }
  39. init('sa');
Add Comment
Please, Sign In to add comment