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

samsmithnz / PuzzleSolver / 4224594480

pending completion
4224594480

Pull #42

github

GitHub
Merge f33b918d0 into d250e254c
Pull Request #42: Path finding increase

859 of 910 branches covered (94.4%)

Branch coverage included in aggregate %.

76 of 76 new or added lines in 3 files covered. (100.0%)

1390 of 1409 relevant lines covered (98.65%)

1281874.69 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

91
                // Process the unsorted piece to work out where it goes
92
                Vector2? destinationLocation = null;
26✔
93
                foreach (SortedDropZone sortedDropZone in SortedDropZones)
198✔
94
                {
73✔
95
                    if (sortedDropZone.Color == Robot.Piece.ImageStats.TopColorGroupColor)
73✔
96
                    {
26✔
97
                        destinationLocation = sortedDropZone.Location;
26✔
98
                        break;
26✔
99
                    }
100
                }
47✔
101

102
                //Get the best adjacent location to the destination
103
                Vector2? pathDestinationLocation = destinationLocation;
26✔
104
                if (destinationLocation != null)
26✔
105
                {
26✔
106
                    Vector2? adjacentLocation = GetAdjacentLocation((Vector2)destinationLocation, Map, SortedDropZones);
26✔
107
                    if (adjacentLocation != null)
26✔
108
                    {
26✔
109
                        pathDestinationLocation = (Vector2)adjacentLocation;
26✔
110
                    }
26✔
111
                }
26✔
112

113
                // Move the sorted piece to the correct pile
114
                robotAction.RobotDropoffStartingLocation = currentRobotLocation;
26✔
115
                if (destinationLocation != null && pathDestinationLocation != null)
26!
116
                {
26✔
117
                    //now find the path
118
                    PathFindingResult pathFindingResultForDropoff = PathFinding.FindPath(Map, currentRobotLocation, (Vector2)pathDestinationLocation);
26✔
119
                    if (pathFindingResultForDropoff != null && pathFindingResultForDropoff.Path.Count >= 0)
26!
120
                    {
26✔
121
                        //Move robot
122
                        robotAction.PathToDropoff = pathFindingResultForDropoff;
26✔
123
                        robotAction.DropoffAction = new ObjectInteraction()
26✔
124
                        {
26✔
125
                            Location = (Vector2)destinationLocation
26✔
126
                        };
26✔
127
                        //Move the piece from the robot to the sorted pile
128
                        Robot.Piece.Location = robotAction.DropoffAction.Location;
26✔
129
                        SortedPieces.Add(Robot.Piece);
26✔
130
                        foreach (SortedDropZone sortedDropZone in SortedDropZones)
198✔
131
                        {
73✔
132
                            if (sortedDropZone.Location == destinationLocation)
73!
133
                            {
26✔
134
                                sortedDropZone.Count++;
26✔
135
                                break;
26✔
136
                            }
137
                        }
47✔
138
                        Robot.Piece = null;
26✔
139
                        robotAction.DropoffPieceCount = GetPieceCount(robotAction.DropoffAction.Location);
26✔
140
                        if (pathFindingResultForDropoff.Path.Count > 0)
26✔
141
                        {
25✔
142
                            currentRobotLocation = pathFindingResultForDropoff.Path.Last();
25✔
143
                        }
25✔
144
                    }
26✔
145
                }
26✔
146
                robotAction.RobotDropoffEndingLocation = currentRobotLocation;
26✔
147

148
                // Add to queue
149
                results.Enqueue(robotAction);
26✔
150
            }
26✔
151

152
            //Add last action to return to the starting point
153
            RobotAction robotActionReset = new RobotAction();
3✔
154
            robotActionReset.RobotPickupStartingLocation = currentRobotLocation;
3✔
155
            if (currentRobotLocation != PickUpLocation)
3✔
156
            {
3✔
157
                PathFindingResult pathFindingResultForPickup = PathFinding.FindPath(Map, currentRobotLocation, PickUpLocation);
3✔
158
                if (pathFindingResultForPickup != null && pathFindingResultForPickup.Path.Any())
3!
159
                {
3✔
160
                    //Move robot
161
                    robotActionReset.PathToPickup = pathFindingResultForPickup;
3✔
162
                    currentRobotLocation = pathFindingResultForPickup.Path.Last();
3✔
163
                }
3✔
164
            }
3✔
165
            robotActionReset.RobotPickupEndingLocation = currentRobotLocation;
3✔
166
            results.Enqueue(robotActionReset);
3✔
167

168
            return results;
3✔
169
        }
3✔
170

171
        public int GetPieceCount(Vector2 dropOfflocation)
172
        {
28✔
173
            int pieceCount = 0;
28✔
174
            foreach (SortedDropZone sortedDropZone in SortedDropZones)
206✔
175
            {
75✔
176
                if (sortedDropZone.Location == dropOfflocation)
75✔
177
                {
28✔
178
                    pieceCount = sortedDropZone.Count;
28✔
179
                    break;
28✔
180
                }
181
            }
47✔
182
            return pieceCount;
28✔
183
        }
28✔
184

185
        private Vector2? GetAdjacentLocation(Vector2 destinationLocation, string[,] map, List<SortedDropZone> sortedDropZones)
186
        {
26✔
187
            Vector2? adjacentLocation = null;
26✔
188
            if (destinationLocation != null)
26✔
189
            {
26✔
190
                if (destinationLocation.X == 0)
26✔
191
                {
21✔
192
                    //it's a right location drop-off
193
                    adjacentLocation = new Vector2((int)destinationLocation.X + 1, (int)destinationLocation.Y);
21✔
194
                }
21✔
195
                else if (destinationLocation.X == map.GetUpperBound(0))
5✔
196
                {
2✔
197
                    //it's a left location drop-off
198
                    adjacentLocation = new Vector2((int)destinationLocation.X - 1, (int)destinationLocation.Y);
2✔
199
                }
2✔
200
                else if (destinationLocation.Y == 0)
3!
201
                {
3✔
202
                    //it's a top location drop-off
203
                    adjacentLocation = new Vector2((int)destinationLocation.X, (int)destinationLocation.Y + 1);
3✔
204
                }
3✔
205
                else if (destinationLocation.Y == map.GetUpperBound(1))
×
206
                {
×
207
                    //it's a bottom location drop-off
208
                    adjacentLocation = new Vector2((int)destinationLocation.X, (int)destinationLocation.Y - 11);
×
209
                }
×
210
            }
26✔
211
            return adjacentLocation;
26✔
212
        }
26✔
213

214
    }
215
}
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

© 2025 Coveralls, Inc