Ejemplo n.º 1
0
 public static void Play()
 {
     while (Unsolved.zeroes.Count() != 0)//runs until there are no empty squares left in board
     {
         Unsolved.PrintBoard();
         int index = GetIndex();   //gets a board array index for the selected cell
         Console.WriteLine("Enter a value for this cell (1-9): ");
         int value = GetOneNine(); //gets a value for the cell
         Console.WriteLine(Legallity(index, value));
     }
     Unsolved.PrintBoard();
     Console.WriteLine("Done");
     Console.ReadLine();
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.Write("Insert the csv sudoku file path: ");
            var   filePath    = Console.ReadLine();
            Board sudokuBoard = new Board(filePath);

            // Board sudokuBoard = new Board("/home/marco/RiderProjects/Sudoku/example_sudoku.txt");
            sudokuBoard.PrintBoard();
            if (sudokuBoard.CheckCompleted())
            {
                Console.WriteLine("sudoku completed");
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter a sudoku board line by line with comma seperated numbers, and a zero for an empty square: ");
            string line = Console.ReadLine();//collects user input

            int[] input = line.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
            Solved.Fill(input);
            Game.Unsolved.Fill(input);
            Solved.PrintBoard();

            while (Solved.zeroes.Count() != 0)
            {
                int initial = Solved.zeroes.Count();
                for (int i = 0; i < Solved.zeroes.Count(); i++)
                {
                    int        boardIndex = Solved.zeroes.ElementAt(i);
                    List <int> currPoss   = PossList(boardIndex, Solved);
                    if (currPoss.Count() == 1 && Solved.zeroes.Contains(boardIndex))//if there is only one possible number for that cell, enter it
                    {
                        int num = currPoss.ElementAt(0);
                        Solved.Reconstruct(boardIndex, num);
                    }
                    else
                    {
                        /*return from zero list matching row, column, and square find unique possibility, implrement, repeat for squ and col
                         * In other words, if a row needs 3 numbers, and one of those numbers only has one place it could go,
                         * regardless of whether you record it as the only possible option or not, it must be the solution and is entered
                         */
                        int   row     = boardIndex / 9;
                        int   col     = boardIndex % 9;
                        int   squ     = (3 * (row / 3)) + (col / 3);
                        int[] rowIndx = new int[9];
                        int[] colIndx = new int[9];
                        int[] squIndx = new int[9];
                        for (int a = 0; a < 9; a++)//populates the index lists for the current row and column
                        {
                            rowIndx[a] = (row * 9) + a;
                            colIndx[a] = col + (9 * a);
                        }
                        for (int c = 0; c < 3; c++)//populates the index lists for the current square
                        {
                            for (int d = 0; d < 3; d++)
                            {
                                squIndx[(c * 3) + (d)] = (((squ - squ % 3) + c) * 9) + ((squ % 3) * 3) + d;
                            }
                        }
                        List <int> zRow = new List <int>();//lists that will hold the indexes of all the empty or 'zero' values in the row column or square
                        List <int> zCol = new List <int>();
                        List <int> zSqu = new List <int>();
                        for (int iter = 0; iter < 9; iter++)//populates zero lists
                        {
                            int r1 = rowIndx[iter];
                            int c  = colIndx[iter];
                            int s  = squIndx[iter];
                            if (Solved.zeroes.Contains(r1))
                            {
                                zRow.Add(r1);
                            }
                            if (Solved.zeroes.Contains(c))
                            {
                                zCol.Add(c);
                            }
                            if (Solved.zeroes.Contains(s))
                            {
                                zSqu.Add(s);
                            }
                        }
                        IndexLogic(zRow);//applies aforementioned logic through the IndexLogic function to the applicable row, column, and square
                        IndexLogic(zCol);
                        IndexLogic(zSqu);
                    }
                }
                if (initial == Solved.zeroes.Count())
                {
                    Console.WriteLine("true"); break;
                }
            }
            Console.WriteLine("Do you wish to solve the puzzle (y/n)?: ");//sets up to pass control over to Game to run the play
            if (Console.ReadLine() == "y")
            {
                Game.Play();
            }
            else
            {
                Solved.PrintBoard();
            }


            Console.Read();
        }