Beispiel #1
0
        public static void Main()
        {
            SplashKit.LoadFont("Roboto Regular", "fonts/Roboto-Regular.ttf");
            SplashKit.LoadFont("Roboto Bold", "fonts/Roboto-Bold.ttf");
            Puzzles.LoadPuzzles("puzzles.txt");

            Window window = new Window("Sudoku", 800, 600);
            Sudoku sudoku = new Sudoku(Puzzles.GetRandomPuzzle(), SplashKit.ScreenCenter());

            do
            {
                SplashKit.ProcessEvents();
                SplashKit.ClearScreen();

                sudoku.Update();
                sudoku.Draw(window);

                SplashKit.RefreshScreen();
            } while (!SplashKit.WindowCloseRequested("Sudoku"));
        }
Beispiel #2
0
        /// <summary>
        /// Updates the sudoku board
        /// </summary>
        public void Update()
        {
            // Select cell using mouse
            if (SplashKit.MouseClicked(MouseButton.LeftButton) && !IsSolved())
            {
                for (int x = 0; x < 9; x++)
                {
                    for (int y = 0; y < 9; y++)
                    {
                        Cell cell = GetCell(x, y);
                        if (CellContainsPoint(cell, SplashKit.MousePosition()))
                        {
                            SelectCell(x, y);
                        }
                    }
                }
            }

            // Reveal clue
            if (SplashKit.KeyTyped(KeyCode.CKey))
            {
                RevealClue();
            }

            // Reset puzzle
            if (SplashKit.KeyTyped(KeyCode.RKey))
            {
                Reset();
            }

            // Solve puzzle
            if (SplashKit.KeyTyped(KeyCode.SKey))
            {
                Solve();
            }

            // Random puzzle
            if (SplashKit.KeyTyped(KeyCode.NKey))
            {
                SetPuzzle(Puzzles.GetRandomPuzzle());
            }

            if (IsSelected())
            {
                if (!IsDefaultCell(_selected))
                {
                    // Enter value using number keys
                    for (int i = 0; i < 9; i++)
                    {
                        if (SplashKit.KeyTyped(KeyCode.Num1Key + i) && !IsSolved())
                        {
                            _selected.Value = i + 1;
                        }
                    }

                    // Remove value
                    if (SplashKit.KeyTyped(KeyCode.BackspaceKey) && !IsSolved())
                    {
                        _selected.Value = 0;
                    }
                }

                // Move selected cell left
                if (SplashKit.KeyTyped(KeyCode.LeftKey))
                {
                    int x = (9 + _selected.X - 1) % 9;
                    SelectCell(x, _selected.Y);
                }

                // Move selected cell right
                if (SplashKit.KeyTyped(KeyCode.RightKey))
                {
                    int x = (_selected.X + 1) % 9;
                    SelectCell(x, _selected.Y);
                }

                // Move selected cell up
                if (SplashKit.KeyTyped(KeyCode.UpKey))
                {
                    int y = (9 + _selected.Y - 1) % 9;
                    SelectCell(_selected.X, y);
                }

                // Move selected cell down
                if (SplashKit.KeyTyped(KeyCode.DownKey))
                {
                    int y = (_selected.Y + 1) % 9;
                    SelectCell(_selected.X, y);
                }
            }

            if (IsSolved() && !_solved)
            {
                Console.WriteLine("Sudoku solved! Press N for a new puzzle.");
                _solved = true;
                DeselectCell();
            }
        }