Example #1
0
        /// <summary>
        /// Does a comparison for different techniques of reading all the lines from a file and performing some rudimentary operations on them.
        /// </summary>
        /// <param name="fileName">Tested data file name</param>
        private void ReadAndProcessLinesFromFile(string fileName)
        {
            var fullPath = GetFullTestDataPath(fileName);

            if (File.Exists(fullPath))
            {
                // Get number of lines in file
                var numberOfLines = File.ReadLines(fullPath).Count();
                PrintTestMethodHeader(fileName, numberOfLines);

                // Start all techniques Iterations times and save all process times
                for (int i = 0; i < Iterations; i++)
                {
                    foreach (var method in _methodsToRun)
                    {
                        var partialResult = PartialResults.GetOrCreate(method.Method.Name);
                        var processTime   = RunFileReader(method, fullPath);
                        partialResult.AddProcessTime(processTime);
                    }
                }

                ProcessResults(fileName, numberOfLines);
                GC.Collect();
            }
            else
            {
                Console.WriteLine($"File {fullPath} doesn't exist");
            }
        }
Example #2
0
        private static PartialResults CheckDiagonals(FourInARowState state)
        {
            PartialResults summary = new PartialResults();

            //// from left bottom to right top

            for (Int32 startY = 0, mStartY = FourInARowState.RowCount - WinningCount + 1; startY < mStartY; startY++)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = 0, y = startY, my = FourInARowState.RowCount, mx = FourInARowState.ColumnCount; y < my && x < mx /* && y < state.LastEmptyRow */; x++, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            for (Int32 startX = 1, mStartX = FourInARowState.ColumnCount - WinningCount + 1; startX < mStartX; startX++)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = startX, y = 0, my = FourInARowState.RowCount, mx = FourInARowState.ColumnCount; y < my && x < mx /* && y < state.LastEmptyRow */; x++, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            //// from right bottom to left top

            for (Int32 startY = 0, mStartY = FourInARowState.RowCount - WinningCount + 1; startY < mStartY; startY++)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = FourInARowState.ColumnCount - 1, y = startY, my = FourInARowState.RowCount; y < my && x >= 0 /* && y < state.LastEmptyRow */; x--, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            for (Int32 sStartX = FourInARowState.ColumnCount - 2, mStartX = WinningCount - 1; sStartX >= mStartX; sStartX--)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = sStartX, y = 0, my = FourInARowState.RowCount; y < my && x >= 0 /* && y < state.LastEmptyRow */; x--, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            return(summary);
        }
Example #3
0
            internal void Add(PartialResults other)
            {
                OneCrossInRow += other.OneCrossInRow;

                TwoCrossesInRow += other.TwoCrossesInRow;

                ThreeCrossesInRow += other.ThreeCrossesInRow;

                FourCrossesInRow += other.FourCrossesInRow;

                OneCircleInRow += other.OneCircleInRow;

                TwoCirclesInRow += other.TwoCirclesInRow;

                ThreeCirclesInRow += other.ThreeCirclesInRow;

                FourCirclesInRow += other.FourCirclesInRow;
            }
Example #4
0
        private static PartialResults CheckColumns(FourInARowState state)
        {
            PartialResults summary = new PartialResults();

            for (Int32 x = 0, mx = FourInARowState.ColumnCount; x < mx; x++)
            {
                FieldContext context = new FieldContext();

                for (Int32 y = 0, my = FourInARowState.RowCount; y < my /* && y < state.LastEmptyRow */; y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            return(summary);
        }
Example #5
0
        /// <summary>
        /// Does a comparison for different collection of adding and sorting all the lines from a file
        /// </summary>
        /// <param name="fileName">Tested data file name</param>
        private void SaveLinesFromFile(string fileName)
        {
            var fullPath = GetFullTestDataPath(fileName);

            if (File.Exists(fullPath))
            {
                // Get number of lines in file
                var numberOfLines = File.ReadLines(fullPath).Count();
                PrintTestMethodHeader(fileName, numberOfLines);

                // Start all techniques Iterations times and save all process times
                for (int i = 0; i < Iterations; i++)
                {
                    foreach (var keyValueCollection in _keyValueCollections)
                    {
                        var partialResultOneCollection = PartialResults.GetOrCreate($"{nameof(T1)} {keyValueCollection.Name}");

                        var processTimeOneCollection = RunProcessFile(T1, fullPath, keyValueCollection, null);
                        partialResultOneCollection.AddProcessTime(processTimeOneCollection);
                        Console.WriteLine();
                        keyValueCollection.Clear();

                        foreach (var uIntCollection in _uIntCollections)
                        {
                            var partialResultTwoCollections = PartialResults.GetOrCreate($"{nameof(T2)} {keyValueCollection.Name} {uIntCollection.Name}");
                            var processTimeTwoCollections   = RunProcessFile(T2, fullPath, keyValueCollection, uIntCollection);
                            partialResultTwoCollections.AddProcessTime(processTimeTwoCollections);
                            Console.WriteLine();
                            uIntCollection.Clear();
                            keyValueCollection.Clear();
                        }
                    }
                }

                ProcessResults(fileName, numberOfLines);
                GC.Collect();
            }
            else
            {
                Console.WriteLine($"File {fullPath} doesn't exist");
            }
        }
Example #6
0
        public Int32 Evaluate(IGameState gameState, GamePlayer player)
        {
            FourInARowState state = (FourInARowState)gameState;

            PartialResults summary = new PartialResults();

            PartialResults result = CheckColumns(state);

            summary.Add(result);

            result = CheckRows(state);

            summary.Add(result);

            result = CheckDiagonals(state);

            summary.Add(result);

            return(CalculateScore(summary, player));
        }
Example #7
0
        private static Int32 CalculateScore(PartialResults summary, GamePlayer player)
        {
            if (summary.FourCrossesInRow > 0)
            {
                return(WinValue * GetSign(FourInARowFieldState.Cross));
            }

            if (summary.FourCirclesInRow > 0)
            {
                return(WinValue * GetSign(FourInARowFieldState.Circle));
            }

            Int32 result;

            if (player == GamePlayer.PlayerMax)
            {
                result =
                    summary.ThreeCrossesInRow * 1000 * 1 +
                    summary.TwoCrossesInRow * 100 * 1 +
                    summary.OneCrossInRow * 10 * 1 +
                    summary.ThreeCirclesInRow * 5000 * -1 +
                    summary.TwoCirclesInRow * 500 * -1 +
                    summary.OneCircleInRow * 50 * -1;
            }
            else
            {
                result =
                    summary.ThreeCrossesInRow * 5000 * 1 +
                    summary.TwoCrossesInRow * 500 * 1 +
                    summary.OneCrossInRow * 50 * 1 +
                    summary.ThreeCirclesInRow * 1000 * -1 +
                    summary.TwoCirclesInRow * 100 * -1 +
                    summary.OneCircleInRow * 10 * -1;
            }

            return(result);
        }