Skip to content

Commit

Permalink
Todo: better generation, menu buttons, bigger puzzle support
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafakalash committed Jan 4, 2021
1 parent d55d6dd commit ec2f1ec
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 31 deletions.
55 changes: 29 additions & 26 deletions sudoku/App.xaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
<Application x:Class="Sudoku.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Sudoku"
xmlns:local="clr-namespace:Sudoku" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
StartupUri="MainWindow.xaml">
<Application.Resources>
<DataTemplate x:Key="NoteTemplate">
<TextBlock FontWeight="Bold" Foreground="Black" FontSize="6" Text="{Binding}" Margin="2, 1" />
</DataTemplate>
<DataTemplate x:Key="CellTemplate">
<Border x:Name="Cell" BorderBrush="DimGray" BorderThickness="1" Background="#FFF">
<TextBlock x:Name="Text" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Foreground="Blue" FontSize="16" Text="{Binding Path=Number}" />
<Border.ContextMenu>
<ContextMenu>
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<ContentPresenter x:Name="Header" ContentSource="Header" RecognizesAccessKey="True" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.ItemContainerStyle>
<ListBox BorderThickness="0" Width="35" Margin="0"
SelectedItem="{Binding Path=Number, Mode=TwoWay}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
ItemsSource="{Binding Path=PossibleValues}" />
</ContextMenu>
</Border.ContextMenu>
</Border>
<Button x:Name="Cell" BorderBrush="DimGray" BorderThickness="1" Background="White" Click="SelectCell" KeyDown="InputNumber">
<Button.Resources>
<CollectionViewSource Source="{Binding Path=Notes}" x:Key="SortedNotes">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="." />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Button.Resources>
<Grid Width="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}" Height="{Binding ActualHeight, RelativeSource={RelativeSource TemplatedParent}}" Margin="-2, -2, 0 ,0">
<TextBlock x:Name="Text" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="Blue" FontSize="16" Text="{Binding Path=Number}" />
<ItemsControl ItemsSource="{Binding Source={StaticResource SortedNotes}}" ItemTemplate="{StaticResource NoteTemplate}" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Button>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SiblingSelected}" Value="True">
<Setter TargetName="Cell" Property="Background" Value="AliceBlue" />
</DataTrigger>
<DataTrigger Binding="{Binding IsValid}" Value="False">
<Setter TargetName="Text" Property="Foreground" Value="Red" />
<Setter TargetName="Cell" Property="Background" Value="Salmon" />
</DataTrigger>
<DataTrigger Binding="{Binding ReadOnly}" Value="True">
<Setter TargetName="Text" Property="Foreground" Value="Black" />
<Setter TargetName="Cell" Property="ContextMenu" Value="{x:Null}" />
</DataTrigger>
<DataTrigger Binding="{Binding Selected}" Value="True">
<Setter TargetName="Cell" Property="Background" Value="#BEE6FD" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Expand Down
68 changes: 67 additions & 1 deletion sudoku/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Sudoku
{
Expand All @@ -13,5 +15,69 @@ namespace Sudoku
/// </summary>
public partial class App : Application
{
public static Board GameBoard;

public void SelectCell(object sender, RoutedEventArgs e) {
Button btn = (Button) sender;
Cell cell = (Cell) btn.DataContext;
if(cell.ReadOnly) {
return;
}
GameBoard.SelectedCell = cell;
}

public void InputNumber(object sender, KeyEventArgs e) {
if(GameBoard.SelectedCell.ReadOnly) {
return;
}

if(e.Key == Key.Back) {
GameBoard.SelectedCell.Number = null;
}

int? n = null;
switch(e.Key) {
case Key.D1:
n = 1;
break;
case Key.D2:
n = 2;
break;
case Key.D3:
n = 3;
break;
case Key.D4:
n = 4;
break;
case Key.D5:
n = 5;
break;
case Key.D6:
n = 6;
break;
case Key.D7:
n = 7;
break;
case Key.D8:
n = 8;
break;
case Key.D9:
n = 9;
break;
}

if(n.HasValue) {
int number = (int) n;
if(e.KeyboardDevice.Modifiers == ModifierKeys.Shift) {
if(GameBoard.SelectedCell.Notes.Contains(number)) {
GameBoard.SelectedCell.Notes.Remove(number);
} else {
GameBoard.SelectedCell.Notes.Add(number);
}
} else {
GameBoard.SelectedCell.Number = number;
}
}
}
}
}
}
26 changes: 25 additions & 1 deletion sudoku/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ public ObservableCollection<ObservableCollection<Grid>> BoardRows {
public readonly int Size;
public readonly int TotalSize;

Cell selectedCellObject;
public Cell SelectedCell {
get {
return selectedCellObject;
}
set {
if(selectedCellObject != value) {
if(selectedCellObject != null) {
selectedCellObject.Selected = false;
foreach(Cell c in selectedCellObject.GetSibilngs()) {
c.SiblingSelected = false;
}
}
selectedCellObject = value;
if(selectedCellObject != null) {
selectedCellObject.Selected = true;
foreach(Cell c in selectedCellObject.GetSibilngs()) {
c.SiblingSelected = true;
}
}
}
}
}

public Board(int size) {
TotalSize = size;
Size = (int) Math.Sqrt(size);
Expand Down Expand Up @@ -162,7 +186,7 @@ public bool TestBoard(ObservableCollection<ObservableCollection<Grid>> rows, ref
if(cell.IsValid) {
if((row == 8 && col == 8) || TestBoard(rows, ref solutionCount)) {
solutionCount++;
break;
return true;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions sudoku/BoardUI.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace Sudoku {
/// Interaction logic for BoardUI.xaml
/// </summary>
public partial class BoardUI : UserControl {
public Board gameBoard = new Board(9);
public BoardUI() {
App.GameBoard = new Board(9);
InitializeComponent();
MainList.DataContext = gameBoard;
gameBoard.GenerateGame();
MainList.DataContext = App.GameBoard;
App.GameBoard.GenerateGame();
}
}
}
40 changes: 40 additions & 0 deletions sudoku/Cell.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
Expand All @@ -11,6 +12,8 @@ public class Cell : INotifyPropertyChanged {
public const string READ_ONLY_EVENT = "ReadOnly";
public const string NUMBER_EVENT = "Number";
public const string IS_VALID_EVENT = "IsValid";
public const string SELECTED_EVENT = "Selected";
public const string SIBLING_SELECTED_EVENT = "SiblingSelected";

bool readOnlyValue = false;
public bool ReadOnly {
Expand Down Expand Up @@ -65,6 +68,43 @@ public List<int> PossibleValues {
}
}

ObservableCollection<int> notesList = new ObservableCollection<int>();
public ObservableCollection<int> Notes {
get {
return notesList;
}
}

bool selectedValue = false;
public bool Selected {
get {
return selectedValue;
}
set {
if(selectedValue != value) {
selectedValue = value;
if(PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(SELECTED_EVENT));
}
}
}
}

bool siblingSelectedValue = false;
public bool SiblingSelected {
get {
return siblingSelectedValue;
}
set {
if(siblingSelectedValue != value) {
siblingSelectedValue = value;
if(PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(SIBLING_SELECTED_EVENT));
}
}
}
}

public readonly int Row, Col, GridRow, GridCol;
public readonly Grid Grid;

Expand Down

0 comments on commit ec2f1ec

Please sign in to comment.