Skip to content

Commit 2c26566

Browse files
author
Frederic Vogels
committed
Merge branch 'student' into solution
2 parents 0860e07 + 427c0d4 commit 2c26566

File tree

7 files changed

+915
-22
lines changed

7 files changed

+915
-22
lines changed

PiCross/DataStructures/Grid.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ public static ISequence<T> Linearize<T>( this IGrid<T> grid )
191191

192192
public static class Grid
193193
{
194+
/// <summary>
195+
/// Creates a new grid with the given size where each element is determined by <paramref name="initializer"/>.
196+
/// </summary>
197+
/// <typeparam name="T">Type of the elements of the grid.</typeparam>
198+
/// <param name="size">Size of the grid.</param>
199+
/// <param name="initializer">Function that determines what value the grid should take at each position.</param>
200+
/// <returns>A new grid.</returns>
194201
public static IGrid<T> Create<T>( Size size, Func<Vector2D, T> initializer )
195202
{
196203
return new Grid<T>( size, initializer );

PiCross/DataStructures/Sequence.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using Cells;
@@ -11,7 +12,7 @@ namespace DataStructures
1112
/// populate the sequence with objects with <see cref="IVar"/>.
1213
/// </summary>
1314
/// <typeparam name="T">Element type.</typeparam>
14-
public interface ISequence<out T>
15+
public interface ISequence<out T> : IEnumerable<T>
1516
{
1617
/// <summary>
1718
/// Number of items in the sequence.
@@ -510,6 +511,16 @@ public IEnumerable<int> Indices
510511
}
511512
}
512513

514+
public IEnumerator<T> GetEnumerator()
515+
{
516+
return this.Items.GetEnumerator();
517+
}
518+
519+
IEnumerator IEnumerable.GetEnumerator()
520+
{
521+
return GetEnumerator();
522+
}
523+
513524
public IEnumerable<T> Items => Indices.Select( i => this[i] );
514525

515526
public bool IsValidIndex( int index )

PiCross/Domain/PiCross/Puzzle.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace PiCross
99
{
10-
public class AmbiguousConstraintsException : PiCrossException
10+
public class InvalidConstraintsException : PiCrossException
1111
{
12-
public AmbiguousConstraintsException()
13-
: base( "Ambiguous constraints" )
12+
public InvalidConstraintsException()
13+
: base( "Invalid constraints" )
1414
{
1515
// NOP
1616
}
@@ -23,13 +23,15 @@ public AmbiguousConstraintsException()
2323
public sealed class Puzzle
2424
{
2525
/// <summary>
26-
/// Creates a Puzzle from the constraints. Since a Puzzle
27-
/// contains the solution, this method solves the given puzzle.
26+
/// Creates a Puzzle from the constraints.
27+
/// Internally, the puzzle is automatically solved.
28+
/// If the constraints are ambiguous or contradictory,
29+
/// an exception is thrown.
2830
/// </summary>
2931
/// <param name="columnConstraints">Column constraints.</param>
3032
/// <param name="rowConstraints">Row constraints.</param>
3133
/// <returns>A Puzzle with the given constraints.</returns>
32-
/// <exception cref="AmbiguousConstraintsException">Thrown when the constraints
34+
/// <exception cref="InvalidConstraintsException">Thrown when the constraints
3335
/// don't lead to a single solution.</exception>
3436
public static Puzzle FromConstraints( ISequence<Constraints> columnConstraints, ISequence<Constraints> rowConstraints )
3537
{
@@ -38,7 +40,7 @@ public static Puzzle FromConstraints( ISequence<Constraints> columnConstraints,
3840

3941
if ( !solverGrid.IsSolved )
4042
{
41-
throw new ArgumentException( "Ambiguous constraints" );
43+
throw new InvalidConstraintsException();
4244
}
4345
else
4446
{
@@ -48,6 +50,25 @@ public static Puzzle FromConstraints( ISequence<Constraints> columnConstraints,
4850
}
4951
}
5052

53+
/// <summary>
54+
/// Creates a Puzzle from the constraints.
55+
/// Internally, the puzzle is automatically solved.
56+
/// If the constraints are ambiguous or contradictory,
57+
/// an exception is thrown.
58+
/// </summary>
59+
/// <param name="columnConstraints">Column constraints.</param>
60+
/// <param name="rowConstraints">Row constraints.</param>
61+
/// <returns>A Puzzle with the given constraints.</returns>
62+
/// <exception cref="InvalidConstraintsException">Thrown when the constraints
63+
/// don't lead to a single solution.</exception>
64+
public static Puzzle FromConstraints( int[][] columnConstraints, int[][] rowConstraints )
65+
{
66+
var columnConstraintsAsSequence = Sequence.FromItems( columnConstraints.Select( Constraints.FromValues ).ToArray() );
67+
var rowConstraintsAsSequence = Sequence.FromItems( rowConstraints.Select( Constraints.FromValues ).ToArray() );
68+
69+
return FromConstraints( columnConstraints: columnConstraintsAsSequence, rowConstraints: rowConstraintsAsSequence );
70+
}
71+
5172
/// <summary>
5273
/// Creates a Puzzle from a solution. The constraints
5374
/// will be inferred.

PiCross/Domain/PiCross/Square.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,15 @@ private Square()
117117

118118
/// <summary>
119119
/// Symbol for this square.
120+
/// Do NOT make use of this property to determine
121+
/// what kind of Square it is. Instead, use equality, e.g.,
122+
/// square == Square.UNKNOWN.
120123
/// </summary>
121124
public abstract char Symbol { get; }
122125

123126
public override bool Equals( object obj )
124127
{
125-
return this == obj;
128+
return object.ReferenceEquals( this, obj );
126129
}
127130

128131
public override int GetHashCode()

0 commit comments

Comments
 (0)