예제 #1
0
        private void OnMenuItemClick(object sender, EventArgs e)
        {
            if (sender is MenuItem item)
            {
                if (item.Parent is ContextMenu owner)
                {
                    var label = (SudokuLabel)owner.SourceControl;
                    Trace.WriteLine(label.Name);
                    label.Font = new Font("Consolas", 14);
                    if (item.Text.Contains("Exclude"))
                    {
                        label.Text = item.Text.Substring(7);
                    }
                    if (item.Text.Contains("Make"))
                    {
                        label.Text = item.Text.Substring(5);
                    }
                    SelectedNumber = label.Value = int.Parse(label.Text);

                    // check if move is valid
                    if (!SudokuPuzzle.IsMoveValid(label.Column, label.Row, label.SubGrid, label.Value))
                    {
                        MessageBox.Show(@"Invalid move.");
                        return;
                    }
                    DisplayCandidatesForAllCells();
                    if (SudokuPuzzle.IsPuzzleSolved())
                    {
                        timer1.Enabled = false;
                        Console.Beep();
                        toolStripStatusLabel1.Text = @"Great Job!";
                    }
                }
            }
        }
예제 #2
0
        public void SetCell(int col, int row, int value)
        {
            // Find the Label control
            var subgrid   = SudokuPuzzle.GetRegion(col, row);
            var labelName = $"Label{col}{row}{subgrid}";
            var control   = tableLayoutPanel1.Controls.Find(labelName, false).FirstOrDefault();
            var label     = (SudokuLabel)control;

            if (label == null)
            {
                return;
            }

            if (value > 0)
            {
                label.Value = value;
                label.Font  = new Font("Consolas", 14, FontStyle.Bold);
                SudokuPuzzle.SetAlternateRegionColors(ref label);
                label.Text = $@"{value}";
            }
            else if (value == 0)
            {
                // set the appearance for the Label control
                label.Font = new Font("Consolas", 8);
                SudokuPuzzle.SetAlternateRegionColors(ref label);
                label.MouseDown += SudokuLabel_MouseDown;
            }
        }
예제 #3
0
        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!GameStarted)
            {
                SudokuPuzzle.DisplayActivity("Game not started yet.", true);
                return;
            }

            SudokuPuzzle.SaveGameToDisk(true);
        }
예제 #4
0
        public void DrawBoard()
        {
            //if (tableLayoutPanel1.Controls.Count >= 81) throw new Exception("The board has already been drawn.");

            // used to store the location of the cell
            var location = new Point();

            // draws the cells
            for (var row = 1; row <= 9; row++)
            {
                for (var col = 1; col <= 9; col++)
                {
                    location.X = col * (CellWidth + 1) + XOffset - 8;
                    location.Y = row * (CellHeight + 1) + YOffset - 28;
                    var subgrid     = SudokuPuzzle.GetRegion(col, row);
                    var sudokuLabel = new SudokuLabel
                    {
                        Name        = $"Label{col}{row}{subgrid}",
                        Tag         = $"Label{col}{row}{subgrid}",
                        Font        = new Font("Consolas", 8, FontStyle.Bold),
                        BorderStyle = BorderStyle.Fixed3D,
                        Location    = location,
                        Width       = CellWidth,
                        Height      = CellHeight,
                        Margin      = new Padding(0),
                        Padding     = new Padding(0),
                        TextAlign   = ContentAlignment.MiddleCenter,
                        BackColor   = UserBackColor,
                        ForeColor   = UserForeColor,
                        Text        = $@"{col}{row}{subgrid}",
                        Column      = col,
                        Row         = row,
                        SubGrid     = SudokuPuzzle.GetRegion(col, row)
                    };
                    SudokuPuzzle.SetAlternateRegionColors(ref sudokuLabel);
                    sudokuLabels.Add(sudokuLabel);
                    if (tableLayoutPanel1.Controls.Count < 81)
                    {
                        tableLayoutPanel1.Controls.Add(sudokuLabel);
                    }
                }
            }
        }
예제 #5
0
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (GameStarted)
            {
                Visible = false;
                var dr = ShowDialog();
                MessageBox.Show(@"Do you want to save current game?");

                if (dr == DialogResult.OK)
                {
                    SudokuPuzzle.SaveGameToDisk(false);
                }
                else if (dr == DialogResult.Cancel)
                {
                    return;
                }
            }

            Application.Exit();
        }
예제 #6
0
        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (GameStarted)
                {
                    var dr = ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        SudokuPuzzle.SaveGameToDisk(false);
                    }
                    else if (dr == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }
            catch (Exception)
            {
                Trace.WriteLine(typeof(Exception).ToString());
            }

            // load the game from disk
            var filter = @"SS files (*.ss)|*.ss|SDO files (*.sdo)|*.sdo|All files (*.*)|*.*";
            var dialog = new OpenFileDialog
            {
                Filter           = filter,
                FilterIndex      = 1,
                InitialDirectory = InitialDirectory,
                RestoreDirectory = false
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                FileContent = File.ReadAllText(dialog.FileName).Replace("X", "0").Replace("\r", "").Replace("\n", "").Replace(".", "0").Replace("|", "").Replace("-", "");
                if (FileContent.Length != 81)
                {
                    throw new InvalidDataException("Input data was not formatted correctly.");
                }

                Text         = dialog.FileName;
                SaveFileName = dialog.FileName;
            }
            else
            {
                return;
            }


            StartNewGame();

            // initialize the board
            var counter = 0;

            foreach (var row in Enumerable.Range(1, 9))
            {
                foreach (var col in Enumerable.Range(1, 9))
                {
                    var i     = counter++;
                    var value = int.Parse(FileContent[i].ToString());
                    SetCell(col, row, value);
                }
            }

            DisplayCandidatesForAllCells();
        }