Exemplo n.º 1
0
 public void Validate(Board.SolutionType solutionType)
 {
     this.size = input.Count * input.Count - 1;
     ValidateShape();
     ValidateNumbers();
     ValidateInversions(solutionType);
 }
Exemplo n.º 2
0
        private void ValidateInversions(Board.SolutionType solutionType)
        {
            int numberOfInversions         = CountNumberOfRegularInversions(input);
            int numberOfInversionsSolution = CountNumberOfRegularInversions(solution);

            int start0Index = -1;
            int goal0Index  = -1;

            for (int i = 0; i < input.Count; i++)
            {
                start0Index = input[i].FindIndex(c => c == 0);
                if (start0Index > -1)
                {
                    start0Index = i * input.Count + start0Index;
                    break;
                }
            }
            for (int i = 0; i < solution.Count; i++)
            {
                goal0Index = solution[i].FindIndex(c => c == 0);
                if (goal0Index > -1)
                {
                    goal0Index = i * solution.Count + goal0Index;
                    break;
                }
            }
            if (input.Count % 2 == 0)               // In this case, the row of the '0' tile matters
            {
                numberOfInversions         += start0Index / input.Count;
                numberOfInversionsSolution += goal0Index / solution.Count;
            }


            if (numberOfInversions % 2 != numberOfInversionsSolution % 2)
            {
                throw new ValidatorException("Unsolvable puzzle");
            }
        }