Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / site_perl / 5.8.0 / sun4-solaris / Tk / demos / widget_lib / puzzle.pl
CommitLineData
86530b38
AT
1# puzzle.pl
2
3use subs qw/puzzle_switch/;
4use vars qw/$TOP/;
5
6sub puzzle {
7
8 # Create a top-level window containing a 15-puzzle game.
9
10 my($demo) = @_;
11 $TOP = $MW->WidgetDemo(
12 -name => $demo,
13 -text => 'A 15-puzzle appears below as a collection of buttons. Click on any of the pieces next to the space, and that piece will slide over the space. Continue this until the pieces are arranged in numerical order from upper-left to lower-right.',
14 -title => '15-Puzzle Demonstration',
15 -iconname => 'puzzle',
16 );
17
18 # Special trick: select a darker color for the space by creating a
19 # scrollbar widget and using its trough color.
20
21 my $s = $TOP->Scrollbar;
22 my $frame = $TOP->Frame(
23 -width => 120,
24 -height => 120,
25 -borderwidth => '2',
26 -relief => 'sunken',
27 -background => $s->cget(-troughcolor),
28 );
29 $frame->pack(qw/-side top -padx 1c -pady 1c/);
30 $s->destroy;
31
32 my(@order) = (3, 1, 6, 2, 5, 7, 15, 13, 4, 11, 8, 9, 14, 10, 12);
33 my %xpos = ();
34 my %ypos = ();
35
36 my($i, $num, $frame_num);
37 for ($i=0; $i<15; $i++) {
38 $num = $order[$i];
39 $xpos{$num} = ($i%4) * 0.25;
40 $ypos{$num} = (int($i/4)) * 0.25;
41 $frame_num = $frame->Button(
42 -relief => 'raised',
43 -text => $num,
44 -highlightthickness => 0,
45 );
46 $frame_num->configure(
47 -command => [\&puzzle_switch, $frame_num, $num, \%xpos, \%ypos],
48 );
49 $frame_num->place(
50 -relx => $xpos{$num},
51 -rely => $ypos{$num},
52 -relwidth => 0.25,
53 -relheight => 0.25,
54 );
55 } # forend all puzzle numbers
56 $xpos{'space'} = 0.75;
57 $ypos{'space'} = 0.75;
58
59} # end puzzle
60
61sub puzzle_switch {
62
63 # Procedure invoked by buttons in the puzzle to resize the puzzle entries.
64
65 my($w, $num, $xpos, $ypos) = @_;
66
67 if ( (($ypos->{$num} >= ($ypos->{'space'} - 0.01)) &&
68 ($ypos->{$num} <= ($ypos->{'space'} + 0.01))
69 && ($xpos->{$num} >= ($xpos->{'space'} - 0.26)) &&
70 ($xpos->{$num} <= ($xpos->{'space'} + 0.26)))
71 || (($xpos->{$num} >= ($xpos->{'space'} - 0.01)) &&
72 ($xpos->{$num} <= ($xpos->{'space'} + 0.01))
73 && ($ypos->{$num} >= ($ypos->{'space'} - 0.26)) &&
74 ($ypos->{$num} <= ($ypos->{'space'} + 0.26))) ) {
75 my $tmp = $xpos->{'space'};
76 $xpos->{'space'} = $xpos->{$num};
77 $xpos->{$num} = $tmp;
78 $tmp = $ypos->{'space'};
79 $ypos->{'space'} = $ypos->{$num};
80 $ypos->{$num} = $tmp;
81 $w->place(-relx => $xpos->{$num}, -rely => $ypos->{$num});
82 }
83
84} # end puzzle_switch
85
861;