Advertisement
Guest User

Untitled

a guest
Mar 24th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.43 KB | None | 0 0
  1. namespace RushHour
  2. {
  3.     public class IDSTree
  4.     {
  5.         private static IDSNode root;
  6.         private Dictionary<int, IDSCar> cars;
  7.         private MainWindow gui;
  8.         private int currentDepth;
  9.  
  10.         public static IDSNode Root {
  11.             get{return root;}
  12.         }
  13.  
  14.         public IDSTree (Dictionary<int, IDSCar> input, MainWindow window)
  15.         {
  16.             cars = input;
  17.             gui = window;
  18.             currentDepth = 0;
  19.  
  20.             root = new IDSNode (cars, null);
  21.  
  22.             window.drawBoard (root.Board);
  23.         }
  24.  
  25.         public bool Solve ()
  26.         {
  27.             // True if the game has a solution
  28.             // False if not
  29.             List<IDSNode> generatedNodes1 = root.generateMoves ();
  30.  
  31.             Queue<IDSNode> que = new Queue<IDSNode>(generatedNodes1);
  32.  
  33.  
  34.  
  35.  
  36.             while (que.Count != 0) {
  37.                 IDSNode node = que.Dequeue();
  38.  
  39.                 generatedNodes1 = node.generateMoves();
  40.                 foreach(var newnode in generatedNodes1){
  41.  
  42.                     que.Enqueue(newnode);
  43.    
  44.                 }
  45.                 gui.drawBoard(node.Board);
  46.  
  47.            
  48.             }
  49.        
  50.  
  51.  
  52.  
  53.             return GameConfig.Solved;
  54.  
  55.         }
  56.     }
  57.  
  58.     public class IDSNode
  59.     {
  60.         private int[] board;
  61.         private IDSNode parent;
  62.         private List<IDSNode> child;
  63.         private Dictionary<int ,IDSCar> cars;
  64. //      private int depth;
  65.  
  66.         public int[] Board {
  67.             get { return board;}
  68.             set { board = value;}
  69.         }
  70.  
  71. //      public int Depth{ get; set; }
  72.  
  73.         public IDSNode Parent {
  74.             get { return parent;}
  75.         }
  76.  
  77.         public Dictionary<int, IDSCar> Cars {
  78.             get{return cars;}
  79.         }
  80.  
  81.         public IDSNode (Dictionary<int, IDSCar> cars, IDSNode parent)
  82.         {
  83.             board = new int[GameConfig.X * GameConfig.Y];
  84.             this.cars = cars;
  85.             this.child = new List<IDSNode> ();
  86.             this.parent = parent;
  87.  
  88.             foreach (var car in cars) {
  89.                 car.Value.drawOnBoard (board);
  90.             }
  91.         }
  92.  
  93.         public List<IDSNode> generateMoves ()
  94.         {
  95.             // generate all posible moves for current board
  96.             /*  program the generator
  97.              *  start at the begining of the list
  98.              *  then validate each move, and if any
  99.              *  is posible then clone create new list
  100.              *  of cars with the new car added and the
  101.              *  original removed, then create
  102.              *  new IDSNode
  103.              */
  104.  
  105.  
  106.             // foreach car check posible moves
  107.             foreach (var pair in cars) {
  108.  
  109.  
  110.                 if(pair.Value.isValidMove(-1, board)){
  111.                     child.Add(createNewNode(-1, pair.Key, pair.Value));
  112.                     GameConfig.GenNodes += 1;
  113.                 }
  114.  
  115.                 if(pair.Value.isValidMove(1, board)){
  116.                     child.Add(createNewNode(1, pair.Key, pair.Value));
  117.                     GameConfig.GenNodes += 1;
  118.                 }
  119.  
  120.             }
  121.  
  122.  
  123.             return child;
  124.         }
  125.  
  126.         private IDSNode createNewNode (int t, int key, IDSCar car)
  127.         {
  128.             Dictionary<int, IDSCar> newCars;
  129.             IDSCar newCar;
  130.  
  131.             newCars = new Dictionary<int, IDSCar> (IDSTree.R);
  132.             newCars.Remove (key);
  133.             newCar = car.Clone ();
  134.             //newCar = car;
  135.             newCar.move (t, board);
  136.             newCars.Add (key, newCar);
  137.  
  138.             return new IDSNode(newCars, this);
  139.         }
  140.     }
  141.    
  142.  
  143.     public class IDSCar : ICloneable
  144.     {
  145.         private int id;
  146.         private int[] pos;
  147.         private bool red;
  148.         private Types typ;
  149.         private Directions dir;
  150.         private int lastMove;
  151.  
  152.         public int ID{
  153.             get{ return id;}
  154.         }
  155.         public IDSCar (int id, int x, int y, Types typ, Directions dir, bool red, int lastMove)
  156.         {
  157.             pos = new int[2];
  158.             this.red = red;
  159.  
  160.  
  161.             pos [0] = x;
  162.             pos [1] = y;
  163.  
  164.             this.id = id;
  165.             this.typ = typ;
  166.             this.dir = dir;
  167.             this.lastMove = lastMove;   // 0 the car has not moved yet
  168.             // 1 the car has moved in the direction by +1
  169.             // -1 the car has moved in the direction by -1
  170.         }
  171.  
  172.  
  173.         public bool move (int t, int[] board)
  174.         {
  175.             int size = (int)typ;
  176.             lastMove = t;
  177. /*
  178.             if (t > 0) {
  179.                 if (dir == Directions.HOR) {
  180.                     //board [pos [1] * GameConfig.X + pos [0]] = 0;
  181.                    
  182.                 } else {
  183.                     //board [pos [1] * GameConfig.X + pos [0]] = 0;                    
  184.                 }
  185.             } else {
  186.                 if (dir == Directions.HOR) {
  187.                     //board [pos [1] * GameConfig.X + pos [0] + size - 1] = 0;
  188.                    
  189.                 } else {
  190.                     //board [(pos [1] + size - 1) * GameConfig.X + pos [0]] = 0;                       
  191.                 }
  192.             }
  193. */
  194.             pos [(int)dir] += t;
  195.  
  196.             return true;
  197.         }
  198.  
  199.  
  200.         public bool isValidMove (int t, int[] board)
  201.         {
  202.             int [] newpos = (int[])pos.Clone ();
  203.  
  204.  
  205.             if (lastMove == t || lastMove == 0) {
  206.                 newpos [(int)dir] += t;
  207.    
  208.  
  209.                 // check if we are not out of the bounds
  210.                 int size = (int)typ;
  211.  
  212.                 if (newpos [0] >= GameConfig.X || newpos [0] < 0 || newpos [1] >= GameConfig.Y || newpos [1] < 0)
  213.                     return false;
  214.                 newpos[(int)dir] += (size - 1);
  215.  
  216.  
  217.  
  218.                 if (newpos [0] >= GameConfig.X || newpos [0] <= 0 || newpos [1] >= GameConfig.Y || newpos [1] <= 0)
  219.                     return false;
  220.  
  221.                 newpos[(int)dir] -= (size - 1);
  222.  
  223.  
  224.  
  225.                
  226.                 if (dir == Directions.HOR) {
  227.                     for (int i = 0; i < size; i++) {
  228.                         if (!validatePosOnBoard (board, newpos [1] * GameConfig.X + newpos [0] + i)) {
  229.                             return false;
  230.                         }
  231.                     }
  232.                 } else {
  233.                     for (int i = 0; i < size; i++) {
  234.                         if (!validatePosOnBoard (board, (newpos [1] + i) * GameConfig.X + newpos [0])) {
  235.                             return false;
  236.                         }
  237.                        
  238.                     }
  239.                 }
  240.             } else {
  241.                 return false;
  242.             }
  243.  
  244.             return true;
  245.         }
  246.  
  247.         public bool drawOnBoard (int[] board)
  248.         {
  249.             int size = (int)typ;
  250.  
  251.             if (dir == Directions.HOR) {
  252.                 for (int i = 0; i < size; i++) {
  253.                     if (validatePosOnBoard (board, pos [1] * GameConfig.X + pos [0] + i)) {
  254.                         board [pos [1] * GameConfig.X + pos [0] + i] = id;
  255.                     } else {
  256.                         // revert changes
  257.                         for (int r = i-1; r >= 0; r--) {
  258.                             board [pos [1] * GameConfig.X + pos [0] + r] = 0;
  259.                         }
  260.                         return false;
  261.                     }
  262.                 }
  263.             } else { // have to do this for the other direction
  264.  
  265.                 for (int i = 0; i < size; i++) {
  266.                     if (validatePosOnBoard (board, (pos [1] + i) * GameConfig.X + pos [0])) {
  267.  
  268.                         board [(pos [1] + i) * GameConfig.X + pos [0]] = id;
  269.                     } else {
  270.  
  271.                         for (int r = i-1; r >= 0; r--) {
  272.                             board [(pos [1] + r) * GameConfig.X + pos [0]] = 0;
  273.                         }
  274.                         return false;
  275.                     }
  276.                    
  277.                 }
  278.             }
  279.             return true;
  280.         }
  281.  
  282.         private bool validatePosOnBoard (int[]board, int position)
  283.         {
  284.  
  285.             if (board [position] == 0 || board [position] == id)
  286.                 return true;
  287.             else
  288.                 return false;  
  289.         }
  290.  
  291.         public override string ToString ()
  292.         {
  293.             return string.Format ("ID:" + id.ToString () + " Pos: " + "[" + pos [0].ToString () + "," + pos [1].ToString () + "]" + " type: " + typ.ToString () + " Direction: " + dir.ToString () + " Red: " + red);
  294.         }
  295.         object ICloneable.Clone ()
  296.         {
  297.             return this.Clone ();
  298.         }
  299.         public IDSCar Clone ()
  300.         {
  301.             return new IDSCar (id, pos [0], pos [1], typ, dir, red, lastMove);
  302.         }
  303.  
  304.  
  305.     }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement