Skip to content

Commit 9d6fcd4

Browse files
committed
Add 15-puzzle-game the example of library use
1 parent f6b2a21 commit 9d6fcd4

7 files changed

Lines changed: 338 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{AF0DA1F4-38EA-42BB-8034-F5C2659D9182}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_15_puzzle</RootNamespace>
10+
<AssemblyName>15 puzzle</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Board.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
<Compile Include="Tile.cs" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<None Include="App.config" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<ProjectReference Include="..\..\SharpPDDL\SharpPDDL.csproj">
56+
<Project>{ab77eead-3e0e-4ee8-906e-815e8e2ac5ed}</Project>
57+
<Name>SharpPDDL</Name>
58+
</ProjectReference>
59+
</ItemGroup>
60+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
61+
</Project>

Examples/15 puzzle/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

Examples/15 puzzle/Board.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace _15_puzzle
8+
{
9+
static class Board
10+
{
11+
//Exterior part
12+
static readonly char LeftUp = '╔';
13+
static readonly string LeftRight = "║";
14+
static readonly char LeftB = '╟';
15+
static readonly char LeftDown = '╚';
16+
static readonly char RightUp = '╗';
17+
static readonly char RightB = '╢';
18+
static readonly char RightDown = '╝';
19+
static readonly char UpDown = '═';
20+
static readonly char UpB = '╤';
21+
static readonly char DownB = '╧';
22+
23+
//Interior part
24+
static readonly string Vertical = "│";
25+
static readonly char Horizontal = '─';
26+
static readonly char Cross = '┼';
27+
28+
private static void LeftMargin(int MarginV) => Console.CursorLeft = MarginV;
29+
30+
private static void PrintLine(IEnumerable<Tile> Line)
31+
{
32+
Console.Write(LeftRight);
33+
Line.OrderBy(L => L.Col);
34+
foreach (Tile t in Line)
35+
{
36+
t.WriteIt();
37+
Console.Write(Vertical);
38+
}
39+
Console.CursorLeft = Console.CursorLeft - 1;
40+
Console.WriteLine(LeftRight);
41+
}
42+
43+
public static void DrawBoard (IEnumerable<Tile> tiles)
44+
{
45+
int LeftPos = 1;
46+
char[] HorizontalInt = new char[] { LeftB, Horizontal, Horizontal, Cross, Horizontal, Horizontal, Cross, Horizontal, Horizontal, Cross, Horizontal, Horizontal, RightB };
47+
IEnumerable<IGrouping<int, Tile>> Lines = tiles.GroupBy(t => t.Row);
48+
Console.SetCursorPosition(LeftPos, 1);
49+
50+
Console.WriteLine(new char[] { LeftUp, UpDown, UpDown, UpB, UpDown, UpDown, UpB, UpDown, UpDown, UpB, UpDown, UpDown, RightUp });
51+
52+
for (int i = 0; i!=4; ++i)
53+
{
54+
LeftMargin(LeftPos);
55+
PrintLine(tiles.Where(t => t.Row == i));
56+
57+
if (i != 3)
58+
{
59+
LeftMargin(LeftPos);
60+
Console.WriteLine(HorizontalInt);
61+
}
62+
}
63+
64+
LeftMargin(LeftPos);
65+
Console.WriteLine( new char[] { LeftDown, UpDown, UpDown, DownB, UpDown, UpDown, DownB, UpDown, UpDown, DownB, UpDown, UpDown, RightDown });
66+
67+
}
68+
}
69+
}

Examples/15 puzzle/Program.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Treatment the puzzle: https://en.wikipedia.org/wiki/15_puzzle
3+
*/
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Linq.Expressions;
9+
using System.Text;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
using SharpPDDL;
13+
14+
namespace _15_puzzle
15+
{
16+
class Program
17+
{
18+
static List<Tile> tiles = new List<Tile>();
19+
static DomeinPDDL GemPuzzleDomein;
20+
21+
static ICollection<Expression<Predicate<Tile>>> ExpressionsOfXTile(int i)
22+
{
23+
List<Expression<Predicate<Tile>>> TileAtSpot = new List<Expression<Predicate<Tile>>>
24+
{
25+
T => T.Col == i % 4,
26+
T => T.Row == i / 4,
27+
T => T.TileValue == i + 1
28+
};
29+
return TileAtSpot;
30+
}
31+
32+
static void AddTileXGoal(object a, object b)
33+
{
34+
GoalPDDL goalPDDL = (GoalPDDL)a;
35+
int c = int.Parse(goalPDDL.Name);
36+
AddTileXGoal(c);
37+
}
38+
39+
static void AddTileXGoal(int i)
40+
{
41+
if (i < 16)
42+
{
43+
GoalPDDL Tile1Goal = new GoalPDDL((i + 1).ToString());
44+
45+
for (int j = 0; j < i + 1; j++)
46+
Tile1Goal.AddExpectedObjectState(ExpressionsOfXTile(j));
47+
48+
//GoalPDDL NextOne = ExpressionsOfXTile(i + 1);
49+
GemPuzzleDomein.AddGoal(Tile1Goal);
50+
Tile1Goal.GoalRealized += AddTileXGoal;
51+
}
52+
}
53+
54+
static void Main(string[] args)
55+
{
56+
tiles.Add(new Tile(12, 0, 0));
57+
tiles.Add(new Tile(1, 1, 0));
58+
tiles.Add(new Tile(2, 2, 0));
59+
tiles.Add(new Tile(15, 3, 0));
60+
61+
tiles.Add(new Tile(11, 0, 1));
62+
tiles.Add(new Tile(6, 1, 1));
63+
tiles.Add(new Tile(5, 2, 1));
64+
tiles.Add(new Tile(8, 3, 1));
65+
66+
tiles.Add(new Tile(7, 0, 2));
67+
tiles.Add(new Tile(10, 1, 2));
68+
tiles.Add(new Tile(9, 2, 2));
69+
tiles.Add(new Tile(4, 3, 2));
70+
71+
tiles.Add(new Tile(16, 0, 3));
72+
tiles.Add(new Tile(13, 1, 3));
73+
tiles.Add(new Tile(14, 2, 3));
74+
tiles.Add(new Tile(3, 3, 3));
75+
76+
Board.DrawBoard(tiles);
77+
78+
GemPuzzleDomein = new DomeinPDDL("GemPuzzle");
79+
80+
foreach (Tile tile in tiles)
81+
GemPuzzleDomein.domainObjects.Add(tile);
82+
83+
Tile Empty = null;
84+
Tile Sliding = null;
85+
86+
Expression<Predicate<Tile>> EmptyIs16 = E => E.TileValue == 16;
87+
Expression<Predicate<Tile, Tile>> DystansEq1 = (E, S) => Math.Abs(E.Col - S.Col) + Math.Abs(E.Row - S.Row) == 1;
88+
89+
ActionPDDL SlideTile = new ActionPDDL("Slide Tile");
90+
91+
SlideTile.AddPrecondiction<Tile, Tile>("Empty is 16", ref Empty, EmptyIs16);
92+
SlideTile.AddPrecondiction("Dystans between tiles is 1", ref Empty, ref Sliding, DystansEq1);
93+
94+
SlideTile.AddEffect("Sliding tile is empty one now", ref Sliding, S => S.TileValue, 16);
95+
SlideTile.AddEffect("Empty tile is slining one now", ref Empty, E => E.TileValue, ref Sliding, S => S.TileValue);
96+
97+
SlideTile.AddExecution("Wait", () => Thread.Sleep(750), false);
98+
SlideTile.AddExecution("Empty tile is slining one now");
99+
SlideTile.AddExecution("Sliding tile is empty one now");
100+
SlideTile.AddExecution("Draw it", () => Board.DrawBoard(tiles), true);
101+
102+
GemPuzzleDomein.AddAction(SlideTile);
103+
104+
AddTileXGoal(0);
105+
106+
GemPuzzleDomein.SetExecutionOptions(null, null, AskToAgree.GO_AHEAD);
107+
GemPuzzleDomein.Start();
108+
109+
Console.ReadKey();
110+
}
111+
}
112+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// Ogólne informacje o zestawie są kontrolowane poprzez następujący
6+
// zestaw atrybutów. Zmień wartości tych atrybutów, aby zmodyfikować informacje
7+
// powiązane z zestawem.
8+
[assembly: AssemblyTitle("15 puzzle")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("HP Inc.")]
12+
[assembly: AssemblyProduct("15 puzzle")]
13+
[assembly: AssemblyCopyright("Copyright © HP Inc. 2025")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Ustawienie elementu ComVisible na wartość false sprawia, że typy w tym zestawie są niewidoczne
18+
// dla składników COM. Jeśli potrzebny jest dostęp do typu w tym zestawie z
19+
// COM, ustaw wartość true dla atrybutu ComVisible tego typu.
20+
[assembly: ComVisible(false)]
21+
22+
// Następujący identyfikator GUID jest identyfikatorem biblioteki typów w przypadku udostępnienia tego projektu w modelu COM
23+
[assembly: Guid("af0da1f4-38ea-42bb-8034-f5c2659d9182")]
24+
25+
// Informacje o wersji zestawu zawierają następujące cztery wartości:
26+
//
27+
// Wersja główna
28+
// Wersja pomocnicza
29+
// Numer kompilacji
30+
// Rewizja
31+
//
32+
// Możesz określić wszystkie wartości lub użyć domyślnych numerów kompilacji i poprawki
33+
// przy użyciu symbolu „*”, tak jak pokazano poniżej:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Examples/15 puzzle/Tile.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
3+
namespace _15_puzzle
4+
{
5+
public class Tile
6+
{
7+
public int TileValue
8+
{
9+
get;
10+
set;
11+
}
12+
public int Col { get; private set; }
13+
public int Row { get; private set; }
14+
public bool JustMoved { get; private set; }
15+
16+
public Tile(int TileValue, int Col, int Row)
17+
{
18+
this.TileValue = TileValue;
19+
this.Col = Col;
20+
this.Row = Row;
21+
this.JustMoved = false;
22+
}
23+
24+
public void WriteIt()
25+
{
26+
if (JustMoved)
27+
Console.ForegroundColor = ConsoleColor.Green;
28+
29+
if (TileValue < 10)
30+
Console.Write(" ");
31+
32+
if (TileValue == 16)
33+
Console.Write(" ");
34+
else
35+
Console.Write(TileValue);
36+
37+
JustMoved = false;
38+
Console.ResetColor();
39+
}
40+
41+
public void MoveIt(int newCol, int newRow)
42+
{
43+
this.JustMoved = true;
44+
this.Col = newCol;
45+
this.Row = newRow;
46+
}
47+
}
48+
}

SharpPDDL.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "River crossing puzzle", "Ri
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Peg solitaire", "Peg solitaire\Peg solitaire.csproj", "{060948D4-DA38-406D-9B85-014C066E24F9}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "15 puzzle", "Examples\15 puzzle\15 puzzle.csproj", "{AF0DA1F4-38EA-42BB-8034-F5C2659D9182}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +47,10 @@ Global
4547
{060948D4-DA38-406D-9B85-014C066E24F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
4648
{060948D4-DA38-406D-9B85-014C066E24F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
4749
{060948D4-DA38-406D-9B85-014C066E24F9}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{AF0DA1F4-38EA-42BB-8034-F5C2659D9182}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{AF0DA1F4-38EA-42BB-8034-F5C2659D9182}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{AF0DA1F4-38EA-42BB-8034-F5C2659D9182}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{AF0DA1F4-38EA-42BB-8034-F5C2659D9182}.Release|Any CPU.Build.0 = Release|Any CPU
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)