Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active October 16, 2015 18:45
Show Gist options
  • Save ab5tract/bb20ff79dddf965febe4 to your computer and use it in GitHub Desktop.
Save ab5tract/bb20ff79dddf965febe4 to your computer and use it in GitHub Desktop.
unit class Grid;
#use Terminal::Print::Commands;
#
#constant T = Terminal::Print::Commands;
has @.grid;
has $!character-supply;
has $!control-supply;
has $.max-columns;
has $.max-rows;
has @.grid-indices;
submethod BUILD( :$!max-columns, :$!max-rows ) {
@!grid-indices = ^$!max-rows X ^$!max-columns;
for ^$!max-columns -> $x {
for ^$!max-rows -> $y {
@!grid[$x;$y] = Str.new(:value(" "));
}
}
$!character-supply = Supply.new;
$!control-supply = Supply.new;
}
method initialize {
start {
react {
whenever $!character-supply -> [$x,$y,$c] {
@!grid[$x;$y] = $c;
}
whenever $!control-supply -> $command {
given $command {
# I have a feeling this isn't actually doing what I think it's doing
# Probably need to rewrite this whole react block as a Promise
# and keep the vow here. Then we can just spin up a new Promise whenever
# initialize gets called again, or throw an exception if the current 'React Promise'
# is not yet kept.
when 'close' { done; }
}
}
}
}
}
method shutdown {
$!control-supply.emit('close');
}
method change-cell($x,$y,$c) {
$!character-supply.emit([$x,$y,$c]);
}
method AT-POS($x,$y) {
@!grid[$x;$y];
}
method EXISTS-POS($x,$y) {
@!grid[$x;$y]:exists;
}
method BIND-POS($x,$y,$v) {
@!grid[$x;$y] := $v;
}
method ASSIGN-POS($x,$y,$v) {
# my $v = @coords[*-1];
say "v:$v,x:$x,y:$y";
# self.AT-POS(@coords) = $v;
@!grid[$x;$y] = $v;
}
use v6;
use lib './';
use test;
use Grid;
my $grid;
lives-ok { $grid = Grid.new(:max-rows(20),:max-columns(10)); dd $grid.grid }, 'Grid survived';
lives-ok { $grid.initialize; dd $grid.grid }, '$grid.initialize works';
lives-ok { $grid.change-cell(1,1,'Y') }, '$grid.change-cell works';
ok $grid[1;1] eq 'Y', 'Inserted value present where expected';
lives-ok { $grid[0;0] = "X" }, 'Can assign "X" to $grid[0;0]';
lives-ok { $grid.shutdown }, '$grid.shutdown works';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment