use strict; use Tk; use Tk::DialogBox; use Tk::FileDialog; use Tk::Checkbutton; use Tk::OptionMenu; use File::Glob; #My custom class. use Skrabbel; #Our Important Globals my $MW; #MainWindow my $VERSION = '0.1'; my $scrabble; #Our class object that maintains the game my $board_frame; #Frame for Scrabble Board my $entry_frame; #Frame for all entries my @square_frames; #1-D Array of Frames my @squares; #2-D Array of Labels (15x15) my $entry_word; #Word to enter onto board my $x_pt; #X coordinate point on board my $y_pt; #Y coordinate point on board my $orientation; #Direction of new word (Horizontal,Vertical) my $my_letters; #Current letters you have to play my $num_results; #Number of results we want when finding best my $value; #current value of word being shown my $best_moves; #reference to array of best moves my $preview_showing; #which preview in array we are showing my $search_time; #time it took to run search #Set up the mainwindow $MW = MainWindow->new( -title => "Scrabbler - v$VERSION" ); $MW->resizable(0,0); $MW->withdraw(); #Set up our board_frame $board_frame = $MW->Frame( )->pack( -side => 'left', ); my $top_axis_frame = $board_frame->Frame( )->pack( -side => 'top', -fill => 'x' ); $top_axis_frame->Label( -text => " # ", -width => 2, -font => [ -weight => 'bold', -size => 12 ], -relief => 'flat', -borderwidth => 1 )->pack( -side => 'left', -padx => 1 ); for (my $x = 0; $x < 15; $x++) { $top_axis_frame->Label( -text => " $x ", -width => 2, -font => [ -weight => 'bold', -size => 12 ], -relief => 'flat', -borderwidth => 1 )->pack( -side => 'left', -padx => 1, -pady => 1 ); } for (my $x = 0; $x < 15; $x++) { $square_frames[$x] = $board_frame->Frame( )->pack( -side => 'top', -fill => 'x', -expand => 1 ); $square_frames[$x]->Label( -text => " $x ", -width => 2, -font => [ -weight => 'bold', -size => 12 ], -relief => 'flat', -borderwidth => 1 )->pack( -side => 'left', -padx => 1 ); for (my $y = 0; $y < 15; $y++) { $squares[$x][$y] = $square_frames[$x]->Label( -text => " ", -width => 2, -font => [ -weight => 'bold', -size => 12 ], -relief => 'sunken', -borderwidth => 1 )->pack( -side => 'left', -padx => 1 ); } } #Color our squares on board_frame #make the 3W (Triple Word) squares foreach my $x (0,7,14) { foreach my $y (0,7,14) { $squares[$x][$y]->configure( -background => 'red' ); } } #make the 2W (Double Word) squares foreach my $x (1,2,3,4,7) { $squares[$x][$x]->configure( -background => 'orange' ); $squares[$x][14 - $x]->configure( -background => 'orange' ); $squares[14 - $x][$x]->configure( -background => 'orange' ); $squares[14 - $x][14 - $x]->configure( -background => 'orange' ); } #make the 3L (Triple Letter) squares foreach my $x (5,9) { foreach my $y (5,9) { $squares[$x][$y]->configure( -background => 'blue' ); } } foreach my $x (1,13) { foreach my $y (1,5) { $squares[$x][$y]->configure( -background => 'blue' ); $squares[$x][14 - $y]->configure( -background => 'blue' ); } } #make the 2L (Double Letter) squares foreach my $x (0,7,14) { foreach my $y (3,11) { $squares[$x][$y]->configure( -background => 'yellow' ); $squares[$y][$x]->configure( -background => 'yellow' ); } } foreach my $x (2,12,6,8) { foreach my $y (6,8) { $squares[$x][$y]->configure( -background => 'yellow' ); $squares[$y][$x]->configure( -background => 'yellow' ); } } #set our Entries frame $entry_frame = $MW->Frame( )->pack( -side => 'right', -fill => 'both', -expand => 1 ); #Our "Place Move" title $entry_frame->Label( -text => "Place Move", -font => [ -size => 12, -weight => 'bold', -underline => 1 ] )->pack( -side => 'top' ); #Our Word-Entry frame my $word_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); my $word_entry = $word_frame->Entry( -textvariable => \$entry_word, -width => 18, -font => [ -size => 10, -weight => 'bold' ], )->pack( -side => 'right' ); $word_frame->Label( -text => "Word: ", -font => [ -size => 10, -weight => 'bold' ], )->pack( -side => 'right' ); #Our Coordinate Entry Frame my $location_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); $location_frame->Entry( -textvariable => \$x_pt, -width => 2, -font => [ -size => 10, -weight => 'bold' ], )->pack( -side => 'right' ); $location_frame->Label( -text => " col:", -font => [ -size => 10 ] )->pack( -side => 'right' ); $location_frame->Entry( -textvariable => \$y_pt, -width => 2, -font => [ -size => 10, -weight => 'bold' ], )->pack( -side => 'right' ); $location_frame->Label( -text => "row:", -font => [ -size => 10 ] )->pack( -side => 'right' ); $location_frame->Label( -text => "Starting Point: ", -font => [ -size => 10, -weight => 'bold' ], )->pack( -side => 'right' ); #Our Direction Entry Frame my $direction_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); $direction_frame->Optionmenu( -variable => \$orientation, -options => [ "Horizontal", "Vertical" ], -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'right' ); $direction_frame->Label( -text => "Orientation: ", -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'right' ); #Our button to activate place-move $entry_frame->Button( -text => "Add Word", -font => [ -size => 10, -weight => 'bold' ], -command => [\&add_word], -background => 'orange', -activebackground => 'green' )->pack( -side => 'top', -anchor => 'e', -pady => 5 ); #A little separator $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 10 ); #Our "Find Best Move" title $entry_frame->Label( -text => "Find Best Move(s)", -font => [ -size => 12, -weight => 'bold', -underline => 1 ], )->pack( -side => 'top' ); #our "My Letters" frame my $letters_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); $letters_frame->Entry( -textvariable => \$my_letters, -width => 15, -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'right', -padx => 5 ); $letters_frame->Label( -text => "My Letters: ", -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'right', -padx => 5 ); #Our "Number of Results" frame my $results_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); $results_frame->Entry( -textvariable => \$num_results, -width => 3, -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'right', -padx => 5 ); $results_frame->Label( -text => "Num of Results: ", -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'right', -padx => 5 ); #Our frame for controlling scrolling results my $scroll_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); $scroll_frame->Button( -text => "next", -font => [ -size => 10, -weight => 'bold' ], -activebackground => 'green', -command => sub { $preview_showing++ if ($preview_showing < scalar(@{$best_moves}) - 1); preview($$best_moves[$preview_showing]); } )->pack( -side => 'right', -padx => 5 ); $scroll_frame->Button( -text => "prev", -height => .5, -font => [ -size => 10, -weight => 'bold' ], -activebackground => 'green', -command => sub { $preview_showing-- if ($preview_showing > 0); preview($$best_moves[$preview_showing]); } )->pack( -side => 'right', -padx => 5 ); $scroll_frame->Label( -text => "Value: ", -font => [ -size => 10, -weight => 'bold', ] )->pack( -side => 'left', -padx => 2 ); $scroll_frame->Label( -textvariable => \$value, -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'left' ); #Our buttons for finding or clearing results my $clear_frame = $entry_frame->Frame( )->pack( -side => 'top', -fill => 'x', -pady => 5 ); my $find_button = $clear_frame->Button( -text => "Find", -font => [ -size => 10, -weight => 'bold' ], -activebackground => 'green', -command => [\&find_results] )->pack( -side => 'right', -padx => 5 ); $clear_frame->Button( -text => "Clear", -font => [ -size => 10, -weight => 'bold' ], -activebackground => 'green', -command => [\&clear_results] )->pack( -side => 'right', -padx => 5 ); $clear_frame->Label( -text => "Time:", -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'left', -padx => 3 ); $clear_frame->Label( -textvariable => \$search_time, -font => [ -size => 10, -weight => 'bold', ], -width => 6 )->pack( -side => 'left', -padx => 3 ); #our bottom button frame my $button_frame = $entry_frame->Frame( )->pack( -side => 'bottom', -fill => 'x', -pady => 5 ); $button_frame->Button( -text => "Save Board", -command => [\&save_select], -font => [ -size => 10, -weight => 'bold' ], -background => 'orange', -activebackground => 'green' )->pack( -side => 'right', -padx => 5 ); $button_frame->Button( -text => "Load Board", -command => [\&load_select], -font => [ -size => 10, -weight => 'bold' ], -background => 'orange', -activebackground => 'green' )->pack( -side => 'right', -padx => 5 ); #configure file browsing object my $dictionary_window = $MW->FileDialog( -Title => 'Select Word List', -SelHook => \&start, -ShowAll => 1, -Create => 0 ); my $load_window = $MW->FileDialog( -Title => 'Select Board to Load', -SelHook => \&load_board, -ShowAll => 1, -Create => 0 ); my $save_window = $MW->FileDialog( -Title => 'Where to Save Board', -SelHook => \&save_board, -ShowAll => 1, -Create => 1 ); #Pop up our window to get word list $dictionary_window->Show(); #This function creates our scrabble class #and gets things started sub start($) { my ($word_list) = @_; my $dir = $word_list; while (chop($dir) ne '/') {} #bring down this window $dictionary_window->destroy(); $load_window->configure( -Path => $dir ); $save_window->configure( -Path => $dir ); #load module and board unless ($scrabble = Skrabbel->new($word_list)) { pop_message("Constructor Failed",1); } $MW->geometry('+100+100'); $MW->Popup(); MainLoop(); exit; } #This just updates the values of our displayed board #based on the 2-D array-ref argument sub update_board($) { my ($new_board) = @_; for (my $x = 0; $x < 15; $x++) { for (my $y = 0; $y < 15; $y++) { my $txt = $$new_board[$x][$y]; $squares[$x][$y]->configure( -text => "$txt" ); } } } #This function previews an additional word on our board sub preview($) { my ($move) = @_; my $val_buffer = $$move{'value'}; $value = $val_buffer; #breaking word into array of chars my @word_array = split(//,uc($$move{'word'})); #first update to actual board update_board($scrabble->curr_board()); #if horiz we preview here if($$move{'orientation'} eq "Horizontal") { for(my ($x,$tmp_x) = (0,$$move{'x_pt'}); $x < length($$move{'word'}); $x++,$tmp_x++) { $squares[$$move{'y_pt'}][$tmp_x]->configure( -text => "$word_array[$x]" ); } } #if vertical then here elsif($$move{'orientation'} eq "Vertical") { for(my ($y,$tmp_y) = (0,$$move{'y_pt'}); $y < length($$move{'word'}); $y++,$tmp_y++) { $squares[$tmp_y][$$move{'x_pt'}]->configure( -text => "$word_array[$y]" ); } } } #this function just clears all our fields #and resets our board sub clear_results() { $num_results = ''; $my_letters = ''; $value = ''; update_board($scrabble->curr_board()); } #This function gathers the data in entry_frame #And tries to add a word to our board! sub add_word() { #Some basic error-checking if ($x_pt =~ /\D/ || $y_pt =~ /\D/ || $x_pt > 14 || $x_pt < 0 || $y_pt > 14 || $y_pt < 0 || $y_pt eq "" || $x_pt eq "") { pop_message("Coordinates Are Improper",0); return 0; } #have our object add this word if($scrabble->add_word($entry_word, $x_pt, $y_pt, $orientation)) { update_board($scrabble->curr_board()); return 1; } else { pop_message("New Word Won't Fit On Board",0); return 0; } } #Here we pop our filedialog to select file to load sub load_select() { $load_window->raise(); $load_window->Show(); } #Here we load the board in the functions first and only argument sub load_board($) { my ($file) = @_; #let's have our filedialog pop up in this dir next time my $dir = $file; while (chop($dir) ne "/") {}; $load_window->configure( -Path => $dir ); #Now we call our class and get it done if($scrabble->load_board($file)) { update_board($scrabble->curr_board()); return 1; } else { pop_message("Couldn't Load Board At: $file",0); return 0; } } #Here we pop up our filedialog to select file to save board to sub save_select() { $save_window->raise(); $save_window->Show(); } #Here we save our board to the file in the functions first #and only argument sub save_board($) { my ($file) = @_; #let's have our filedialog pop up in this dir next time my $dir = $file; while (chop($dir) ne "/") {}; $save_window->configure( -Path => $dir ); #Now we call our class and get it done if($scrabble->save_board($file)) { pop_message("Board Saved Successfully",0); return 1; } else { pop_message("Couldn't Save Board To: $file",0); return 0; } } #Here we ask our object to do it's thing and we find #the top results. sub find_results() { my $time = time(); $best_moves = $scrabble->find_results($my_letters, $num_results); $time = time() - $time; my $min = int($time/60); my $sec = $time - ($min * 60); $search_time = "$min" . "m " . "$sec" . "s"; if($best_moves) { preview($$best_moves[0]); $preview_showing = 0; pop_message("Move Search Complete",0); } else { pop_message("Find Request Failed",0); } } #This function replaces our Win32 MsgBox's. We pop #up message and then whether it's a program ender #or just a message. sub pop_message($$) { my ($msg,$death) = @_; #Create our toplevel object my $PW = $MW->Toplevel( -title => "Message For Ya", -takefocus => 1 ); $PW->resizable(0,0); $PW->protocol('WM_DELETE_WINDOW',sub {;}); $PW->withdraw(); #If it's the end we take away MW if ($death) { $MW->withdraw(); } #Now make our label and button $PW->Label( -text => "$msg", -font => [ -size => 10, -weight => 'bold' ] )->pack( -side => 'top', -pady => 5 ); my $button = $PW->Button( -text => "Ok", -font => [ -size => 10, -weight => 'bold' ], -activebackground => 'green', -width => 10, -command => [ sub { if ($death) { $MW->destroy(); } else { $PW->destroy(); } } ] )->pack( -side => 'top', -pady => 5 ); #Pop up our window $PW->Popup(); $button->focus(); }