Example #1
0
        private void решитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            int[,] field   = new int[9, 9];
            int index = -1;

            foreach (Button btn in buttons)
            {
                ++index;
                field[index / 9, index % 9] = (btn.Text == "") ? 0 : Int32.Parse(btn.Text);
                if (gameMode == GameMode.FROM_EXISTING && btn.ForeColor == Color.Red)
                {
                    field[index / 9, index % 9] = 0;
                }
            }
            SudokuLogic sudoku = new SudokuLogic(field);
            bool        err;

            int[,] ans = sudoku.SolveSudoku(out err);
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (field[i, j] == 0)
                    {
                        buttons[i * 9 + j].Text      = ans[i, j].ToString();
                        buttons[i * 9 + j].ForeColor = Color.Red;
                    }
                    else
                    {
                        buttons[i * 9 + j].ForeColor = Color.Blue;
                    }
                    buttons[i * 9 + j].ContextMenuStrip = null;
                }
            }
            if (err)
            {
                MessageBox.Show(Constants.NO_SOLUTION, Constants.MAIN_WINDOW_TITLE, MessageBoxButtons.OK);
            }
            новыйToolStripMenuItem.Visible  = true;
            решитьToolStripMenuItem.Visible = false;
            gameMode = GameMode.NO_GAME;
            toolStripStatusLabel1.Text = Constants.INVITATION;
            toolStripStatusLabel2.Text = "";
        }
Example #2
0
        private void existingGame()
        {
            if (currentGameState[1].ToString().Equals(Constants.LEVEL_EASY))
            {
                level = 0;
            }
            if (currentGameState[1].ToString().Equals(Constants.LEVEL_MEDIUM))
            {
                level = 1;
            }
            if (currentGameState[1].ToString().Equals(Constants.LEVEL_HARD))
            {
                level = 2;
            }
            toolStripStatusLabel1.Text      = toolStripStatusLabel2.Text = "";
            timer1.Enabled                  = false;
            новыйToolStripMenuItem.Visible  = false;
            решитьToolStripMenuItem.Visible = true;
            решитьToolStripMenuItem.Text    = Constants.PLAYER_GIVES_UP;
onemoretime:
            int[,] field = new int[9, 9];
            Random rand = new Random(unchecked ((int)(DateTime.Now.Ticks)));

            field[0, 0] = 1 + rand.Next() % 9;
            field[1, 3] = 1 + rand.Next() % 9;
            field[2, 6] = 1 + rand.Next() % 9;
            field[3, 1] = 1 + rand.Next() % 9;
            field[4, 4] = 1 + rand.Next() % 9;
            field[5, 7] = 1 + rand.Next() % 9;
            field[6, 2] = 1 + rand.Next() % 9;
            field[7, 5] = 1 + rand.Next() % 9;
            field[8, 8] = 1 + rand.Next() % 9;
            SudokuLogic logic = new SudokuLogic(field);
            bool        err;

            int[,] res = logic.SolveSudoku(out err);
            if (err)
            {
                goto onemoretime;
            }
            int[] perm = Tools.getPermutationByNumber(1 + rand.Next() % 362880);
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    res[i, j] = perm[res[i, j] - 1];
                }
            }
            int curPos = 0, block = 1, space = 1;

            if (level == 0)
            {
                block = 3;
                space = 3;
            }
            if (level == 1)
            {
                block = 6;
                space = 3;
            }
            if (level == 2)
            {
                block = 7;
                space = 2;
            }
            while (curPos < 81)
            {
                int x = 1 + rand.Next() % block;
                int y = 1 + rand.Next() % space;
                for (int i = curPos; i <= curPos + x - 1; i++)
                {
                    if (i < 81)
                    {
                        res[i / 9, i % 9] = 0;
                    }
                }
                curPos += (x + y);
            }
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (res[i, j] > 0)
                    {
                        buttons[i * 9 + j].Text             = res[i, j].ToString();
                        buttons[i * 9 + j].ForeColor        = Color.Blue;
                        buttons[i * 9 + j].ContextMenuStrip = null;
                    }
                    else
                    {
                        buttons[i * 9 + j].Text             = "";
                        buttons[i * 9 + j].ForeColor        = Color.Red;
                        buttons[i * 9 + j].ContextMenuStrip = contextMenuStrip1;
                    }
                }
            }
            foreach (Button btn in buttons)
            {
                if (btn.ForeColor == Color.Red)
                {
                    btn.Focus();
                    break;
                }
            }
            gameMode       = GameMode.FROM_EXISTING;
            timer1.Enabled = true;
            seconds        = 0;
        }