require 'tk'
class Board
    def initialize(board_frame)
        frames = Array.new;
        @board = Array.new;
        15.times {
            frames.push(TkFrame.new(board_frame) {
                border 2
                background 'black'
                pack('side' => 'top')
            });
        }
        brdFont = TkFont.new('courier');
        brdFont.configure('weight' => 'bold', 'size' => 12);
        frames.each_with_index { |item, row|
            @board[row] = Array.new();
            0.upto(14) { |col|
                curColor = color(row, col);
                @board[row][col] = TkLabel.new(item) {
                    width 2
                    background curColor
                    pack('side' => 'left', 'padx' => 2)
                };
                @board[row][col].font(brdFont)
            }
        }
    end

    def color(row, col)
        case Scrabble.premSq[row][col]
            when "W" then "red"
            when "w" then "yellow"
            when "L" then "orange"
            when "l" then "pink"
            else "white"
        end
    end

    def setB(mx)
        0.upto(14) {|row| 0.upto(14) {|col|
            @board[row][col].configure('text' => 
                mx[row][col].capitalize);
        } }
    end
    
    def placeMove(nMove)
        horiz = nMove.kind_of? HMove;
        row, col, word = nMove.mrow, nMove.mcol, nMove.word.dup;
        while (not word.empty? and (lr = word.slice!(0,1)) != '0')
            @board[row][col].configure('text' =>
                lr.capitalize);
            horiz ? col -= 1 : row -= 1;
        end
        row, col = nMove.mrow, nMove.mcol;
        horiz ? col = nMove.mcol+1 : row = nMove.mrow+1;
        while (not word.empty?)
            @board[row][col].configure('text' =>
                word.slice!(0,1).capitalize);
            horiz ? col += 1 : row += 1;
        end
    end
end
