Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
Cool assembler maze generator. Enjoy.
Browse files Browse the repository at this point in the history
  • Loading branch information
tklein23 committed Feb 27, 2014
1 parent 366c13a commit 5d1eaf2
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 2014-02-27/maze-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Generate a maze to walk through:
================================
* ```genmaze.pl``` to create a random 32x32 maze
* ```maze2asm.pl``` to convert the maze to 6502 assembler

Usage:
======
* ```./genmaze.pl | ./maze2asm.pl >coolmaze.dat```
* open ```coolmaze.dat``` in your favourite text editor
* copy the content into your mouse
* paste it into http://skilldrick.github.io/easy6502/ and run
129 changes: 129 additions & 0 deletions 2014-02-27/maze-generator/genmaze.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/perl

use strict;
use warnings;

use Time::HiRes qw( usleep );
use POSIX;
use Data::Dumper;

my @maze = map {[(undef)x32]} 1..32;
my @path = ();

sub push_path {
my $px = shift;
my $py = shift;

die "px=$px out of bounds" unless $px >= 0 && $px < 32;
die "py=$py out of bounds" unless $py >= 0 && $py < 32;

print STDERR "adding path element [$px, $py]\n";
push @path, [$px, $py];
$maze[$px][$py] = 1;
}

sub get_directions {
my $fx = shift;
my $fy = shift;

return grep {$_->[0] >= 0 && $_->[0] < 32 && $_->[1] >= 0 && $_->[1] < 32} (
# [$fx-1, $fy-1],
[$fx-1, $fy],
# [$fx-1, $fy+1],
[$fx, $fy-1],
# [$fx, $fy],
[$fx, $fy+1],
# [$fx+1, $fy-1],
[$fx+1, $fy],
# [$fx+1, $fy+1],
);
}

sub get_neighbors {
my $fx = shift;
my $fy = shift;

return grep {$_->[0] >= 0 && $_->[0] < 32 && $_->[1] >= 0 && $_->[1] < 32} (
[$fx-1, $fy-1],
[$fx-1, $fy],
[$fx-1, $fy+1],
[$fx, $fy-1],
# [$fx, $fy],
[$fx, $fy+1],
[$fx+1, $fy-1],
[$fx+1, $fy],
[$fx+1, $fy+1],
);
}

sub get_unmarked {
return grep {!$maze[$_->[0]][$_->[1]]} @_;
}

sub get_marked {
return grep {$maze[$_->[0]][$_->[1]]} @_;
}

sub print_maze {
for (1..30) {
print STDERR "\n";
}

my $lineno = 0;
foreach my $row (@maze) {
printf STDERR "%2d ", $lineno;
foreach my $field (@$row) {
if ($field) {
print STDERR ".";
}
else {
print STDERR "#";
}
}
print STDERR "\n";
$lineno++;
}
}



push_path(0,0);

while (@path) {
usleep(2*1000);
print_maze();

my $p = $path[$#path];
my $px = $p->[0];
my $py = $p->[1];

my %Np = map {($_->[0]).",".($_->[1]) => 1} get_directions($px, $py);

my @D = get_unmarked(get_directions($px, $py));
my @candidates = grep {1 == int(
grep {!$Np{($_->[0]).",".($_->[1])}} get_marked(get_neighbors($_->[0], $_->[1]))
)} @D;

if (@candidates) {
my $i = floor(rand(int(@candidates)));
push_path($candidates[$i][0], $candidates[$i][1]);
}
else {
print STDERR "dont know what to do on [$px;$py]\n";
pop @path;
}
}



foreach my $row (@maze) {
foreach my $field (@$row) {
if ($field) {
print " ";
}
else {
print "X";
}
}
print "\n";
}
29 changes: 29 additions & 0 deletions 2014-02-27/maze-generator/maze2asm.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/perl

use strict;
use warnings;

my $startaddr = 0x0200;

my %colmap = (
"X" => 0x00,
" " => 0x0e,
);

while (my $line = <>) {
chomp $line;

die "wrong length" unless length($line) == 32;
my @pixels = map {$colmap{$_}} split //, $line;

for (my $i=0; $i<int(@pixels); $i++) {
if (defined($pixels[$i])) {
printf "LDA #\$%02x\n", $pixels[$i];
printf "STA \$%04x\n", $startaddr+$i;
printf "\n";

}
}

$startaddr += 32;
}

0 comments on commit 5d1eaf2

Please sign in to comment.