Skip to content
Snippets Groups Projects
Commit 797ece09 authored by DamianKomdeur's avatar DamianKomdeur
Browse files

Sudoku Generator

Sudoku Generator + Manual placement + Small settings fixes (Now all menu items change color correctly and the settings are accessible from a puzzleform)
parent 4ac3a0fd
No related branches found
No related tags found
No related merge requests found
......@@ -44,7 +44,7 @@ namespace PuzzlePlayer_Namespace
if (s == default) s = new Size(700, 420);
this.Size = this.MinimumSize = s;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.DarkGreen;
this.BackColor = SettingForm.primaryColor;
this.Text = "PuzzlePlayer";
bufferedGraphics = BufferedGraphicsManager.Current.Allocate(this.CreateGraphics(), this.DisplayRectangle);
bufferedGraphics.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
......@@ -103,6 +103,30 @@ namespace PuzzlePlayer_Namespace
menuStrip.Items.Add(menuSettings);
this.Controls.Add(menuStrip);
menuSettings.Click += (object o, EventArgs e) =>
{
this.Hide();
SettingForm settingForm = new SettingForm();
settingForm.FormClosed += (object s, FormClosedEventArgs args) =>
{
this.BackColor = SettingForm.primaryColor;
this.ForeColor = SettingForm.tertiaryColor;
foreach (Control control in this.Controls)
{
SettingForm.UpdateSettings(control);
}
bufferedGraphics.Graphics.Clear(this.BackColor);
this.Show();
};
settingForm.Show();
};
void CreateButton(Button b)
{
Controls.Add(b);
......@@ -167,6 +191,9 @@ namespace PuzzlePlayer_Namespace
informationbox.Text = board.description;
informationbox.Font = new Font("Verdana", 10);
this.BackColor = SettingForm.primaryColor;
this.ForeColor = SettingForm.tertiaryColor;
UpdateUI();
}
private void UpdateUI() //resizes the boardspace rectangle and updates the rest of the ui around the new boardspace size
......
......@@ -29,6 +29,7 @@ namespace PuzzlePlayer_Namespace
puzzleForms.Clear();
puzzleForms.Add(new PuzzleForm(new Binary()));
puzzleForms.Add(new PuzzleForm(new Maze()));
puzzleForms.Add(new PuzzleForm(new Sudoku()));
}
private void SetUpUI()
......@@ -97,6 +98,9 @@ namespace PuzzlePlayer_Namespace
{
string image = "../../..//Resources/" + $"{puzzleForms[i].puzzleType}";
Image normalImage = Image.FromFile(image + ".jpg");
Image grayImage = Image.FromFile(image + "Gray.jpg");
// Set the name of the button
RoundedButton button = new RoundedButton
{
......@@ -104,7 +108,7 @@ namespace PuzzlePlayer_Namespace
Margin = new Padding(10),
Height = 345,
Width = 345,
Image = Image.FromFile(image + ".jpg"),
Image = normalImage,
FlatStyle = FlatStyle.Flat,
Font = SettingForm.mainFont,
Name = i.ToString()
......@@ -112,12 +116,12 @@ namespace PuzzlePlayer_Namespace
button.MouseEnter += (object o, EventArgs e) =>
{
button.Image = Image.FromFile(image + "Gray.jpg");
button.Image = grayImage;
};
button.MouseLeave += (object o, EventArgs e) =>
{
button.Image = Image.FromFile(image + ".jpg");
button.Image = normalImage;
};
button.MouseClick += (object o, MouseEventArgs e) =>
......
PuzzlePlayer/Resources/Sudoku.jpg

26.5 KiB

PuzzlePlayer/Resources/SudokuGray.jpg

24.5 KiB

......@@ -11,9 +11,10 @@ namespace PuzzlePlayer_Namespace
// Public variables for all settings
public static Color primaryColor = Color.FromArgb(66, 69, 73);
public static Color secondaryColor = Color.FromArgb(54, 57, 62);
public static Color tertiaryColor = Color.FromArgb(66, 69, 73);
public static Color tertiaryColor = Color.FromArgb(0, 0, 0);
public static int fontSize = 16;
public static Font mainFont = new Font("Gotham", fontSize);
public static FlowLayoutPanel settingsPanel;
public SettingForm()
{
......@@ -69,6 +70,7 @@ namespace PuzzlePlayer_Namespace
};
themeSelector.Items.Add("Dark");
themeSelector.Items.Add("Light");
themeSelector.Items.Add("Pink");
themeSelector.SelectedIndexChanged += (object o, EventArgs e) =>
{
......@@ -84,6 +86,11 @@ namespace PuzzlePlayer_Namespace
SettingForm.secondaryColor = Color.FromArgb(210, 211, 219);
SettingForm.tertiaryColor = Color.FromArgb(72, 75, 106);
break;
case "Pink":
SettingForm.primaryColor = Color.FromArgb(255, 209, 220);
SettingForm.secondaryColor = Color.FromArgb(252, 178, 197);
SettingForm.tertiaryColor = Color.FromArgb(251, 199, 255);
break;
}
};
......@@ -95,7 +102,7 @@ namespace PuzzlePlayer_Namespace
Width = 250,
BackColor = SettingForm.secondaryColor,
ForeColor = Color.Black,
Margin = new Padding(20)
Margin = new Padding(20),
};
sizeSelector.Items.Add("Small");
sizeSelector.Items.Add("Medium");
......@@ -116,9 +123,10 @@ namespace PuzzlePlayer_Namespace
break;
}
mainFont = new Font("Gotham", fontSize);
settingsPanel.PerformLayout();
};
FlowLayoutPanel settingsPanel = new FlowLayoutPanel
settingsPanel = new FlowLayoutPanel
{
Location = new Point(0, menuStrip.Height),
Size = new Size(ClientSize.Width, ClientSize.Height - menuStrip.Height - 100),
......@@ -126,6 +134,7 @@ namespace PuzzlePlayer_Namespace
FlowDirection = FlowDirection.TopDown,
WrapContents = true,
AutoScroll = true,
Padding = new Padding(20),
};
//
......@@ -189,6 +198,29 @@ namespace PuzzlePlayer_Namespace
{
UpdateSettings(childControl);
}
if (control is MenuStrip menuStrip)
{
foreach (ToolStripItem item in menuStrip.Items)
{
UpdateToolStripSettings(item);
}
}
}
public static void UpdateToolStripSettings(ToolStripItem item)
{
item.BackColor = secondaryColor;
item.ForeColor = tertiaryColor;
item.Font = mainFont;
if (item is ToolStripMenuItem menuItem)
{
foreach (ToolStripItem subItem in menuItem.DropDownItems)
{
UpdateToolStripSettings(subItem);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
namespace PuzzlePlayer_Namespace
{
internal class Sudoku : Binary
{
private static int boardLength;
private static int rootBoardLength;
public Sudoku(int boardSize = 9)
{
boardState = GetClearBoard(boardSize, boardSize);
lastGeneratedBoard = GetClearBoard(boardSize, boardSize);
boardLength = boardSize;
rootBoardLength = (int)Math.Sqrt(boardLength);
description = "SUDOKU !!!!! !!!!";
drawFactor = 8;
}
public override void Draw(Graphics gr, Rectangle r)
{
Size tilesize = new Size(r.Width / boardState.GetLength(0), r.Height / boardState.GetLength(1));
Pen border = new Pen(Color.Black, 2);
gr.FillRectangle(Brushes.Beige, r.X, r.Y, tilesize.Width * boardState.GetLength(0), tilesize.Height * boardState.GetLength(1));
for (int i = 0; i < boardState.GetLength(0); i++)
{
for (int j = 0; j < boardState.GetLength(1); j++)
{
gr.DrawRectangle(Pens.LightGray,
r.X + i * tilesize.Width,
r.Y + j * tilesize.Height,
tilesize.Width,
tilesize.Height);
if (boardState[i,j] != -1)
{
gr.DrawString(
(boardState[i, j]).ToString(),
new Font("Arial", 42),
Brushes.Black,
(float)(r.X + i * tilesize.Width + tilesize.Width / 4),
(float)(r.Y + j * tilesize.Height + tilesize.Height / 4)
);
}
if (lastGeneratedBoard[i, j] != Board.emptySpace)
{
gr.FillRectangle(Brushes.LightGray,
(int)(r.X + ((double)i + 0.4375) * tilesize.Width),
(int)(r.Y + ((double)j + 0.4375) * tilesize.Height),
tilesize.Width / 8,
tilesize.Height / 8);
}
}
}
}
public override void Generate()
{
boardState = GetClearBoard(boardLength, boardLength);
for (int i = 0; i < boardLength; i += rootBoardLength)
{
FillBox(i, i);
}
FillSudoku(0, rootBoardLength);
}
private int RandomNumber(int number)
{
Random random = new Random();
return (int)Math.Floor((double)(random.NextDouble() * number + 1));
}
private void FillBox(int row, int col)
{
int num;
for (int i = 0; i < rootBoardLength; i++)
{
for (int j = 0; j < rootBoardLength; j++)
{
do
{
num = RandomNumber(boardLength);
}
while (BoxFlag(row, col, num));
boardState[row + i, col + j] = num;
}
}
}
private bool DoAllChecks(int i, int j, int num)
{
return (BoxFlag(i - i % rootBoardLength, j - j % rootBoardLength, num) || RowFlag(i, num) || ColFlag(j, num));
}
private bool BoxFlag(int row, int col, int num)
{
for (int i = 0; i < rootBoardLength; i++)
{
for (int j = 0; j < rootBoardLength; j++)
{
if (boardState[row + i, col + j] == num)
{
return true;
}
}
}
return false;
}
private bool RowFlag(int i, int num)
{
for (int j = 0; j < boardLength; j++)
{
if (num == boardState[i, j])
{
return true;
}
}
return false;
}
private bool ColFlag(int j, int num)
{
for (int i = 0; i < boardLength; i++)
{
if(num == boardState[i, j])
{
return true;
}
}
return false;
}
private bool FillSudoku(int row, int col)
{
if (row == boardLength - 1 && col == boardLength)
return true;
if (col == boardLength)
{
row++;
col = 0;
}
if (boardState[row, col] != -1)
{
return FillSudoku(row, col + 1);
}
for (int num = 1; num <= boardLength; num++)
{
if (!DoAllChecks(row, col, num))
{
boardState[row, col] = num;
if (FillSudoku(row, col + 1))
return true;
boardState[row, col] = -1;
}
}
return false;
}
private static string BoardToString(int[,] board)
{
string result = "";
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
result += (board[i, j] == emptySpace ? "." : board[i, j].ToString());
}
result += "|";
}
return result;
}
private static int[,] BoardFromString(string board)
{
string[] parts = board.Split('|', StringSplitOptions.RemoveEmptyEntries);
int[,] result = new int[parts.Length, parts.Length];
for (int i = 0; i < result.GetLength(0); i++)
{
for (int j = 0; j < result.GetLength(1); j++)
{
string s = parts[i][j].ToString();
if (s == ".")
result[i, j] = emptySpace;
else if (int.Parse(s) >= 0 && int.Parse(s) <= 8)
result[i, j] = int.Parse(s);
else
return null;
}
}
return result;
}
public override void TileInput(Point? p, Keys k)
{
if (p == null) return;
int num = (int)k - 48;
if (num >= 1 && num <= 9) boardState[((Point)p).X, ((Point)p).Y] = num;
}
public override void TileClick(Point p, int x)
{
if (x == 1)
{
x = -1;
}
if (x == 0)
{
x = 1;
}
if (boardState[p.X, p.Y] == emptySpace)
{
if (x == 1)
{
boardState[p.X, p.Y] = 1;
return;
}
else
{
boardState[p.X, p.Y] = 9;
return;
}
}
else if (boardState[p.X, p.Y] == 9)
{
if (x == 1)
{
boardState[p.X, p.Y] = emptySpace;
return;
}
else
{
boardState[p.X, p.Y] += x;
return;
}
}
else if (boardState[p.X, p.Y] == 1)
{
if (x == -1)
{
boardState[p.X, p.Y] = emptySpace;
return;
}
else
{
boardState[p.X, p.Y] += x;
return;
}
}
else
{
boardState[p.X, p.Y] += x;
return;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment