I constantly get the following message in my output/debug windows. The app doesn't crash but I was wondering what the deal with it is:
A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
my code :sol.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Sol
{
public LinkedList<int> tower1 = new LinkedList<int>();
public LinkedList<int> tower2 = new LinkedList<int>();
public LinkedList<int> tower3 = new LinkedList<int>();
public static LinkedList<string> BFS = new LinkedList<string>();
public static LinkedList<string> DFS = new LinkedList<string>();
public static LinkedList<string> IDS = new LinkedList<string>();
public int depth;
public LinkedList<Sol> neighbors;
public Sol()
{
}
public Sol(LinkedList<int> tower1, LinkedList<int> tower2, LinkedList<int> tower3)
{
this.tower1 = tower1;
this.tower2 = tower2;
this.tower3 = tower3;
neighbors = new LinkedList<Sol>();
}
public virtual void getneighbors()
{
Sol temp = this.copy();
Sol neighbor1 = this.copy();
Sol neighbor2 = this.copy();
Sol neighbor3 = this.copy();
Sol neighbor4 = this.copy();
Sol neighbor5 = this.copy();
Sol neighbor6 = this.copy();
if (temp.tower1.Count != 0)
{
if (neighbor1.tower2.Count != 0)
{
if (neighbor1.tower1.First.Value < neighbor1.tower2.First.Value)
{
neighbor1.tower2.AddFirst(neighbor1.tower1.First);
neighbor1.tower1.RemoveFirst();
neighbors.AddLast(neighbor1);
}
}
else
{
neighbor1.tower2.AddFirst(neighbor1.tower1.First);
neighbor1.tower1.RemoveFirst();
neighbors.AddLast(neighbor1);
}
if (neighbor2.tower3.Count != 0)
{
if (neighbor2.tower1.First.Value < neighbor2.tower3.First.Value)
{
neighbor2.tower3.AddFirst(neighbor2.tower1.First);
neighbor2.tower1.RemoveFirst();
neighbors.AddLast(neighbor2);
}
}
else
{
neighbor2.tower3.AddFirst(neighbor2.tower1.First);
neighbor2.tower1.RemoveFirst();
neighbors.AddLast(neighbor2);
}
}
//-------------
if (temp.tower2.Count != 0)
{
if (neighbor3.tower1.Count != 0)
{
if (neighbor3.tower2.First.Value < neighbor3.tower1.First.Value)
{
neighbor3.tower1.AddFirst(neighbor3.tower2.First);
neighbor3.tower2.RemoveFirst();
neighbors.AddLast(neighbor3);
}
}
else
{
neighbor3.tower1.AddFirst(neighbor3.tower2.First);
neighbor3.tower2.RemoveFirst();
neighbors.AddLast(neighbor3);
}
if (neighbor4.tower3.Count != 0)
{
if (neighbor4.tower2.First.Value < neighbor4.tower3.First.Value)
{
neighbor4.tower3.AddFirst(neighbor4.tower2.First);
neighbor4.tower2.RemoveFirst();
neighbors.AddLast(neighbor4);
}
}
else
{
neighbor4.tower3.AddFirst(neighbor4.tower2.First);
neighbor4.tower2.RemoveFirst();
neighbors.AddLast(neighbor4);
}
}
//------------------------
if (temp.tower3.Count() != 0)
{
if (neighbor5.tower1.Count() != 0)
{
if (neighbor5.tower3.ElementAtOrDefault(0) < neighbor5.tower1.ElementAtOrDefault(0))
{
neighbor5.tower1.AddFirst(neighbor5.tower3.First);
neighbor5.tower3.RemoveFirst();
neighbors.AddLast(neighbor5);
}
}
else
{
neighbor5.tower1.AddFirst(neighbor5.tower3.First);
neighbor5.tower3.RemoveFirst();
neighbors.AddLast(neighbor5);
}
if (neighbor6.tower2.Count() != 0)
{
if (neighbor6.tower3.ElementAtOrDefault(0) < neighbor6.tower2.ElementAtOrDefault(0))
{
neighbor6.tower2.AddFirst(neighbor6.tower3.First);
neighbor6.tower3.RemoveFirst();
neighbors.AddLast(neighbor6);
}
}
else
{
neighbor6.tower2.AddFirst(neighbor6.tower3.First);
neighbor6.tower3.RemoveFirst();
neighbors.AddLast(neighbor6);
}
}
}
public override string ToString()
{
string str;
str = "tower1" + tower1.ToString() + " tower2" + tower2.ToString() + " tower3" + tower3.ToString();
return str;
}
public Sol copy()
{
Sol So;
LinkedList<int> l1 = new LinkedList<int>();
LinkedList<int> l2 = new LinkedList<int>();
LinkedList<int> l3 = new LinkedList<int>();
for (int i = 0; i <= this.tower1.Count() - 1; i++)
{
l1.AddLast(tower1.ElementAt(i));
}
for (int i = 0; i <= this.tower2.Count - 1; i++)
{
l2.AddLast(tower2.ElementAt(i));
}
for (int i = 0; i <= this.tower3.Count - 1; i++)
{
l3.AddLast(tower3.ElementAt(i));
}
So = new Sol(l1, l2, l3);
return So;
}
public bool Equals(Sol sol)
{
if (this.tower1.Equals(sol.tower1) & this.tower2.Equals(sol.tower2) & this.tower3.Equals(sol.tower3))
return true;
return false;
}
public virtual bool containedin(Stack<Sol> vec)
{
bool found = false;
for (int i = 0; i <= vec.Count - 1; i++)
{
if (vec.ElementAt(i).tower1.Equals(this.tower1) && vec.ElementAt(i).tower2.Equals(this.tower2) && vec.ElementAt(i).tower3.Equals(this.tower3))
{
found = true;
break;
}
}
return found;
}
public virtual bool breadthFirst(Sol start, Sol goal)
{
Stack<Sol> nextStack = new Stack<Sol>();
Stack<Sol> traversed = new Stack<Sol>();
bool found = false;
start.depth = 0;
nextStack.Push(start);
while (nextStack.Count != 0)
{
Sol sol = nextStack.Pop();
BFS.AddFirst("poped State:" + sol.ToString() + "level " + sol.depth);
traversed.Push(sol);
if (sol.Equals(goal))
{
found = true;
BFS.AddFirst("Goal:" + sol.ToString());
break;
}
else
{
sol.getneighbors();
foreach (Sol neighbor in sol.neighbors)
{
if (!neighbor.containedin(traversed) && !neighbor.containedin(nextStack))
{
neighbor.depth = (sol.depth + 1);
nextStack.Push(neighbor);
}
}
}
}
return found;
}
public virtual bool depthFirst(Sol start, Sol goal)
{
Stack<Sol> nextStack = new Stack<Sol>();
Stack<Sol> traversed = new Stack<Sol>();
bool found = false;
start.depth = 0;
nextStack.Push(start);
while (nextStack.Count != 0)
{
//Dequeue next State for comparison
//And add it 2 list of traversed States
Sol sol = nextStack.Pop();
DFS.AddFirst("poped State:" + sol.ToString() + "level " + sol.depth);
traversed.Push(sol);
if (sol.Equals(goal))
{
found = true;
DFS.AddFirst("Goal:" + sol.ToString());
break;
}
else
{
sol.getneighbors();
foreach (Sol neighbor in sol.neighbors)
{
if (!neighbor.containedin(traversed) && !neighbor.containedin(nextStack))
{
neighbor.depth = sol.depth + 1;
nextStack.Push(neighbor);
}
}
}
}
return found;
}
public virtual bool iterativedeepening(Sol start, Sol goal)
{
bool found = false;
for (int level = 0; ; level++)
{
Stack<Sol> nextStack = new Stack<Sol>();
Stack<Sol> traversed = new Stack<Sol>();
start.depth = 0;
nextStack.Push(start);
while (nextStack.Count != 0)
{
Sol sol = nextStack.Pop();
IDS.AddFirst("poped State:" + sol.ToString() + "Level" + sol.depth);
traversed.Push(sol);
if (sol.Equals(goal))
{
found = true;
IDS.AddFirst("Goal:" + sol.ToString());
break;
}
else if (sol.depth < level)
{
sol.getneighbors();
foreach (Sol neighbor in sol.neighbors)
{
if (!neighbor.containedin(traversed) && !neighbor.containedin(nextStack))
{
neighbor.depth = sol.depth + 1;
nextStack.Push(neighbor);
} //end if
} //end for each
} //end else if
} // end while
if (found == true)
break;
} // end for
return found;
}
}
}
Just wondering if I may be doing something wrong somewhere or something.