<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using SharedKernel;
using System.Collections.ObjectModel;

namespace GameplayContext
{
    public class ScoreChecker
    {
        private readonly List<Coordinate> _coordsToRemove = new List<Coordinate>();

        public List<Coordinate> RemovedItems 
        {
            get
            {
               return _coordsToRemove;
            }
        }

        public int CheckScoreAndUpdate(List<ObservableCollection<Tile>> columns)
        {
            var score = DoCheck(columns, CheckVertically);
            if (score == 0)
            {
                score = DoCheck(columns, CheckHorizontally);
            }

            return score;
        }

        private int DoCheck(List<ObservableCollection<Tile>> columns, Func<List<ObservableCollection<Tile>>, int> checker)
        {
            _coordsToRemove.Clear();
            var score = checker(columns);
            DoRemove(columns);
            return score;
        }

        private int CheckVertically(List<ObservableCollection<Tile>> columns)
        {
            var numberVerticalItemsInSeries = 1;
            string lastItemChecked = null;

            for (int i = 0; i < columns.Count;i++)
            {
                numberVerticalItemsInSeries = 1;
                for (int j = 0; j < columns[i].Count;j++)
                {
                    if (j != 0 && columns[i][j].ImageId == lastItemChecked)
                    {
                        numberVerticalItemsInSeries++;
                        if (numberVerticalItemsInSeries > 2 && (columns[i].Count == j+1 || columns[i][j+1].ImageId != lastItemChecked))
                        {
                            // We just finished finding a match
                            for (int k = j; k >= 0 && k > (j - numberVerticalItemsInSeries); k--)
                            {
                                _coordsToRemove.Add (new Coordinate (i, k));
                            }

                            return CalculateScore (numberVerticalItemsInSeries);
                        }
                    }
                    else
                    {
                        numberVerticalItemsInSeries = 1;
                    }

                    lastItemChecked = columns[i][j].ImageId;
                }
            }

            return 0;
        }

        private int CheckHorizontally(List<ObservableCollection<Tile>> columns)
        {
            var score = 0;
            var numberHorizontalItemsInSeries = 1;
            string lastItemChecked = null;

            var lastPopulatedRow = -1;
            var highestNumberTiles = columns.Max(x => x.Count);
            for (int i = 0; i < highestNumberTiles;i++)
            {
                for (int j = 0; j < columns.Count;j++)
                {
                    if (i >= columns[j].Count)
                    {
                        // Column not populated
                        score += CheckForMatch(j, i, columns, numberHorizontalItemsInSeries, lastPopulatedRow, false);
                        lastItemChecked = null;
                        numberHorizontalItemsInSeries = 1;
                    }
                    else if (j != 0 && columns[j][i].ImageId == lastItemChecked)
                    {
                        // Column part of a series
                        numberHorizontalItemsInSeries++;
                        lastPopulatedRow = i;
                    }
                    else
                    {
                        // New series starting
                        score += CheckForMatch(j, i, columns, numberHorizontalItemsInSeries, lastPopulatedRow, false);
                        lastItemChecked = columns[j][i].ImageId;
                        numberHorizontalItemsInSeries = 1;
                        lastPopulatedRow = i;
                    }
                }
            }

            // Check whether we finished on a match
            if (lastPopulatedRow >= 0)
            {
                score += CheckForMatch(columns.Count, lastPopulatedRow, columns, numberHorizontalItemsInSeries, lastPopulatedRow, false);
            }

            return score;
        }

        private int CalculateScore (int numberHorizontalItemsInSeries)
        {
            return 100 + ((numberHorizontalItemsInSeries - 3) * 150);
        }

        private int CheckForMatch(int xIndex, int yIndex, List<ObservableCollection<Tile>> columns, int numberVerticalItemsInSeries, int lastPopulatedColumn, bool isVertical)
        {
            if (numberVerticalItemsInSeries > 2) {
                // We just finished finding a match
                AddCoordsToRemove (xIndex, yIndex, columns, numberVerticalItemsInSeries, lastPopulatedColumn, isVertical);
                return CalculateScore (numberVerticalItemsInSeries);
            }

            return 0;
        }

        private void AddCoordsToRemove(int xIndex, int rowIndex, List<ObservableCollection<Tile>> columns, int numberVerticalItemsInSeries, int lastPopulatedRow, bool isVertical)
        {
            var matchFinishedOnPreviousRow = xIndex == 0;
            var rowToLookAt = matchFinishedOnPreviousRow ? lastPopulatedRow : rowIndex;
            var xIndexOfLastMatch = xIndex - 1;
            if (matchFinishedOnPreviousRow) 
            {
                // work out first column that is populated
                for (int i = columns.Count - 1; i >= 0; i--)
                {
                    if (columns [i].Count > rowToLookAt)
                    {
                        xIndexOfLastMatch = i;
                        break;
                    }
                }
            }

            for (int a = xIndexOfLastMatch; a >= 0 && a > (xIndexOfLastMatch - numberVerticalItemsInSeries); a--)
            {
                _coordsToRemove.Add (new Coordinate (a, rowToLookAt));
            }
        }

        private void DoRemove (List<ObservableCollection<Tile>> columns)
        {
            foreach (var coord in _coordsToRemove.OrderByDescending(x => x.Y))
            {
                columns[coord.X].RemoveAt(coord.Y);
            }
        }
    }
}
<commit_msg>ScoreChecker refactoring<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using SharedKernel;
using System.Collections.ObjectModel;

namespace GameplayContext
{
    public class ScoreChecker
    {
        private readonly List<Coordinate> _coordsToRemove = new List<Coordinate>();

        public List<Coordinate> RemovedItems 
        {
            get
            {
               return _coordsToRemove;
            }
        }

        public int CheckScoreAndUpdate(List<ObservableCollection<Tile>> columns)
        {
            var score = DoCheck(columns, CheckVertically);
            if (score == 0)
            {
                score = DoCheck(columns, CheckHorizontally);
            }

            return score;
        }

        private int DoCheck(List<ObservableCollection<Tile>> columns, Func<List<ObservableCollection<Tile>>, int> checker)
        {
            _coordsToRemove.Clear();
            var score = checker(columns);
            DoRemove(columns);
            return score;
        }

        private int CheckVertically(List<ObservableCollection<Tile>> columns)
        {
            var numberVerticalItemsInSeries = 1;
            string lastItemChecked = null;

            for (int i = 0; i < columns.Count;i++)
            {
                numberVerticalItemsInSeries = 1;
                for (int j = 0; j < columns[i].Count;j++)
                {
                    if (j != 0 && columns[i][j].ImageId == lastItemChecked)
                    {
                        numberVerticalItemsInSeries++;
                        if (numberVerticalItemsInSeries > 2 && (columns[i].Count == j+1 || columns[i][j+1].ImageId != lastItemChecked))
                        {
                            // We just finished finding a match
                            for (int k = j; k >= 0 && k > (j - numberVerticalItemsInSeries); k--)
                            {
                                _coordsToRemove.Add (new Coordinate (i, k));
                            }

                            return CalculateScore (numberVerticalItemsInSeries);
                        }
                    }
                    else
                    {
                        numberVerticalItemsInSeries = 1;
                    }

                    lastItemChecked = columns[i][j].ImageId;
                }
            }

            return 0;
        }

        private int CheckHorizontally(List<ObservableCollection<Tile>> columns)
        {
            var numberHorizontalItemsInSeries = 1;
            string lastItemChecked = null;
            var highestNumberTiles = columns.Max(x => x.Count);
            for (int i = 0; i < highestNumberTiles;i++)
            {
                numberHorizontalItemsInSeries = 1;
                for (int j = 0; j < columns.Count;j++)
                {
                    if (i >= columns[j].Count)
                    {
                        lastItemChecked = null;
                        continue;
                    }
                    else if (columns[j][i].ImageId == lastItemChecked)
                    {
                        numberHorizontalItemsInSeries++;
                        if (numberHorizontalItemsInSeries > 2 && ((j + 1 >= columns.Count || i >= columns[j + 1].Count) || columns[j + 1][i].ImageId != lastItemChecked))
                        {
                            // Finished finding a match
                            for (int a = j; a >= 0 && a > (j - numberHorizontalItemsInSeries); a--)
                            {
                                _coordsToRemove.Add (new Coordinate (a, i));
                            }

                            return CalculateScore (numberHorizontalItemsInSeries);
                        }
                    }

                    lastItemChecked = columns[j][i].ImageId;
                }
            }

            return 0;
        }

        private int CalculateScore (int numberHorizontalItemsInSeries)
        {
            return 100 + ((numberHorizontalItemsInSeries - 3) * 150);
        }

        private void DoRemove (List<ObservableCollection<Tile>> columns)
        {
            foreach (var coord in _coordsToRemove.OrderByDescending(x => x.Y))
            {
                columns[coord.X].RemoveAt(coord.Y);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08323075466338414, 'fraction_numerical': 0.00606003219392103, 'mean_word_length': 1.2640943193997856, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '497150', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2424, 'n_words': 572}