/// <summary> /// Eliminate will remove the value from the tile and propagate changes via calls to Assign /// if there is only a single value remaining /// NOTE: This method mutates the CPBoard cpb parameter /// </summary> /// <param name="cpb"></param> /// <param name="tile"></param> /// <param name="value"></param> /// <returns></returns> static public bool Eliminate(CPBoard cpb, int tileIndex, string value) { var values = cpb.Get(tileIndex); CDebug.Assert(values.Length > 0, "values.Length > 0"); if (values.Length == 1) { // Reached a contradiction, eliminating this value will leave the tile EMPTY! if (values == value) { return(false); } // Value has already been eliminated from this tile return(true); } var newValues = values.Replace(value, ""); cpb.Set(tileIndex, newValues); // If there is only one possible value after the change // Call assign to propagate changes if (newValues.Length == 1) { return(Assign(cpb, tileIndex, newValues)); } return(true); }
public void TestCopy() { var cpb = new CPBoard(bSolvableByCP); CPBoard copy = cpb.Copy(); // Changing original does not mutate copy Assert.NotEqual("4", copy.Get(0)); cpb.Set(0, "4"); Assert.NotEqual("4", copy.Get(0)); // Chaning copy does not change original Assert.NotEqual("2", cpb.Get(1)); copy.Set(1, "2"); Assert.NotEqual("2", cpb.Get(1)); }
/// <summary> /// Assign a single value to the tile, propagate the changes via calls to Eliminate /// NOTE: Method mutates the CPBoard cpb parameter /// </summary> /// <param name="cpb"></param> /// <param name="tileIndex"></param> /// <param name="value"></param> /// <returns></returns> static public bool Assign(CPBoard cpb, int tileIndex, string value) { CDebug.Assert(value.Length == 1, "Cannot assign multiple values"); var values = cpb.Get(tileIndex); // Cannot assign a value that is a not possible value of the tile if (!values.Contains(value)) { return(false); } // Attempt to eliminate values from peers cpb.Set(tileIndex, value); foreach (var peer in Peers[tileIndex]) { if (!Eliminate(cpb, peer, value)) { return(false); } } return(true); }