Skip to content

Instantly share code, notes, and snippets.

@laserbat
Created September 5, 2011 13:56
Show Gist options
  • Save laserbat/1195042 to your computer and use it in GitHub Desktop.
Save laserbat/1195042 to your computer and use it in GitHub Desktop.
microRL
#!/usr/bin/perl
use Curses;sub a{$V=$Z>100;$VV=$V;for$x(0..79){for$y(0..23){if($x==0or$y==0or$x==79or$y==23){$u[$x][$y]=0}else{$u[$x][$y]=r(2)}}}for$x(2..77){for$y(2..21){$c=$u[$x+1][$y]+$u[$x+1][$y+1]+$u[$x][$y+1]+$u[$x-1][$y]+$u[$x-1][$y-1]+$u[$x][$y-1]+$u[$x+1][$y-1]+$u[$x-1][$y+1];if($c>=5){$u[$x][$y]=1}elsif($c<=3){$u[$x][$y]=0}if($x==0or$y==0or$x==79or$y==23){$u[$x][$y]=0}}}$X=r(80);$Y=r(24);while(!$u[$X][$Y]){$X=r(80);$Y=r(24)}i($X,$Y);my$g;for my$x(0..79){for my$y(0..23){if($u[$x][$y]==3){$u[$x][$y]= $V?1:r(20)?1:5;$g++}else{$u[$x][$y]=0}}}a()if($g>3000 or$g<500);$B=r(80);$N=r(24);while(!$u[$B][$N]){$B=r(80);$N=r(24)}by_init :D}sub r{int rand shift}sub z{($x,$y)=(shift,shift);$u[$x][$y]<2and($u[$x][$y]>0or$u[$x][$y]==-1)}sub n{($x,$y)=(shift,shift);h($x,$y)<3}sub h{($x,$y)=(shift,shift);(($x-$X)**2+($y-$Y)**2)}sub i{my$x=shift;my$y=shift;return if$u[$x][$y]^1;$u[$x][$y]=3;i($x+1,$y);i($x-1,$y);i($x,$y+1);i($x,$y-1)}$U=100;$Z=0;initscr;noecho;&a;while($U>0){if($VV){$u[$B][$N]=5;$VV=0};clear;for my$x(0..79){for my$y(0..23){if($x==$X and$y==$Y){addstr($y,$x,"@")}elsif(h($x,$y)<100){if($x==$B and$y==$N and!$V){addstr($y,$x,">")}elsif($u[$x][$y]==5){addstr($y,$x,$V?"@":"&")}elsif($u[$x][$y]==-1){addstr($y,$x,"%")}else{addstr($y,$x,$u[$x][$y]?'.':'#')}}}}addstr(0,0,"$U:$Z");addstr($Y,$X,"");$A=getch;($I,$J)=($X,$Y);if($A eq"q"){$U=0}elsif($A eq"h"){$I--}elsif($A eq"j"){$J++}elsif($A eq"k"){$J--}elsif($A eq"l"){$I++}elsif($A eq"y"){$I--;$J--}elsif($A eq"u"){$I++;$J--}elsif($A eq"b"){$I--;$J++}elsif($A eq"n"){$I++;$J++}if(z($I,$J)){$X=$I;$Y=$J;if($X==$B and$Y==$N and!$V){$U+=r(30);a()}}elsif($u[$I][$J]==5){if($V){if(!r(5)){$u[$I][$J]=-1;$Z+=100;$Z="$Z (Won)";$U=0}}else{$u[$I][$J]=-1;++$Z}};for my$x(0..79){for my$y(0..23){if($u[$x][$y]==5){$O=$x+int(rand(4)-2);$P=$y+int(rand(4)-2);if(z($O,$P)and$O^$X and$P^$Y){$u[$x][$y]=1;$u[$O][$P]=4}}}}for my$x(0..79){for my$y(0..23){if($u[$x][$y]==4){$u[$x][$y]=5}if($u[$x][$y]==5and n($x,$y)){$U-=$V?r(10)+1:r(3)+1}}}};endwin;print"Score: $Z!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment