< Summary

Information
Class: BallSort.Engine.Logic.MoveGenerator
Assembly: BallSort.Engine
File(s): /home/runner/work/BallSort/BallSort/src/BallSort.Engine/Logic/MoveGenerator.cs
Tag: 238
Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 165
Line coverage: 100%
Branch coverage
100%
Covered branches: 68
Total branches: 68
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%1010100%
CheckForEmpty(...)100%1010100%
CheckForMerges(...)100%1818100%
IsMergeCandidate(...)100%88100%

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
 2610    private readonly List<Move> _newMoves = [];
 11
 12    private Move _lastMove;
 13
 14    private int _moveId;
 15
 2616    public MoveGenerator(Board board)
 17    {
 2618        _board = board;
 2619    }
 20
 21    public List<Move> GetMoves(Move lastMove)
 22    {
 623223        _lastMove = lastMove;
 24
 623225        var moves = new List<Move>();
 26
 19466027        for (var x = 0; x < _board.Width; x++)
 28        {
 9109829            var ball = _board.Top(x);
 30
 9109831            if (ball == Colour.Empty)
 32            {
 33                continue;
 34            }
 35
 9070836            if (_board.IsComplete(x))
 37            {
 38                continue;
 39            }
 40
 8147041            if (_board.IsPure(x) && _board.BallCount(x) == _board.Height - 1)
 42            {
 43                continue;
 44            }
 45
 7028246            GetMoves(ball, x);
 47
 17422448            foreach (var move in _newMoves)
 49            {
 1683050                if (! (move.Source == _lastMove.Target && move.Target == _lastMove.Source))
 51                {
 1287252                    moves.Add(move);
 53                }
 54            }
 55        }
 56
 623257        return moves;
 58    }
 59
 60    private void GetMoves(Colour ball, int source)
 61    {
 7028262        _newMoves.Clear();
 63
 7028264        CheckForCompletion(ball, source);
 65
 7028266        if (_newMoves.Count > 0)
 67        {
 63468            return;
 69        }
 70
 6964871        CheckForMerges(ball, source);
 72
 6964873        if (_newMoves.Count > 0)
 74        {
 1439275            return;
 76        }
 77
 5525678        if (_board.BallCount(source) > 1)
 79        {
 5321080            CheckForEmpty(source);
 81        }
 5525682    }
 83
 84    private void CheckForCompletion(Colour ball, int source)
 85    {
 226908486        for (var x = 0; x < _board.Width; x++)
 87        {
 106489488            if (x == source)
 89            {
 90                continue;
 91            }
 92
 99491893            if (_board.Top(x) == ball && _board.Capacity(x) == 1 && _board.IsPure(x))
 94            {
 63495                _newMoves.Add(new Move(source, x, ++_moveId));
 96
 63497                return;
 98            }
 99        }
 69648100    }
 101
 102    private void CheckForEmpty(int source)
 103    {
 53210104        if (_board.TopRunLength(source) == _board.BallCount(source))
 105        {
 5572106            return;
 107        }
 108
 1563708109        for (var x = 0; x < _board.Width; x++)
 110        {
 736020111            if (x == _lastMove.Target || _board.Capacity(x) == 0)
 112            {
 113                continue;
 114            }
 115
 245956116            if (_board.IsEmpty(x))
 117            {
 1804118                _newMoves.Add(new Move(source, x, ++_moveId));
 119
 1804120                return;
 121            }
 122        }
 45834123    }
 124
 125    private void CheckForMerges(Colour ball, int source)
 126    {
 441492127        for (var i = _board.Height - 2; i > 0; i--)
 128        {
 5331976129            for (var x = 0; x < _board.Width; x++)
 130            {
 2514890131                if (_board.Top(x) != ball || _board.Capacity(x) == 0)
 132                {
 133                    continue;
 134                }
 135
 73780136                if (! IsMergeCandidate(x, source))
 137                {
 138                    continue;
 139                }
 140
 26954141                if (_board.TopRunLength(x) == i)
 142                {
 14422143                    if (_board.IsPure(source) && _board.IsPure(x) && _board.BallCount(source) > _board.BallCount(x))
 144                    {
 145                        continue;
 146                    }
 147
 14392148                    _newMoves.Add(new Move(source, x, ++_moveId));
 149
 14392150                    return;
 151                }
 152            }
 153        }
 55256154    }
 155
 156    private bool IsMergeCandidate(int x, int source)
 157    {
 73780158        if (x == source || _board.IsEmpty(x) || _board.IsFull(x) || _board.IsComplete(x))
 159        {
 46826160            return false;
 161        }
 162
 26954163        return true;
 164    }
 165}