class Move
    def Move.Mx=(newMx)
        @@Mx = newMx;
    end

    include Comparable
    attr_reader :mrow, :mcol, :word, :value, :lrs
    def initialize(nrow, ncol, nword)
        @mrow = nrow;
        @mcol = ncol;
        @word = nword;
        @value, @lrs = 0, Array.new();
    end

    def <=>(r)
        return (@value <=> r.value);
    end

    def to_s
        "(#{@mrow},#{@mcol}):\"#{@word}\"==#{@value}";
    end
end


class HMove < Move
    def initialize(nrow, ncol, nword)
        super(nrow, ncol, nword.clone);
        mult = false;
        row, col, word = @mrow, @mcol, nword.clone;
        while (not word.empty? and (lr = word.slice!(0,1)) != '0')
            if (@@Mx[row][col].empty?)
                @lrs.push(lr);
                case Scrabble.premSq[row][col]
                    when 'W' then mult = 3;
                    when 'w' then mult ||= 2;
                    when 'L' then @value += Scrabble.lrvals[lr] * 3;
                    when 'l' then @value += Scrabble.lrvals[lr] * 2;
                    else @value += Scrabble.lrvals[lr];
                end
            else
                @value += Scrabble.lrvals[lr];
            end
            col -= 1;
        end
        row, col = @mrow, @mcol+1;
        while (not word.empty?)
            lr = word.slice!(0,1);
            if (@@Mx[row][col].empty?)
                @lrs.push(lr);
                case Scrabble.premSq[row][col]
                    when 'W' then mult = 3;
                    when 'w' then mult ||= 2;
                    when 'L' then @value += Scrabble.lrvals[lr] * 3;
                    when 'l' then @value += Scrabble.lrvals[lr] * 2;
                    else @value += Scrabble.lrvals[lr];
                end
            else
                @value += Scrabble.lrvals[lr];
            end
            col += 1;
        end
        @value *= mult if mult;
    end

    def apply()
        row, col, word = @mrow, @mcol, @word.dup;
        while (not word.empty? and (lr = word.slice!(0,1)) != '0')
            @@Mx[row][col] = lr;
            col -= 1;
        end
        row, col = @mrow, @mcol+1;
        while (not word.empty?)
            @@Mx[row][col] = word.slice!(0,1);
            col += 1;
        end
    end

    def valUp(origRow, col, lr)
        value, mult = 0, nil;
        if (@@Mx[origRow][col].empty?)
            case Scrabble.premSq[origRow][col]
                when 'W' then mult = 3;
                when 'w' then mult ||= 2;
                when 'L' then value += Scrabble.lrvals[lr] * 3;
                when 'l' then value += Scrabble.lrvals[lr] * 2;
                else value += Scrabble.lrvals[lr];
            end
        else
            value += Scrabble.lrvals[lr];
        end
        row = origRow - 1;
        while(row >= 0 and not @@Mx[row][col].empty?)
            value += Scrabble.lrvals[@@Mx[row][col]];
            row -= 1;
        end
        if (origRow < 14)
            row = origRow + 1;
            while(row <= 14 and not @@Mx[row][col].empty?)
                value += Scrabble.lrvals[@@Mx[row][col]];
                row += 1;
            end
        end
        value *= mult if mult;
        return value;
    end

    def to_s()
        "{H-" + super + "}";
    end
end

class VMove < Move
    def initialize(nrow, ncol, nword)
        super(nrow, ncol, nword.clone);
        mult = false;
        row, col, word = @mrow, @mcol, nword.clone;
        while (not word.empty? and (lr = word.slice!(0,1)) != '0')
            if (@@Mx[row][col].empty?)
                @lrs.push(lr);
                case Scrabble.premSq[row][col]
                    when 'W' then mult = 3;
                    when 'w' then mult ||= 2;
                    when 'L' then @value += Scrabble.lrvals[lr] * 3;
                    when 'l' then @value += Scrabble.lrvals[lr] * 2;
                    else @value += Scrabble.lrvals[lr];
                end
            else
                @value += Scrabble.lrvals[lr];
            end
            row -= 1;
        end
        row, col = @mrow+1, @mcol;
        while (not word.empty?)
            lr = word.slice!(0,1);
            if (@@Mx[row][col].empty?)
                @lrs.push(lr);
                case Scrabble.premSq[row][col]
                    when 'W' then mult = 3;
                    when 'w' then mult ||= 2;
                    when 'L' then @value += Scrabble.lrvals[lr] * 3;
                    when 'l' then @value += Scrabble.lrvals[lr] * 2;
                    else @value += Scrabble.lrvals[lr];
                end
            else
                @value += Scrabble.lrvals[lr];
            end
            row += 1;
        end
        @value *= mult if mult;
    end

    def apply()
        row, col, word = @mrow, @mcol, @word.dup;
        while (not word.empty? and (lr = word.slice!(0,1)) != '0')
            @@Mx[row][col] = lr;
            row -= 1;
        end
        row, col = @mrow+1, @mcol;
        while (not word.empty?)
            @@Mx[row][col] = word.slice!(0,1);
            row += 1;
        end
    end

    def valLeft(row, origCol, lr)
        value, mult = 0, nil;
        if (@@Mx[row][origCol].empty?)
            case Scrabble.premSq[row][origCol]
                when 'W' then mult = 3;
                when 'w' then mult ||= 2;
                when 'L' then value += Scrabble.lrvals[lr] * 3;
                when 'l' then value += Scrabble.lrvals[lr] * 2;
                else value += Scrabble.lrvals[lr];
            end
        else
            value += Scrabble.lrvals[lr];
        end
        col = origCol - 1;
        while(row >= 0 and not @@Mx[row][col].empty?)
            value += Scrabble.lrvals[@@Mx[row][col]];
            col -= 1;
        end
        if (origRow < 14)
            row = origRow + 1;
            while(row <= 14 and not @@Mx[row][col].empty?)
                value += Scrabble.lrvals[@@Mx[row][col]];
                col += 1;
            end
        end
        value *= mult if mult;
        return value;
    end
    
    def to_s
        "{V-" + super + "}";
    end
end
