#!perl

# Rubix Values and Routines

@GetEdge=( # All 12 Edge Locations
"1:37",
"5:28",
"7:19",
"3:10",
"14:21",
"16:48",
"12:41",
"23:30",
"25:46",
"32:39",
"34:50",
"43:52");
@GetCorner=( # All 9 Corner Locations
"0:9:38",
"2:36:29",
"8:27:20",
"6:18:11",
"45:17:24",
"47:26:33",
"53:35:42",
"51:44:15");

@colors=(
"Red","White","Green","Blue","Yellow","Orange","Cyan","Pink","Magenta");

@table=(
"</td><td>","</td></tr><tr><td>","</td><td>","</td><td>","</td><td>","</td></tr><tr><td></td><td>");

# Used for Applet
$facelets = "102112122101111121100110120220210200221211201222212202000001002010011012020021022322321320312311310302301300502512522501511521500510520422421420412411410402401400";

sub compact
{
  local($_)=@_;
  while (($i=index($_,'S'))>=$[)
  {
    substr($_,$i,1)="";
    substr($_,$i)=~tr/D-O/G-OD-F/;
  }
  s/[T-Z]/T/g;
  1 while (s/AC|BB|CA|DF|EE|FD|GI|HH|IG|JL|KK|LJ|MO|NN|OM|PR|QQ|RP//g
           || s/ATC|BTB|CTA|DTF|ETE|FTD|GTI|HTH|ITG|JTL|KTK|LTJ|MTO|NTN|OTM|PTR|QTQ|RTP/T/g
           || s/([A-C])(T*)([P-R])/$3$2$1/g
           || s/([D-F])(T*)([J-L])/$3$2$1/g
           || s/([G-I])(T*)([M-O])/$3$2$1/g
           || s/B(T*)C|C(T*)B/$1$2A/g || s/A(T*)A|C(T*)C/$1$2B/g || s/A(T*)B|B(T*)A/$1$2C/g
           || s/E(T*)F|F(T*)E/$1$2D/g || s/D(T*)D|F(T*)F/$1$2E/g || s/D(T*)E|E(T*)D/$1$2F/g
           || s/H(T*)I|I(T*)H/$1$2G/g || s/G(T*)G|I(T*)I/$1$2H/g || s/G(T*)H|H(T*)G/$1$2I/g
           || s/K(T*)L|L(T*)K/$1$2J/g || s/J(T*)J|L(T*)L/$1$2K/g || s/J(T*)K|K(T*)J/$1$2L/g
           || s/N(T*)O|O(T*)N/$1$2M/g || s/M(T*)M|O(T*)O/$1$2N/g || s/M(T*)N|N(T*)M/$1$2O/g
           || s/Q(T*)R|R(T*)Q/$1$2P/g || s/P(T*)P|R(T*)R/$1$2Q/g || s/P(T*)Q|Q(T*)P/$1$2R/g);
  s/T/t/; s/T/U/; s/T/V/; s/T/W/; s/T/X/; s/T/Y/; s/T/Z/; s/t/T/;
  $_;
}

sub Export
{
  $result="";
  for ($k=0;$k<6;$k++)
  {for ($i=0;$i<3;$i++)
  {for ($j=0;$j<3;$j++)
  {$result.=$taken{$r{"$i$j$k"}};}}}
  $result;
}

sub Import
{
  local($ent,@temp);
  ($ent)=@_;
  %r=();
  @temp=(0,1,2,3,4,5,6);
  foreach (keys(%taken))
  {$temp[$taken{$_}]=$_;}
  for ($k=0;$k<6;$k++)
  {for ($i=0;$i<3;$i++)
  {for ($j=0;$j<3;$j++)
  {
    $r{"$i$j$k"}=$temp[substr($ent,0,1)];
    $ent=substr($ent,1);
  }}}
}

sub Steps
{
  my ($codes)=@_;
  my $h;
  $codes=~s/[S-Z]//g;
  $h=&Human($codes);
  "\n<param name=\"steps3\" value=\"$h\">";
}

sub Human
{
  my $h="";
  foreach (unpack("c*",$_[0]))
  {$h.=substr("To Ti Ta Lo Li La Fo Fi Fa Ro Ri Ra Bo Bi Ba Do Di Da RotTedTcoMidBcpBcoBepBeo",($_-65)*3,3);}
  $h=~s%Rot%ROTATE! %;
  $h=~s%Ted%<nobr><font color=green>[Step 1 - Top Edges (4) Are Complete!]</font></nobr> %;
  $h=~s%Tco%<nobr><font color=green>[Step 2 - Top Corners (4) Are Complete!]</font></nobr> %;
  $h=~s%Mid%<nobr><font color=green>[Step 3 - Middle Layer Edges (4) Are Complete!]</font></nobr> %;
  $h=~s%Bcp%<nobr><font color=green>[Step 4 - Bottom Corners (4) Are Placed Correctly!]</font></nobr> %;
  $h=~s%Bco%<nobr><font color=green>[Step 5 - Bottom Corners (4) Are Oriented Perfectly!]</font></nobr> %;
  $h=~s%Bep%<nobr><font color=green>[Step 6 - Bottom Edges (4) Are Placed Correctly!]</font></nobr> %;
  $h=~s%Beo%<nobr><font color=green>[Step 7 - Bottom Edges (4) Are Oriented Perfectly!]</font></nobr> %;
  $h;
}

sub DoToCube
{
  local($codes)=@_;
  $codes=~s/[T-Z]//g;
  foreach (unpack("c*",$codes))
  {$_ == 83 ? &Rotate() : &OneMove(int(($_-65)/3)+1,($_-65)%3+1);}
}

sub Rotate
{
  $main=~s/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.........)(.........)(.........)(.........)(.)(.)(.)(.)(.)(.)(.)(.)(.)/$7$4$1$8$5$2$9$6$3$11$12$13$10$16$19$22$15$18$21$14$17$20/;
  $main=~tr/123456/152346/;
}

sub OneMove
{
  ($face,$times)=@_;
  $temp=substr($main,($face-1)*9,9);
  for ($a=0;$a<$times;$a++)
  {$temp=~s/(.)(.)(.)(.)(.)(.)(.)(.)(.)/$7$4$1$8$5$2$9$6$3/;}
  substr($main,($face-1)*9,9)=$temp;
  for ($a=0;$a<$times;$a++)
  {
    $main=~s/(.........)(...)(......)(...)(......)(...)(......)(...)(...............)/$1$4$3$6$5$8$7$2$9/ if ($face == 1);
    $main=~s/(.)(..)(.)(..)(.)(...........)(.)(..)(.)(..)(.)(.............)(.)(..)(.)(..)(.)(.)(..)(.)(..)(.)(..)/$17$2$15$4$13$6$1$8$3$10$5$12$22$14$20$16$18$7$19$9$21$11$23/ if ($face == 2);
    $main=~s/(......)(.)(.)(.)(..)(.)(..)(.)(..)(.)(.........)(.)(..)(.)(..)(.)(...........)(.)(.)(.)(......)/$1$10$8$6$5$18$7$19$9$20$11$2$13$3$15$4$17$16$14$12$21/ if ($face == 3);
    $main=~s/(..)(.)(..)(.)(..)(.)(...........)(.)(..)(.)(..)(.)(.........)(.)(..)(.)(..)(.)(....)(.)(..)(.)(..)(.)/$1$8$3$10$5$12$7$20$9$22$11$24$13$6$15$4$17$2$19$18$21$16$23$14/ if ($face == 4);
    $main=~s/(.)(.)(.)(......)(.)(..)(.)(..)(.)(.............)(.)(..)(.)(..)(.)(...............)(.)(.)(.)/$11$13$15$4$3$6$2$8$1$10$19$12$18$14$17$16$5$7$9/ if ($face == 5);
    $main=~s/(...............)(...)(......)(...)(......)(...)(......)(...)(.........)/$1$8$3$2$5$4$7$6$9/ if ($face == 6);
  }
}

sub LocateEdge
{
  ($pp,$study)=@_;
  ($c1,$c2)=split(//,$pp);
  foreach $edg (@GetEdge)
  {
    ($d1,$d2)=split(/\:/,$edg);
    if (substr($study,$d1,1) eq $c1 &&
        substr($study,$d2,1) eq $c2)
    {return (int($d1/9)+1).(int($d2/9)+1);}
    if (substr($study,$d1,1) eq $c2 &&
        substr($study,$d2,1) eq $c1)
    {return (int($d2/9)+1).(int($d1/9)+1);}
  }
  &crash("<blink>Stickers have been switched! You NERD!</blink> PC #$c1$c2");
}

sub LocateCorner
{
  ($pp,$study)=@_;
  ($c1,$c2,$c3)=split(//,$pp);
  foreach $cor (@GetCorner)
  {
    ($d1,$d2,$d3)=split(/\:/,$cor);
    if (substr($study,$d1,1) eq $c1 &&
        substr($study,$d2,1) eq $c2 &&
        substr($study,$d3,1) eq $c3)
    {return (int($d1/9)+1).(int($d2/9)+1).(int($d3/9)+1);}
    if (substr($study,$d2,1) eq $c1 &&
        substr($study,$d3,1) eq $c2 &&
        substr($study,$d1,1) eq $c3)
    {return (int($d2/9)+1).(int($d3/9)+1).(int($d1/9)+1);}
    if (substr($study,$d3,1) eq $c1 &&
        substr($study,$d1,1) eq $c2 &&
        substr($study,$d2,1) eq $c3)
    {return (int($d3/9)+1).(int($d1/9)+1).(int($d2/9)+1);}
  }
  &crash("<blink>Stickers have been switched! You NERD!</blink> PC #$c1$c2$c3");
}

1;
