• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samsmithnz / PuzzleSolver / 4175332345

pending completion
4175332345

push

github

GitHub
Merge pull request #37 from samsmithnz/MoreUnityFeatures

837 of 886 branches covered (94.47%)

Branch coverage included in aggregate %.

52 of 52 new or added lines in 2 files covered. (100.0%)

1315 of 1334 relevant lines covered (98.58%)

1134080.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.78
/src/PuzzleSolver/Board.cs
1
using PuzzleSolver.Images;
2
using PuzzleSolver.Map;
3
using SixLabors.ImageSharp.PixelFormats;
4
using System.Collections.Generic;
5
using System.Diagnostics;
6
using System.Linq;
7
using System.Numerics;
8

9
namespace PuzzleSolver
10
{
11
    public class Board
12
    {
13
        public string[,] Map { get; set; }
11✔
14

15
        //Color Palette
16
        public List<Rgb24> ColorPalette { get; set; }
4✔
17

18
        //Pieces
19
        public Vector2 UnsortedPiecesLocation { get; set; }
7✔
20
        public Queue<Piece> UnsortedPieces { get; set; }
22✔
21
        public List<Piece> SortedPieces { get; set; }
9✔
22
        public List<SortedDropZone> SortedDropZones { get; set; }
8✔
23

24
        //Characters
25
        public Robot Robot { get; set; }
37✔
26

27
        //Constructor
28
        public Board(string[,] map,
2✔
29
            Vector2 unsortedPiecesLocation,
2✔
30
            List<Rgb24> colorPalette,
2✔
31
            List<Piece> unsortedPieceList,
2✔
32
            List<SortedDropZone> sortedDropZones,
2✔
33
            Robot robot)
2✔
34
        {
2✔
35
            Map = map;
2✔
36
            UnsortedPiecesLocation = unsortedPiecesLocation;
2✔
37
            ColorPalette = colorPalette;
2✔
38
            UnsortedPieces = new Queue<Piece>();
2✔
39
            ImageColorGroups imageProcessing = new ImageColorGroups(ColorPalette);
2✔
40
            for (int i = 0; i < unsortedPieceList.Count; i++)
20✔
41
            {
8✔
42
                Piece piece = unsortedPieceList[i];
8✔
43
                piece.ImageStats = imageProcessing.ProcessStatsForImage(null, piece.Image);
8✔
44
                UnsortedPieces.Enqueue(piece);
8✔
45
            }
8✔
46
            SortedDropZones = sortedDropZones;
2✔
47
            SortedPieces = new List<Piece>();
2✔
48
            Robot = robot;
2✔
49
        }
2✔
50

51
        //Function to calculate the robot moves
52
        public Queue<RobotAction> RunRobot()
53
        {
1✔
54
            Queue<RobotAction> results = new Queue<RobotAction>();
1✔
55

56
            //Get the pickup location
57
            Vector2 PickUpLocation = UnsortedPiecesLocation;
1✔
58
            if (UnsortedPiecesLocation.Y > 0)
1✔
59
            {
1✔
60
                PickUpLocation = new Vector2(UnsortedPiecesLocation.X, UnsortedPiecesLocation.Y - 1);
1✔
61
            }
1✔
62
            Vector2 currentRobotLocation = Robot.Location;
1✔
63

64
            //Loop through the queue of unsorted pieces
65
            while (UnsortedPieces.Count > 0)
5✔
66
            {
4✔
67
                RobotAction robotAction = new RobotAction();
4✔
68

69
                // Move to unsorted pile
70
                robotAction.RobotPickupStartingLocation = currentRobotLocation;
4✔
71
                if (currentRobotLocation != PickUpLocation)
4✔
72
                {
3✔
73
                    PathFindingResult pathFindingResultForPickup = PathFinding.FindPath(Map, currentRobotLocation, PickUpLocation);
3✔
74
                    if (pathFindingResultForPickup != null && pathFindingResultForPickup.Path.Any())
3!
75
                    {
3✔
76
                        //Move robot
77
                        robotAction.PathToPickup = pathFindingResultForPickup;
3✔
78
                        currentRobotLocation = pathFindingResultForPickup.Path.Last();
3✔
79
                    }
3✔
80
                }
3✔
81
                robotAction.RobotPickupEndingLocation = currentRobotLocation;
4✔
82

83
                // Pickup an unsorted piece from the unsorted pile
84
                Robot.Piece = UnsortedPieces.Dequeue();
4✔
85
                robotAction.PieceId = Robot.Piece.Id;
4✔
86
                robotAction.PickupAction = new ObjectInteraction()
4✔
87
                {
4✔
88
                    Location = PickUpLocation
4✔
89
                };
4✔
90

91
                // Process the unsorted piece to work out where it goes
92
                Vector2? destinationLocation = null;
4✔
93
                foreach (SortedDropZone sortedDropZone in SortedDropZones)
36✔
94
                {
12✔
95
                    if (sortedDropZone.Color == Robot.Piece.ImageStats.TopColorGroupColor)
12✔
96
                    {
4✔
97
                        destinationLocation = sortedDropZone.Location;
4✔
98
                    }
4✔
99
                }
12✔
100
                //Check that nothing went wrong
101
                if (destinationLocation == null)
4!
102
                {
×
103
                    Debug.WriteLine("Destination location is null");
×
104
                }
×
105

106
                // Move the sorted piece to the correct pile
107
                robotAction.RobotDropoffStartingLocation = currentRobotLocation;
4✔
108
                if (destinationLocation != null)
4✔
109
                {
4✔
110
                    PathFindingResult pathFindingResultForDropoff = PathFinding.FindPath(Map, currentRobotLocation, (Vector2)destinationLocation);
4✔
111
                    if (pathFindingResultForDropoff != null && pathFindingResultForDropoff.Path.Any())
4!
112
                    {
4✔
113
                        //Move robot
114
                        robotAction.PathToDropoff = pathFindingResultForDropoff;
4✔
115
                        robotAction.DropoffAction = new ObjectInteraction()
4✔
116
                        {
4✔
117
                            Location = (Vector2)destinationLocation
4✔
118
                        };
4✔
119
                        currentRobotLocation = pathFindingResultForDropoff.Path.Last();
4✔
120
                    }
4✔
121
                }
4✔
122
                robotAction.RobotDropoffEndingLocation = currentRobotLocation;
4✔
123

124
                //Move the piece from the robot to the sorted pile
125
                Robot.Piece.Location = robotAction.DropoffAction.Location;
4✔
126
                SortedPieces.Add(Robot.Piece);
4✔
127
                Robot.Piece = null;
4✔
128

129
                // Add to queue
130
                results.Enqueue(robotAction);
4✔
131
            }
4✔
132

133
            //Add last action to return to the starting point
134
            RobotAction robotActionReset = new RobotAction();
1✔
135
            robotActionReset.RobotPickupStartingLocation = currentRobotLocation;
1✔
136
            if (currentRobotLocation != PickUpLocation)
1✔
137
            {
1✔
138
                PathFindingResult pathFindingResultForPickup = PathFinding.FindPath(Map, currentRobotLocation, PickUpLocation);
1✔
139
                if (pathFindingResultForPickup != null && pathFindingResultForPickup.Path.Any())
1!
140
                {
1✔
141
                    //Move robot
142
                    robotActionReset.PathToPickup = pathFindingResultForPickup;
1✔
143
                    currentRobotLocation = pathFindingResultForPickup.Path.Last();
1✔
144
                }
1✔
145
            }
1✔
146
            robotActionReset.RobotPickupEndingLocation = currentRobotLocation;
1✔
147
            results.Enqueue(robotActionReset);
1✔
148

149
            return results;
1✔
150
        }
1✔
151
    }
152
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc