Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace RushHour
- {
- public class IDSTree
- {
- private static IDSNode root;
- private Dictionary<int, IDSCar> cars;
- private MainWindow gui;
- private int currentDepth;
- public static IDSNode Root {
- get{return root;}
- }
- public IDSTree (Dictionary<int, IDSCar> input, MainWindow window)
- {
- cars = input;
- gui = window;
- currentDepth = 0;
- root = new IDSNode (cars, null);
- window.drawBoard (root.Board);
- }
- public bool Solve ()
- {
- // True if the game has a solution
- // False if not
- List<IDSNode> generatedNodes1 = root.generateMoves ();
- Queue<IDSNode> que = new Queue<IDSNode>(generatedNodes1);
- while (que.Count != 0) {
- IDSNode node = que.Dequeue();
- generatedNodes1 = node.generateMoves();
- foreach(var newnode in generatedNodes1){
- que.Enqueue(newnode);
- }
- gui.drawBoard(node.Board);
- }
- return GameConfig.Solved;
- }
- }
- public class IDSNode
- {
- private int[] board;
- private IDSNode parent;
- private List<IDSNode> child;
- private Dictionary<int ,IDSCar> cars;
- // private int depth;
- public int[] Board {
- get { return board;}
- set { board = value;}
- }
- // public int Depth{ get; set; }
- public IDSNode Parent {
- get { return parent;}
- }
- public Dictionary<int, IDSCar> Cars {
- get{return cars;}
- }
- public IDSNode (Dictionary<int, IDSCar> cars, IDSNode parent)
- {
- board = new int[GameConfig.X * GameConfig.Y];
- this.cars = cars;
- this.child = new List<IDSNode> ();
- this.parent = parent;
- foreach (var car in cars) {
- car.Value.drawOnBoard (board);
- }
- }
- public List<IDSNode> generateMoves ()
- {
- // generate all posible moves for current board
- /* program the generator
- * start at the begining of the list
- * then validate each move, and if any
- * is posible then clone create new list
- * of cars with the new car added and the
- * original removed, then create
- * new IDSNode
- */
- // foreach car check posible moves
- foreach (var pair in cars) {
- if(pair.Value.isValidMove(-1, board)){
- child.Add(createNewNode(-1, pair.Key, pair.Value));
- GameConfig.GenNodes += 1;
- }
- if(pair.Value.isValidMove(1, board)){
- child.Add(createNewNode(1, pair.Key, pair.Value));
- GameConfig.GenNodes += 1;
- }
- }
- return child;
- }
- private IDSNode createNewNode (int t, int key, IDSCar car)
- {
- Dictionary<int, IDSCar> newCars;
- IDSCar newCar;
- newCars = new Dictionary<int, IDSCar> (IDSTree.R);
- newCars.Remove (key);
- newCar = car.Clone ();
- //newCar = car;
- newCar.move (t, board);
- newCars.Add (key, newCar);
- return new IDSNode(newCars, this);
- }
- }
- public class IDSCar : ICloneable
- {
- private int id;
- private int[] pos;
- private bool red;
- private Types typ;
- private Directions dir;
- private int lastMove;
- public int ID{
- get{ return id;}
- }
- public IDSCar (int id, int x, int y, Types typ, Directions dir, bool red, int lastMove)
- {
- pos = new int[2];
- this.red = red;
- pos [0] = x;
- pos [1] = y;
- this.id = id;
- this.typ = typ;
- this.dir = dir;
- this.lastMove = lastMove; // 0 the car has not moved yet
- // 1 the car has moved in the direction by +1
- // -1 the car has moved in the direction by -1
- }
- public bool move (int t, int[] board)
- {
- int size = (int)typ;
- lastMove = t;
- /*
- if (t > 0) {
- if (dir == Directions.HOR) {
- //board [pos [1] * GameConfig.X + pos [0]] = 0;
- } else {
- //board [pos [1] * GameConfig.X + pos [0]] = 0;
- }
- } else {
- if (dir == Directions.HOR) {
- //board [pos [1] * GameConfig.X + pos [0] + size - 1] = 0;
- } else {
- //board [(pos [1] + size - 1) * GameConfig.X + pos [0]] = 0;
- }
- }
- */
- pos [(int)dir] += t;
- return true;
- }
- public bool isValidMove (int t, int[] board)
- {
- int [] newpos = (int[])pos.Clone ();
- if (lastMove == t || lastMove == 0) {
- newpos [(int)dir] += t;
- // check if we are not out of the bounds
- int size = (int)typ;
- if (newpos [0] >= GameConfig.X || newpos [0] < 0 || newpos [1] >= GameConfig.Y || newpos [1] < 0)
- return false;
- newpos[(int)dir] += (size - 1);
- if (newpos [0] >= GameConfig.X || newpos [0] <= 0 || newpos [1] >= GameConfig.Y || newpos [1] <= 0)
- return false;
- newpos[(int)dir] -= (size - 1);
- if (dir == Directions.HOR) {
- for (int i = 0; i < size; i++) {
- if (!validatePosOnBoard (board, newpos [1] * GameConfig.X + newpos [0] + i)) {
- return false;
- }
- }
- } else {
- for (int i = 0; i < size; i++) {
- if (!validatePosOnBoard (board, (newpos [1] + i) * GameConfig.X + newpos [0])) {
- return false;
- }
- }
- }
- } else {
- return false;
- }
- return true;
- }
- public bool drawOnBoard (int[] board)
- {
- int size = (int)typ;
- if (dir == Directions.HOR) {
- for (int i = 0; i < size; i++) {
- if (validatePosOnBoard (board, pos [1] * GameConfig.X + pos [0] + i)) {
- board [pos [1] * GameConfig.X + pos [0] + i] = id;
- } else {
- // revert changes
- for (int r = i-1; r >= 0; r--) {
- board [pos [1] * GameConfig.X + pos [0] + r] = 0;
- }
- return false;
- }
- }
- } else { // have to do this for the other direction
- for (int i = 0; i < size; i++) {
- if (validatePosOnBoard (board, (pos [1] + i) * GameConfig.X + pos [0])) {
- board [(pos [1] + i) * GameConfig.X + pos [0]] = id;
- } else {
- for (int r = i-1; r >= 0; r--) {
- board [(pos [1] + r) * GameConfig.X + pos [0]] = 0;
- }
- return false;
- }
- }
- }
- return true;
- }
- private bool validatePosOnBoard (int[]board, int position)
- {
- if (board [position] == 0 || board [position] == id)
- return true;
- else
- return false;
- }
- public override string ToString ()
- {
- return string.Format ("ID:" + id.ToString () + " Pos: " + "[" + pos [0].ToString () + "," + pos [1].ToString () + "]" + " type: " + typ.ToString () + " Direction: " + dir.ToString () + " Red: " + red);
- }
- object ICloneable.Clone ()
- {
- return this.Clone ();
- }
- public IDSCar Clone ()
- {
- return new IDSCar (id, pos [0], pos [1], typ, dir, red, lastMove);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement