Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed the problem with the who-won algorithm

...by not being so attached to a custom-made class. A simple hash worked
just as well.
  • Loading branch information...
commit 5444d9fa17a0419770899a3b4de773a0285f09ee 1 parent 1fb7066
Carl Mäsak authored

Showing 1 changed file with 13 additions and 26 deletions. Show diff stats Hide diff stats

  1. +13 26 lib/Druid/Game.pm
39 lib/Druid/Game.pm
@@ -2,14 +2,6 @@ use v6;
2 2
3 3 use Druid::Game::Subject;
4 4
5   -# RAKUDO: Would like to make this class local to move_was_winning, using
6   -# 'my class', but that is not implemented yet.
7   -class Pos {
8   - has $.row is rw;
9   - has $.column is rw;
10   - method Str { join ',', $.row, $.column }
11   -}
12   -
13 5 # RAKUDO: Cannot declare class after use-ing Druid::Game::Subject.
14 6 # [perl #62898]
15 7 class Druid::Game_ does Druid::Game::Subject {
@@ -109,42 +101,37 @@ class Druid::Game_ does Druid::Game::Subject {
109 101 default { return False; } # unknown move type
110 102 }
111 103
112   - my @pos_queue = Pos.new( :row($row), :column($column) );
  104 + my @pos_queue = { :$row, :$column };
113 105
114 106 my $last_color = @!colors[$row][$column];
115 107
116   - my &above = { .row < $!size - 1 && .clone( :row(.row + 1) ) };
117   - my &below = { .row > 0 && .clone( :row(.row - 1) ) };
118   - my &right = { .column < $!size - 1 && .clone( :column(.column + 1) ) };
119   - my &left = { .column > 0 && .clone( :column(.column - 1) ) };
  108 + my &above = { .<row> < $!size - 1 && { :row(.<row> + 1),
  109 + :column(.<column>) } };
  110 + my &below = { .<row> > 0 && { :row(.<row> - 1),
  111 + :column(.<column>) } };
  112 + my &right = { .<column> < $!size - 1 && { :row(.<row>),
  113 + :column(.<column> + 1) } };
  114 + my &left = { .<column> > 0 && { :row(.<row>),
  115 + :column(.<column> - 1) } };
120 116
121 117 my %visited;
122 118 my $reached_one_end = False;
123 119 my $reached_other_end = False;
124 120
125   - # I can't quite figure out why, but debug statements reveal that the
126   - # loops test the initial position twice, despite the fact that it's only
127   - # added to the array once.
128 121 while shift @pos_queue -> $pos {
129 122 ++%visited{~$pos};
130 123
131 124 for &above, &below, &right, &left -> &direction {
132   - my $r = $pos.row;
133   - my $c = $pos.column;
  125 + my ($r, $c) = .<row>, .<column> given $pos;
134 126 if direction($pos) -> $neighbor {
135 127
136 128 if !%visited.exists(~$neighbor)
137   - && @!colors[$neighbor.row][$neighbor.column]
138   - == $last_color {
  129 + && @!colors[$neighbor<row>][$neighbor<column>]
  130 + == $last_color {
139 131
140   - push @pos_queue, Pos.new( :row($neighbor.row),
141   - :column($neighbor.column) );
  132 + push @pos_queue, $neighbor;
142 133 }
143 134 }
144   - # RAKUDO: Need to restore the values of $pos this way as long as
145   - # the .clone bug persists.
146   - $pos.row = $r;
147   - $pos.column = $c;
148 135 }
149 136
150 137 if $last_color == 1 && !above($pos)

0 comments on commit 5444d9f

Please sign in to comment.
Something went wrong with that request. Please try again.