|
| 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 | +} |
0 commit comments