示例#1
0
        public void StartMainLoop()
        {
            while (gameIsRunning)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                    switch (pressedKey.Key)
                    {
                    case ConsoleKey.LeftArrow:
                    case ConsoleKey.RightArrow:
                    case ConsoleKey.UpArrow:
                    case ConsoleKey.DownArrow:
                        MoveCursor(pressedKey.Key);
                        break;


                    case ConsoleKey.Enter:
                        if (currentMenuItem != MenuItem.None)
                        {
                            OnMenuItemSelected(currentMenuItem);
                        }
                        break;


                    case ConsoleKey.NumPad1:
                    case ConsoleKey.NumPad2:
                    case ConsoleKey.NumPad3:
                    case ConsoleKey.NumPad4:
                    case ConsoleKey.NumPad5:
                    case ConsoleKey.NumPad6:
                    case ConsoleKey.NumPad7:
                    case ConsoleKey.NumPad8:
                    case ConsoleKey.NumPad9:
                        if (CurrentCellRowIndex != -1)
                        {
                            cells[CurrentCellRowIndex, CurrentCellColIndex].isCorrect = true;
                            var value = pressedKey.KeyChar - '0';
                            grid.WriteNumber(CurrentCellRowIndex, CurrentCellColIndex, (pressedKey.KeyChar - '0'));


                            for (var i = 0; i < 9; i++)
                            {
                                if (i != CurrentCellRowIndex)
                                {
                                    if (value == cells[i, CurrentCellColIndex].value)
                                    {
                                        cells[CurrentCellRowIndex, CurrentCellColIndex].isCorrect = false;
                                        break;
                                    }
                                }
                                if (i != CurrentCellColIndex)
                                {
                                    if (value == cells[CurrentCellRowIndex, i].value)
                                    {
                                        cells[CurrentCellRowIndex, CurrentCellColIndex].isCorrect = false;
                                        break;
                                    }
                                }
                            }
                            if (cells[CurrentCellRowIndex, CurrentCellColIndex].isCorrect)
                            {
                                List <int> subBox     = new List <int>();
                                int        correntRow = CurrentCell.RowPosition;
                                int        correntCol = CurrentCell.ColumnPosition;
                                if ((correntRow + 1) % 3 != 0)
                                {
                                    correntRow--;
                                }
                                else if ((correntCol + 1) % 3 != 0)
                                {
                                    correntCol--;
                                }
                                else
                                {
                                    bool moveOn = cells[CurrentCellRowIndex, CurrentCellColIndex].isCorrect;
                                    for (int row = 0; row < 3 && moveOn; row++)
                                    {
                                        for (int col = 0; col < 3 && moveOn; col++)
                                        {
                                            //if ((row + 1) % 3 == 0 && (col + 1) % 3 == 0)
                                            //{
                                            if (row != CurrentCellRowIndex && col != CurrentCellColIndex)
                                            {
                                                subBox.Add(cells[row, col].value);
                                                subBox.Add(cells[row, col - 1].value);
                                                subBox.Add(cells[row, col - 2].value);

                                                subBox.Add(cells[row - 1, col].value);
                                                subBox.Add(cells[row - 1, col - 1].value);
                                                subBox.Add(cells[row - 1, col - 2].value);

                                                subBox.Add(cells[row - 2, col].value);
                                                subBox.Add(cells[row - 2, col - 1].value);
                                            }
                                            if (!IsBoxCorrect(subBox))
                                            {
                                                cells[CurrentCellRowIndex, CurrentCellColIndex].isCorrect =
                                                    !subBox.Contains(value);
                                                moveOn = false;

                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            RefreshCellValues();

                            //test
                            if (grid.EmptyCells == 0)
                            {
                                Console.ReadKey(true);
                            }
                        }
                        break;

                    case ConsoleKey.Backspace:
                        if (CurrentCellRowIndex != -1)
                        {
                            grid.WriteNumber(CurrentCellRowIndex, CurrentCellColIndex, SudokuGrid.EmptyCellValue);
                            RefreshCellValues();
                        }
                        break;
                    }



                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                }
            }
        }
示例#2
0
        //Starts the main game loop.
        public void StartMainLoop()
        {
            while (gameIsRunning)
            {
                //Check if any keys where pressed
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                    switch (pressedKey.Key)
                    {
                    //Procedure for moving the cursor
                    case ConsoleKey.LeftArrow:
                    case ConsoleKey.RightArrow:
                    case ConsoleKey.UpArrow:
                    case ConsoleKey.DownArrow:
                        MoveCursor(pressedKey.Key);
                        break;

                    //Procedure for selecting an item from the menu
                    case ConsoleKey.Enter:
                        if (currentMenuItem != MenuItem.None)
                        {
                            OnMenuItemSelected(currentMenuItem);
                        }
                        break;

                    //Prcedure for entering or changing a number in the sudoku grid
                    case ConsoleKey.D1:
                    case ConsoleKey.D2:
                    case ConsoleKey.D3:
                    case ConsoleKey.D4:
                    case ConsoleKey.D5:
                    case ConsoleKey.D6:
                    case ConsoleKey.D7:
                    case ConsoleKey.D8:
                    case ConsoleKey.D9:
                        if (CurrentCellRowIndex != -1)
                        {
                            grid.WriteNumber(CurrentCellRowIndex, CurrentCellColIndex, (pressedKey.KeyChar - '0'));
                            RefreshCellValues();

                            //test
                            if (grid.EmptyCells == 0)
                            {
                                Close();
                            }
                        }
                        break;

                    //Procedure for deleting a number
                    case ConsoleKey.Backspace:
                        if (CurrentCellRowIndex != -1)
                        {
                            grid.WriteNumber(CurrentCellRowIndex, CurrentCellColIndex, SudokuGrid.EmptyCellValue);
                            RefreshCellValues();
                        }
                        break;
                    }



                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(true);
                    }
                }
            }
        }