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

#if NO_UNITY
using Conditions.Guards;
#endif

namespace ReversiKit
{
	public class TurnCalculator : ITurnCalculator
	{
		public TurnCalculator(IBoardState board)
		{
			this._board = board;
		}
			
		#region ITurnCalculator
		public IEnumerable<IReversiTurn> GetValidTurnsForBoard(IBoardState board)
		{
			var turnCandidates = this._board.GetEmptyEnemyNeighbours();

			var result = new List<IReversiTurn>();
			foreach (ICellCoordinates cell in turnCandidates)
			{
				var turn = this.TurnForCell(cell);
                if (null == turn)
                {
                    continue;
                }

				result.Add(turn);
			}

            bool isNoValidTurnsLeft = (0 == result.Count);
            if (isNoValidTurnsLeft)
            {
                return null;
            }

			return result;
		}
		#endregion

        private IReversiTurn TurnForCell(ICellCoordinates turnCandidate)
		{
			var cellNeighbours = this._board.GetNeighboursForCell(turnCandidate);
			var directions = cellNeighbours.Where(n =>
			{
                return this._board.IsCellTakenByInactivePlayer(n);
			});


            var allFlippedCells = new List<ICellCoordinates>();
            foreach (ICellCoordinates singleDirection in directions)
            {
                IEnumerable<ICellCoordinates> flippedCells = 
                    this.FlippedCellsForDirectionOfTurn(singleDirection, turnCandidate);

                if (null != flippedCells)
                {
                    allFlippedCells.AddRange(flippedCells);
                }
            }


            bool isNoDirectionFlippedEnemyItems = (0 == allFlippedCells.Count);
            if (isNoDirectionFlippedEnemyItems)
            {
                return null;
            }
			
            var result = new ReversiTurnPOD();
            {
                result.Position = turnCandidate;
                result.PositionsOfFlippedItems = allFlippedCells;
            }

            return result;
		}

        private IEnumerable<ICellCoordinates> FlippedCellsForDirectionOfTurn(
            ICellCoordinates direction, 
            ICellCoordinates turnCandidate)
        {
            var result = new List<ICellCoordinates>();

            int rowIncrement    = direction.Row    - turnCandidate.Row   ;
            int columnIncrement = direction.Column - turnCandidate.Column;

            #if NO_UNITY
            Check.If(Math.Abs(rowIncrement   )).IsBetween(0, 1);
            Check.If(Math.Abs(columnIncrement)).IsBetween(0, 1);
            #endif


            CellCoordinates current = new CellCoordinates(direction.Row, direction.Column);
            bool isMyColourFound = false;
            while (current.Row    >= 0 && current.Row    < MatrixBoard.BOARD_SIZE &&
                   current.Column >= 0 && current.Column < MatrixBoard.BOARD_SIZE)
            {
                if (this._board.IsCellTakenByInactivePlayer(current))
                {
                    var currentClone = current.Clone() as CellCoordinates;
                    result.Add(currentClone);
                }
                else if (this._board.IsCellTakenByCurrentPlayer(current))
                {
                    isMyColourFound = true;
                    break;
                }
                else // empty cell
                {
                    break;
                }
            }
                
            if (!isMyColourFound)
            {
                return null;
            }

            return result;
        }

		private IBoardState _board;
	}
}

<commit_msg>[bugfix] Added missing counter increments<commit_after>﻿using System;
using System.Linq;
using System.Collections.Generic;

#if NO_UNITY
using Conditions.Guards;
#endif

namespace ReversiKit
{
	public class TurnCalculator : ITurnCalculator
	{
		public TurnCalculator(IBoardState board)
		{
			this._board = board;
		}
			
		#region ITurnCalculator
		public IEnumerable<IReversiTurn> GetValidTurnsForBoard(IBoardState board)
		{
			var turnCandidates = this._board.GetEmptyEnemyNeighbours();

			var result = new List<IReversiTurn>();
			foreach (ICellCoordinates cell in turnCandidates)
			{
				var turn = this.TurnForCell(cell);
                if (null == turn)
                {
                    continue;
                }

				result.Add(turn);
			}

            bool isNoValidTurnsLeft = (0 == result.Count);
            if (isNoValidTurnsLeft)
            {
                return null;
            }

			return result;
		}
		#endregion

        private IReversiTurn TurnForCell(ICellCoordinates turnCandidate)
		{
			var cellNeighbours = this._board.GetNeighboursForCell(turnCandidate);
			var directions = cellNeighbours.Where(n =>
			{
                return this._board.IsCellTakenByInactivePlayer(n);
			});


            var allFlippedCells = new List<ICellCoordinates>();
            foreach (ICellCoordinates singleDirection in directions)
            {
                IEnumerable<ICellCoordinates> flippedCells = 
                    this.FlippedCellsForDirectionOfTurn(singleDirection, turnCandidate);

                if (null != flippedCells)
                {
                    allFlippedCells.AddRange(flippedCells);
                }
            }


            bool isNoDirectionFlippedEnemyItems = (0 == allFlippedCells.Count);
            if (isNoDirectionFlippedEnemyItems)
            {
                return null;
            }
			
            var result = new ReversiTurnPOD();
            {
                result.Position = turnCandidate;
                result.PositionsOfFlippedItems = allFlippedCells;
            }

            return result;
		}

        private IEnumerable<ICellCoordinates> FlippedCellsForDirectionOfTurn(
            ICellCoordinates direction, 
            ICellCoordinates turnCandidate)
        {
            var result = new List<ICellCoordinates>();

            int rowIncrement    = direction.Row    - turnCandidate.Row   ;
            int columnIncrement = direction.Column - turnCandidate.Column;

            #if NO_UNITY
            Check.If(Math.Abs(rowIncrement   )).IsBetween(0, 1);
            Check.If(Math.Abs(columnIncrement)).IsBetween(0, 1);
            #endif


            CellCoordinates current = new CellCoordinates(direction.Row, direction.Column);
            bool isMyColourFound = false;
            while (current.Row    >= 0 && current.Row    < MatrixBoard.BOARD_SIZE &&
                   current.Column >= 0 && current.Column < MatrixBoard.BOARD_SIZE)
            {
                if (this._board.IsCellTakenByInactivePlayer(current))
                {
                    var currentClone = current.Clone() as CellCoordinates;
                    result.Add(currentClone);
                }
                else if (this._board.IsCellTakenByCurrentPlayer(current))
                {
                    isMyColourFound = true;
                    break;
                }
                else // empty cell
                {
                    break;
                }

                current.Row += rowIncrement;
                current.Column += columnIncrement;
            }
                
            if (!isMyColourFound)
            {
                return null;
            }

            return result;
        }

		private IBoardState _board;
	}
}


{'fraction_non_alphanumeric': 0.07566545061478179, 'fraction_numerical': 0.002161870017565194, 'mean_word_length': 1.5990168539325842, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '3737381', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1915, 'n_words': 362}