< Summary

Information
Class: BallSort.Engine.Logic.MoveGenerator
Assembly: BallSort.Engine
File(s): /home/runner/work/BallSort/BallSort/src/BallSort.Engine/Logic/MoveGenerator.cs
Tag: 216
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 154
Line coverage: 100%
Branch coverage
100%
Covered branches: 62
Total branches: 62
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetMoves(...)100%1616100%
GetMoves(...)100%66100%
CheckForCompletion(...)100%88100%
CheckForEmpty(...)100%66100%
CheckForMerges(...)100%1616100%
IsMergeCandidate(...)100%1010100%

File(s)

/home/runner/work/BallSort/BallSort/src/BallSort.Engine/Logic/MoveGenerator.cs

#LineLine coverage
 1using BallSort.Engine.Game;
 2using BallSort.Engine.Models;
 3
 4namespace BallSort.Engine.Logic;
 5
 6public class MoveGenerator
 7{
 8    private readonly Board _board;
 9
 10    private int _moveId;
 11
 2612    private readonly List<Move> _newMoves = [];
 13
 2614    public MoveGenerator(Board board)
 15    {
 2616        _board = board;
 2617    }
 18
 19    public List<Move> GetMoves(Move lastMove)
 20    {
 623421        var moves = new List<Move>();
 22
 19471623        for (var x = 0; x < _board.Width; x++)
 24        {
 9112425            var ball = _board.Top(x);
 26
 9112427            if (ball == Colour.Empty)
 28            {
 29                continue;
 30            }
 31
 9073432            if (_board.IsComplete(x))
 33            {
 34                continue;
 35            }
 36
 8149437            if (_board.IsPure(x) && _board.BallCount(x) == _board.Height - 1)
 38            {
 39                continue;
 40            }
 41
 7030242            GetMoves(ball, x);
 43
 17447244            foreach (var move in _newMoves)
 45            {
 1693446                if (! (move.Source == lastMove.Target && move.Target == lastMove.Source))
 47                {
 1297248                    moves.Add(move);
 49                }
 50            }
 51        }
 52
 623453        return moves;
 54    }
 55
 56    private void GetMoves(Colour ball, int source)
 57    {
 7030258        _newMoves.Clear();
 59
 7030260        CheckForCompletion(ball, source);
 61
 7030262        if (_newMoves.Count > 0)
 63        {
 791064            return;
 65        }
 66
 6239267        CheckForMerges(ball, source);
 68
 6239269        if (_newMoves.Count > 0)
 70        {
 712271            return;
 72        }
 73
 5527074        if (_board.BallCount(source) > 1)
 75        {
 5322476            CheckForEmpty(source);
 77        }
 5527078    }
 79
 80    private void CheckForCompletion(Colour ball, int source)
 81    {
 227977682        for (var x = 0; x < _board.Width; x++)
 83        {
 106958684            if (x == source)
 85            {
 86                continue;
 87            }
 88
 99928489            if (_board.Top(x) == ball && _board.Capacity(x) == 1)
 90            {
 794891                _newMoves.Add(new Move(source, x, ++_moveId));
 92            }
 93        }
 7030294    }
 95
 96    private void CheckForEmpty(int source)
 97    {
 5322498        if (_board.TopRunLength(source) == _board.BallCount(source))
 99        {
 5574100            return;
 101        }
 102
 1564044103        for (var x = 0; x < _board.Width; x++)
 104        {
 736176105            if (_board.IsEmpty(x))
 106            {
 1804107                _newMoves.Add(new Move(source, x, ++_moveId));
 108
 1804109                return;
 110            }
 111        }
 45846112    }
 113
 114    private void CheckForMerges(Colour ball, int source)
 115    {
 438220116        for (var i = _board.Height - 2; i > 0; i--)
 117        {
 5193040118            for (var x = 0; x < _board.Width; x++)
 119            {
 2446984120                if (_board.Top(x) != ball)
 121                {
 122                    continue;
 123                }
 124
 216548125                if (! IsMergeCandidate(x, source))
 126                {
 127                    continue;
 128                }
 129
 18030130                if (_board.TopRunLength(x) == i)
 131                {
 7212132                    if (_board.IsPure(source) && _board.IsPure(x) && _board.BallCount(source) > _board.BallCount(x))
 133                    {
 134                        continue;
 135                    }
 136
 7182137                    _newMoves.Add(new Move(source, x, ++_moveId));
 138
 7182139                    break;
 140                }
 141            }
 142        }
 62392143    }
 144
 145    private bool IsMergeCandidate(int x, int source)
 146    {
 216548147        if (x == source || _board.IsEmpty(x) || _board.IsFull(x) || _board.Capacity(x) == 1 || _board.IsComplete(x))
 148        {
 198518149            return false;
 150        }
 151
 18030152        return true;
 153    }
 154}