static void Main(string[] args) { Map m = new Map(); while (true) { Console.WriteLine("Input: rowNum,lineNum,value"); string inputLine = Console.ReadLine(); System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch(); if (inputLine == "win") { m = new Map(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { string a = Console.ReadLine(); m.SetSquareValue(i, j, a); DrawMap(m); } } } else if (inputLine=="gen") { m = new Map(); st.Start(); m.Init(); DrawMap(m); st.Stop(); Console.WriteLine(st.Elapsed.TotalSeconds); } else if (inputLine == "erase") { m.EraseRandomSquare(); DrawMap(m); } else { try { m = new Map(); string[] input = inputLine.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); m.SetSquareValue(int.Parse(input[0]), int.Parse(input[1]), input[2]); } catch (Exception ex) { Console.WriteLine("Invalid input!"); continue; } } if (m.MapStatus == GameEnum.MapStat.Completed) { Console.WriteLine("Win"); break; } } }
private static void DrawMap(Map m) { for (int i = 0; i < 9; i++) { Console.WriteLine(); if ((i) % 3 == 0) Console.WriteLine("---------| ---------| ---------|"); for (int j = 0; j < 9; j++) { Square s = m.SquareList.Find(obj => obj.Row == i && obj.Column == j); if (!s.IsValidate) Console.Write("!"); else Console.Write(" "); Console.Write(s.SquareValue + " "); if ((j + 1) % 3 == 0) Console.Write("| "); } } Console.WriteLine(); Console.WriteLine("---------| ---------| ---------|"); }
public List<Square> Solver(Map m) { List<Square> emptySquares = m.squareList.FindAll(obj => obj.SquareValue == "0"); emptySquares.Sort(delegate(Square x, Square y) { return y.ValidateValue.Count.CompareTo(x.ValidateValue.Count); }); List<List<string>> triedValue = new List<List<string>>(); for (int i = 0; i < this.squareList.Count; i++) { triedValue.Add(new List<string>()); } Random r = new Random(); for (int i = 0; i < emptySquares.Count; i++) { Square s = emptySquares[i]; List<string> plist = s.ValidateValue.FindAll(obj => (!triedValue[i].Exists(obj2 => (obj2 == obj)))); if (plist.Count > 0) { s.SquareValue = plist[r.Next(0, plist.Count)]; triedValue[i].Add(s.SquareValue); } else //trace back { s.SquareValue = "0"; //Once we trace a square back and fill it again with a new value, the following square's tried history should be cleared; for (int j = i + 1; j < this.squareList.Count; j++) { triedValue[j].Clear(); } i -= 2; if (i < 0) return null; } } return emptySquares; }