/**
 * @(#)ninemensmorriscube.js  1.0  2010-03-26
 *
 * Changes:
 * 2010-03-26: First release
 *
 * Copyright (c) 2010 
 * Walter & Werner Randelshofer, CH-6405 Immensee, Switzerland, www.randelshofer.ch
 * Andre Boulouard, F-22220 Treguier, France, www.mementoslangues.fr
 * All rights reserved.
 *
 * Algorithms computed with AlgorithmPicker
 * AlgorithmPicker, Copyright (c) 2009, Andre Boulouard, Walter Randelshofer, Werner Randelshofer
 *
 * The copyright of this software is owned by Walter Randelshofer, Werner Randelshofer and Andre Boulouard. 
 * You may not use, copy or modify this software, except in accordance with the license agreement you entered 
 * into with Walter Randelshofer, Werner Randelshofer and Andre Boulouard.
 */

/**
 * This file is intended to be included in the head of a HTML document. 
 *
 * Example:
 * <html>
 *   <head>
 *     <title>Page Title</title>
 *     <script language="JavaScript" src="../lib/cubeplayer.js"></script>
 *     <script language="JavaScript" src="../lib/7x_ninemensmorriscube.js"></script>
 *   </head>
 *   <body>
 *     <table width="470" cellspacing="0" cellpadding="0" border="0">
 *       <tr>
 *         <td><script language="JavaScript">
 *           
 *           var script = decodeURIComponent(location.search.substring(1));
 *           if (script == null || script.length==0) {
 *             script = getNineMensMorrisScript(nmm_string);
 *           }
 *           insertCubePlayer("ninemensmorris", script, "player");
 *         </script></td>
 *       </tr>
 *     </table>
 *   </body>
 * </html>
 */



// Variables
function hello() {
  
  var i;
  var newAlgorithm;
  var oldStateArray = [0];
  var newStateArray = [0];
  
  //Note 1: state arrays must be initialized only *once* at page load
  //Note 2: array newState must be updated *each* time a player has moved a token
  //Note 3: array oldState is automatically updated to newState at the end of the algorithm search
  
  //Initialize State Arrays
  initStateArrays(oldStateArray, newStateArray);
  
  //Demo 1: compound algorithm
  //Update newState array with new values
  //Blank: 0, Blue: 1, Red: 2
  newStateArray = [
    0, 0, 0, 0, 0, 2, 0, 0, 
    0, 0, 1, 1, 1, 2, 1, 1, 
    0, 0, 2, 2, 2, 0, 0, 1  
  ];
  
  //Get new algorithm from updated newStateArray
  newAlgorithm = getAlgorithm(oldStateArray, newStateArray);
  
  //Write new algorithm to current web page
  document.writeln("Nine Men's Morris Cube Demo 1 - Compound algorithm:" + "<br />");
  document.writeln(newAlgorithm + "<br />");
  document.writeln("<br />");
  
  //Demo 2: incremental algorithm
  //Update newState Array with new values
  //Blank: 0, Blue: 1, Red: 2
  newStateArray = [
    0, 0, 0, 0, 0, 2, 0, 0, 
    0, 0, 1, 1, 1, 2, 1, 1, 
    0, 0, 2, 2, 0, 2, 0, 1
  ];
  
  //Get new algorithm from updated newStateArray
  newAlgorithm = getAlgorithm(oldStateArray, newStateArray);
  
  //Write new algorithm to current web page
  document.writeln("Nine Men's Morris Cube Demo 2 - Incremental algorithm:" + "<br />");
  document.writeln(newAlgorithm + "<br />");
  document.writeln("<br />");
}


function initStateArrays(oldStateArray, newStateArray) {
  
  //Initialize State Arrays
  var i;
  for (i = 0 ; i < 24 ; i++) {
    oldStateArray[i] = 0;
    newStateArray[i] = 0;
  }
}


function getAlgorithm(oldStateArray, newStateArray) {
  
  //Get new algorithm from new state
  //Update old state to new state afterwards
  var i;
  var newAlgorithm;
  
  //Initialize
  newAlgorithm = "";
  
  //Initialize Algorithm Arrays
  //Array of Algorithms
  var algorithmArray = new Array(
  "R F' D2 F R' U2 R F' D2 F R' U2 ",
  "B' R B MR B' R' B MR' ",
  "U B' L2 B U' R2 U B' L2 B U' R2 ",
  "MU' B' D B MU B' D' B ",
  "D2 B' R U2 R' B D2 B' R U2 R' B ",
  "B' L B MR' B' L' B MR ",
  "L2 F' U R2 U' F L2 F' U R2 U' F ",
  "MU B' U B MU' B' U' B ",
  "NL B NU B' NL' B NU' B' ",
  "B' MR' B NU B' MR B NU' ",
  "NR B' NU B NR' B' NU' B ",
  "B' NR B MU B' NR' B MU' ",
  "NR B ND B' NR' B ND' B' ",
  "B' MR B ND B' MR' B ND' ",
  "NL B' ND B NL' B' ND' B ",
  "B' NL B MU' B' NL' B MU ",
  "N3L B N3U B' N3L' B N3U' B' ",
  "B' MR' B N3U B' MR B N3U' ",
  "N3R B' N3U B N3R' B' N3U' B ",
  "B' N3R B MU B' N3R' B MU' ",
  "N3R B N3D B' N3R' B N3D' B' ",
  "B' MR B N3D B' MR' B N3D' ",
  "N3L B' N3D B N3L' B' N3D' B ",
  "B' N3L B MU' B' N3L' B MU ");
  
  //Array of Inverted Algorithms
  var invertedAlgorithmArray = new Array(
  "U2 R F' D2 F R' U2 R F' D2 F R' ",
  "MR B' R B MR' B' R' B ",
  "R2 U B' L2 B U' R2 U B' L2 B U' ",
  "B' D B MU' B' D' B MU ",
  "B' R U2 R' B D2 B' R U2 R' B D2 ",
  "MR' B' L B MR B' L' B ",
  "F' U R2 U' F L2 F' U R2 U' F L2 ",
  "B' U B MU B' U' B MU' ",
  "B NU B' NL B NU' B' NL' ",
  "NU B' MR' B NU' B' MR B ",
  "B' NU B NR B' NU' B NR' ",
  "MU B' NR B MU' B' NR' B ",
  "B ND B' NR B ND' B' NR' ",
  "ND B' MR B ND' B' MR' B ",
  "B' ND B NL B' ND' B NL' ",
  "MU' B' NL B MU B' NL' B ",
  "B N3U B' N3L B N3U' B' N3L' ",
  "N3U B' MR' B N3U' B' MR B ",
  "B' N3U B N3R B' N3U' B N3R' ",
  "MU B' N3R B MU' B' N3R' B ",
  "B N3D B' N3R B N3D' B' N3R' ",
  "N3D B' MR B N3D' B' MR' B ",
  "B' N3D B N3L B' N3D' B N3L' ",
  "MU' B' N3L B MU B' N3L' B ");
  
  //Compare newStateArray to oldStateArray
  j = 0;
  for (i = 0 ; i < 24 ; i++) {
    if (oldStateArray[i] == 0) {
      if (newStateArray[i] == 0) {
        newAlgorithm += "";
      } else if (newStateArray[i] == 1) {
        if (j == 0) {
          newAlgorithm +=        algorithmArray[i];
        } else {
          newAlgorithm += ". " + algorithmArray[i];
        }
        j = j + 1;
      } else if (newStateArray[i] == 2) {
        if (j == 0) {
          newAlgorithm +=        invertedAlgorithmArray[i];
        } else {
          newAlgorithm += ". " + invertedAlgorithmArray[i];
        }
        j = j + 1;
      }
    } else if (oldStateArray[i] == 1) {
      if (newStateArray[i] == 0) {
        if (j == 0) {
          newAlgorithm +=        invertedAlgorithmArray[i];
        } else {
          newAlgorithm += ". " + invertedAlgorithmArray[i];
        }
        j = j + 1;
      } else if (newStateArray[i] == 1) {
        newAlgorithm += "";
      } else if (newStateArray[i] == 2) {
        if (j == 0) {
          newAlgorithm +=        algorithmArray[i];
        } else {
          newAlgorithm += ". " + algorithmArray[i];
        }
        j = j + 1;
      }
    } else if (oldStateArray[i] == 2) {
      if (newStateArray[i] == 0) {
        if (j == 0) {
          newAlgorithm +=        algorithmArray[i];
        } else {
          newAlgorithm += ". " + algorithmArray[i];
        }
        j = j + 1;
      } else if (newStateArray[i] == 1) {
        if (j == 0) {
          newAlgorithm +=        invertedAlgorithmArray[i];
        } else {
          newAlgorithm += ". " + invertedAlgorithmArray[i];
        }
        j = j + 1;
      } else if (newStateArray[i] == 2) {
        newAlgorithm += "";
      }
    }
  }
  
  //Update oldStateArray
  for (i = 0 ; i < 24 ; i++) {
    oldStateArray[i] = newStateArray[i];
  }
  
  //Return new algorithm
  return newAlgorithm;
}

