public Dictionary <Int16, Int16> GroupForOnlyRemaining(dta data) { Dictionary <Int16, Int16> rtn = new Dictionary <Int16, Int16> (); List <Int16>[] grouped = new List <Int16> [9]; for (Int16 i = 0; i < 9; i++) { grouped[i] = new List <Int16> (); } for (Int16 i = 0; i < 9; i++) { if (data.dtaset[i] > 0) { continue; } foreach (Int16 val in data.possible[i]) { grouped[i].Add(val); } } for (Int16 i = 0; i < 9; i++) { if (grouped[i].Count == 1) { rtn.Add(grouped[i][0], i); } } return(rtn); }
public dta Row(Int16 row) { dta rtn = new dta(); for (int i = 0; i < 9; i++) { rtn.dtaset[i] = data[row, i]; rtn.possible[i] = remaining[row, i]; } return(rtn); }
public dta Column(Int16 col) { dta rtn = new dta(); for (int i = 0; i < 9; i++) { rtn.dtaset[i] = data[i, col]; rtn.possible[i] = remaining[i, col]; } return(rtn); }
public dta Square(Int16 square) { dta rtn = new dta(); Int16 vtxRow = (Int16)((Int32)square % 3); Int16 vtxCol = (Int16)Math.Floor((double)square / (double)3); for (int i = 0; i < 9; i++) { rtn.dtaset[i] = data[vtxRow + (Int16)((Int32)i % 3), vtxCol + (Int16)Math.Floor((double)i / (double)3)]; rtn.possible[i] = remaining[3 * vtxRow + (Int16)((Int32)i % 3), 3 * vtxCol + (Int16)Math.Floor((double)i / (double)3)]; } return(rtn); }
private Boolean SetByOnlyPossible() { Boolean newUpdate = false; for (Int16 i = 0; i < 9; i++) { // check for the only value available on rows dta rw = Row(i); if (!rw.Solved()) { foreach (KeyValuePair <Int16, Int16> kvp in GroupForOnlyRemaining(rw)) { SetValue(i, kvp.Key, kvp.Value); newUpdate = true; } } // check for the only value avalible on columns dta cl = Column(i); if (!cl.Solved()) { foreach (KeyValuePair <Int16, Int16> kvp in GroupForOnlyRemaining(cl)) { SetValue(kvp.Key, i, kvp.Value); newUpdate = true; } } // check for the only value avalible on squares dta sq = Square(i); if (!sq.Solved()) { foreach (KeyValuePair <Int16, Int16> kvp in GroupForOnlyRemaining(sq)) { SetValue((Int16)((int)i % 3 + (int)kvp.Key % 3), (Int16)(Math.Floor((double)i / (double)3) + Math.Floor((double)kvp.Key / (double)3)), kvp.Value); newUpdate = true; } } } return(newUpdate); }