stephhhcodes / LewisProjectSudoku

0 stars 1 forks source link

Steps you want to take #1

Open georgepwall1991 opened 1 year ago

georgepwall1991 commented 1 year ago

So in your form, you want to start out by just creating your top level form

using System; using System.Windows.Forms;

namespace SimpleSudoku { public partial class SudokuForm : Form { public SudokuForm() { InitializeComponent(); } } }

something like this, the next step you'll want to actually use a loop to create your sudoku grid will be [9,9]

so you'd want to add another method the form will now have the InitializeTextBoxes Method

using System; using System.Windows.Forms;

namespace SimpleSudoku { public partial class SudokuForm : Form { private TextBox[,] textBoxes = new TextBox[9, 9];

    public SudokuForm()
    {
        InitializeComponent();
        InitializeTextBoxes();
    }

    private void InitializeTextBoxes()
    {
        for (int row = 0; row < 9; row++)
        {
            for (int col = 0; col < 9; col++)
            {
                textBoxes[row, col] = new TextBox();
                textBoxes[row, col].Width = 30;
                textBoxes[row, col].Height = 30;
                textBoxes[row, col].Left = 30 * col;
                textBoxes[row, col].Top = 30 * row;
                textBoxes[row, col].MaxLength = 1;
                this.Controls.Add(textBoxes[row, col]);
            }
        }
    }
}

}

you'd then want to actually check the solution being valid - this designer should generate the form for you, the form should look something like this

using System; using System.Windows.Forms;

namespace SimpleSudoku { public partial class SudokuForm : Form { private TextBox[,] textBoxes = new TextBox[9, 9];

    public SudokuForm()
    {
        InitializeComponent();
        InitializeTextBoxes();
        InitializeCheckSolutionButton();
    }

    private void InitializeTextBoxes()
    {
        // Same as before
    }

    private void InitializeCheckSolutionButton()
    {
        Button checkSolutionButton = new Button();
        checkSolutionButton.Text = "Check Solution";
        checkSolutionButton.Width = 100;
        checkSolutionButton.Height = 50;
        checkSolutionButton.Left = 300;
        checkSolutionButton.Top = 250;
        checkSolutionButton.Click += new EventHandler(CheckSolutionButton_Click);
        this.Controls.Add(checkSolutionButton);
    }

    private void CheckSolutionButton_Click(object sender, EventArgs e)
    {
        int[,] board = new int[9, 9];
        for (int row = 0; row < 9; row++)
        {
            for (int col = 0; col < 9; col++)
            {
                if (textBoxes[row, col].Text == "")
                {
                    board[row, col] = 0;
                }
                else
                {
                    board[row, col] = int.Parse(textBoxes[row, col].Text);
                }
            }
        }

        if (IsSudokuSolutionValid(board))
      {
        MessageBox.Show("Correct solution!");
      }
     else
    { 
       MessageBox.Show("Incorrect solution.");
   }

} private bool IsSudokuSolutionValid(int[,] board) { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { int num = board[row, col]; if (num < 1 || num > 9) { return false; } for (int i = 0; i < 9; i++) { if (i != row && board[i, col] == num) { return false; } if (i != col && board[row, i] == num) { return false; } } int rowStart = (row / 3) 3; int colStart = (col / 3) 3; for (int i = rowStart; i < rowStart + 3; i++) { for (int j = colStart; j < colStart + 3; j++) { if (i != row && j != col && board[i, j] == num) { return false; } } } } } return true; } }

georgepwall1991 commented 1 year ago

using System; using System.Windows.Forms;

namespace SimpleSudoku { public class SudokuForm : Form { private readonly TextBox[,] textBoxes = new TextBox[9, 9];

    public SudokuForm()
    {
        InitializeComponent();
        InitializeTextBoxes();
        InitializeCheckSolutionButton();
    }

    private void InitializeTextBoxes()
    {
        // Same as before
    }

    private void InitializeCheckSolutionButton()
    {
        var checkSolutionButton = new Button();
        checkSolutionButton.Text = "Check Solution";
        checkSolutionButton.Width = 100;
        checkSolutionButton.Height = 50;
        checkSolutionButton.Left = 300;
        checkSolutionButton.Top = 250;
        checkSolutionButton.Click += CheckSolutionButton_Click;
        Controls.Add(checkSolutionButton);
    }

    private void CheckSolutionButton_Click(object sender, EventArgs e)
    {
        var board = new int[9, 9];
        for (var row = 0; row < 9; row++)
        for (var col = 0; col < 9; col++)
            if (textBoxes[row, col].Text == "")
                board[row, col] = 0;
            else
                board[row, col] = int.Parse(textBoxes[row, col].Text);

        if (IsSudokuSolutionValid(board))
            MessageBox.Show("Correct solution!");
        else
            MessageBox.Show("Incorrect solution.");
    }

    private bool IsSudokuSolutionValid(int[,] board)
    {
        for (var row = 0; row < 9; row++)
        for (var col = 0; col < 9; col++)
        {
            var num = board[row, col];
            if (num < 1 || num > 9) return false;

            for (var i = 0; i < 9; i++)
            {
                if (i != row && board[i, col] == num) return false;

                if (i != col && board[row, i] == num) return false;
            }

            var rowStart = row / 3 * 3;
            var colStart = col / 3 * 3;
            for (var i = rowStart; i < rowStart + 3; i++)
            for (var j = colStart; j < colStart + 3; j++)
                if (i != row && j != col && board[i, j] == num)
                    return false;
        }

        return true;
    }
}

}

georgepwall1991 commented 1 year ago

the above is how your final code shouldusing System; using System.Windows.Forms;

namespace SimpleSudoku { public class SudokuForm : Form { private readonly TextBox[,] textBoxes = new TextBox[9, 9];

    public SudokuForm()
    {
        InitializeComponent();
        InitializeTextBoxes();
        InitializeCheckSolutionButton();
    }

    private void InitializeTextBoxes()
    {
        for (int row = 0; row < 9; row++)
        {
            for (int col = 0; col < 9; col++)
            {
                textBoxes[row, col] = new TextBox();
                textBoxes[row, col].Width = 30;
                textBoxes[row, col].Height = 30;
                textBoxes[row, col].Left = 30 * col;
                textBoxes[row, col].Top = 30 * row;
                textBoxes[row, col].MaxLength = 1;
                this.Controls.Add(textBoxes[row, col]);
            }
        }
    }

    private void InitializeCheckSolutionButton()
    {
        var checkSolutionButton = new Button();
        checkSolutionButton.Text = "Check Solution";
        checkSolutionButton.Width = 100;
        checkSolutionButton.Height = 50;
        checkSolutionButton.Left = 300;
        checkSolutionButton.Top = 250;
        checkSolutionButton.Click += CheckSolutionButton_Click;
        Controls.Add(checkSolutionButton);
    }

    private void CheckSolutionButton_Click(object sender, EventArgs e)
    {
        var board = new int[9, 9];
        for (var row = 0; row < 9; row++)
        for (var col = 0; col < 9; col++)
            if (textBoxes[row, col].Text == "")
                board[row, col] = 0;
            else
                board[row, col] = int.Parse(textBoxes[row, col].Text);

        if (IsSudokuSolutionValid(board))
            MessageBox.Show("Correct solution!");
        else
            MessageBox.Show("Incorrect solution.");
    }

    private bool IsSudokuSolutionValid(int[,] board)
    {
        for (var row = 0; row < 9; row++)
        for (var col = 0; col < 9; col++)
        {
            var num = board[row, col];
            if (num < 1 || num > 9) return false;

            for (var i = 0; i < 9; i++)
            {
                if (i != row && board[i, col] == num) return false;

                if (i != col && board[row, i] == num) return false;
            }

            var rowStart = row / 3 * 3;
            var colStart = col / 3 * 3;
            for (var i = rowStart; i < rowStart + 3; i++)
            for (var j = colStart; j < colStart + 3; j++)
                if (i != row && j != col && board[i, j] == num)
                    return false;
        }

        return true;
    }
}

} look