Debian Codegraph @

texk/texlive/linked_scripts/rubik/rubikrotation.pl

raw
1 #!/usr/bin/env perl
2 ##
3 use Carp;
4 use Fatal;
5 use warnings;
6 ##
7 our $version = "v5.0 (25 February 2018)";
8 ##
9 ## rubikrotation.pl
10 ## VERSION 5.0
11 ##
12 ## Copyright February 2018,
13 ## RWD Nickalls ([email protected])
14 ## A Syropoulos ([email protected])
15 ##
16 ## HISTORY
17 ##-------------------------------
18 ## v5.0 (25 February 2018)
19 ##-------------------------------
20 ##
21 ## 4.2h (07 February 2018)
22 ## --- adjusted the no of leading dots for the gprint command
23 ## when writing the up, down,.. colour state to the log file
24 ## so as to make the array form a square block (ie easier to read)
25 ## (in MAIN)
26 ##
27 ## 4.2g
28 ## --- 29 October 2017 added syntax checking for (( )) /inside/ squarebrackets
29 ## (in SUB checksyntax)
30 ## --- 24 October 2017 changed {};--> (), in SequenceNameNew (in SUB writestate)
31 ## this repairs these chars back to their original state.
32 ## --- 22 October 2017 minor adjustments to syntax checking (in SUB checksyntax)
33 ## to allow some extra chars in the [name] and <info> blocks.
34 ## Ideally, we want to be able to use /any/ chars inside these infoblocks.
35 ##
36 ## 4.2f
37 ## --- 05 Oct 2017 bugfix: added a ShowSequence [\space] bug fix in SUB checksyntax
38 ## --- 04 Oct 2017 adjusted brackets < > error messages (lines 2643--2659)
39 ##
40 ## 4.2e: (29 Sept 2017)
41 ## --- added a ``Western'' notation filter (provisional \& works)
42 ##
43 ## 4.2d: (10 August 2017)
44 ## --- placed a checkstate() command inside the rotation keyword
45 ## and stopped TEX writing the keyword checkstate to the rubikstate.dat file
46 ##
47 ## 4.2c: (2 August 2017)
48 ## --- added new rubikkeyword "cubesize" to hold cube size (three or two)
49 ## so we can tell which sort of cube is being processed
50 ## We can use this to detect when using the TwoRotation command
51 ## (for the TWOcube) vs when using the RubikRotation command
52 ## (for the THREEcube); for eample with regard to random rotations
53 ## (see random SUB; see RubikTwoCube.sty)
54 ##
55 ## 4.2b: (1 Aug 2017)
56 ## --- Removed the random,0 option --> n=50 (random SUB)
57 ## a zero or missing integer now generates an error message.
58 ##
59 ## 4.2a: (28 July 2017)
60 ## --- bugfix: error if spaces in RubikRotation{random,n} string from LaTeX.
61 ## Fixed to accommodate spaces, and uppercase random (lines 366 -- 388 approx)
62 ##
63 ##-------------------------------
64 ## v4.0 (3 March 2017)
65 ##-------------------------------
66 ## changes in v3.6 (January 2017)
67 ## --- included Jaap Rm and Rc notation
68 ## --- new sub for improved expansion of mod-4 multiples of rotations (Oct 2016)
69 ## --- restructured to facilitate processing arrays through the rotation sub
70 ## --- included option for an <info> block
71 ## --- included Randelshofer superset ENG 3x3 notation
72 ## --- implemented an `inverse' mode
73 ## --- improved syntax checking
74 ## --- used perltidy to polish the program layout
75 ## (but only when making the pdf documentation-- see file rubikrotationPL.pdf)
76 ## --- included a lot of new subroutines
77 ##--------------------------------
78 ## changes in v3.2:
79 ## v3.2h: (2 Oct 2016)
80 ## improved the mod 4 routine using SUB rubikmod()
81 ## improved comments to log file re: rotation processing
82 ##
83 ## v3.2e:(25 Sept 2016)
84 ## changed some command names: use short & long for the Rubik R2 --> R,R code
85 ## (more intuitive than Clean)
86 ## \Sequence{} --> SecquenceShort{}
87 ## \SequenceClean{} --> SecquenceLong{}
88 ## removed the [ and ] around [name] variable
89 ##
90 ## v3.2d: changed the returned command names (removed the Rotation part to keep it simple)
91 ## \Sequence{} = orig seq + NO NAME
92 ## \SequenceName{} = NAME only
93 ## \SequenceClean{} = clean seq + NO NAME
94 ##
95 ## v3.2c: added new commands:
96 ## \RotationSequenceName{}
97 ## \RotationSequenceClean{}
98 ##
99 ## v3.2a: added a \RubikSeqNEW{...} output line in the output file
100 ## to facilitate typesetting the rotation sequence (works OK just now)
101 ##
102 ## v3.2: --- added leading ... to the comments written by the <writestate> sub
103 ## (the ... code indicates that comments are written by the Perl script)
104 ## --- changed the word program, prog --> script
105 ##--------------------------------
106 ## changes in v3.0:
107 ## --- accepts command-line arguments for input (mandatory) and output (optional) filenames
108 ## default output filename is: rubikOUT.txt
109 ## --- included the symbols [ and ] to denote a rotation-name label (ie as well as *)
110 ## --- fixed some of the variable definitions (as highlighted by <use strict> pragma)
111 ##--------------------------------
112 ## changes in v2.3:
113 ## --- accepts a single commandline argument (datafilename)
114 ## --- uses the standard modules Carp and Fatal (give extra line info on error)
115 ##--------------------------------
116 ## changes in v2.2:
117 ## --- changed licence --> LatexPP
118 ## --- included random n errors in ERROR messages (lines 492--495)
119 ## --- included version number in error message
120 ##------------------------------
121 #
122 # This file is part of the LaTeX rubikrotation package, and
123 # requires rubikcube.sty and rubikrotation.sty
124 #
125 # rubikrotation.pl is a Perl-5 program and free software:
126 # This program can be redistributed and/or modified under the terms
127 # of the LaTeX Project Public License Distributed from CTAN
128 # archives in directory macros/latex/base/lppl.txt; either
129 # version 1 of the License, or any later version.
130 #
131 # rubikrotation.pl is distributed in the hope that it will be useful,
132 # but WITHOUT ANY WARRANTY; without even the implied warranty of
133 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134 # GNU General Public License for more details.
135
136 ##---------------------------------------------------------------------
137 ## OVERVIEW
138 ## This program is part of the rubikrotation package, and is complementary to
139 ## the LaTeX rubikcube package. It processes Rubik rotation sequences on-the-fly.
140 ## The program reads a datafile (rubikstate.dat) output by the rubikcube package
141 ## and writes the new state to the file rubikstateNEW.dat, which is then input
142 ## by the TeX file. Further documentation accompanies the rubikrotation package.
143
144 ## Note that all possible state changing rotations of a 3x3x3 cube are
145 ## either combinations of, or the inverse of, just 9 different rotations,
146 ## three associated with each XYZ axis.
147 ##----------------------------------------------------------------------
148
149 ##==MAIN==
150 ##
151 ## This main module opens three files, and
152 ## sets up an array for collecting all errors (%error), and sets an error flag to "",
153 ## reads in the rubik state data file =rubikstate.dat (written by rubikrotation.sty),
154 ## and calls subs to write the TeX_OUT_FILE,
155 ## and finally closes all files.
156 ## Each line of the input file (rubikstate.dat) is a comma separated list of arguments.
157 ## The first argument in each line of the file rubikstate.dat is a rubikkeyword.
158 ##
159 ##---------------
160 ## set autoflush for outputs
161 ## $|=1;
162 ##--------------
163 our $source_file="";
164 our $out_file="rubikOUT.txt"; #default
165 our $argc=@ARGV;
166 our $commandLineArgs = join(" ", @ARGV);
167 our $showargs="\tcommandline args = $commandLineArgs\n";
168 our $usage="\tUsage: rubikrotation [-h|--help|-v|--version] -i <input file> [-o <out file>]\n";
169 our $rubikversion="\tVersion: this is rubikrotation version $version\n";
170 #
171 ## check for correct number of commandline arguments and allocate filenames
172 #
173 if ($argc == 0||$argc > 4 ){ # croak if 0 or more than 4 arguments
174 croak $rubikversion,$showargs,
175 "\tWrong no of arguments\n",
176 $usage;
177 }
178 else {
179 SWITCHES:
180 while($_ = $ARGV[0]) {
181 shift;
182 if (/^-h$/ || /^--help$/ ) {
183 die $rubikversion,$usage,
184 "\twhere,\n" .
185 "\t[-h|--help]\tgives this help listing\n" .
186 "\t[-v|--version]\tgives version\n" .
187 "\t[-i] \tcreates specified input file\n",
188 "\t[-o] \tcreates specified output file\n",
189 "\tFor documentation see: rubikrotation.pdf,\n",
190 "\trubikrotationPL.pdf and rubikcube.pdf.\n\n";
191 }
192 elsif (/^-v$/ || /^--version$/ ) {die $rubikversion;}
193 elsif (/^-i$/) {
194 if (!@ARGV){
195 croak $showargs,
196 "\tNo input file specified!\n",
197 $usage;
198 }
199 else {
200 $source_file = $ARGV[0],
201 shift;
202 }
203 }
204 elsif (/^-o$/) {
205 if (!@ARGV) {
206 croak $showargs,
207 "\tNo output file specified!\n",
208 $usage;
209 }
210 else {
211 $out_file = $ARGV[0],
212 shift;
213 }
214 }
215 elsif (/^-\w+/) {
216 croak $showargs,
217 "\t$_: Illegal command line switch!\n",
218 $usage;
219 }
220 else {
221 croak $showargs,
222 "\tmissing filenames or ? missing -i or -o switch!\n",
223 $usage;
224 }
225 } # end of while
226 }; # end of else
227
228 #================================
229
230 open(IN_FILE, "<$source_file") ||croak "\tCan't open source file: $source_file\n";
231 open(TeX_OUT_FILE, ">$out_file")||croak "\tCan't open output file: $out_file\n";
232
233 ## create error file (for append)
234 open (ERROR_OUT_FILE, ">>rubikstateERRORS.dat")||croak "ERROR: can't open file rubikstateERRORS.dat\n";
235
236
237 ## use dots for Perl messages (I have used dashes for LaTeX messages in the .sty)
238 ## gprint sub prints its argument (message) to both the screen and to the TeX_OUT_FILE
239
240 gprint (""); # newline
241 gprint ("...PERL process..................................");
242 gprint ("...script = rubikrotation.pl $version");
243
244 ## setup global error parameters, so we can write all the errors to a file as an array
245 our %error = (); # setup an array for error messages (was %)
246 our $erroralert = ""; # error flag
247 our $errornumber = 0; #set number of errors to zero
248
249 gprint ("...reading the current cube state (from File: $source_file)");
250
251 our $dataline = "";
252 our $newdataline ="";
253 our $rubikkeyword = "";
254 our $cubesize = ""; ## to hold the size, as three (Rubik) or two (twocube)
255 our $rotationcommand = "";
256 our @data=();
257
258 our $Sequence=""; ## will hold the original (SHORT) sequence
259 our $rotationseqNEW = ""; ## will hold the LONG sequence
260 our $RotationSequenceName="";
261
262 our $SequenceName="";
263 our $SequenceShort="";
264 our $SequenceLong="";
265 our $SequenceInfo="";
266
267
268 our $jrcode = 0; ## We initialise a loop counter for use in the rotation sub
269 ## (see line 624)
270
271 #---------inverse mode------------
272 # a keyword INVERSE or inverse in an infoblock <..>
273 # FLAG is set (line 400) in response to detecting an infloblock.
274 # A set FLAG triggers (a) reversing rotation sequence (line 484),
275 # and (b) inverting each rotation (to generate the inverse sequence).
276 # Here we define direction FLAG for the INVERSE sequence of rotations.
277 # The conditional test is in the SUB rotation
278 our $inverse="INV";
279 our $directionflag="";
280 #-------------------------
281
282
283 LINE: while (<IN_FILE>){
284 next LINE if /^#/; #skip comments
285 next LINE if /^%/; #skip comments
286 next LINE if /^$/; #skip blank lines
287
288
289 print " \n TOP --------- (new line)\n\n";
290
291 $dataline = $_; # grab the whole line as a string
292 chomp $dataline; # remove the line-ending character
293
294 ## clean leading and trailing whitespace
295 $dataline = cleanstring($dataline);
296
297
298 #check syntax of the string
299 $rotationcommand=$dataline; ## needed for error messages
300 CheckSyntax($dataline);
301
302 ## form an array so we can process the (rubik)keywords.
303 @data=split (/,/, $dataline); # create an array called data
304
305 print " dataline array = @data\n";
306 #-------------------------
307
308 ## we have 10 fields (0--9)
309 ## check for rubikkeyword= cubesize, up,down,left,right,front,back,checkstate,rotation:
310 $rubikkeyword=$data[0];
311
312 ##--------------------------------
313 ## RWDN 2 August 2017
314 ## introduced keyword cubesize so prog can distinguish
315 ## between a TWOcube and an THREEcube.
316 ## Here we check for the rubikkeyword `cubesize'
317 ## cubesize is currently only being used to change the array size in random SUB
318 if ($rubikkeyword eq 'cubesize') {
319 gprint ("...");
320 $rotationcommand=$dataline; ## used in output message
321 gprint ("...command = $rotationcommand");
322 $cubesize = RemoveAllSpaces($data[1]);
323 if ($cubesize eq "two") {gprint ("...cube = TWOcube")};
324 if ($cubesize eq "three") {gprint ("...cube = THREEcube")};
325 gprint ("...");
326 next LINE ;
327 };
328 ## ------------------------------------
329
330 ## RWDN 7 February 2018
331 ## we vary the number of leading dots for the gprint command
332 ## so as to make the array of colour codes (X,W,Y, etc) form
333 ## a nice square when printed to the log file (the standard no is 3 dots)
334
335 if ($rubikkeyword eq 'up') { gprint ("......$dataline");
336 $Ult[0]=$data[1], $Umt[0]=$data[2],$Urt[0]=$data[3],
337 $Ulm[0]=$data[4], $Umm[0]=$data[5],$Urm[0]=$data[6],
338 $Ulb[0]=$data[7], $Umb[0]=$data[8],$Urb[0]=$data[9];
339 next LINE;
340 };
341
342 if ($rubikkeyword eq 'down') { gprint ("....$dataline");
343 $Dlt[0]=$data[1], $Dmt[0]=$data[2],$Drt[0]=$data[3],
344 $Dlm[0]=$data[4], $Dmm[0]=$data[5],$Drm[0]=$data[6],
345 $Dlb[0]=$data[7], $Dmb[0]=$data[8],$Drb[0]=$data[9];
346 next LINE;
347 };
348
349 if ($rubikkeyword eq 'left') { gprint ("....$dataline");
350 $Llt[0]=$data[1], $Lmt[0]=$data[2],$Lrt[0]=$data[3],
351 $Llm[0]=$data[4], $Lmm[0]=$data[5],$Lrm[0]=$data[6],
352 $Llb[0]=$data[7], $Lmb[0]=$data[8],$Lrb[0]=$data[9];
353 next LINE;
354 };
355
356 if ($rubikkeyword eq 'right') { gprint ("...$dataline");
357 $Rlt[0]=$data[1], $Rmt[0]=$data[2],$Rrt[0]=$data[3],
358 $Rlm[0]=$data[4], $Rmm[0]=$data[5],$Rrm[0]=$data[6],
359 $Rlb[0]=$data[7], $Rmb[0]=$data[8],$Rrb[0]=$data[9];
360 next LINE;
361 };
362
363 if ($rubikkeyword eq 'front') { gprint ("...$dataline");
364 $Flt[0]=$data[1], $Fmt[0]=$data[2],$Frt[0]=$data[3],
365 $Flm[0]=$data[4], $Fmm[0]=$data[5],$Frm[0]=$data[6],
366 $Flb[0]=$data[7], $Fmb[0]=$data[8],$Frb[0]=$data[9];
367 next LINE;
368 };
369
370 if ($rubikkeyword eq 'back') { gprint ("....$dataline");
371 $Blt[0]=$data[1], $Bmt[0]=$data[2],$Brt[0]=$data[3],
372 $Blm[0]=$data[4], $Bmm[0]=$data[5],$Brm[0]=$data[6],
373 $Blb[0]=$data[7], $Bmb[0]=$data[8],$Brb[0]=$data[9];
374 next LINE;
375 };
376
377 ## if the rubikkeyword is `checkstate'
378 ## we just check the state and write the output data to a file.
379 if ($rubikkeyword eq 'checkstate') {
380 gprint ("...");
381 $rotationcommand=$dataline; ## used in output message
382 gprint ("...command = $rotationcommand");
383 checkstate();
384 next LINE ;
385 };
386
387 ## IF the rubikkeyword is `rotation'
388 ## we first check to see if the second argument=random.
389 ## ---if so, then we check that the third argument is an integer,
390 ## ---if it is an integer n --> random => random(n)
391 ## ELSE it must be a rotation sequence --> send elements to rotation sub.
392
393
394 if ($rubikkeyword eq 'rotation')
395
396 { ## this IF runs down to near end of MAIN
397
398 ##RWDN 10 Aug 2017
399 ## moved checkstate to be inside rotation (so a next LINE will terminate prog)
400 gprint ("...");
401 gprint ("...rotation keyword");
402 checkstate();
403
404
405 gprint ("..."); ## logfile marker for begining of `rotation/random' process
406
407 # we now grab a copy of the dataline, and we shall use this
408 # in the ErrorMessage SUB to indicate which command
409 # an error is in.
410 $rotationcommand=$dataline; ## used in output message
411 gprint ("...command = $rotationcommand");
412
413
414 # need to check that a second argument exists (else --> ErrorMessage).
415 # ---should be either `random',
416 # ---or a macroname for a rotation sequence,
417 # ---or the first element of a rotation sequence.
418
419 if ($data[1] eq "")
420 { # no second argument
421 gprint ("..*missing second argument");
422 ErrorMessage ("QUITTING PERL PROGRAM --- missing second argument:");
423 ErrorMessage ("--- ? bad rotation macro-name");
424 quitprogram();
425 };
426
427
428
429 ##---------keyword = random-----------------
430 ## (command used for scrambling the cube)
431 ## if second argument in $dataline = random
432 ## THEN we also need to check if third argument is an integer;
433 ## if so send integer --> random sub.
434
435 ##----------------------------
436 ## (28 July 2017: RWDN) : bugfix:
437 ## better syntax checking required for the <random,n> command
438 ## as spaces before or after commas caused errors.
439 ##-----------------------------
440
441
442 ## allow upper and lowercase keyword random
443 if ( lc( $data[1] ) =~ m/random/ )
444 {
445 ## the string contains the keyword random
446
447 ## now check for missing comma after the keyword
448 if ( lc( RemoveAllSpaces($data[1]) ) ne "random" )
449 {## error, ? missing comma
450 ErrorMessage("[$data[1]] --- missing comma after `random' ");
451 next LINE;
452 };
453
454 ## now check for the trailing integer
455
456 if ( ( lc( RemoveAllSpaces($data[1]) ) eq "random") and ($data[2] eq "") )
457 {
458 ## missing integer
459 ErrorMessage("[$data[2]] --- missing integer after `random,'");
460 next LINE;
461 };
462
463
464 if (RemoveAllSpaces($data[2]) =~ /\D/)
465 {
466 ## Note that the \D operator sees , 23, as a word not an integer.
467 ## so if true then cannot be a number (D matches word and space elements)
468 ErrorMessage("[$data[2]] --- this is not an integer");
469 next LINE;
470 }
471 else {## string consists of one or more integers
472 ## check to see if more than one integer exists
473 ## by seeing if the string changes if we remove all the spaces
474 ## (note we have to use a string with the m operator)
475 my $RAS = RemoveAllSpaces($data[2]);
476 if ($data[2] =~ m/$RAS/ )
477 {
478 ## OK so this must be a single integer
479 ## so we can now do n random rotations
480 ## by sending the integer to the random SUB
481 random($data[2]);
482 next LINE;
483 }
484 else {## there must be spaces separating several integers;
485 ErrorMessage("[$data[2]] --- only one integer allowed");
486 next LINE;
487 };
488 }; ## end of else
489 } ## end of IF
490 ##-----------------------------------------------------------
491
492 else {
493 ## -----rotation sequence---------
494 ## the line must be a rotation sequence line, so send the sequence
495 # to the rotation sub;
496
497 # Note that a copy of the rotation command is already held in the
498 # variable rotationcommand (see above). It is used in the
499 # ErrorMessage SUB.
500
501
502 #----<infoblocks>---------------------------
503
504 # infoblocks are strings bounded by angle brackets <..>
505 # and are designed for holding metadata.
506 #
507 # Multiple comma separated infoblocks are allowed (but NOT nested).
508 # All infoblocks are eventually concaternated into a colon separated string, and
509 # returned into the OUT file (= rubikstateNEW.dat) as the macro \SequenceInfo.
510 #
511 # We process and then remove any infoblocks which exist.
512 # infoblocks are chars delimited by <...>
513 #
514 # The SUB infoblockcolon replaces any commas with a colon (so as to
515 # facilitate string manipulation, and allows us to distinguish between
516 # a string and a data array), and returns a new string (= $newdataline).
517 #
518 # The RubikRotation argument allows <infoblocks> for carrying special
519 # keywords, eg <inverse> which can be used to influence the process.
520 # If several infoblocks exist, then we collect the contents into
521 # variable SequenceInfo, and separate them with a colon;
522 #
523 # The SUB cutinfoblock returns TWO strings:
524 # (1) the name of the new revised string = newdataline, (with infoblocks removed)
525 # (2) the contents of the infoblock = $SequenceInfo
526
527
528 infoblockcolon($dataline);
529
530 ## rename the returned newdataline string to dataline
531 ## and reinitialise the string newdataline so it can be used again.
532 $dataline=$newdataline;
533 $newdataline=""; ## reset the variable
534
535
536 gprint ("...dataline = $dataline");
537
538
539 ## now pass the string to cutinfoblock
540 local @seq=();
541
542
543 while ( (index $dataline, '<') !=-1 ){
544
545 cutinfoblock($dataline);
546 # best to use the whole word <inverse> to avoid errors
547 # best to force lowercase so users can type the word as they want
548
549 if ( lc($SequenceInfo) =~ m/(inverse)/) {
550 ## set a FLAG
551 $directionflag=$inverse;
552 print " FLAG set to = $inverse\n";
553 };
554 # append each infoblock to an array
555 push @seq, $SequenceInfo;
556 $dataline = $newdataline;
557 };
558
559 # finally, we join the seqInfo array into a string so we can print it
560 $SequenceInfo = join ("; ", @seq);
561
562
563
564 #--------repeat blocks--------------------------
565
566 ## there are now no more infoblocks, so we now look for repeat-blocks.
567 ## these are embedded inside the rotation sequence
568
569 ## we first reformulate any repeat blocks (,) --> {;} if they exist
570 ## this is to allow us to process any repeat blocks as separate elements
571 ## so we look for curved brackets ie indicating a repeat block, and
572 ## if we find a ( we then send the dataline to the SUB fixrepeatelement()
573 ## the SUB fixrepeatelement() then returns the new revised dataline string
574 ## containing the FIRST repeat block which has been expanded.
575 ## If there is another ( then we repeat the procedure until all
576 ## repeat blocks have been expanded, and incorporated into the mail rotation string.
577
578 while ( (index $dataline, '(') !=-1 ){
579 fixrepeatelement ($dataline);
580 $dataline=$newdataline;
581 $newdataline=""; ## reset the variable
582 };
583
584 ## rename remaining dataline string as SequenceShortBrace
585 ## since if there are any repeat blocks, they are now reformulated with braces and semicolons
586 ## ie (,) --> {;} etc
587 $SequenceShortBrace=$dataline;
588
589
590 ## clean leading and trailing whitespace
591 $SequenceShortBrace = cleanstring($SequenceShortBrace);
592
593 ##----------------
594 ## form a new array from $SequenceShortBrace (since we have changed the format
595 ## slightly; ie some commands may have been reformulated as semicolons).
596 @data=split (/,/, $SequenceShortBrace);
597
598
599 ## need to remove keyword <rotation> (= first element in the array)
600 ## removing it late like this is convenient for error checking purposes,
601 ## as then the keyword `rotation' is on the string
602 shift (@data);
603
604
605
606 ## now need to recreate the string from the array @data for use later
607 ## (as rotation keyword has been removed)
608
609 $SequenceShortBrace = join (",", @data);
610
611
612 #-----create SequenceShort, so we can output it later----
613
614 # since the `rotating' keyword has been removed from the string,
615 # we can replace (repair to original state) any braces or or semicolons
616 # around repeat strings (if exist) and then rename it as SequenceShort
617 # which we will output at the end (in SUB writestate).
618
619 if ( (index $SequenceShortBrace, '{') !=-1 )
620 {
621 print " repairing braces and semicolon--> ()\n";
622 ## swap: BBook p 138--139
623 $SequenceShortBrace =~ tr/\{/(/; # swap { --> (
624 $SequenceShortBrace =~ tr/\}/)/; # swap } --> )
625 $SequenceShortBrace =~ tr/;/,/; # swap ; --> ,
626 };
627
628 #rename to SequenceShort
629 $SequenceShort = $SequenceShortBrace;
630
631 print " SequenceShort = $SequenceShort\n";
632 ##----------------------------
633
634
635 ## now we continue processing the array "data"
636
637 my $n = 0; ##total no of array elements in "data"
638 $n = ($#data +1);
639 print " processing rotation arguments: = @data (n= $n)\n";
640
641
642
643 ## --------check for state of direction flag----------------------
644 ## FLAG defined in line 224.
645 ## FLAG is set in line 400 on detecting <..> delimeters = infoblock
646 ## if flag set (by inverse keyword) then reverse the sequence array
647 if ( $directionflag eq $inverse){
648 # FLAG is set, so we need to inverse the array
649 gprint ("...directionFLAG set; reversing array...");
650 # but before reversing, look at the first array element
651 # to see if it is a square bracket element = NAME element
652 # so check the first char to see if it is [
653 if (substr ($data[0], 0,1) eq '[') {
654 $SequenceName=$data[0];
655 print " SequenceName (inv) = $SequenceName \n";
656 };
657
658 @data = reverse @data;
659 print " processing rotation arguments: = @data (n= $n)\n";
660 };
661
662 # send each rotation element to the sub rotation()
663 print " CALLing SUB rotation\n";
664
665 foreach $element (@data) {
666 ## clean leading and trailing white space
667 $element = cleanstring($element);
668 ## send element to rotation SUB
669 rotation($element);
670 };
671
672 } # end of else
673 }; # end of IF ( re: rotation keyword)
674
675 #----------------------------------------------
676 ## place any new keywords for processing here
677 ##---------------------------------------------
678
679 }; ## end of while
680
681 ## we have now finished reading in all the lines from the source file,
682 ## and processing all the rotations etc,
683 ## so we now just write the new cube state
684 ## to the output file = TeX_OUT_FILE (so LaTeX can read it)
685 ## plus any ErrorMessages
686 ## -- all these are handled by the quitprogram sub
687
688 quitprogram();
689
690 ##==============end of main==========================
691
692 sub rotation {
693
694
695 print " SUB rotation\n";
696
697 ## here we process the array @data (from main) consisting of all
698 ## the rotation commands associated with
699 ## a single RubikRotation command -- the `rotation' key word has already been removed
700 ## so we start here with [name] if it exists.
701
702 ##---------------------------
703 ## variables used in SUBs rotation() and rubikmod()
704 ## need to be defined outside the SUBs
705
706 $modnumber=-1; #multiple associated with the char, eg D2 etc
707 $rotcode="";
708 $rotnumber=0;
709 #------------------
710 my @repeatcode = ();
711
712 my $m=-1;
713 my $originalrcode="";
714 my $j; ## used with m below
715 my $numberofchars; ## length of a string
716 my $nfrontchars;
717
718 ##-----------------
719
720 ## grab the rotation code passed to this sub from MAIN
721 my $rcode = $_[0];
722
723
724 ## now we start a big loop processing each cs-element (= rcode),
725 ## and collecting these elements into two cs-strings
726 ## ($Sequence --> original string output as SHORT string (has codes like R2,L3 etc),
727 ## and $rotationseqNEW --> output as LONG string -- all short codes expanded)
728
729
730 ## first, clean leading and trailing white space (eg between, R ,)
731 $rcode = cleanstring($rcode);
732
733
734 ## grab a copy of the element (char) for use if m Mod4=0
735 $originalrcode=$rcode;
736
737
738 ## increment the LOOP counter
739 ## (initialised using <our> in MAIN = line 226)
740 ## for use in the rotation SUB.
741 ## This counter is used to identify the first element (rcode)
742 ## and used to grab [name] --> SequenceName.
743 $jrcode=$jrcode+1; ## increment rotation element (char) counter
744
745
746 ## -----check for [nameblocks]-----------------------------
747 ##
748 ## We look at the first character of each element in the sequence
749 ## if an element has a leading [ then it is a label (not a rotation)
750 ## If this is the case, then jump to next element in the array
751
752 ## BUT if trailing comma is missing, then (error as next rotation will be included
753 ## as part of the label) so need to trap this and
754 ## make the test: is first AND last char a sq bracket?
755 ## (strictly only need to look at /first/ char, as the early syntax check will have
756 ## detected any unbalanced brackets already)
757
758 if ( (substr ($rcode,0,1) =~ /\[/) and (substr ($rcode,-1) ) =~ /\]/) {
759
760 gprint ("...$rcode is a label OK");
761
762 if ($directionflag eq $inverse) {
763 # do nothing
764 }
765
766 else{
767 ## if this `label' is also the FIRST element, then label = nameblock
768 if ($jrcode ==1) {$SequenceName=$rcode};
769 }; # end of IF
770
771 ## now get next rotation element
772 next;
773 }; ## end of if
774
775 ##--------------
776
777
778 ## the rcode must therefore be either a rotation code or a repeat-block.
779
780 ##-------check for (repeatblocks)---------------------------------
781 ##
782 ## we have already replaced any repeat chars (,) with {;}
783 ## so we now check for elements with leading { and then expand them
784 ## the appropriate number of times.
785 ## Note that the actual expansion is done by SUB repeat()
786
787 ## Note that if there is NO comma before the {} of a {repeat block}, then
788 ## the true repeat block will not be recognised by the
789 ## usual test -- since the test is looking for a leading { etc.
790 ## However, in this event, the string being handled (not a true element)
791 ## will be processed as if it were a rotation, and an
792 ## error will be thrown, so it will get picked up OK.
793
794 if (substr ($rcode,0,1) =~ /\{/ )
795 {
796 print " repeat block found = $rcode \n";
797
798 ## since we now want to send each rotation element in the repeat block to
799 ## the rotation sub, we need to replace any ; with commas
800 ## therefore translate ; --> , but retain the {}
801 $rcode =~ tr/;/,/;
802
803 print " repeat block reformulated = $rcode \n";
804
805
806 #-------log file message--------
807 ## log file: we want to show the repeat string in the users original form
808 ## so we translate it back to the user's orig form {,} --> (,)
809 $origrcode=$rcode;
810 $origrcode =~ tr/\{/(/;
811 $origrcode =~ tr/\}/)/;
812 gprint ("...Expanding: $origrcode ...");
813 #-------------
814
815
816 #-----------
817 ## expand the code in the repeat block
818 print " CALLing SUB: repeat($rcode)\n";
819
820 repeat($rcode); # this expands the repeated elements in the block
821
822 ## this sub returns the expanded form as $insert
823 $expanded_repeatcode=$insert;
824
825 print " expanded_repeatcode = $expanded_repeatcode\n\n";
826 #------------
827
828 #----------------
829 # process each new element in the expanded_repeatcode --> rotation
830 # make expanded_repeatcode into an array, and send each element on
831
832 @repeatcode = split (/,/, $expanded_repeatcode) ;
833
834
835 ## -----------check for direction flag-----------------
836 ## if flag set then reverse the array
837 if ( $directionflag eq $inverse){@repeatcode = reverse @repeatcode};
838
839
840 # send each element to rotation SUB for processing
841 print " CALLing SUB rotation\n";
842 foreach $E (@repeatcode) {
843 print " sending repeat element $E to rotation SUB\n";
844 rotation($E)
845 };
846
847 # when this foreach is finished, then get next rotation element from
848 # the original @data array (see foreach.. near end of MAIN)
849 next;
850
851 }; ## end of if
852
853 ##================================
854
855 ## if an element has got this far, it must be a single rotation code
856 ## (maybe with a trailing digit), so it needs processing as a rotation
857 ## and appending the code to what will become the SequenceLONG string.
858
859 ##------------------------
860
861 ## CALL the sub rubikmod to process the rotation element,
862 ## and to return the front code (= $rotcode), orig no = $rotnumber,
863 ## and mod4 value (= $modnumber).
864
865 rubikmod($rcode);
866
867 ## update rcode <--- rotcode (returned by the SUB rubikmod() )
868 ## collect $m <--- modnumber (returned by the SUB rubikmod() )
869 $rcode = $rotcode;
870 $m = $modnumber;
871
872 ## we collect all the new versions of rcode into a cs-string = $SequenceLong
873 ## which will finally be output as the LONG string
874
875 #-----------------------
876 # check with directionflag
877 if ($directionflag eq $inverse) {$rcode = inverse ($rcode)};
878 #-------------------------
879
880 ##--------------------
881 if ( $m == 0 )
882 {
883 ## do NOT implement the rotation code, and do NOT append to SequenceLong
884 ## print the /original/ rcode (eg R4, or D0 etc)
885 gprint ("..*rotation ,$originalrcode, ERROR ($rotnumber = 0 mod 4) not implemented");
886 ErrorMessage(",$originalrcode, -- ($rotnumber = 0 mod 4) not implemented");
887 next;
888 };
889
890 if ( $m == 1 )
891 {
892 if($rotnumber >=5)
893 {gprint ("...Expanding $originalrcode ($rotnumber = $m mod 4) ...")};
894 $SequenceLong=$SequenceLong.$rcode.",";
895 }
896 else {
897 # m = 2 or 3
898 if($rotnumber >=5)
899 {gprint ("...Expanding $originalrcode ($rotnumber = $m mod 4) ...")}
900 else {gprint ("...Expanding $originalrcode ...")};
901
902 for($j=1; $j<=$m; $j++)
903 {## append rcode m times to sequenceLONG
904 $SequenceLong=$SequenceLong.$rcode.","
905 };
906 }; ## end of else
907
908 ##-------------------
909
910 ## if single trailing digit present,
911 ## then we implement the rotation command m times.
912 ## if more than one trailing digit
913 ## then the error is trapped at the end (as frontstring will not be recognised
914 ## ie will not be in the following list, and hence will be trapped as an error, eg R3)
915
916
917
918 ##-----------------------
919 ## RWDN Sept29 2017 testing to fix WESTERN notation problem
920 ## arrange for user to be able to include <western> in metadata etc
921 ## -- as this will mean that one can store these algorithms as a macro
922
923 if ( lc($SequenceInfo) =~ m/(western)/ ) {
924
925 if ($rcode eq "l") {$rcode = "Lw"; gprint ("...WESTERN NOTATION: rotation l --> Lw, OK") };
926 if ($rcode eq "lp") {$rcode = "Lwp"; gprint ("...WESTERN NOTATION: rotation lp --> Lwp, OK") };
927 if ($rcode eq "r") {$rcode = "Rw"; gprint ("...WESTERN NOTATION: rotation r --> Rw, OK") };
928 if ($rcode eq "rp") {$rcode = "Rwp"; gprint ("...WESTERN NOTATION: rotation rp --> Rwp, OK") };
929 if ($rcode eq "f") {$rcode = "Fw"; gprint ("...WESTERN NOTATION: rotation f --> Fw, OK") };
930 if ($rcode eq "fp") {$rcode = "Fwp"; gprint ("...WESTERN NOTATION: rotation fp --> Fwp, OK") };
931 if ($rcode eq "b") {$rcode = "Bw"; gprint ("...WESTERN NOTATION: rotation b --> Bw, OK") };
932 if ($rcode eq "bp") {$rcode = "Bwp"; gprint ("...WESTERN NOTATION: rotation bp --> Bwp, OK") };
933 if ($rcode eq "u") {$rcode = "Uw"; gprint ("...WESTERN NOTATION: rotation u --> Uw, OK") };
934 if ($rcode eq "up") {$rcode = "Uwp"; gprint ("...WESTERN NOTATION: rotation up --> Uwp, OK") };
935 if ($rcode eq "d") {$rcode = "Dw"; gprint ("...WESTERN NOTATION: rotation d --> Dw, OK") };
936 if ($rcode eq "dp") {$rcode = "Dwp"; gprint ("...WESTERN NOTATION: rotation dp --> Dwp, OK") };
937
938 };
939 ##-----------------------
940
941
942
943 if ($rcode eq "L") {for($j=1;$j<=$m;$j++) {gprint ("...rotation L, OK (= Lp3)"); &rrL}}
944 elsif ($rcode eq "Lp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Lp, OK"); &rrLp}}
945 elsif ($rcode eq "Lw") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Lw, OK (= Lp3 + Srp)"); &rrLw}}
946 elsif ($rcode eq "Lwp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Lwp, OK (= Lp + Sr)"); &rrLwp}}
947 elsif ($rcode eq "Ls") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Ls, OK (= L + Rp)"); &rrLs}}
948 elsif ($rcode eq "Lsp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Lsp, OK (= Lp + R)"); &rrLsp}}
949 elsif ($rcode eq "La") {for($j=1;$j<=$m;$j++) {gprint ("...rotation La, OK (= L + R)"); &rrLa}}
950 elsif ($rcode eq "Lap") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Lap, OK (= Lp + Rp)"); &rrLap}}
951 ####
952 elsif ($rcode eq "R") {for($j=1;$j<=$m;$j++) {gprint ("...rotation R, OK"); &rrR}}
953 elsif ($rcode eq "Rp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Rp, OK (= R3)"); &rrRp}}
954 elsif ($rcode eq "Rw") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Rw, OK (= R + Sr)"); &rrRw}}
955 elsif ($rcode eq "Rwp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Rwp, OK (= Rp + Srp)"); &rrRwp}}
956 elsif ($rcode eq "Rs") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Rs, OK (= R + Lp)"); &rrRs}}
957 elsif ($rcode eq "Rsp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Rsp, OK (= Rp + L)"); &rrRsp}}
958 elsif ($rcode eq "Ra") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Ra, OK (= R + L)"); &rrRa}}
959 elsif ($rcode eq "Rap") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Rap, OK (= Rp + Lp)"); &rrRap}}
960 ####
961 elsif ($rcode eq "U") {for($j=1;$j<=$m;$j++) {gprint ("...rotation U, OK"); &rrU}}
962 elsif ($rcode eq "Up") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Up, OK (= U3)"); &rrUp}}
963 elsif ($rcode eq "Uw") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Uw, OK (= U + Su)"); &rrUw}}
964 elsif ($rcode eq "Uwp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Uwp, OK (= Up + Sup)"); &rrUwp}}
965 elsif ($rcode eq "Us") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Us, OK (= U + Dp)"); &rrUs}}
966 elsif ($rcode eq "Usp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Usp, OK (= Up + D)"); &rrUsp}}
967 elsif ($rcode eq "Ua") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Ua, OK (= U + D)"); &rrUa}}
968 elsif ($rcode eq "Uap") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Uap, OK (= Up + Dp)"); &rrUap}}
969 ####
970 elsif ($rcode eq "D") {for($j=1;$j<=$m;$j++) {gprint ("...rotation D, OK (= Dp3)"); &rrD}}
971 elsif ($rcode eq "Dp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Dp, OK "); &rrDp}}
972 elsif ($rcode eq "Dw") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Dw, OK (= Dp3 + Sup)"); &rrDw}}
973 elsif ($rcode eq "Dwp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Dwp, OK (= Dp + Su)"); &rrDwp}}
974 elsif ($rcode eq "Ds") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Ds, OK (= D + Up)"); &rrDs}}
975 elsif ($rcode eq "Dsp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Dsp, OK (= Dp + U)"); &rrDsp}}
976 elsif ($rcode eq "Da") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Da, OK (= D + U)"); &rrDa}}
977 elsif ($rcode eq "Dap") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Dap, OK (= Dp + Up)"); &rrDap}}
978 ####
979 elsif ($rcode eq "F") {for($j=1;$j<=$m;$j++) {gprint ("...rotation F, OK"); &rrF}}
980 elsif ($rcode eq "Fp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fp, OK (= F3)"); &rrFp}}
981 elsif ($rcode eq "Fw") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fw, OK (= F + Sf)"); &rrFw}}
982 elsif ($rcode eq "Fwp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fwp, OK (= Fp + Sfp)"); &rrFwp}}
983 elsif ($rcode eq "Fs") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fs, OK (= F + Bp)"); &rrFs}}
984 elsif ($rcode eq "Fsp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fsp, OK (= Fp + B)"); &rrFsp}}
985 elsif ($rcode eq "Fa") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fa, OK (= F + B)"); &rrFa}}
986 elsif ($rcode eq "Fap") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Fap, OK (= Fp + Bp)"); &rrFap}}
987 ####
988 elsif ($rcode eq "B") {for($j=1;$j<=$m;$j++) {gprint ("...rotation B, OK (= Fp3)"); &rrB}}
989 elsif ($rcode eq "Bp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Bp, OK"); &rrBp}}
990 elsif ($rcode eq "Bw") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Bw, OK (= Fp3 + Sfp)"); &rrBw}}
991 elsif ($rcode eq "Bwp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Bwp, OK (= Fp + Sf)"); &rrBwp}}
992 elsif ($rcode eq "Bs") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Bs, OK (= B + Fp)"); &rrBs}}
993 elsif ($rcode eq "Bsp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Bsp, OK (= Bp + F)"); &rrBsp}}
994 elsif ($rcode eq "Ba") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Ba, OK (= B + F)"); &rrBa}}
995 elsif ($rcode eq "Bap") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Bap, OK (= Bp + Fp)"); &rrBap}}
996
997 #### --------------------------------------
998
999 #### inner-slice (= middle slice)
1000 ## need to include MES (middle slice) notation
1001 elsif ($rcode eq "M") {for($j=1;$j<=$m;$j++) {gprint ("...rotation M, OK (= Sl) "); &rrSl}}
1002 elsif ($rcode eq "Mp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Mp, OK (= Sr) "); &rrSr}}
1003 elsif ($rcode eq "E") {for($j=1;$j<=$m;$j++) {gprint ("...rotation E, OK (= Sd) "); &rrSd}}
1004 elsif ($rcode eq "Ep") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Ep, OK (= Su) "); &rrSu}}
1005 elsif ($rcode eq "S") {for($j=1;$j<=$m;$j++) {gprint ("...rotation S, OK (= Sf) "); &rrSf}}
1006 elsif ($rcode eq "Sp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sp, OK (= Sb) "); &rrSb}}
1007
1008 #### middle slice rotations (Singmaster)
1009 elsif ($rcode eq "Su") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Su, OK "); &rrSu}}
1010 elsif ($rcode eq "Sup") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sup, OK (= Su3)"); &rrSup}}
1011 elsif ($rcode eq "Sd") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sd, OK (= Sup)"); &rrSd}}
1012 elsif ($rcode eq "Sdp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sdp, OK (= Su)"); &rrSdp}}
1013 elsif ($rcode eq "Sl") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sl, OK (= Srp)"); &rrSl}}
1014 elsif ($rcode eq "Slp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Slp, OK (= Sr)"); &rrSlp}}
1015 elsif ($rcode eq "Sr") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sr, OK"); &rrSr}}
1016 elsif ($rcode eq "Srp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Srp, OK (= Sr3)"); &rrSrp}}
1017 elsif ($rcode eq "Sf") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sf, OK"); &rrSf}}
1018 elsif ($rcode eq "Sfp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sfp, OK (= Sf3)"); &rrSfp}}
1019 elsif ($rcode eq "Sb") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sb, OK (= Sfp)"); &rrSb}}
1020 elsif ($rcode eq "Sbp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation Sbp, OK (= Sf)"); &rrSbp}}
1021
1022 ## need to include Jaap Puzzles website for middle slice notation (Lm, Lmp)
1023 ## also include Randelshofer website middle slice notation (ML,MLp..)
1024
1025 elsif ($rcode eq "ML" or $rcode eq "MRp" or $rcode eq "Lm" or $rcode eq "Rmp")
1026 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Lm = M = Sl) "); &rrSl}}
1027
1028 elsif ($rcode eq "MR" or $rcode eq "MLp" or $rcode eq "Rm" or $rcode eq "Lmp")
1029 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Rm = Mp = Sr) "); &rrSr}}
1030
1031 elsif ($rcode eq "MU" or $rcode eq "MDp" or $rcode eq "Um" or $rcode eq "Dmp")
1032 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Um = Ep = Su) "); &rrSu}}
1033
1034 elsif ($rcode eq "MD" or $rcode eq "MUp" or $rcode eq "Dm" or $rcode eq "Ump")
1035 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Dm = E = Sd) "); &rrSd}}
1036
1037 elsif ($rcode eq "MF" or $rcode eq "MBp" or $rcode eq "Fm" or $rcode eq "Bmp")
1038 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Fm = S = Sf) "); &rrSf}}
1039
1040 elsif ($rcode eq "MB" or $rcode eq "MFp" or $rcode eq "Bm" or $rcode eq "Fmp")
1041 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Bm = Sp = Sb) "); &rrSb}}
1042 ##----------------------------------
1043
1044 #### double outer slice (wide) notation
1045 #### need to include Randelshofer TL, TLp double outer slice notation
1046 #### (equiv to the w wide notation)
1047 elsif ($rcode eq "TL") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TL, OK (= Lw = Lp3 + Srp)"); &rrLw}}
1048 elsif ($rcode eq "TLp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TLp, OK (= Lwp = Lp + Sr)"); &rrLwp}}
1049 elsif ($rcode eq "TR") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TR, OK (= Rw = R + Sr)"); &rrRw}}
1050 elsif ($rcode eq "TRp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TRp, OK (= Rwp = Rp + Srp)"); &rrRwp}}
1051 elsif ($rcode eq "TU") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TU, OK (= Uw = U + Su)"); &rrUw}}
1052 elsif ($rcode eq "TUp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TUp, OK (= Uwp = Up + Sup)"); &rrUwp}}
1053 elsif ($rcode eq "TD") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TD, OK (= Dw = Dp3 + Sup)"); &rrDw}}
1054 elsif ($rcode eq "TDp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TDp, OK (= Dwp = Dp + Su)"); &rrDwp}}
1055 elsif ($rcode eq "TF") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TF, OK (= Fw = F + Sf)"); &rrFw}}
1056 elsif ($rcode eq "TFp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TFp, OK (= Fwp = Fp + Sfp)"); &rrFwp}}
1057 elsif ($rcode eq "TB") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TB, OK (= Bw = Fp3 + Sfp)"); &rrBw}}
1058 elsif ($rcode eq "TBp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation TBp, OK (= Bwp = Fp + Sf)"); &rrBwp}}
1059
1060
1061 ## ---------------------------
1062 ## opposite slice notation of Randelshofer (SR, SRp) (= standard Rs, Rsp)
1063 ## opposite outer slices rotated in SAME direction as the FACE
1064
1065 elsif ($rcode eq "SL") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Ls = L + Rp)"); &rrLs}}
1066 elsif ($rcode eq "SLp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Lsp = Lp + R)"); &rrLsp}}
1067
1068 elsif ($rcode eq "SR") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Rs = R + Lp)"); &rrRs}}
1069 elsif ($rcode eq "SRp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Rsp = Rp + L)"); &rrRsp}}
1070
1071 elsif ($rcode eq "SU") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Us = U + Dp)"); &rrUs}}
1072 elsif ($rcode eq "SUp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Usp = Up + D)"); &rrUsp}}
1073
1074 elsif ($rcode eq "SD") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Ds = D + Up)"); &rrDs}}
1075 elsif ($rcode eq "SDp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Dsp = Dp + U)"); &rrDsp}}
1076
1077
1078 elsif ($rcode eq "SF") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Fs = F + Bp)"); &rrFs}}
1079 elsif ($rcode eq "SFp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Fsp = Fp + B)"); &rrFsp}}
1080
1081 elsif ($rcode eq "SB") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Bs = B + Fp)"); &rrBs}}
1082 elsif ($rcode eq "SBp") {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= Bsp = Bp + F)"); &rrBsp}}
1083
1084
1085 ## -------------------------
1086
1087 ## whole cube rotations
1088 ## need to include x,y,z (upper and lowercase) and also u,d,l,r,f,b (lowercase only) equivalents
1089 elsif ($rcode eq "X" or $rcode eq "x" or $rcode eq "r" or $rcode eq "lp")
1090 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= x = R + Sr + Lp)"); &rrR;&rrSr;&rrLp}}
1091 elsif ($rcode eq "Xp" or $rcode eq "xp" or $rcode eq "l" or $rcode eq "rp")
1092 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= xp = Rp + Srp + L)");&rrRp;&rrSrp;&rrL}}
1093 elsif ($rcode eq "Y" or $rcode eq "y" or $rcode eq "u" or $rcode eq "dp")
1094 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= y = U + Su + Dp)"); &rrU;&rrSu;&rrDp}}
1095 elsif ($rcode eq "Yp" or $rcode eq "yp" or $rcode eq "d" or $rcode eq "up")
1096 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= yp = Up + Sup + D)");&rrUp;&rrSup;&rrD}}
1097 elsif ($rcode eq "Z" or $rcode eq "z" or $rcode eq "f" or $rcode eq "bp")
1098 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= z = F + Sf + Bp)"); &rrF;&rrSf;&rrBp}}
1099 elsif ($rcode eq "Zp" or $rcode eq "zp" or $rcode eq "b" or $rcode eq "fp")
1100 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode, OK (= zp = Fp + Sfp + B)");&rrFp;&rrSfp;&rrB}}
1101
1102 ## more whole cube notation
1103 ## need to include Jaap website whole cube Lc notation
1104 ## also include Randelshofer C notation (CL, CLp.)
1105
1106 elsif ($rcode eq "CL" or $rcode eq "CRp" or $rcode eq "Lc" or $rcode eq "Rcp")
1107 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Lc = xp = Rp + Srp + L)");&rrRp;&rrSrp;&rrL}}
1108
1109 elsif ( $rcode eq "CR" or $rcode eq "CLp" or $rcode eq "Rc" or $rcode eq "Lcp")
1110 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Rc = x = R + Sr + Lp)"); &rrR;&rrSr;&rrLp}}
1111
1112 elsif ($rcode eq "CU" or $rcode eq "CDp" or $rcode eq "Uc" or $rcode eq "Dcp")
1113 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Uc = y = U + Su + Dp)"); &rrU;&rrSu;&rrDp}}
1114
1115 elsif ($rcode eq "CD" or $rcode eq "CUp" or $rcode eq "Dc" or $rcode eq "Ucp")
1116 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Dc = yp = Up + Sup + D)");&rrUp;&rrSup;&rrD}}
1117
1118 elsif ($rcode eq "CF" or $rcode eq "CBp" or $rcode eq "Fc" or $rcode eq "Bcp")
1119 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Fc = z = F + Sf + Bp)"); &rrF;&rrSf;&rrBp}}
1120
1121 elsif ($rcode eq "CB" or $rcode eq "CFp" or $rcode eq "Bc" or $rcode eq "Fcp")
1122 {for($j=1;$j<=$m;$j++) {gprint ("...rotation $rcode OK (= Bc = zp = Fp + Sfp + B)");&rrFp;&rrSfp;&rrB}}
1123
1124 ## -----------------------------
1125
1126 ## check empty string --> missing rotation
1127 elsif ($rcode eq "") {for($j=1;$j<=$m;$j++)
1128 {
1129 gprint ("..*rotation ,$rcode, ERROR ? typo or missing rotation");
1130 ErrorMessage(",$rcode, -- ? typo or missing rotation");
1131 }
1132 }
1133 ## finally ----------------------
1134 else {
1135 ## to fall this far then the rotation (char) must be undefined
1136 ## but before we can send these rotation code strings out in ErrorMessages
1137 ## we need to check that they are in the original format.
1138 ## ie., do not have any {;} chars etc. If they do, then we need to
1139 ## translate them back, ie {;} --> (,) etc
1140
1141 ## we use `originalcode' in the ErrorMessage because the user needs to be
1142 ## shown the `bad' code as it was originally input by the RubikRotation{} command.
1143
1144
1145 ## check for code with { ; } and restore to normal syntax
1146 if ( $rcode =~ m/(\{|;|\}|\])/ ) {
1147 $rcode = restorebrackets($rcode);
1148 $originalrcode = restorebrackets($originalrcode);
1149 };
1150
1151
1152 if ( $rcode =~ m/(\(|\)|\[|\])/) {
1153 gprint ("..*rotation $rcode ERROR -- code not known ? missing comma or nested brackets");
1154 ErrorMessage("$originalrcode -- code not known ? missing comma or nested brackets");
1155 ## DO NOT --> (next LINE;) here as need to check /all/ the rotation codes in the string.
1156 }
1157 else{
1158 gprint ("..*rotation $rcode ERROR -- code not known ? typo or missing comma");
1159 ErrorMessage("$originalrcode -- code not known ? typo or missing comma");
1160 ## DO NOT --> (next LINE;) here as need to check /all/ the rotation codes in the string.
1161 };
1162
1163 #-----------------------------
1164 next;
1165 }; #end of else
1166
1167 next;
1168 } # end of sub
1169
1170
1171
1172 #======================================
1173
1174 sub random {
1175
1176 print " SUB random\n";
1177
1178
1179 ## scramble randomly using n rotations
1180 ## example command = RubikRotation{random,74}
1181 ## if no n given (second argument = ""), then use default n=50
1182 ## if second argument is some string (not integer) then --> ERROR
1183 ##
1184 ## assign numbers to the minimal set of rotations to be used using a hash array list
1185 ## (perl 5 book page 68)
1186 ## ? maybe we should only use the 18 rotations mentioned in Rokicki 2013 paper?
1187 ## but here I have included all the slice (Xm) ones as well.
1188
1189
1190 ## initialise the array for the random rotations
1191 my @rrlist = ();
1192
1193
1194 ## (RWDN 2 Aug 2017):
1195 ## now check to see if TWOcube or Rubikcube being used
1196 ## use cubesize as the filter
1197
1198 if ($cubesize eq 'two')
1199 {
1200 ## using the TwoRotation command (from RubikTwoCube.sty)
1201 ## no slice rotations
1202 ##
1203 @rrlist = ("U", "Up",
1204 "D", "Dp",
1205 "L", "Lp",
1206 "R", "Rp",
1207 "F", "Fp",
1208 "B", "Bp");
1209 }
1210
1211 else {
1212 ## using the RubikRotation command (from RubikRotation.sty)
1213 @rrlist = ("U", "Up", "Um", "Ump",
1214 "D", "Dp", "Dm", "Dmp",
1215 "L", "Lp", "Lm", "Lmp",
1216 "R", "Rp", "Rm", "Rmp",
1217 "F", "Fp", "Fm", "Fmp",
1218 "B", "Bp", "Bm", "Bmp");
1219 };
1220
1221
1222 my $rrlistnumber=$#rrlist;
1223 print " rrlistnumber = $rrlistnumber\n";
1224 gprint ("...random SUB: rrlistnumber (array size) = $rrlistnumber");
1225 # these are numbered 0--$rrlistnumber,
1226
1227 ## let default no of random rotations for scrambling = 50
1228 my $defaultn = 50; ## RWDN (1 Aug 2017): not being used any more
1229 my $maxn = 200;
1230
1231 ## grab the integer passed from the random() command in main
1232 my $s = $_[0];
1233
1234 if ($s >= $maxn) {$s = $maxn;
1235 gprint ("..*WARNING: maximum n = 200");
1236 ErrorMessage ("random: max n = 200 (n=200 was used)")}
1237 elsif ($s == 0) {## $s = $defaultn;
1238 gprint ("..*ERR: integer n = 0 (invalid)");
1239 ErrorMessage (" --- integer n = 0 (invalid)");
1240 next LINE;
1241 };
1242
1243 my @rr; ## array to hold all the random rotations
1244 print " randomising the available rotations\n";
1245
1246
1247 ## set the seed for the randomisation (Perl BlackBook p 235)
1248 srand;
1249
1250 ## now select s numbers at random (with replacement) from range 0--listnumber+1
1251 ## Since we are using int(rand x), and using nos from 0--lastindex number,
1252 ## then max rand vaue = (lastindexnumber -1).99999, the integer of which
1253 ## = (lastindexnumber -1). Therefore we need to use the range 0--(lastindexnumber+1)
1254 ## in order to randomise all posibilities on our list.
1255
1256 my $j;
1257
1258 for ($j = 1; $j <=$s; $j=$j+1)
1259 {
1260 my $p= int(rand ($rrlistnumber +1));
1261 print "Rotation = $p, $rrlist[$p] \n";
1262 ## push rotation code $rrlist[$p] on to END of array @rr
1263 push (@rr, $rrlist[$p]);
1264 };
1265
1266 ## we assume the user is starting from a solved cube (ie use the state given by user)
1267 gprint ("...scrambling cube using $s random rotations");
1268
1269 ## now send the array off to the rotation sub
1270
1271 my $E;
1272
1273 foreach $E (@rr) { rotation($E) };
1274
1275 } ##end of sub
1276
1277
1278 #======================================
1279
1280 sub writestate {
1281
1282 print " SUB writestate\n";
1283
1284 ## this writes the final state to the TeX_OUT_FILE (= rubikstateNEW.dat) will be read by latex.
1285
1286 print (TeX_OUT_FILE "\%\% ...output datafile=$out_file\n");
1287 print (TeX_OUT_FILE "\%\% ...PERL script=rubikrotation.pl version $version\n");
1288 print (TeX_OUT_FILE "\\typeout{...writing new cube state to file $out_file}\%\n");
1289 print (TeX_OUT_FILE "\\RubikFaceUp\{$Ult[0]\}\{$Umt[0]\}\{$Urt[0]\}\{$Ulm[0]\}\{$Umm[0]\}\{$Urm[0]\}\{$Ulb[0]\}\{$Umb[0]\}\{$Urb[0]\}\%\n");
1290 print (TeX_OUT_FILE "\\RubikFaceDown\{$Dlt[0]\}\{$Dmt[0]\}\{$Drt[0]\}\{$Dlm[0]\}\{$Dmm[0]\}\{$Drm[0]\}\{$Dlb[0]\}\{$Dmb[0]\}\{$Drb[0]\}\%\n");
1291 print (TeX_OUT_FILE "\\RubikFaceLeft\{$Llt[0]\}\{$Lmt[0]\}\{$Lrt[0]\}\{$Llm[0]\}\{$Lmm[0]\}\{$Lrm[0]\}\{$Llb[0]\}\{$Lmb[0]\}\{$Lrb[0]\}\%\n");
1292 print (TeX_OUT_FILE "\\RubikFaceRight\{$Rlt[0]\}\{$Rmt[0]\}\{$Rrt[0]\}\{$Rlm[0]\}\{$Rmm[0]\}\{$Rrm[0]\}\{$Rlb[0]\}\{$Rmb[0]\}\{$Rrb[0]\}\%\n");
1293 print (TeX_OUT_FILE "\\RubikFaceFront\{$Flt[0]\}\{$Fmt[0]\}\{$Frt[0]\}\{$Flm[0]\}\{$Fmm[0]\}\{$Frm[0]\}\{$Flb[0]\}\{$Fmb[0]\}\{$Frb[0]\}\%\n");
1294 print (TeX_OUT_FILE "\\RubikFaceBack\{$Blt[0]\}\{$Bmt[0]\}\{$Brt[0]\}\{$Blm[0]\}\{$Bmm[0]\}\{$Brm[0]\}\{$Blb[0]\}\{$Bmb[0]\}\{$Brb[0]\}\%\n");
1295
1296 ##-----RWDN 2016---create four new holder commands for separate strings----------
1297
1298 ## these four names are defined in the rubikrotation.sty file so they can be renewed etc
1299 ## SequenceInfo
1300 ## SequenceName
1301 ## SequenceShort
1302 ## SequenceLong
1303
1304
1305 ## ----RWDN 25 Sept 2016 ----------------------
1306 ## now remove the first and last chars of [name] to output just NAME without [ and ]
1307
1308 ## initialise some variables we shall need
1309 $numberofcharsinstring=0;
1310 $nmiddlecharsinstring=0;
1311
1312
1313 ##----------SequenceName----------------------------
1314
1315 ## the SequenceName currently includes the [..]
1316 ## need to remove the [] before senting it to LaTeX,
1317 ## so need to detect when NAME string itself is empty, eg []
1318 ## so create a variable:
1319 $SequenceNameNew="";
1320
1321 $numberofcharsinstring = length $SequenceName;
1322
1323
1324 ## NEED to create error message if [] and empty string etc
1325
1326 if ($numberofcharsinstring <= 2)
1327 {$SequenceNameNew = $SequenceName}
1328 else {
1329
1330 $nmiddlecharsinstring = ($numberofcharsinstring - 2);
1331 ## reassign the string without first and last chars
1332 ### format of substr = (origstring, start possn, no of chars to use)
1333 $SequenceNameNew = substr($SequenceName,1,$nmiddlecharsinstring);
1334 };
1335
1336 ## RWDN 24 October 2017
1337 ## swap char changes back before writing output string
1338 ## only swap the brackets
1339 ## (do NOT swap ; --> , as ONLY use commas /outside/ infoblocks and between rotation sequences)
1340 ## swap: BBook p 138--139
1341
1342 $SequenceNameNew =~ tr/\{/(/; # swap { --> (
1343 $SequenceNameNew =~ tr/\}/)/; # swap } --> )
1344
1345
1346 print (TeX_OUT_FILE "\\renewcommand\\SequenceName\{$SequenceNameNew\}\%\n");
1347
1348 print (TeX_OUT_FILE "\\typeout{...SequenceName = $SequenceNameNew}\%\n");
1349 #-----------------
1350
1351
1352
1353 #----------SequenceInfo----------------
1354 ## we need to preserve any {} structures in the info string (as used by Kociemba),
1355 ## so we have to change { } --> [ ] since otherwise they will disappear
1356 ## or cause an error when printed in LaTeX
1357
1358 $SequenceInfo=~ tr/\{/\[/; ## swap { --> [
1359 $SequenceInfo=~ tr/\}/\]/; ## swap } --> ]
1360
1361 print (TeX_OUT_FILE "\\renewcommand\\SequenceInfo\{$SequenceInfo\}\%\n");
1362
1363 print (TeX_OUT_FILE "\\typeout{...SequenceInfo = $SequenceInfo}\%\n");
1364 #-----------------
1365
1366
1367
1368 ##---------SequenceShort------------------
1369 ## generated in MAIN
1370 ## SequenceShort = original argument of \RubikRotation{} /without/ any infoblocks
1371 ## therefore it may contain square brackets
1372
1373 print (TeX_OUT_FILE "\\renewcommand\\SequenceShort\{$SequenceShort\}\%\n");
1374
1375 print (TeX_OUT_FILE "\\typeout{...SequenceShort = $SequenceShort}\%\n");
1376 ##-----------------------
1377
1378 ##-----------SequenceLong-------------------
1379 ## now prepare the new LONG rotation sequence for output =(LONG sequence + NO NAME)
1380 ## BUT before outputting the string, we need to remove the terminal comma
1381
1382 $numberofcharsinstring = length $SequenceLong;
1383 $nfrontcharsinstring = $numberofcharsinstring -1;
1384 ## reassign the string except the terminal comma
1385 $SequenceLong = substr($SequenceLong,0,$nfrontcharsinstring);
1386
1387
1388 #----------
1389 print (TeX_OUT_FILE "\\renewcommand\\SequenceLong\{$SequenceLong\}\%\n");
1390
1391 print (TeX_OUT_FILE "\\typeout{...SequenceLong = $SequenceLong}\%\n");
1392
1393
1394
1395 ##-----------------------
1396
1397 ## now include any error messages generated
1398 ## (these are all in an array waiting to be printed out)
1399
1400 if ($erroralert eq "YES")
1401 {
1402 ## write errors to a separate file (just for errors---we append the errrors to end of file)
1403 ## the error file (rubikstateERRORS.dat) was created by the TeX file
1404 my $ne; #number of errors
1405 $ne=$#error; ## number of errors= largest index num since we started at zero
1406
1407 ## do not attach error to a <checkstate> command, since we really want
1408 ## to see the checkstate errors (in the ERROR file) printed AFTER the `rotation' command.
1409 if ($rotationcommand eq "checkstate") {}
1410 else {print (ERROR_OUT_FILE "*ERR cmd= $rotationcommand\n") };
1411
1412 ## last index number or array = $#arrayname (Black book p 62)
1413 my $k;
1414
1415 for ($k=0; $k<=$ne; $k=$k+1) {
1416
1417 ## restore correct brackets etc before outputting to Latex
1418 my $errorstring = $error[$k];
1419 $errorstring = restorebrackets($errorstring);
1420
1421 print (TeX_OUT_FILE "\\typeout{$errorstring}\%\n");
1422 print (ERROR_OUT_FILE "$errorstring\n");
1423
1424 }; # end of for
1425 }; # end of IF
1426 print " Perl output file written OK\n";
1427
1428 } #end of sub
1429
1430
1431 #======================================
1432
1433 sub ErrorMessage {
1434
1435 ## writes the argument as a standard error message to out file
1436
1437 my $errormess = $_[0]; ## parameter passed to sub
1438
1439 ## restore correct brackets etc before outputting to Latex
1440 $errormess = restorebrackets($errormess);
1441
1442 $erroralert = "YES"; ## set error alert flag (for use in out message)
1443 $error[$errornumber] = "*ERR $errormess";
1444 $errornumber++; ## increment number
1445 };
1446
1447
1448 #======================================
1449
1450 sub gprint {
1451
1452 ## prints argument (comments) to screen and also to TeX_OUT_FILE.
1453 ## The typeout commands will find its way into the log file when read by latex
1454 ## Important to include trailing % for messages written to the TeX_OUT_FILE
1455 ## to stop extra <spaces> being seen by TeX.
1456
1457 my $gmess=$_[0];
1458 print "$gmess\n";
1459 print (TeX_OUT_FILE "\\typeout{$gmess}\%\n");
1460 };
1461
1462
1463 #======================================
1464
1465 sub checkstate{
1466
1467 print " SUB checkstate\n";
1468
1469 ## only a simple check -- to see if wrong no of colours being used etc
1470 ## uses the cubie colours as used by rubikcube package= ROYGBWX
1471
1472 gprint ("...checking state of cube");
1473
1474 my @cubies=($Ult[0],$Umt[0],$Urt[0], $Ulm[0],$Umm[0],$Urm[0], $Ulb[0],$Umb[0],$Urb[0],
1475 $Dlt[0],$Dmt[0],$Drt[0], $Dlm[0],$Dmm[0],$Drm[0], $Dlb[0],$Dmb[0],$Drb[0],
1476 $Llt[0],$Lmt[0],$Lrt[0], $Llm[0],$Lmm[0],$Lrm[0], $Llb[0],$Lmb[0],$Lrb[0],
1477 $Rlt[0],$Rmt[0],$Rrt[0], $Rlm[0],$Rmm[0],$Rrm[0], $Rlb[0],$Rmb[0],$Rrb[0],
1478 $Flt[0],$Fmt[0],$Frt[0], $Flm[0],$Fmm[0],$Frm[0], $Flb[0],$Fmb[0],$Frb[0],
1479 $Blt[0],$Bmt[0],$Brt[0], $Blm[0],$Bmm[0],$Brm[0], $Blb[0],$Bmb[0],$Brb[0]);
1480
1481 my $R=0,my $O=0,my $Y=0,my $G=0,my $B=0,my $W=0,my $X=0;
1482
1483 my $cubiecolour = "";
1484
1485 foreach $cubiecolour (@cubies)
1486 {
1487 if ($cubiecolour eq R) {$R = $R+1}
1488 elsif ($cubiecolour eq O) {$O = $O+1}
1489 elsif ($cubiecolour eq Y) {$Y = $Y+1}
1490 elsif ($cubiecolour eq G) {$G = $G+1}
1491 elsif ($cubiecolour eq B) {$B = $B+1}
1492 elsif ($cubiecolour eq W) {$W = $W+1}
1493 elsif ($cubiecolour eq X) {$X = $X+1}
1494 else {
1495 gprint ("..*cubie-colour counting ERROR");
1496 }
1497 };
1498
1499 my $cubiesum=0;
1500 $cubiesum = $R+$O+$Y+$G+$B+$W+$X;
1501 gprint ("...cubiesum = $cubiesum (Red=$R, Or=$O, Ye=$Y, Gr=$G, Bl=$B, Wh=$W, X=$X)");
1502
1503 # only generate ErrorMessages if n>9 (as may be using a Grey cube)
1504 if ($cubiesum != 54) {
1505 ErrorMessage ("cubiesum not = 54");
1506 gprint ("..*cubiesum not = 54") };
1507
1508 if ($R >9){
1509 ErrorMessage("red cubies > 9 (=$R)");
1510 gprint ("..*red cubies > 9 (=$R)");
1511 };
1512
1513 if ($O >9){
1514 ErrorMessage("orange cubies > 9 (=$O)");
1515 gprint ("..*orange cubies > 9 (=$O)");
1516 };
1517
1518 if ($Y >9){
1519 ErrorMessage("yellow cubies > 9 (=$Y)");
1520 gprint ("..*yellow cubies > 9 (=$Y)");
1521 # next LINE
1522 };
1523
1524 if ($G >9){
1525 ErrorMessage("green cubies > 9 (=$G)");
1526 gprint ("..*green cubies > 9 (=$G)");
1527 };
1528
1529 if ($B >9){
1530 ErrorMessage("blue cubies > 9 (=$B)");
1531 gprint ("..*blue cubies > 9 (=$B)");
1532 };
1533
1534 if ($W >9){
1535 ErrorMessage("white cubies > 9 (=$W)");
1536 gprint ("..*white cubies > 9 (=$W)");
1537 };
1538
1539 if ($X == 54){
1540 ErrorMessage("no colours allocated (X=54)");
1541 gprint ("..*no colours allocated (X=54)") };
1542
1543 print " done\n\n";
1544 };
1545
1546
1547 #======================================
1548
1549 ## Overview of rotation transform subs
1550
1551 #======================================
1552 ## The following 9 (90 degree) rotation transformations are used
1553 ## to generate all the rotations used in the `rotation sub'
1554 ## each of these is a permutation for both colours and numbers
1555 ## of the cubie facelets.
1556 ## The following 9 subroutines are named as follows:
1557 ## (about X-axis) rrR, rrSr, rrLp
1558 ## (about Y-axis) rrU, rrSu, rrDp
1559 ## (about Z-axis) rrF, rrSf, rrBp
1560 ## see the rubikcube package documentation for full details regarding
1561 ## rotation notation and commands.
1562 ## METHOD & NOTATION
1563 ## each sub (below) starts by making an array[0] for the cubie colour
1564 ## and an array[1] for the cubie number.
1565 ## Each of the face rotations (rrR, rrLp, rrU, rrDp, rrF, rrBp) is involved with
1566 ## two pairs of connected but different permutations/transformations as follows:
1567 ## (a) one pair for the 12 Side cubies (arrays = @Xs0 (for Side colours), @Xs1 (for Side numbers)), and
1568 ## (b) one pair for the 9 Face cubies (arrays = @Xf0 (for Face colours), @Xf1 (for Face numbers)).
1569 ## Each of the middle slice rotations (rrSr, rrSu, rrSf) is involved with just one pair of
1570 ## permutations for the 12 Side cubies (arrays = @Xs0 (for Side colours), @Xs1 (for Side numbers)).
1571 ## We document only the side and face of the first sub (rrR) in detail, since
1572 ## the other subs are of similar form.
1573 #======================================
1574
1575
1576 #======================================
1577
1578 sub rrR {
1579
1580 ## the RIGHT (slice + face) transform
1581 ## R = RIGHT, s = side; 0=colour, 1= number
1582 ## make the clockwise rotation permutation
1583 ## In this permutation the Front-right-bottom (Frb) (side)facelet rotates to
1584 ## the new position of Up-right-bottom (Urb) (side)facelet.
1585 ##-----------SIDE-------
1586 ## 12 side cubie facelets in arrays @Rs0 (colours) and @Rs1 (numbers)
1587 ## these are the initial positions
1588
1589 @Rs0=($Frb[0],$Frm[0],$Frt[0],
1590 $Urb[0],$Urm[0],$Urt[0],
1591 $Blt[0],$Blm[0],$Blb[0],
1592 $Drb[0],$Drm[0],$Drt[0]);
1593
1594 @Rs1=($Frb[1],$Frm[1],$Frt[1],
1595 $Urb[1],$Urm[1],$Urt[1],
1596 $Blt[1],$Blm[1],$Blb[1],
1597 $Drb[1],$Drm[1],$Drt[1]);
1598
1599 ## now we reallocate the initial array elements to the new
1600 ## post (90 degree clockwise) rotation position.
1601 ## Cube is viewed from FRONT.
1602 ## Positions of side facelets of Right slice are numbered 0-11 in clockwise direction,
1603 ## (as seen from Right face) starting with Up-right-bottom facelet.
1604 ## First line example:
1605 ## variable $Urb[0] (Upface-right-bottom colour) <-- colour of first element in @Rs0 (=Frb[0])
1606 ## variable $Urb[1] (Upface-right-bottom number) <-- number of first element in @Rs1 (=Frb[1])
1607
1608 $Urb[0]=$Rs0[0]; $Urb[1]=$Rs1[0];
1609 $Urm[0]=$Rs0[1]; $Urm[1]=$Rs1[1];
1610 $Urt[0]=$Rs0[2]; $Urt[1]=$Rs1[2];
1611
1612 $Blt[0]=$Rs0[3]; $Blt[1]=$Rs1[3];
1613 $Blm[0]=$Rs0[4]; $Blm[1]=$Rs1[4];
1614 $Blb[0]=$Rs0[5]; $Blb[1]=$Rs1[5];
1615
1616 $Drb[0]=$Rs0[6]; $Drb[1]=$Rs1[6];
1617 $Drm[0]=$Rs0[7]; $Drm[1]=$Rs1[7];
1618 $Drt[0]=$Rs0[8]; $Drt[1]=$Rs1[8];
1619
1620 $Frb[0]=$Rs0[9]; $Frb[1]=$Rs1[9];
1621 $Frm[0]=$Rs0[10]; $Frm[1]=$Rs1[10];
1622 $Frt[0]=$Rs0[11]; $Frt[1]=$Rs1[11];
1623
1624 ##-------------Right FACE---------------------
1625 ## RIGHT FACE (9 cubies in each array)
1626 ## (numbered in rows: 1,2,3/4,5,6/7,8,9 from top left(1) to bottom right(9))
1627 ## R=Right, f = face; 0=colour, 1= number
1628 ## do the Rface (90 degree) rotation transform
1629 ## here the Right-left-bottom (Rlb) facelet rotates to the possn of Right-left-top (Rlt)
1630 ## we start with two arrays (one for colours @Rf0, one for numbers @Rf1) with 9 elements each.
1631
1632 @Rf0=($Rlb[0], $Rlm[0], $Rlt[0], $Rmb[0], $Rmm[0], $Rmt[0], $Rrb[0], $Rrm[0], $Rrt[0]);
1633 @Rf1=($Rlb[1], $Rlm[1], $Rlt[1], $Rmb[1], $Rmm[1], $Rmt[1], $Rrb[1], $Rrm[1], $Rrt[1]);
1634
1635 ## now we reallocate the array elements to the new
1636 ## post (90 degree clockwise) rotation facelet position.
1637 ## Right face is viewed from RIGHT.
1638 ## First line example:
1639 ## variable $Rlt[0] (=Right-left-top colour) <-- colour of first element in @Rf0 (=Rlb[0])
1640 ## variable $Rlt[1] (=Right-left-top number) <-- number of first element in @Rf1 (=Rlb[1])
1641
1642 $Rlt[0]=$Rf0[0]; $Rlt[1]=$Rf1[0];
1643 $Rmt[0]=$Rf0[1]; $Rmt[1]=$Rf1[1];
1644 $Rrt[0]=$Rf0[2]; $Rrt[1]=$Rf1[2];
1645
1646 $Rlm[0]=$Rf0[3]; $Rlm[1]=$Rf1[3];
1647 $Rmm[0]=$Rf0[4]; $Rmm[1]=$Rf1[4];
1648 $Rrm[0]=$Rf0[5]; $Rrm[1]=$Rf1[5];
1649
1650 $Rlb[0]=$Rf0[6]; $Rlb[1]=$Rf1[6];
1651 $Rmb[0]=$Rf0[7]; $Rmb[1]=$Rf1[7];
1652 $Rrb[0]=$Rf0[8]; $Rrb[1]=$Rf1[8];
1653
1654 }
1655
1656
1657 #======================================
1658
1659 sub rrSr {
1660
1661 ## Sr = RIGHT middle SLICE rotation (only 12 side facelets)
1662 ## modified from rrR (change the U,D,F, r --> m and Back Bl-->Bm; Rs--> ?Srs)
1663 ## change only the slice
1664 ## s = side; 0=colour, 1= number
1665 ## make the post rotation permutation
1666
1667 @SRs0=($Fmb[0],$Fmm[0],$Fmt[0],
1668 $Umb[0],$Umm[0],$Umt[0],
1669 $Bmt[0],$Bmm[0],$Bmb[0],
1670 $Dmb[0],$Dmm[0],$Dmt[0]);
1671
1672 @SRs1=($Fmb[1],$Fmm[1],$Fmt[1],
1673 $Umb[1],$Umm[1],$Umt[1],
1674 $Bmt[1],$Bmm[1],$Bmb[1],
1675 $Dmb[1],$Dmm[1],$Dmt[1]);
1676
1677
1678 $Umb[0]=$SRs0[0]; $Umb[1]=$SRs1[0];
1679 $Umm[0]=$SRs0[1]; $Umm[1]=$SRs1[1];
1680 $Umt[0]=$SRs0[2]; $Umt[1]=$SRs1[2];
1681
1682 $Bmt[0]=$SRs0[3]; $Bmt[1]=$SRs1[3];
1683 $Bmm[0]=$SRs0[4]; $Bmm[1]=$SRs1[4];
1684 $Bmb[0]=$SRs0[5]; $Bmb[1]=$SRs1[5];
1685
1686 $Dmb[0]=$SRs0[6]; $Dmb[1]=$SRs1[6];
1687 $Dmm[0]=$SRs0[7]; $Dmm[1]=$SRs1[7];
1688 $Dmt[0]=$SRs0[8]; $Dmt[1]=$SRs1[8];
1689
1690 $Fmb[0]=$SRs0[9]; $Fmb[1]=$SRs1[9];
1691 $Fmm[0]=$SRs0[10]; $Fmm[1]=$SRs1[10];
1692 $Fmt[0]=$SRs0[11]; $Fmt[1]=$SRs1[11];
1693
1694 }
1695
1696
1697 #======================================
1698
1699 sub rrLp {
1700
1701 ## LEFT slice (side + face) anticlockwise rotation
1702 ## s = side; 0=colour, 1= number
1703 ##-------------side-----------
1704
1705 @LPs0=($Flb[0],$Flm[0],$Flt[0],
1706 $Ulb[0],$Ulm[0],$Ult[0],
1707 $Brt[0],$Brm[0],$Brb[0],
1708 $Dlb[0],$Dlm[0],$Dlt[0]);
1709
1710 @LPs1=($Flb[1],$Flm[1],$Flt[1],
1711 $Ulb[1],$Ulm[1],$Ult[1],
1712 $Brt[1],$Brm[1],$Brb[1],
1713 $Dlb[1],$Dlm[1],$Dlt[1]);
1714
1715
1716 $Ulb[0]=$LPs0[0]; $Ulb[1]=$LPs1[0];
1717 $Ulm[0]=$LPs0[1]; $Ulm[1]=$LPs1[1];
1718 $Ult[0]=$LPs0[2]; $Ult[1]=$LPs1[2];
1719
1720 $Brt[0]=$LPs0[3]; $Brt[1]=$LPs1[3];
1721 $Brm[0]=$LPs0[4]; $Brm[1]=$LPs1[4];
1722 $Brb[0]=$LPs0[5]; $Brb[1]=$LPs1[5];
1723
1724 $Dlb[0]=$LPs0[6]; $Dlb[1]=$LPs1[6];
1725 $Dlm[0]=$LPs0[7]; $Dlm[1]=$LPs1[7];
1726 $Dlt[0]=$LPs0[8]; $Dlt[1]=$LPs1[8];
1727
1728 $Flb[0]=$LPs0[9]; $Flb[1]=$LPs1[9];
1729 $Flm[0]=$LPs0[10]; $Flm[1]=$LPs1[10];
1730 $Flt[0]=$LPs0[11]; $Flt[1]=$LPs1[11];
1731
1732
1733 ##---------------Left FACE-------------
1734 ## do the LEFT face transform (in rows: 1,2,3//4,5,6//7,8,9)
1735 ## f = face; 0=colour, 1= number
1736 ## NOTES: not same as for R
1737
1738 @LPf0=($Lrt[0], $Lrm[0], $Lrb[0], $Lmt[0], $Lmm[0], $Lmb[0], $Llt[0], $Llm[0], $Llb[0]);
1739 @LPf1=($Lrt[1], $Lrm[1], $Lrb[1], $Lmt[1], $Lmm[1], $Lmb[1], $Llt[1], $Llm[1], $Llb[1]);
1740
1741 $Llt[0]=$LPf0[0]; $Llt[1]=$LPf1[0];
1742 $Lmt[0]=$LPf0[1]; $Lmt[1]=$LPf1[1];
1743 $Lrt[0]=$LPf0[2]; $Lrt[1]=$LPf1[2];
1744
1745 $Llm[0]=$LPf0[3]; $Llm[1]=$LPf1[3];
1746 $Lmm[0]=$LPf0[4]; $Lmm[1]=$LPf1[4];
1747 $Lrm[0]=$LPf0[5]; $Lrm[1]=$LPf1[5];
1748
1749 $Llb[0]=$LPf0[6]; $Llb[1]=$LPf1[6];
1750 $Lmb[0]=$LPf0[7]; $Lmb[1]=$LPf1[7];
1751 $Lrb[0]=$LPf0[8]; $Lrb[1]=$LPf1[8];
1752
1753 }
1754
1755
1756 #======================================
1757
1758 sub rrU {
1759
1760 ## UP slice (side + face)
1761 ## do the Uside transform
1762 ## s = side; 0=colour, 1= number
1763 ## ----------SIDE--------------
1764
1765 @Us0=($Lrt[0],$Lmt[0],$Llt[0],
1766 $Brt[0],$Bmt[0],$Blt[0],
1767 $Rrt[0],$Rmt[0],$Rlt[0],
1768 $Frt[0],$Fmt[0],$Flt[0]);
1769
1770 @Us1=($Lrt[1],$Lmt[1],$Llt[1],
1771 $Brt[1],$Bmt[1],$Blt[1],
1772 $Rrt[1],$Rmt[1],$Rlt[1],
1773 $Frt[1],$Fmt[1],$Flt[1]);
1774
1775
1776 $Brt[0]=$Us0[0]; $Brt[1]=$Us1[0];
1777 $Bmt[0]=$Us0[1]; $Bmt[1]=$Us1[1];
1778 $Blt[0]=$Us0[2]; $Blt[1]=$Us1[2];
1779
1780 $Rrt[0]=$Us0[3]; $Rrt[1]=$Us1[3];
1781 $Rmt[0]=$Us0[4]; $Rmt[1]=$Us1[4];
1782 $Rlt[0]=$Us0[5]; $Rlt[1]=$Us1[5];
1783
1784 $Frt[0]=$Us0[6]; $Frt[1]=$Us1[6];
1785 $Fmt[0]=$Us0[7]; $Fmt[1]=$Us1[7];
1786 $Flt[0]=$Us0[8]; $Flt[1]=$Us1[8];
1787
1788 $Lrt[0]=$Us0[9]; $Lrt[1]=$Us1[9];
1789 $Lmt[0]=$Us0[10]; $Lmt[1]=$Us1[10];
1790 $Llt[0]=$Us0[11]; $Llt[1]=$Us1[11];
1791
1792 ##-------------Up FACE-------------------
1793 ## do the Rface transform (in rows: 1,2,3//4,5,6//7,8,9)
1794 ## f = face; 0=colour, 1= number
1795
1796 @Uf0=($Ulb[0], $Ulm[0], $Ult[0], $Umb[0], $Umm[0], $Umt[0], $Urb[0], $Urm[0], $Urt[0]);
1797 @Uf1=($Ulb[1], $Ulm[1], $Ult[1], $Umb[1], $Umm[1], $Umt[1], $Urb[1], $Urm[1], $Urt[1]);
1798
1799 $Ult[0]=$Uf0[0]; $Ult[1]=$Uf1[0];
1800 $Umt[0]=$Uf0[1]; $Umt[1]=$Uf1[1];
1801 $Urt[0]=$Uf0[2]; $Urt[1]=$Uf1[2];
1802
1803 $Ulm[0]=$Uf0[3]; $Ulm[1]=$Uf1[3];
1804 $Umm[0]=$Uf0[4]; $Umm[1]=$Uf1[4];
1805 $Urm[0]=$Uf0[5]; $Urm[1]=$Uf1[5];
1806
1807 $Ulb[0]=$Uf0[6]; $Ulb[1]=$Uf1[6];
1808 $Umb[0]=$Uf0[7]; $Umb[1]=$Uf1[7];
1809 $Urb[0]=$Uf0[8]; $Urb[1]=$Uf1[8];
1810
1811 }
1812
1813
1814 #======================================
1815
1816 sub rrSu {
1817
1818 ## middle slice rotation (side only 12 facelets)
1819 ## s = side; 0=colour, 1= number
1820 ## make the post rotation permutation
1821 ##-----------SIDE-------------------
1822
1823 @SUs0=($Lrm[0],$Lmm[0],$Llm[0],
1824 $Brm[0],$Bmm[0],$Blm[0],
1825 $Rrm[0],$Rmm[0],$Rlm[0],
1826 $Frm[0],$Fmm[0],$Flm[0]);
1827
1828 @SUs1=($Lrm[1],$Lmm[1],$Llm[1],
1829 $Brm[1],$Bmm[1],$Blm[1],
1830 $Rrm[1],$Rmm[1],$Rlm[1],
1831 $Frm[1],$Fmm[1],$Flm[1]);
1832
1833
1834 $Brm[0]=$SUs0[0]; $Brm[1]=$SUs1[0];
1835 $Bmm[0]=$SUs0[1]; $Bmm[1]=$SUs1[1];
1836 $Blm[0]=$SUs0[2]; $Blm[1]=$SUs1[2];
1837
1838 $Rrm[0]=$SUs0[3]; $Rrm[1]=$SUs1[3];
1839 $Rmm[0]=$SUs0[4]; $Rmm[1]=$SUs1[4];
1840 $Rlm[0]=$SUs0[5]; $Rlm[1]=$SUs1[5];
1841
1842 $Frm[0]=$SUs0[6]; $Frm[1]=$SUs1[6];
1843 $Fmm[0]=$SUs0[7]; $Fmm[1]=$SUs1[7];
1844 $Flm[0]=$SUs0[8]; $Flm[1]=$SUs1[8];
1845
1846 $Lrm[0]=$SUs0[9]; $Lrm[1]=$SUs1[9];
1847 $Lmm[0]=$SUs0[10]; $Lmm[1]=$SUs1[10];
1848 $Llm[0]=$SUs0[11]; $Llm[1]=$SUs1[11];
1849
1850 }
1851
1852
1853 #======================================
1854
1855 sub rrDp {
1856
1857 ## Down Face anticlockwise rotation (side and face)
1858 ## s = side; 0=colour, 1= number
1859 ## make the post rotation permutation
1860 ##--------------SIDE----------------
1861
1862 @DPs0=($Lrb[0],$Lmb[0],$Llb[0],
1863 $Brb[0],$Bmb[0],$Blb[0],
1864 $Rrb[0],$Rmb[0],$Rlb[0],
1865 $Frb[0],$Fmb[0],$Flb[0]);
1866
1867 @DPs1=($Lrb[1],$Lmb[1],$Llb[1],
1868 $Brb[1],$Bmb[1],$Blb[1],
1869 $Rrb[1],$Rmb[1],$Rlb[1],
1870 $Frb[1],$Fmb[1],$Flb[1]);
1871
1872 $Brb[0]=$DPs0[0]; $Brb[1]=$DPs1[0];
1873 $Bmb[0]=$DPs0[1]; $Bmb[1]=$DPs1[1];
1874 $Blb[0]=$DPs0[2]; $Blb[1]=$DPs1[2];
1875
1876 $Rrb[0]=$DPs0[3]; $Rrb[1]=$DPs1[3];
1877 $Rmb[0]=$DPs0[4]; $Rmb[1]=$DPs1[4];
1878 $Rlb[0]=$DPs0[5]; $Rlb[1]=$DPs1[5];
1879
1880 $Frb[0]=$DPs0[6]; $Frb[1]=$DPs1[6];
1881 $Fmb[0]=$DPs0[7]; $Fmb[1]=$DPs1[7];
1882 $Flb[0]=$DPs0[8]; $Flb[1]=$DPs1[8];
1883
1884 $Lrb[0]=$DPs0[9]; $Lrb[1]=$DPs1[9];
1885 $Lmb[0]=$DPs0[10]; $Lmb[1]=$DPs1[10];
1886 $Llb[0]=$DPs0[11]; $Llb[1]=$DPs1[11];
1887
1888 ##---------------Down FACE-------------------
1889 ## f = face; 0=colour, 1= number
1890
1891 @DPf0=($Dlt[0], $Dlm[0], $Dlb[0], $Dmt[0], $Dmm[0], $Dmb[0], $Drt[0], $Drm[0], $Drb[0]);
1892 @DPf1=($Dlt[1], $Dlm[1], $Dlb[1], $Dmt[1], $Dmm[1], $Dmb[1], $Drt[1], $Drm[1], $Drb[1]);
1893
1894 $Dlb[0]=$DPf0[0]; $Dlb[1]=$DPf1[0];
1895 $Dmb[0]=$DPf0[1]; $Dmb[1]=$DPf1[1];
1896 $Drb[0]=$DPf0[2]; $Drb[1]=$DPf1[2];
1897
1898 $Dlm[0]=$DPf0[3]; $Dlm[1]=$DPf1[3];
1899 $Dmm[0]=$DPf0[4]; $Dmm[1]=$DPf1[4];
1900 $Drm[0]=$DPf0[5]; $Drm[1]=$DPf1[5];
1901
1902 $Dlt[0]=$DPf0[6]; $Dlt[1]=$DPf1[6];
1903 $Dmt[0]=$DPf0[7]; $Dmt[1]=$DPf1[7];
1904 $Drt[0]=$DPf0[8]; $Drt[1]=$DPf1[8];
1905
1906 }
1907
1908
1909 #======================================
1910
1911 sub rrF {
1912
1913 ## do the Fside transform (side and face)
1914 ## s = side; 0=colour, 1= number
1915 ## -----------SIDE-----------------
1916
1917 @Fs0=($Lrb[0],$Lrm[0],$Lrt[0],
1918 $Ulb[0],$Umb[0],$Urb[0],
1919 $Rlt[0],$Rlm[0],$Rlb[0],
1920 $Drt[0],$Dmt[0],$Dlt[0]);
1921
1922 @Fs1=($Lrb[1],$Lrm[1],$Lrt[1],
1923 $Ulb[1],$Umb[1],$Urb[1],
1924 $Rlt[1],$Rlm[1],$Rlb[1],
1925 $Drt[1],$Dmt[1],$Dlt[1]);
1926
1927 $Ulb[0]=$Fs0[0]; $Ulb[1]=$Fs1[0];
1928 $Umb[0]=$Fs0[1]; $Umb[1]=$Fs1[1];
1929 $Urb[0]=$Fs0[2]; $Urb[1]=$Fs1[2];
1930
1931 $Rlt[0]=$Fs0[3]; $Rlt[1]=$Fs1[3];
1932 $Rlm[0]=$Fs0[4]; $Rlm[1]=$Fs1[4];
1933 $Rlb[0]=$Fs0[5]; $Rlb[1]=$Fs1[5];
1934
1935 $Drt[0]=$Fs0[6]; $Drt[1]=$Fs1[6];
1936 $Dmt[0]=$Fs0[7]; $Dmt[1]=$Fs1[7];
1937 $Dlt[0]=$Fs0[8]; $Dlt[1]=$Fs1[8];
1938
1939 $Lrb[0]=$Fs0[9]; $Lrb[1]=$Fs1[9];
1940 $Lrm[0]=$Fs0[10]; $Lrm[1]=$Fs1[10];
1941 $Lrt[0]=$Fs0[11]; $Lrt[1]=$Fs1[11];
1942
1943 ## -------Front FACE-------------------
1944 ## f = face; 0=colour, 1= number
1945
1946 @Lf0=($Flb[0], $Flm[0], $Flt[0], $Fmb[0], $Fmm[0], $Fmt[0], $Frb[0], $Frm[0], $Frt[0]);
1947 @Lf1=($Flb[1], $Flm[1], $Flt[1], $Fmb[1], $Fmm[1], $Fmt[1], $Frb[1], $Frm[1], $Frt[1]);
1948
1949 $Flt[0]=$Lf0[0]; $Flt[1]=$Lf1[0];
1950 $Fmt[0]=$Lf0[1]; $Fmt[1]=$Lf1[1];
1951 $Frt[0]=$Lf0[2]; $Frt[1]=$Lf1[2];
1952
1953 $Flm[0]=$Lf0[3]; $Flm[1]=$Lf1[3];
1954 $Fmm[0]=$Lf0[4]; $Fmm[1]=$Lf1[4];
1955 $Frm[0]=$Lf0[5]; $Frm[1]=$Lf1[5];
1956
1957 $Flb[0]=$Lf0[6]; $Flb[1]=$Lf1[6];
1958 $Fmb[0]=$Lf0[7]; $Fmb[1]=$Lf1[7];
1959 $Frb[0]=$Lf0[8]; $Frb[1]=$Lf1[8];
1960
1961 }
1962
1963
1964 #======================================
1965
1966 sub rrSf {
1967
1968 ## do the FRONT middle slice Fm transform (side only)
1969 ## s = side; 0=colour, 1= number
1970 ##----------SIDE---------------
1971
1972 @SFs0=($Lmb[0],$Lmm[0],$Lmt[0],
1973 $Ulm[0],$Umm[0],$Urm[0],
1974 $Rmt[0],$Rmm[0],$Rmb[0],
1975 $Drm[0],$Dmm[0],$Dlm[0]);
1976
1977 @SFs1=($Lmb[1],$Lmm[1],$Lmt[1],
1978 $Ulm[1],$Umm[1],$Urm[1],
1979 $Rmt[1],$Rmm[1],$Rmb[1],
1980 $Drm[1],$Dmm[1],$Dlm[1]);
1981
1982 $Ulm[0]=$SFs0[0]; $Ulm[1]=$SFs1[0];
1983 $Umm[0]=$SFs0[1]; $Umm[1]=$SFs1[1];
1984 $Urm[0]=$SFs0[2]; $Urm[1]=$SFs1[2];
1985
1986 $Rmt[0]=$SFs0[3]; $Rmt[1]=$SFs1[3];
1987 $Rmm[0]=$SFs0[4]; $Rmm[1]=$SFs1[4];
1988 $Rmb[0]=$SFs0[5]; $Rmb[1]=$SFs1[5];
1989
1990 $Drm[0]=$SFs0[6]; $Drm[1]=$SFs1[6];
1991 $Dmm[0]=$SFs0[7]; $Dmm[1]=$SFs1[7];
1992 $Dlm[0]=$SFs0[8]; $Dlm[1]=$SFs1[8];
1993
1994 $Lmb[0]=$SFs0[9]; $Lmb[1]=$SFs1[9];
1995 $Lmm[0]=$SFs0[10]; $Lmm[1]=$SFs1[10];
1996 $Lmt[0]=$SFs0[11]; $Lmt[1]=$SFs1[11];
1997 }
1998
1999
2000 #======================================
2001
2002 sub rrBp {
2003
2004 ## Back rotation anticlockwise (side + face)
2005 ## do the Bp side transform
2006 ## s = side; 0=colour, 1= number
2007 ## --------------Side-----------------
2008
2009 @BPs0=($Llb[0],$Llm[0],$Llt[0],
2010 $Ult[0],$Umt[0],$Urt[0],
2011 $Rrt[0],$Rrm[0],$Rrb[0],
2012 $Drb[0],$Dmb[0],$Dlb[0]);
2013
2014 @BPs1=($Llb[1],$Llm[1],$Llt[1],
2015 $Ult[1],$Umt[1],$Urt[1],
2016 $Rrt[1],$Rrm[1],$Rrb[1],
2017 $Drb[1],$Dmb[1],$Dlb[1]);
2018
2019 $Ult[0]=$BPs0[0]; $Ult[1]=$BPs1[0];
2020 $Umt[0]=$BPs0[1]; $Umt[1]=$BPs1[1];
2021 $Urt[0]=$BPs0[2]; $Urt[1]=$BPs1[2];
2022
2023 $Rrt[0]=$BPs0[3]; $Rrt[1]=$BPs1[3];
2024 $Rrm[0]=$BPs0[4]; $Rrm[1]=$BPs1[4];
2025 $Rrb[0]=$BPs0[5]; $Rrb[1]=$BPs1[5];
2026
2027 $Drb[0]=$BPs0[6]; $Drb[1]=$BPs1[6];
2028 $Dmb[0]=$BPs0[7]; $Dmb[1]=$BPs1[7];
2029 $Dlb[0]=$BPs0[8]; $Dlb[1]=$BPs1[8];
2030
2031 $Llb[0]=$BPs0[9]; $Llb[1]=$BPs1[9];
2032 $Llm[0]=$BPs0[10]; $Llm[1]=$BPs1[10];
2033 $Llt[0]=$BPs0[11]; $Llt[1]=$BPs1[11];
2034
2035 ##-----------------Back FACE-------------
2036 ## do the B face transform (in rows: 1,2,3/4,5,6/7,8,9)
2037 ## f = face; 0=colour, 1= number
2038
2039 @BPf0=($Brb[0], $Brm[0], $Brt[0], $Bmb[0], $Bmm[0], $Bmt[0], $Blb[0], $Blm[0], $Blt[0]);
2040
2041 @BPf1=($Brb[1], $Brm[1], $Brt[1], $Bmb[1], $Bmm[1], $Bmt[1], $Blb[1], $Blm[1], $Blt[1]);
2042
2043 $Brt[0]=$BPf0[0]; $Brt[1]=$BPf1[0];
2044 $Bmt[0]=$BPf0[1]; $Bmt[1]=$BPf1[1];
2045 $Blt[0]=$BPf0[2]; $Blt[1]=$BPf1[2];
2046
2047 $Brm[0]=$BPf0[3]; $Brm[1]=$BPf1[3];
2048 $Bmm[0]=$BPf0[4]; $Bmm[1]=$BPf1[4];
2049 $Blm[0]=$BPf0[5]; $Blm[1]=$BPf1[5];
2050
2051 $Brb[0]=$BPf0[6]; $Brb[1]=$BPf1[6];
2052 $Bmb[0]=$BPf0[7]; $Bmb[1]=$BPf1[7];
2053 $Blb[0]=$BPf0[8]; $Blb[1]=$BPf1[8];
2054 }
2055
2056
2057 #======================================
2058
2059
2060 #======================================
2061
2062 ## Overview of derivative transform subs
2063
2064 #======================================
2065
2066 ##---------------------------------------------
2067 ## Note that we have defined (as rotation SUBs above) just 9 primary rotation transforms:
2068 ## (x axis): rrR, rrSr, rrLp
2069 ## (y axis): rrU, rrSu, rrDp
2070 ## (z axis): rrF, rrSf, rrBp
2071 ## and since all remaining possible rotations are simply combinations of these 9
2072 ## we now define all the other rotation subs in terms of these 9 primary rotations.
2073 ## Do NOT use multiples here: write each rotation separately
2074 ## ----------------
2075 ## NB: the Sr, Su, Sf are the middle slice rotations (= Rm, Um, Fm respectively)
2076 ## (the `m' notation is much more intuitive than the S., but too late to change notation now)
2077 ## -------------
2078
2079 ## ----derivative subs from R and Sr and Lp----
2080 sub rrRp{&rrR;&rrR;&rrR}; # (=rrR3)
2081 sub rrRw{&rrR; &rrSr}; # (= rrR + rrSr)
2082 sub rrRwp{&rrR;&rrR;&rrR; &rrSr;&rrSr;&rrSr}; # (= rrRp + rrSrp)
2083 sub rrRs{&rrR;&rrLp};
2084 sub rrRsp{&rrRp;&rrL};
2085 sub rrRa{&rrR;&rrL};
2086 sub rrRap{&rrRp;&rrLp};
2087
2088 ## ---------------------
2089 sub rrL{&rrLp;&rrLp;&rrLp}; # (= rrLp3)
2090 sub rrLw{&rrLp;&rrLp;&rrLp;&rrSrp}; # (=rrLp3 + rrSrp)
2091 sub rrLwp{&rrLp;&rrSr};
2092 sub rrLs{&rrL;&rrRp};
2093 sub rrLsp{&rrLp;&rrR};
2094 sub rrLa{&rrL;&rrR};
2095 sub rrLap{&rrLp;&rrRp};
2096
2097 ## ----derivative subs from U ----
2098 sub rrUp{&rrU;&rrU;&rrU}; # (=rrU3)
2099 sub rrUw{&rrU;&rrSu}; #
2100 sub rrUwp{&rrUp;&rrSup};
2101 sub rrUs{&rrU;&rrDp};
2102 sub rrUsp{&rrUp;&rrD};
2103 sub rrUa{&rrU;&rrD};
2104 sub rrUap{&rrUp;&rrDp};
2105
2106 ## ---------------------
2107 sub rrD{&rrDp;&rrDp;&rrDp}; # (= rrDp3)
2108 sub rrDw{&rrDp;&rrDp;&rrDp;&rrSup}; # (=rrDp3 + rrSup)
2109 sub rrDwp{&rrDp;&rrSu};
2110 sub rrDs{&rrD;&rrUp};
2111 sub rrDsp{&rrDp;&rrU};
2112 sub rrDa{&rrD;&rrU};
2113 sub rrDap{&rrDp;&rrUp};
2114
2115 ## ----derivative subs from F ----
2116 sub rrFw{&rrF; &rrSf}; # (= rrF + rrSf)
2117 sub rrFp{ &rrF;&rrF;&rrF}; # (=rrF3)
2118 sub rrFwp{&rrF;&rrF;&rrF; &rrSf;&rrSf;&rrSf}; # (= rrF3 + rrSf3)
2119 sub rrFs{&rrF;&rrBp};
2120 sub rrFsp{&rrFp;&rrB};
2121 sub rrFa{&rrF;&rrB};
2122 sub rrFap{&rrFp;&rrBp};
2123
2124 ## ---------------------
2125 sub rrB{&rrBp;&rrBp;&rrBp}; # (= rrBp3)
2126 sub rrBw{&rrBp;&rrBp;&rrBp; &rrSfp}; # (=rrBp3 + rrSfp)
2127 sub rrBwp{&rrBp;&rrSf};
2128 sub rrBs{&rrB;&rrFp};
2129 sub rrBsp{&rrBp;&rrF};
2130 sub rrBa{&rrB;&rrF};
2131 sub rrBap{&rrBp;&rrFp};
2132
2133 ## ----bring all the S versions together ----
2134 sub rrSup{&rrSu;&rrSu;&rrSu}; # (=rrSu3)
2135 sub rrSd{&rrSup}; # (=rrSup)
2136 sub rrSdp{&rrSu}; # (=rrSu)
2137 sub rrSl{&rrSrp}; # (=rrSrp)
2138 sub rrSlp{&rrSr}; # (=rrSr)
2139 sub rrSrp{&rrSr;&rrSr;&rrSr}; # (=rrSr3)
2140 sub rrSfp{&rrSf;&rrSf;&rrSf}; # (=rrSf3)
2141 sub rrSb{&rrSfp}; # (=rrSfp)
2142 sub rrSbp{&rrSf}; # (=rrSf)
2143
2144
2145
2146 #======================================
2147
2148 sub rubikmod {
2149
2150 ## for MODifying (MOD 4)
2151 print " SUB rubikmod\n";
2152 ## passing one RotationElement as a parameter, & return a modified one
2153 ## make local variables
2154
2155
2156 my $rot="";
2157 my $lencode="";
2158 my $char="";
2159 my $m4=-1;
2160 my $num=-1;
2161 my $p=0;
2162
2163
2164 # grab the parameter string
2165 # $code = @_[0];
2166 $code = $_[0]; ## Perl says this is better
2167
2168 $lencode=length $code;
2169
2170 ## we want to split the code string into the front (Rubikcode) and terminal number
2171 ## so grab 1 char sequentially starting from the end of the string
2172 ## and identify the position of the first non-digit char we get to
2173 ## example: $lastchar = substr $code,-1,1 ;
2174 for ($p=-1; $p>-$lencode-1; $p=$p-1){
2175 $char = substr $code,$p,1 ;
2176 if ($char =~ /\d/) {}
2177 else{
2178 ## this char is the first non-digit from the end"
2179 ## its position = $p
2180 last;
2181 };
2182 };
2183
2184 ## now use the value of $p to split the code string
2185 ## into front part (= $rot) and back part (= $num)
2186
2187 ## get $rot
2188 $rot = substr $code, 0, ($lencode + $p + 1);
2189
2190 ## get $num
2191 $num = substr $code, $lencode +$p +1, ($lencode -(length $rot));
2192
2193 ##--------------
2194 ## if no number at all (eg D) then this Rubikcode needs to be implemented just once
2195 ## so allocate its num to have value = 1
2196
2197 ## if ($num == ""){$num=1}; ## BUT this gives an ErrorMessage when num="" etc
2198 ## so I have rewritten [if numlength --> 0 then..] then it works OK
2199
2200 $numlength = ($lencode -(length $rot));
2201 if ($numlength == 0) {$num=1};
2202 ##---------------------
2203
2204 ## determine mod 4 of the value num
2205 $m4 = $num % 4;
2206
2207 ## now return the results
2208 $rotcode=$rot;
2209 $rotnumber=$num; ## we return this so we can use it as a check
2210 $modnumber=$m4;
2211
2212 return $rotcode, $rotnumber, $modnumber;
2213
2214 } #end of sub
2215
2216
2217 #======================================
2218
2219 sub cleanstring {
2220
2221 # to clean leading and trailing whitespace from a string
2222 # from Black Book page 147
2223
2224 my $line="";
2225
2226 $line = $_[0]; # copied from my RubikMOD()
2227
2228 #clean leading & trailing whitespace
2229
2230 $line =~ s/^\s+//; ## clean leading whitespace
2231 $line =~ s/\s+$//; ## clean trailing whitespace
2232
2233 return $line;
2234 }
2235
2236
2237 #======================================
2238
2239 sub cutinfoblock {
2240
2241 ## remove each <infoblock> if any exists
2242
2243 ## pass the whole dataline
2244
2245 print " SUB cutinfoblock\n";
2246
2247 my $dataline = $_[0];
2248
2249 ## we know all brackets are balanced - as this has been checked already.
2250
2251 print " dataline = $dataline\n";
2252 my $Langle=0;
2253 my $Rangle=0;
2254 my $angleblock="";
2255 my $lenangleblock=0;
2256
2257 ## first see if there is a terminal infoblock
2258 $Langle = index $dataline, '<'; ## <
2259 $Rangle = index $dataline, '>'; ## <
2260 $lenangleblock = $Rangle - $Langle +1;
2261
2262 ##-----------------
2263 ## angleblock is the whole block <...> including both angles
2264 ## check both angles exist
2265 if ( ($Langle !=-1) and ($Rangle !=-1) )
2266 {
2267
2268 my $angleblock = substr ($dataline, $Langle, $lenangleblock);
2269 print " infoblock(s) present: first = $angleblock\n";
2270
2271 my $lenangleblock = length $angleblock;
2272 my $lendataline = length $dataline;
2273
2274
2275 # now need to remove the infoblock from $dataline
2276 # need to get front and back strings
2277 my $frontstring="";
2278 my $newfrontstring="";
2279 my $backstring="";
2280
2281 $lenbackstring= $lendataline - $Rangle -1;
2282 $frontstring = substr ($dataline,0, $Langle); # string before Langle
2283 $backstring = substr ($dataline,$Rangle +1); # string beyond Rangle
2284
2285 print " Langle possn = $Langle\n";
2286 print " Rangle possn = $Rangle\n";
2287 print " lenangleblock (diff + 1) = $lenangleblock\n";
2288 print " lendataline = $lendataline\n";
2289 print " lenbackstring = $lenbackstring\n";
2290 print " frontstring = $frontstring\n";
2291
2292 #remove the terminal comma from front string
2293 $newfrontstring = substr($frontstring, 0, $Langle -1);
2294 print " new frontstring = $newfrontstring\n";
2295 print " backstring = $backstring\n";
2296
2297 ##-------
2298 # remove angleblock from dataline (join front and back strings)
2299 $newdataline =$newfrontstring.$backstring;
2300
2301 $SequenceInfo = substr ($angleblock, 1, $lenangleblock -2);
2302
2303 print " new dataline = $newdataline\n";
2304 print " SequenceInfo = $SequenceInfo\n";
2305 print " newdataline = $newdataline\n";
2306 print " done\n\n";
2307
2308 return $SequenceInfo, $newdataline;
2309 }
2310
2311 else{
2312 # no infoblock, so need to make newdataline same as orig dataline
2313 $newdataline=$dataline;
2314 print " no <infoblock> to remove.\n\n";
2315 return $newdataline;
2316 };
2317 #-----------------
2318
2319
2320 } ## end of sub
2321
2322
2323 #======================================
2324
2325 sub fixrepeatelement {
2326
2327 print " SUB fixrepeatelement\n";
2328
2329 print " reformatting any repeat elements...\n";
2330
2331 ## this sub replaces ,-->; and (--> { and ) --> } for the repeat element
2332 ## and inserts it back into the original rotation sequence, where it now
2333 ## appears as a separate rotation element.
2334
2335 my $repeatstring="";
2336 my $lenrepeatstring="";
2337 my $newrepeatstring="";
2338 my $frontstring="";
2339 my $backstring="";
2340 my $p=0;
2341 my $q=0;
2342 my $len = 0;
2343 my $k1=0;
2344 my $k2 = 0;
2345
2346
2347 ## pass the whole dataline without the keyword
2348 my $dataline = $_[0]; # copied from my RubikMOD()
2349
2350
2351 $p = index $dataline, '(';
2352 $q = index $dataline, ')';
2353
2354 print " p = $p, q = $q\n";
2355
2356
2357 $lenrepeatstring = $q -$p +1 ;
2358 $repeatstring = substr ($dataline, $p, $lenrepeatstring);
2359
2360 print " first repeat string = $repeatstring\n";
2361 print " length of repeat string = $lenrepeatstring\n";
2362
2363 ## translate the chars
2364 $repeatstring =~ tr/,/;/; ## swap , --> ; Black book page 138--139
2365 $repeatstring =~ tr/\(/\{/; ## swap ( --> {
2366 $repeatstring =~ tr/\)/\}/; ## swap ) --> }
2367
2368 $newrepeatstring = $repeatstring;
2369
2370 print "...new repeat string = $newrepeatstring\n";
2371
2372 #------------
2373 $k1=$p; #start of cut
2374 $k2= $p + $lenrepeatstring; #end of cut
2375 $frontstring = substr ($dataline,0, $k1);
2376 $backstring = substr ($dataline,$k2);
2377 print " frontstring = $frontstring\n";
2378 print " backstring = $backstring\n";
2379
2380 # add insert
2381 $newdataline =$frontstring.$newrepeatstring.$backstring;
2382 print " new dataline = $newdataline\n";
2383
2384
2385 print " done\n\n";
2386
2387 } # end of sub
2388
2389
2390 #======================================
2391
2392 sub repeat {
2393
2394 print " SUB repeat\n";
2395
2396 ## this SUB expand the repeating elements
2397 ## this SUB receives a repeat string in the form {L,R, }3
2398 ## The original () were converted (above) into {} so we can distinguish the brackets.
2399 ## we than extract the code sequence and the terminal repeat number
2400 ## Then we join n copies of the code string to form a long cs string.
2401 ## then we insert this new long string into the main rotation sequence without the {}
2402 ## Ultimately the fully expanded rotation sequence is fed into SUB rotation for processing.
2403
2404
2405
2406 ## grab the whole repeatstring = {...}n
2407 my $repeatstring = $_[0];
2408
2409 # the string ={code}n
2410 # get the code sequence and the terminal digit
2411
2412 my $p=0;
2413 my $q=0;
2414 my $repeatnumber=0;
2415 my $repeatcode="";
2416 my $lenrepeatcode =0;
2417
2418 $p = index $repeatstring, '{';
2419 $q = index $repeatstring, '}';
2420 $lenrepeatcode = $q - $p -1;
2421
2422
2423
2424 $repeatcode = substr ($repeatstring,1,$lenrepeatcode);
2425 print " repeatcode = $repeatcode\n"; ## correct
2426
2427
2428 $lenrepeatstring= length $repeatstring;
2429
2430
2431 print " lenrepeatstring = $lenrepeatstring\n";
2432 print " lenrepeatcode = $lenrepeatcode\n";
2433 print " p = $p\n";
2434 print " q = $q\n";
2435
2436 ##-----------------------
2437 ## now get the repeat number
2438
2439 if ($lenrepeatcode == ($lenrepeatstring-2)) {
2440 print " there is no trailing number --> 1\n";
2441 $repeatnumber=1;
2442 print " set repeatnumber = $repeatnumber\n";
2443 }
2444 else{
2445 $repeatnumber= substr ($repeatstring, $q + 1); # correct
2446 print " repeatnumber = $repeatnumber\n";
2447
2448 ## need to check that repeatnumber is a valid integer
2449 if ($repeatnumber =~ /\D/){
2450 # not a valid number
2451
2452 ## renormalise brackets etc before outputting to LaTeX
2453 $repeatnumber= restorebrackets($repeatnumber);
2454
2455 gprint ("..*repeat-no. ERROR: $repeatnumber not numeric");
2456 ErrorMessage ("repeat-no. $repeatnumber not numeric ?missing comma or nested ()");
2457 };
2458
2459 }; #end of else
2460 ##-------------------
2461
2462 ## now make n copies of repeatcode and name the string = $insert
2463 ## (which is then used by another part of the prog)
2464 ## we need commas only between elements (not at end)
2465
2466 $insert=""; ## $insert = global
2467 $insert=$repeatcode;
2468 for ($t=1; $t < $repeatnumber; $t=$t+1) {$insert=$insert.",".$repeatcode};
2469
2470 print " insert = $insert\n";
2471 print " done\n\n";
2472
2473 } # end sub
2474
2475
2476 #======================================
2477
2478 sub quitprogram {
2479
2480 ## exiting the program cleanly
2481 print " closing down: writing state...\n";
2482 writestate(); ## write to the output files
2483 close; ## close all files
2484 exit;
2485 }
2486
2487
2488 #======================================
2489
2490 sub showarray {
2491
2492 # show the array as a string
2493
2494 my @newarray = @_; # copied from my RubikMOD()
2495 my $arraystring= join (",",@newarray);
2496 print " the array = *$arraystring*\n\n";
2497 }
2498
2499
2500 #======================================
2501
2502 sub cleanarray {
2503
2504 # cleans array elements of leading and trailing whitespace
2505
2506 my @cleanset=();
2507 my @line = @_;
2508 my $E;
2509
2510 foreach $E (@line) {
2511 $E =~ s/^\s+//; ## clean leading whitespace
2512 $E =~ s/\s+$//; ## clean trailing whitespace
2513 push @cleanset, $E;
2514 };
2515
2516 return @cleanset;
2517
2518 }
2519
2520
2521 #======================================
2522
2523 sub restorebrackets {
2524
2525 my $line = $_[0];
2526
2527 ## translate the chars
2528 $line =~ tr/;/,/; ## swap , --> ; Black book page 138--139
2529 $line =~ tr/\{/\(/; ## swap ( --> {
2530 $line =~ tr/\}/\)/; ## swap ) --> }
2531
2532 return $line;
2533
2534 }
2535
2536
2537 #======================================
2538
2539 sub infoblockcolon {
2540
2541 print "...SUB InfoblockColon\n";
2542
2543 ## pass the whole dataline without the keyword
2544 my $line = $_[0]; # copied from my RubikMOD()
2545
2546 if ( (index $line, '<' ) == -1) {
2547 # no infoblock, so need to make newdataline same as orig dataline
2548 print " no <infoblock> found.\n\n";
2549 $newdataline=$dataline;
2550 return $newdataline;
2551 }
2552 else{
2553 print " infoblock(s) present\n";
2554
2555 print " start-string = $line\n";
2556
2557 # look at each char
2558 my $j=0;
2559 my $char="";
2560
2561 my $lenstring = 0;
2562 $lenstring= length $line;
2563
2564 # set initial state of inout-flag
2565 my $inoutflag="outside";
2566
2567 #------------------------
2568 for ($j=0; $j<=$lenstring; $j=$j+1) {
2569 $char = substr ($line,$j,1);
2570
2571 if ( ($char eq ',') and ($inoutflag eq 'inside')) {
2572 # replace the char with ;
2573 substr ($line, $j, 1, ";");
2574 print " colon-string = $line\n";
2575 };
2576
2577 ## need these at end of the loop
2578 if ($char eq '<'){$inoutflag = "inside"};
2579 if ($char eq '>'){$inoutflag = "outside"};
2580
2581 }; # end of for
2582
2583 #---------------------------
2584
2585 # -- repeat for [ ] brackets------
2586 $inoutflag="outside";
2587
2588 for ($j=0; $j<=$lenstring; $j=$j+1) {
2589 $char = substr ($line,$j,1);
2590
2591 if ( ($char eq ',') and ($inoutflag eq 'inside')) {
2592 # replace the char with ;
2593 substr ($line, $j, 1, ";");
2594 print " colon-string = $line\n";
2595 };
2596
2597 ## need these at end of the loop
2598 if ($char eq '['){$inoutflag = "inside"};
2599 if ($char eq ']'){$inoutflag = "outside"};
2600
2601 }; # end of for
2602
2603
2604 #----------------------------------
2605 ## make an array from the string so we can manipulate the elements
2606 our @linedata=();
2607 @linedata= split (/,/, $line);
2608
2609 #----------clean the array-------
2610 my $E;
2611 my @cleandata=();
2612
2613 foreach $E (@linedata) {
2614 $E =~ s/^\s+//; ## clean leading whitespace
2615 $E =~ s/\s+$//; ## clean trailing whitespace
2616 push @cleandata, $E;
2617 };
2618
2619 print " colon-array = @cleandata\n";
2620
2621 #=================
2622
2623 # Because <infoblocks> can be located inside curved brackets
2624 # as for example, (\sixspot)2, [\sixspot macro contains an infoblock].
2625 # Consequently, we need to remove
2626 # the <..> blocks as parts of a string, not as elements in an array.
2627 # --otherwise, removing the terminal infoblock associated with \sixspot
2628 # will result in also removing the right-hand curved bracket --> error.
2629 # So we return the data as a string, and then send it to sub cutinfoblock later.
2630
2631 $newdataline = join (",", @cleandata);
2632 print "...done\n\n";
2633
2634 return $newdataline;
2635
2636 } ## end of else
2637 } ## end of sub
2638
2639
2640 #======================================
2641
2642 sub RemoveAllSpaces {
2643
2644 # remove all spaces in a string
2645 # from Black book page 143
2646
2647 my $string=$_[0];
2648
2649 $string =~ s/\s//g; # OK
2650
2651 return $string;
2652
2653 }
2654
2655 #======================================
2656
2657 sub CheckSyntax {
2658
2659 ## this check is used at an early stage in the program, so we can terminate early
2660 ## if necessary. We check that all () {} <> are matched (if any exist),
2661 ## missing commas, illegal combinations of chars etc.
2662 ## if any serious errors (eg brackets not balanced), then we SET an errorflag,
2663 ## and terminate the program.
2664
2665 print " SUB CheckSyntax\n";
2666
2667 my $dataline = $_[0];
2668
2669 ## first clean out all spaces in a string
2670 ## so we can then look for specific combinations of characters
2671 $dataline=RemoveAllSpaces($dataline);
2672
2673 print " dataline = $dataline\n";
2674
2675 ##-------------------------------------
2676 ## first we check for unbalanced brackets
2677 ## count brackets; Angle, Square, Curved
2678 my ($nleftA, $nrightA) = 0;
2679 my ($nleftS, $nrightS) = 0;
2680 my ($nleftC, $nrightC) = 0;
2681 my ($leftsum, $rightsum) =0;
2682
2683 ## Blackbook p 139 - counting chars in a string
2684 $nleftA = ($dataline =~ tr/</</);
2685 $nrightA = ($dataline =~ tr/>/>/);
2686
2687 $nleftS = ($dataline =~ tr/[/[/);
2688 $nrightS = ($dataline =~ tr/]/]/);
2689
2690 $nleftC = ($dataline =~ tr/(/(/);
2691 $nrightC = ($dataline =~ tr/)/)/);
2692
2693 print " left and right <> = $nleftA, $nrightA\n";
2694 print " left and right [] = $nleftS, $nrightS\n";
2695 print " left and right () = $nleftC, $nrightC\n";
2696
2697 $leftsum = $nleftA + $nleftS + $nleftC;
2698 $rightsum =$nrightA + $nrightS + $nrightC;
2699
2700 print " leftsum, rightsum = $leftsum, $rightsum\n";
2701
2702
2703 my $errorflag = "";
2704
2705 if ($leftsum != $rightsum)
2706 {
2707
2708 if ( $nleftS != $nrightS )
2709 ## Square brackets
2710 {
2711 gprint ("..*brackets ERROR [ ] Left [$nleftS not equal to Right ]$nrightS");
2712 ErrorMessage ("brackets [ ]: Left [$nleftS not equal to Right ]$nrightS");
2713 $errorflag="SET";
2714 }
2715
2716 if ( $nleftC != $nrightC )
2717 ## Curved brackets
2718 {
2719 gprint ("..*brackets ERROR ( ) Left ($nleftC not equal to Right )$nrightC");
2720 ErrorMessage ("brackets ( ): Left ($nleftC not equal to Right )$nrightC");
2721 $errorflag="SET";
2722 }
2723
2724 if ( $nleftA != $nrightA )
2725 ## Angle brackets
2726 {
2727 gprint ("..*brackets ERROR < > Left <$nleftA not equal to Right >$nrightA");
2728 ErrorMessage ("brackets < >: Left <$nleftA not equal to Right >$nrightA");
2729 $errorflag="SET";
2730 }
2731
2732 };
2733
2734 ##--------------------------
2735
2736
2737
2738 ## check for other bad syntax, eg illegal pairings of characters
2739 ## BlackBook p136
2740
2741 my ($char1, $char2, $charpair) = "";
2742 my ($j, $lenstring) = 0;
2743 $lenstring= length $dataline;
2744 print " lenstring = $lenstring\n";
2745
2746
2747 ## we set up a system which allows us to know whether or not we are
2748 ## inside a set of brackets. To do this we increment / decrement counters
2749 ## each time we pass through a bracket.
2750 ## If sum NOT equal to zero, then we are inside etc.
2751
2752 ## first initialise each left and right variable.
2753 ## seems important that these initialisations are done separately.
2754
2755 my $angleNumLeft = 0;
2756 my $angleNumRight = 0;
2757 my $angleNumSum = 0;
2758
2759 my $squareNumLeft = 0;
2760 my $squareNumRight = 0;
2761 my $squareNumSum = 0;
2762
2763 my $curvedNumLeft = 0;
2764 my $curvedNumRight = 0;
2765 my $curvedNumSum = 0;
2766
2767
2768 ## look at each char, and each pair of chars
2769 ## with brackets, we increment (right) and decrement (left) the count
2770 ## so we can tell if we are inside or outside a set of nested brackets.
2771 ## (we need to detect errors in the rotation sequence itself
2772 ## and also inside squarebrackets] since these can occur anywhere,
2773 ## but not in the angle infoblocks <..> where we want to be able to write anything)
2774
2775 for ($j=0; $j<= $lenstring; $j=$j+1) {
2776 $charpair = substr ($dataline,$j,2);
2777 $char1 = substr ($dataline,$j,1);
2778 $char2 = substr ($dataline,$j+1,1);
2779
2780 ## at top of FOR loop
2781 if ($char1 eq '<'){ $angleNumLeft = $angleNumLeft+1};
2782 if ($char1 eq '>'){$angleNumRight = $angleNumRight-1};
2783
2784 if ($char1 eq '['){$squareNumLeft = $squareNumLeft+1};
2785 if ($char1 eq ']'){$squareNumRight = $squareNumRight-1};
2786
2787 if ($char1 eq '('){$curvedNumLeft = $curvedNumLeft+1};
2788 if ($char1 eq ')'){$curvedNumRight = $curvedNumRight-1};
2789
2790
2791 $angleNumSum = $angleNumLeft + $angleNumRight;
2792 $squareNumSum = $squareNumLeft + $squareNumRight;
2793 $curvedNumSum = $curvedNumLeft + $curvedNumRight;
2794
2795 ##RWDN 22 Oct 2017
2796
2797 ##------------
2798 ## need to trap nested (( )) inside squarebrackets
2799
2800 if ($squareNumSum != 0) {
2801 if ( $charpair =~ m/(\(\(|\)\))/ )
2802 { # nested curved brackets inside sq brackets
2803 gprint ("..*syntax error: $charpair -- nested ((..)) in [ ]");
2804 ErrorMessage("$charpair -- syntax error: nested ((..)) not allowed in [ ]");
2805 $errorflag="SET";
2806 };
2807 };
2808 ##--------------
2809
2810 ## if outside angle brackets AND outside square brackets
2811 ## then we are checking ONLY the rotation sequence codes
2812
2813 if ( ($angleNumSum == 0) and ($squareNumSum == 0) ) {
2814
2815 ## A-Za-z< A-Za-z[ A-Za-z( )A-Za-z >A-Za-z ]A-Za-z
2816 ## ]< ][ ]( ]) ]< )< )[ )( >< >[ >( d( d[ d<
2817
2818 if ( $charpair =~ m/([A-Za-z]\<|[A-Za-z]\[|[A-Za-z]\(|\)[A-Za-z]|\>[A-Za-z]|\][A-Za-z]|\]\<|\]\[|\]\(|\]\)|\]\<|\)\<|\)\[|\)\(|\>\<|\>\[|\>\(|\d\(|\d\[|\d\<|\[\[|\<\<|\(\(|\)\)|\]\]|\>\>)/ )
2819 {
2820 gprint ("..*syntax error: $charpair -- missing comma");
2821 ErrorMessage("$charpair -- syntax error: missing comma");
2822 $errorflag="SET";
2823 next;
2824 };
2825
2826 # trap nested curved brackets (= inside)
2827 if ( ($char2 eq "(" ) and ($curvedNumSum != 0) ) {
2828 ## nested curved brackets
2829 gprint ("..*syntax error: $charpair -- nested ((..))");
2830 ErrorMessage("$charpair -- syntax error: nested ((..)) not allowed");
2831 $errorflag="SET";
2832 };
2833
2834 #-----remove--------------------------------------
2835 # trap comma inside [ ] ( eq inside)
2836 # if ($squareNumSum != 0){
2837 # if ($char1 eq ",") {
2838 # gprint ("..*syntax error: $charpair -- comma not allowed in [ ]");
2839 # ErrorMessage("$charpair -- syntax error: comma not allowed in [ ]");
2840 # $errorflag="SET";
2841 # next;
2842 # };
2843 # }; # end of if
2844 #-------------------------------------------------
2845
2846 ## detect end of string
2847 if ($j == $lenstring -1) {last};
2848
2849
2850 }; # end of if
2851
2852 }; # end of for
2853 #---------------------------
2854
2855
2856 if ($errorflag eq "SET") {
2857
2858 ## closing down
2859 gprint ("..*Quiting Perl program -- syntax error");
2860 ErrorMessage ("QUITTING PERL PROGRAM -- syntax error");
2861
2862 ##------bug fix-----------------------
2863 ## RWDN 5 October 2017
2864 ## problem = since we are here checking syntax (ie before processing any
2865 ## output SequenceXX strings) all four SequenceXX strings will be empty just now.
2866 ## This then causes an error if the Rubik user code includes a ShowSequence command,
2867 ## since the ForEachX macro used by ShowSequence macro cannot handle an empty string
2868 ## when dealing with SequenceShort and SequenceLong.
2869 ## So we heve to force these two strings to be just a [\space] before they are output
2870 ## by the SUB writestate.
2871 ## ie we set SequenceShort and SequenceLong strings to \space here before
2872 ## CALLing the SUB quitprogram().
2873 ## (to avoid a Rubikcube ShowSequence{}{}{} error if argument is empty
2874 ## or is an expandable macro)
2875 ## This problem arises because the ShowSequence macro uses the ForEachX macro
2876 ## to process each cs element in a string.
2877 ## Also need to add at least one empty char or comma at end of SequenceLong string,
2878 ## as final char (comma) is removed when writing to the out file in SUB writestate
2879 ## (CALLed by SUB quitprogram) just prior to closing down.
2880 ## NB: if there is no extra terminal char for SequenceLong string,
2881 ## then \space --> \spac --> TEX error message
2882 ##
2883 ## This issue does not seem to be a problem for SequenceInfo and SequenceName,
2884 ## as they are not returned as cs strings.
2885
2886 $SequenceShort="\\space";
2887 $SequenceLong= "\\space,";
2888
2889 ##---------------------------
2890
2891 print " closing down -- writing state........... OK\n";
2892
2893 quitprogram();
2894
2895 }
2896 else{
2897 print " syntax OK; brackets balanced OK\n";
2898 print " done\n\n";
2899 };
2900
2901
2902 } ## end sub
2903
2904
2905
2906 #====================
2907
2908 sub inverse {
2909
2910 my $E = $_[0];
2911
2912 my $lastchar = substr ($E, -1,1);
2913 my $frontchars = substr ($E, 0,-1); # correct
2914
2915 if ($lastchar eq "2") {$newE = $E}
2916
2917 elsif ($lastchar eq "p") { $newE = $frontchars}
2918
2919 else { $newE = $E."p"};
2920
2921 return $newE;
2922 }
2923
2924 ##======================
2925 ##EOF
2926