/**************************************************************************
*                                                                         *
* Mastermind-Code-Generator version 0.99 / 21/Feb/2010                    *
* (c) 2010 Jürgen Müller-Lütken kontakt@p-six.de                          *
* free to use with kind permission if you leave this note inside          *
*                                                                         *
**************************************************************************/

// configure the length of the code and
// the kind of code (colors, digits etc.)
var codeLength = 4;
var elementsForCoding = new Array("rot", "orange", "gelb", "blau", "braun", "gruen");


/**************************************************************************
*                                                                         *
* Example of using the generator:                                         *
*                                                                         *
* createPatterns(codeLength);                                             *
* var copiedElements = elementsForCoding.Copy();                          *
* secretCode = createSecretCode(copiedElements, codeLength, getPattern());*
*                                                                         *
**************************************************************************/

// don't change anything above, only if you know what you do
var patterns = new Array();

// helper functions
Array.prototype.Shuffle = function()
{
  var tmp, rand;
  for(var i =0; i < this.length; i++)
  {
    rand = Math.floor(Math.random() * this.length);
    tmp = this[i];
    this[i] = this[rand];
    this[rand] = tmp;
  }
}

Array.prototype.Copy = function()
{
  var copiedArray = new Array();
  for(var i =0; i < this.length; i++)
  {
    copiedArray[i] = this[i];
  }
  return copiedArray;
}

function checkStringLength(str, len)
{
  if(str.length < len)
  {
    return true;
  }
  return false;
}

// function creating all possible patterns (for a code of four colors it
// will create an array with these patterns: 1234, 1123, 1122, 1112, 1111)
function createPatterns(codeLength)
{
  var blockLength = 0;

  while(blockLength < codeLength)
  {
    var pattern = '';
    var goAhead = false;
    var blocks = 1;
    var possibilities = true;
    var numberOfBlocks = 0;
    var innerBlockLength = 2;

    blockLength++;

    while(possibilities == true)
    {
      pattern = '';
      if(blockLength > 1)
      {
        numberOfBlocks = Math.floor((codeLength-blockLength)/innerBlockLength);
        if((codeLength-blockLength) >= 2 && numberOfBlocks == 0)
          break;
      }
      for(var i=1; i<=codeLength; i++)
      {
        if(i == 1)
        {
          for(var j=0; j<blockLength; j++)
          {
            pattern += String(i) + "|";
          }
          goAhead = true;
        }
        else if(numberOfBlocks && blocks > 1 && i <= blocks)
        {
          for(var x=0; x<innerBlockLength; x++)
          {

            if(goAhead = checkStringLength(pattern, (2*codeLength)))
              pattern +=  (i + '|');
            else
              break;
          }
        }
        else
        {
          if(goAhead = checkStringLength(pattern, (2*codeLength)))
              pattern +=  (i + '|');
          else
            break;
        }
        if(!goAhead)
          break;
      }
      if(blockLength > 1 && blocks > numberOfBlocks)
      {
        blocks = 1;
        innerBlockLength++;
      }

      if(innerBlockLength > blockLength || numberOfBlocks == 0)
        possibilities = false;
      blocks++;
      patterns.push(pattern.substring(0, pattern.length-1));
    }
  }
  patterns.Shuffle();
}

// choose a random pattern from the shuffled array of all patterns
function getPattern()
{
  var rnd = Math.floor(Math.random()*(patterns.length));
  var pattern = patterns[rnd].split('|');
  pattern.Shuffle();
  return pattern;
}

// change the pattern into the code (of colors, digits, symbols etc.)
function createSecretCode(copiedElements, codeLength, pattern)
{
  var secretCode = new Array(codeLength);
  var fullCode = false;
  var count = 0;
  for(j=0; j<secretCode.length; j++)
    secretCode[j] = 'x';
  for(var k=0; k<codeLength; k++)
  {
    var rnd = Math.floor(Math.random()*copiedElements.length);
    var color = copiedElements[rnd];
    var deleted = false;
    var sC = pattern[k];
    for(var q=0; q<pattern.length; q++)
    {
      if(secretCode[q] == 'x' && pattern[q] == sC)
      {
        secretCode[q] = color;
        count++;
        if(count == codeLength)
        {
          fullCode = true;
          break;
        }
        if(!deleted)
        {
          copiedElements.splice(rnd, 1);
          deleted = true;
        }
      }
    }
    if(fullCode)
      break;
  }
  return secretCode;
}