Beispiel #1
0
        private List <SquireUnit> GetSquiresForMarbleMoveByDirection(Marble marble, MoveDirection marbleDirection)
        {
            var(from, to) = (0, 0);
            var squires = new List <SquireUnit>();

            switch (marbleDirection)
            {
            case MoveDirection.N:
                (from, to) = (marble.Location.Row, 0);
                for (int i = from; i >= to; i--)
                {
                    squires.Add(SquireUnits[i, marble.Location.Column]);
                }
                break;

            case MoveDirection.S:
                (from, to) = (marble.Location.Row, Size);
                for (int i = from; i < to; i++)
                {
                    squires.Add(SquireUnits[i, marble.Location.Column]);
                }
                break;

            case MoveDirection.E:
                (from, to) = (marble.Location.Column, Size);
                for (int i = from; i < to; i++)
                {
                    squires.Add(SquireUnits[marble.Location.Row, i]);
                }
                break;

            case MoveDirection.W:
                (from, to) = (marble.Location.Column, 0);
                for (int i = from; i >= to; i--)
                {
                    squires.Add(SquireUnits[marble.Location.Row, i]);
                }
                break;

            default: break;
            }
            return(squires);
        }
Beispiel #2
0
        public static List <TestCaseInputDataModel> ParseTestCaseInputDataFromFile(string fileName = "./input.txt")
        {
            var testCaseDataModels = new List <TestCaseInputDataModel>();
            var inputText          = System.IO.File.ReadAllText(fileName);
            var inputDataLines     = Array.ConvertAll(inputText.Split('\n'), item => item);
            TestCaseInputDataModel testCaseData = null;

            foreach (var line in inputDataLines)
            {
                var inputDataLine = Array.ConvertAll(line.Split(' '), item => Convert.ToInt32(item));
                // The input file ends with a line containing 3 zeros
                if (inputDataLine.Length == 3 &&
                    inputDataLine.All(item => item == 0))
                {
                    break;
                }
                //first line of each test case contains 3 numbers: N M W
                else if (inputDataLine.Length == 3)
                {
                    testCaseData = new TestCaseInputDataModel
                    {
                        Marbles = new List <Marble>(),
                        Holes   = new List <Hole>(),
                        Walls   = new List <Wall>()
                    };
                    testCaseDataModels.Add(testCaseData);

                    // first the size N [2, 40] of the board
                    var size = testCaseData.BoardSize = inputDataLine[0];
                    if (size < 2 || size > 40)
                    {
                        throw new Exception("Game-board size should be in range of [2, 40]");
                    }
                    // init squire-units
                    testCaseData.SquireUnits = new SquireUnit[size, size];
                    for (int i = 0; i < size; i++)
                    {
                        for (int j = 0; j < size; j++)
                        {
                            var squireUnitLocation = new Location(i, j);
                            var squireUnit         = new SquireUnit(squireUnitLocation);

                            testCaseData.SquireUnits[i, j] = squireUnit;
                        }
                    }

                    // second the number M (M > 0) of marbles
                    testCaseData.MarbleOrHoleCount = inputDataLine[1];
                    if (testCaseData.MarbleOrHoleCount <= 0)
                    {
                        throw new Exception("Marble or hole count should be positive integer");
                    }

                    // third the number W of walls
                    testCaseData.WallCount = inputDataLine[2];
                    if (testCaseData.WallCount < 0)
                    {
                        throw new Exception("Wall count can not be negative");
                    }

                    continue;
                }
                // next M lines contains 2 integers: location(row, column)
                else if (inputDataLine.Length == 2)
                {
                    if (testCaseData.Marbles.Count < testCaseData.MarbleOrHoleCount)
                    {
                        // the locations of the marbles
                        var location = new Location(
                            row: inputDataLine[0],   // first integer is a row location
                            column: inputDataLine[1] // the second is a column location
                            );
                        // Marbles are numbered from 1 to M
                        var marbleNumber = testCaseData.Marbles.Count + 1;
                        var marble       = new Marble(marbleNumber, location);
                        testCaseData.Marbles.Add(marble);
                        continue;
                    }
                    // next M of those lines represent the locations of the holes
                    else if (testCaseData.Holes.Count < testCaseData.MarbleOrHoleCount)
                    {
                        // the locations of holes
                        var location = new Location(
                            row: inputDataLine[0],   // first integer is a row location
                            column: inputDataLine[1] // the second is a column location
                            );
                        // Holes are numbered from 1 to M
                        var holeNumber = testCaseData.Holes.Count + 1;
                        var hole       = new Hole(holeNumber, location);
                        testCaseData.Holes.Add(hole);
                        continue;
                    }
                }
                // next W lines represent the wall locations, each with 4 integers
                else if (inputDataLine.Length == 4)
                {
                    if (testCaseData.Walls.Count < testCaseData.WallCount)
                    {
                        // first pair are the (row, column) of the square on one side of the wall
                        var location1 = new Location(
                            row: inputDataLine[0],
                            column: inputDataLine[1]
                            );
                        // he second pair are the (row, column) of the square on the other side of the wall
                        var location2 = new Location(
                            row: inputDataLine[2],
                            column: inputDataLine[3] // the second is a column location
                            );
                        if ((location1.Row > location2.Row && location1.Column == location2.Column) ||
                            (location1.Row == location2.Row && location1.Column > location2.Column))
                        {
                            var tmp = location1;
                            location1 = location2;
                            location2 = tmp;
                        }
                        var wall = new Wall(location1, location2);
                        testCaseData.Walls.Add(wall);
                        continue;
                    }
                }
            }
            return(testCaseDataModels);
        }