Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C sharp bible #430

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// </copyright>
// <summary></summary>
// ***********************************************************************
using BaseLib.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -30,7 +31,7 @@ namespace Sudoku_Base.Models.Interfaces;
/// Extends the <see cref="INotifyPropertyChanged" />
/// </summary>
/// <seealso cref="INotifyPropertyChanged" />
public interface ISudokuField : INotifyPropertyChanged, INotifyPropertyChanging
public interface ISudokuField : INotifyPropertyChanged, INotifyPropertyChanging, IPersistence
{
/// <summary>
/// Gets the position.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using BaseLib.Interfaces;

/// <summary>
/// The Models namespace.
Expand All @@ -27,7 +28,7 @@ namespace Sudoku_Base.Models.Interfaces
/// Interface ISudokuModel
/// </summary>
/// <autogeneratedoc />
public interface ISudokuModel : INotifyPropertyChanged
public interface ISudokuModel : INotifyPropertyChanged, IPersistence
{
IReadOnlyList<ISudokuField> Fields { get; }

Expand All @@ -36,9 +37,10 @@ public interface ISudokuModel : INotifyPropertyChanged

IRelayCommand UndoCommand { get; }
IRelayCommand RedoCommand { get; }
int UndoIndex { get; }
int RedoIndex { get; }

bool WriteToStream(Stream stream,bool xInclState);

bool ReadFromStream(Stream stream);
}
}
88 changes: 58 additions & 30 deletions CSharpBible/Games/Sudoku_Base/Models/SudokuField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
// </copyright>
// <summary></summary>
// ***********************************************************************
using BaseLib.Interfaces;
using CommunityToolkit.Mvvm.ComponentModel;
using Sudoku_Base.Models.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using BaseLib.Helper;
namespace Sudoku_Base.Models;

public partial class SudokuField : ObservableObject, ISudokuField
Expand All @@ -36,6 +37,16 @@ public partial class SudokuField : ObservableObject, ISudokuField
private ObservableCollection<int> _possibleValues = new();
IList<int> ISudokuField.PossibleValues => PossibleValues;

public static IEnumerable<(string, Type)> PropTypes => [
(nameof(Position),typeof(Point)),
(nameof(Value), typeof(int)),
(nameof(IsPredefined),typeof(bool)),
("Dummy", typeof(byte)),
(nameof(PossibleValues),typeof(IEnumerable<int>))
];

IEnumerable<(string, Type)> IPersistence.PropTypes => PropTypes;

public SudokuField()
{
}
Expand All @@ -45,7 +56,7 @@ public SudokuField(Point position, int? value, bool isPredefined, int[] pValues)
Position = position;
Value = value;
IsPredefined = isPredefined;
foreach (var pValue in pValues)
foreach (var pValue in pValues)
PossibleValues.Add(pValue);
}

Expand All @@ -68,41 +79,58 @@ public void RemovePossibleValue(int value)
}
public void ReadFromStream(Stream stream)
{
var streamBytes = new byte[sizeof(int)*2];
int i;
stream.Read(streamBytes, 0, sizeof(int)*2);
Position =new Point( BitConverter.ToInt32(streamBytes, 0), BitConverter.ToInt32(streamBytes, sizeof(int)));
stream.Read(streamBytes, 0, sizeof(int) * 2);
Value = (i = BitConverter.ToInt32(streamBytes, 0))!=-1?i:null;

IsPredefined = BitConverter.ToBoolean(streamBytes, sizeof(int));
var count = BitConverter.ToInt16(streamBytes, sizeof(int)+2);
streamBytes = new byte[sizeof(int) * count];
stream.Read(streamBytes, 0, sizeof(int) * count);
for (i=0; i<count; i++ )
{
PossibleValues.Add(BitConverter.ToInt32(streamBytes, sizeof(int)*i));
}

ReadFromEnumerable(stream.StreamToEnumerable( PropTypes));
}

public void WriteToStream(Stream stream)
{
stream.Write(BitConverter.GetBytes(Position.X), 0, sizeof(int));
stream.Write(BitConverter.GetBytes(Position.Y), 0, sizeof(int));
stream.Write(BitConverter.GetBytes(Value??-1), 0, sizeof(int));
stream.Write(BitConverter.GetBytes(IsPredefined), 0, sizeof(bool));
stream.WriteByte((byte)0);//padding
stream.Write(BitConverter.GetBytes((short)PossibleValues.Count), 0, sizeof(short));
foreach (var value in PossibleValues)
{
stream.Write(BitConverter.GetBytes(value), 0, sizeof(int));
}
stream.EnumerateToStream(EnumerateProp());
}

public void Clear()
{
Value = null;
IsPredefined = false;
PossibleValues.Clear();
Value = null;
IsPredefined = false;
PossibleValues.Clear();
}

public IEnumerable<(string, object)> EnumerateProp()
{
yield return (nameof(Position), Position);
yield return (nameof(Value), Value ?? -1);
yield return (nameof(IsPredefined), IsPredefined);
yield return ("Dummy", (byte)0);
yield return (nameof(PossibleValues), PossibleValues);
}

public bool ReadFromEnumerable(IEnumerable<(string, object)> enumerable)
{
foreach ((string prop, object value) pv in enumerable)
{
switch (pv)
{
case (nameof(Position),Point p):
Position = p;
break;
case (nameof(Value),int i):
Value = i != -1 ? i : null;
break;
case (nameof(IsPredefined),bool x):
IsPredefined = x;
break;
case (nameof(PossibleValues), IEnumerable<int?> ei):
PossibleValues.Clear();
foreach (var value in ei)
{
PossibleValues.Add(value.Value);
}
break;
default: // Unbekannte Werte werden ignoriert
break;
}
}
return true;
}

}
Loading
Loading