Example #1
0
        private void DisplayPuzzle(SudokuModel model)
        {
            var box_size = (int)Math.Sqrt(size_puzzle);

            for (int row = 0; row < size_puzzle; row++)
            {
                if (row % box_size == 0)
                {
                    DisplayDivision();
                    Console.WriteLine();
                }

                for (int column = 0; column < size_puzzle; column++)
                {
                    if (column % box_size == 0)
                    {
                        Console.Write("| ");
                    }

                    string value = model.puzzle[row][column].digit + " ";
                    if (model.puzzle[row][column].digit == 0)
                    {
                        value = "X ";
                    }

                    Console.Write(value);
                }

                Console.Write("| ");
                Console.WriteLine();
            }

            DisplayDivision();
            Console.WriteLine();
        }
Example #2
0
		/**********************************************************
		ANROP:      Anropas då Spela-knappen clickas.
		UPPGIFT:    Läser av radiobutton och skickar vidare vilken
					nivå som användaren har valt.
					Visar och döljer även spelplan och meny.
		***********************************************************/
		private void spela_Click(object sender, RoutedEventArgs e)
		{
            Timer();

			main = Application.Current.MainWindow as MainWindow;
            model = new SudokuModel();
            model.GetSetNewGame = true;
			main.spelplanComponent.lblAntalDrag.Content = "0";

            main.spelplanComponent.Visibility = Visibility.Visible;
			main.menuComponent.Visibility = Visibility.Collapsed;
                gbL.Visibility = Visibility.Collapsed;
                gbM.Visibility = Visibility.Collapsed;
                gbS.Visibility = Visibility.Collapsed;
                rbL.IsChecked = false;
                rbM.IsChecked = false;
                rbS.IsChecked = false;
                rbGrid.Margin = new Thickness(70, 0, 70, 160);
                btnSpela.IsEnabled = false;
           

			main.gridPrintComponent = model.PrintGrid(radioButtonChecked, main.gridPrintComponent);
			main.gridPrintComponent.Visibility = Visibility.Visible;
					
            
		}
Example #3
0
        private SudokuModel SetUpPuzzle(double difficulty)
        {
            SudokuModel model = SudokuModel.GetInstance(size_puzzle, difficulty);

            int[] available_values = GetAllValues();
            model.FormSolution(0, 0, available_values);

            return(model);
        }
Example #4
0
        /// <summary>
        ///     Generate and return the instance
        ///     of the SudokuModel that will enable
        ///     a Singleton Design Pattern.
        /// </summary>
        /// <param name="size_puzzle">
        ///     Number of rows and columns for puzzle
        /// </param>
        /// <param name="difficulty">
        ///     Amount of puzzle to be shown
        ///     0.6 : Easy, 0.45 : Medium, 0.25 : Hard
        /// </param>
        /// <returns>
        ///     Single instance of Sudoku Model that
        ///     exists.
        /// </returns>
        public static SudokuModel GetInstance(int size_puzzle, double difficulty)
        {
            if (!(model_instance is SudokuModel))
            {
                model_instance = new SudokuModel(size_puzzle, difficulty);
            }

            return(model_instance);
        }
Example #5
0
        /// <summary>
        ///     Display a 2-d matrix displaying puzzle only at places
        ///     mentioned in positions and a X at other places as well
        ///     as the solution for it.
        /// </summary>
        /// <param name="difficulty">
        ///     Fraction of puzzle to be shown
        /// </param>
        public void ShowSudoku(double difficulty)
        {
            SudokuModel model = SetUpPuzzle(difficulty);

            //Solution
            DisplayPuzzle(model);

            Console.WriteLine();

            // Puzzle
            model.CalculatePuzzleDisplay();
            DisplayPuzzle(model);
        }
Example #6
0
        static void Main(string[] args)
        {
            var sudoku = new SudokuModel();

            Console.WriteLine(sudoku.Board);
            while (true)
            {
                Console.Write("/> ");
                string[] input   = Console.ReadLine().Split(" ");
                string   command = input[0].ToUpper();

                sudoku.ProcessCommand(command, input);
            }
        }
Example #7
0
        private void StoreSolution(SudokuModel model)
        {
            this.solution = new ObservableCollection <ObservableCollection <Cell> >();

            for (int row = 0; row < size_puzzle; row++)
            {
                this.solution.Add(new ObservableCollection <Cell>());
                for (int column = 0; column < size_puzzle; column++)
                {
                    this.solution[row].Add(new Cell {
                        Digit = model.puzzle[row][column].Digit
                    });
                }
            }
        }
Example #8
0
        private void StorePuzzle(SudokuModel model)
        {
            // Modify puzzle using generated puzzle

            for (int row = 0; row < size_puzzle; row++)
            {
                this.displayedPositions[row] = new bool[size_puzzle];
                for (int column = 0; column < size_puzzle; column++)
                {
                    this.puzzle[row][column].Digit = model.puzzle[row][column].Digit;
                    this.puzzle[row][column].Color = model.puzzle[row][column].Color;

                    // Displayed in original puzzle or not
                    this.displayedPositions[row][column] = this.puzzle[row][column].digit > 0;
                }
            }
        }
Example #9
0
        private static void InitializeModel(int size_puzzle, double difficulty, SudokuModel model)
        {
            model.size_puzzle = size_puzzle;
            model.difficulty  = difficulty;

            model.puzzle    = new Cell[size_puzzle][];
            model.positions = new bool[size_puzzle][];

            // To avoid NullReferenceException
            for (int puzzle_row = 0; puzzle_row < size_puzzle; puzzle_row++)
            {
                model.puzzle[puzzle_row]    = new Cell[size_puzzle];
                model.positions[puzzle_row] = new bool[size_puzzle];

                for (int puzzle_column = 0; puzzle_column < size_puzzle; puzzle_column++)
                {
                    model.puzzle[puzzle_row][puzzle_column] = new Cell();
                }
            }
        }
Example #10
0
        public void GameWon(string nameinput, string time)
        {
            var main = Application.Current.MainWindow as MainWindow;
            SudokuModel model = new SudokuModel();

            string moves = lblAntalDrag.Content.ToString();

            // if winnersList.rows =< 10
            // else 
            // if tiden är bättre än nr 10 i winnersList
            string solution = model.getThisSolution();

            main.highscoreComponent.addHighscore(nameinput, solution, time, moves);
        }
Example #11
0
 //När "Fusk" klickas hämtas lösning i SudokuModel
 private void clickFusk(object sender, RoutedEventArgs e)
 {
     var main = Application.Current.MainWindow as MainWindow;
     SudokuModel model = new SudokuModel();
     model.fuska(main.gridPrintComponent);
 }
Example #12
0
        public void OpenFile_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            FileHandeling openSavedGame = new FileHandeling();
            model = this.menuComponent.GetSudokuModel;
            string[] savedFile = openSavedGame.OpenFile();
            //menuComponent.Visibility = Visibility.Collapsed;
            spelplanComponent.Visibility = Visibility.Visible;

            try
            {
                string[] savedGame = new string[81];
                for (int i = 0; i < 81; i++)
                {
                    savedGame[i] = savedFile[1].Substring(i, 1);
                }
                menuComponent.Visibility = Visibility.Collapsed;
                gridPrintComponent = model.PrintGrid(savedFile[2], gridPrintComponent, savedGame);
                gridPrintComponent.Visibility = Visibility.Visible;
                model.GetSetNewGame = true;
            }

            catch (Exception ex)
            {
                string error = ex.Data.ToString();
                if (model.GetSetNewGame)
                    spelplanComponent.Visibility = Visibility.Visible;
                else
                {
                    menuComponent.Visibility = Visibility.Visible;
                    spelplanComponent.Visibility = Visibility.Collapsed;
                }
            }
        }
Example #13
0
        public void SaveFile_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            string[] game2Save = new string[4];
            model = this.menuComponent.GetSudokuModel;
            //Spara hårdkodad spelplan
            string[] strGameBoard = model.GetUseThisGrid;
            
            StringBuilder strbGameBoard = new StringBuilder();
            for (int i = 0; i < 81; i++)
                strbGameBoard.Append(strGameBoard[i]);
            game2Save[0] = strbGameBoard.ToString();

            //Spara användarens inmatade siffror            
            game2Save[1] = model.GetSetGame2Save;

            //Spara vilken spelplan/svårighetsgrad användaren valt
            game2Save[2] = model.GetDifficulty;

            FileHandeling saveGame = new FileHandeling();
            saveGame.SaveFile(game2Save);
        }
        /// <summary>
        /// The main method
        /// </summary>
        /// <param name="args">
        /// no arguments required
        /// </param>
        static void Main(string[] args)
        {
            var size    = Enumerable.Range(0, 9).ToList();
            var section = size.GroupBy(s => s / 3);

            // you may manipulate the following game, which is the initial state of the Sudoku to your liking
            // note: obviously not observing the rules of Sudoku here makes the problem infeasible
            var game = new int?[, ] {
                //  0    1      2     3     4    5     6     7     8
                { null, 3, null, null, null, null, null, null, null },
                { null, null, null, 1, 9, 5, null, null, null },
                { null, null, 8, null, null, null, null, 6, null },

                { 8, null, null, null, 6, null, null, null, null },
                { 4, null, null, 8, null, null, null, null, 1 },
                { null, null, null, null, 2, null, null, null, null },

                { null, 6, null, null, null, null, 2, 8, null },
                { null, null, null, 4, 1, 9, null, null, 5 },
                { null, null, null, null, null, null, null, 7, null },
            };

            // use default settings
            var config = new Configuration();

            config.NameHandling            = NameHandlingStyle.UniqueLongNames;
            config.ComputeRemovedVariables = true;
            using (var scope = new ModelScope(config))
            {
                // create a model, based on given data and the model scope
                var sudokuModel = new SudokuModel(size, section, game);

                // get a solver instance, change your solver
                var solver = new CplexSolver();

                // solve the model
                var solution = solver.Solve(sudokuModel.Model);

                // print objective and variable decisions
                Console.WriteLine("Result: ");
                foreach (var row in size)
                {
                    foreach (var col in size)
                    {
                        foreach (var value in size)
                        {
                            if (sudokuModel.field[col, row, value].Value > 0)
                            {
                                Console.Write(string.Format("   {0}", value + 1));
                            }
                        }
                        if ((col + 1) % 3 == 0)
                        {
                            Console.Write("  ");
                        }
                    }
                    if ((row + 1) % 3 == 0)
                    {
                        Console.WriteLine();
                    }
                    Console.WriteLine();
                }
            }
        }
Example #15
0
		public Meny()
		{
			InitializeComponent();
            model = new SudokuModel();
		}
Example #16
0
		/****************************************************************
		ANROP:      Rätta();
		UPPGIFT:    Kontrollerar om alla textbox är ifyllda, skickar dem
					isåfall till Rättafunktion i Sudokumodel.
		*****************************************************************/
		public void Rätta(GridPrint gridprint) 
		{
			string[] arr = new string[81];
			for (int i = 0; i < 81; i++)  // Läser in alla textbox och lägger i en array
			{
				TextBox tb = (TextBox)nameGridPrint.Children[i];
				arr[i] = tb.Text;
			}

			SudokuModel model = new SudokuModel();
			model.Rätta(arr, gridprint);               // skicka till Rätta-funktion i Sudokumodel                      
		}