Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 67b372df8a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 214 lines (176 sloc) 6.365 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
use v6;

use Druid::Game;
use Druid::View;

#=[A textual view of a C<Druid::Game>. Draws a large isometric 3D view, with
the pieces rendered as ASCII blocks, and two smaller 2D views giving
information about the colors and heights of the pieces on the board.]
class Druid::View::Text is Druid::View;

has $!cached-board;

my $v-piece = '
+-----+
/|#v#v#|
||#v#v#|
|+-----+
/-----/
';

my $h-piece = '
+-----+
/|#h#h#|
||#h#h#|
|+-----+
/-----/
';

my $cover-right = '

#
#
';

my $cover-top = '
#####
';

my $cover-top-right = '
#
';

#=[Returns a string containing an ASCII picture of an empty Druid board of
the given size.]
sub make-empty-board($size) {
    # The 'join $sep, gather { ... }' pattern allows us to put a long
    # string together, without having to refer to the same variable over
    # and over.
    return join "\n", gather {
        take '';
        take my $heading
            = [~] ' ', map {" $_ "}, map {chr($_+ord('A'))}, ^$size;
        take my $line = [~] ' ', '+-----' x $size, '+';
        for (1..$size).reverse -> $r {
            take [~] (sprintf '%2d |', $r),
                     ' ' x ($size) - 1,
                     " | $r";
            take [~] ' |', ' ' x ($size) - 1, ' |';
            if $r > 1 {
                take [~] ' +', ' +' x $size;
            }
        }
        take $line;
        take $heading;
        take '';
    };
}

submethod BUILD() {
    $!cached-board = make-empty-board($!game.size);
}

#=[Prints the 3D game board and the two smaller sub-boards, reflecting the
current state of the game.]
method show() {
    .print for $!cached-board, self.colors-and-heights();
}

#=[Returns the 3D game board and the two smaller sub-boards, reflecting the
current state of the game.]
method Str() {
    return [~] $!cached-board, self.colors-and-heights();
}

method build-layers($board is copy, $from) {
    my @layers = @.layers[$from .. @.layers.end];
    for @layers.kv -> $relheight, $layer {
        my $height = $relheight + $from;
        for $layer.kv.reverse -> $line, $row {
            for $line.kv.reverse -> $cell, $column {

                next if $cell == 0;

                given ($v-piece, $h-piece)[$cell-1] -> $piece {
                    $board = put( $piece, $board, $height, $row, $column );
                    if $column < $.size - 1
                       && $layer[$row][$column] == $layer[$row][$column+1] {
                        $board = put( $cover-right, $board,
                                      $height, $row, $column );
                    }
                    if $row < $.size - 1
                       && $layer[$row][$column] == $layer[$row+1][$column] {
                        $board = put( $cover-top, $board,
                                      $height, $row, $column );
                    }
                    if $row & $column < $.size - 1
                       && $layer[$row][$column]
                          == $layer[$row+1][$column]
                          == $layer[$row][$column+1]
                          == $layer[$row+1][$column+1] {
                        $board = put( $cover-top-right, $board,
                                      $height, $row, $column );
                    }
                }
            }
        }
    }

    return $board;
}

#=[Given a string representing a piece and one representing the board,
returns a new board with the piece inserted into some coordinates. This
sub assumes that pieces are drawn in an order that makes sense, so that
pieces in front cover those behind.]
sub put($piece, $board, $height, $row, $column) {
    my @lines = $board.split("\n");

    my $coord-line = +@lines - 8 - 3 * $row - $height;

    return put($piece, "\n" ~ $board, $height, $row, $column)
        if $coord-line < 0;

    my $coord-column = 3 + 6 * $column + $height;

    for $piece.split("\n").kv -> $line-no, $piece-line {
        my $board-line = @lines[$coord-line + $line-no];
        @lines[ $coord-line + $line-no ]
            = merge($board-line, $piece-line, $coord-column);
    }

    return @lines.join("\n");
}

#=[Given a string (assumed to contain no newlines), replaces a section of
that string, starting from $column, with the contents of $new.
When replacing characters, two excpetions are made:
=for item
    spaces in $new are treated as 'transparent', so that they
    don't replace the old character,
=for item
octothorpes '#' insert actual spaces, i.e. act as a sort of
    escape character for spaces.]
sub merge($old, $new, $start) {
    my @old = $old.split('');
    my @new = $new.split('');

    # RAKUDO: xx and push don't seem to work as advertised.
    push @old, ' ' for ^($start + $new.chars - $old.chars);

    for @new.kv -> $i, $char {
        @old[$start + $i] = $char unless $char eq ' ';
        @old[$start + $i] = ' ' if $char eq '#'
    }

    return @old.join('');
}

#=[Prints two smaller boards representing
=item who owns each location, and
=item how many stones have been piled on each location.]
method colors-and-heights() {

    my &from-pretty = { $^pretty.trans( ['>>', '<<', '.']
                                        => ['%2d','%-2d','%s'] ) };

    my &format-colors = { <. v h>[$^color] };
    my &format-heights = { $^height || '.' };

    my $letters = 'A'..chr(ord('A') + $.size - 1);

    my $inter-board-space
        = ' ' x (1 + 6*$.size - 2*$.size - 2*($.size-1) - 14);
    my $board-line = [~] '>> ', ('.' xx $.size).join(' '), ' <<';

    my $footer = [~] "\n ", $letters.join(' '),
                     ' ' x 8, $inter-board-space,
                     $letters.join(' '), "\n";
    my $header = "$footer\n";

    return gather {
        take $header;
        for (1..$.size).reverse -> $row {
            take sprintf from-pretty(
                    [~] ' ', $board-line, $inter-board-space, $board-line
                ),
                $row, (map &format-colors, @.colors[$row-1].values), $row,
                $row, (map &format-heights, @.heights[$row-1].values), $row;
            take "\n";
        }
        take $footer;
    };
}

method add-piece($height, $row, $column, $color) {
    $!cached-board = self.build-layers($!cached-board, $height);
}

method reset() {
    $!cached-board = self.build-layers($!cached-board, 0);
}

# vim: filetype=perl6
Something went wrong with that request. Please try again.