#############################################################
#THIS IS OUR TEST ZONE
#############################################################
require 'scrabble.rb';
require 'rubybug.rb';

myBug = RubyBug.new(Proc.new {
myBud = Scrabble.new("lexDict.rbo"); #create new object

#make all of the tiles for a practice game
if (not ARGV.empty? and ARGV.first == "make")
    tiles = (["a"]*9)+(["b"]*2)+(["c"]*2)+(["d"]*4)+(["e"]*12)+
        (["f"]*2)+(["g"]*3)+(["h"]*2)+(["i"]*9)+["j"]+["k"]+
        (["l"]*4)+(["m"]*2)+(["n"]*6)+(["o"]*8)+(["p"]*2)+["q"]+
        (["r"]*6)+(["s"]*4)+(["t"]*6)+(["u"]*4)+(["v"]*2)+
        (["w"]*2)+["x"]+(["y"]*2)+["z"];
    tiles.sort!{rand(3)-1};
    File.open("tiles.rbo", "w+") { |file|
        Marshal.dump(tiles, file);
    }
    exit(0);
end

tiles = Marshal.load(File.open("tiles.rbo").read);
letters = Array.new(); #this'll have letters for each play
myBud.vertIns(7, 1, "lazy"); #put "lazy" vertically on our board
num = 0; #counts number of plays we make

start = Time.now; #count our time of execution
30.times {
    break if tiles.empty? and letters.include?(nil)
    letters.push(tiles.shift()) while (not tiles.empty? and 
        letters.length < 7);
    moves = myBud.search(letters); #here's our search
    num += 1;
    unless (not moves.empty?)
        letters.push(tiles.shift());
        next;
    end

    best = moves.last; #get the best move, at end of sorted results

    #output info to file
    File.open("out.test", "a") { |file| #open file to output debug info
        myBud.Mx.each {|row| file.puts row.map{|x|
            x.empty? ? " " : x}.join("-");}
        file.puts "Letters: [#{letters.join(", ")}]";
        file.puts "Best: [#{best.mrow},#{best.mcol}]--#{best.word}," +
            " #{best.value}";
        file.puts moves.map{ |m|
            "#{m.mrow},#{m.mcol}: #{m.word}, #{m.value}"}.join("\n\t");
        file.puts "----------------------------------------";
    }

    letters -= best.lrs;
    best.apply; #place move onto board
    myBud.saveB("board.txt");
}

puts Time.now - start;
puts num;
});

myBug.trace("@word", "@mrow", "@mcol", "@value", "@lrs",
            "word", "row", "col", "@srow", "@scol",
            "alone", "lrs", "nextL", "@results", "lr");

myBug.breakPoints({
    "scrabble.rb" => [34, 38, 86, 143]
});

if (not ARGV.empty?)
    myBug.justRun();
else
    myBug.start;
end
