private void OnClickGenerate(object sender, EventArgs e) { int n = Convert.ToInt32(_nudNValue.Value); _pegPuzzle = new PegPuzzle(n); DisplayPuzzle(_pegPuzzle); _btnGenerate.Enabled = false; _btnStartPoint.Enabled = true; }
private void DisplayPuzzle(PegPuzzle pegPuzzle) { double rows = _pegPuzzle.NValue; double columns = (_pegPuzzle.NValue * 2) - 1; _tblPegBoard.RowCount = (int)rows; _tblPegBoard.ColumnCount = (int)columns; int centerColumn = Convert.ToInt32(Math.Ceiling(columns / 2)) - 1; for (int i = 0; i < rows; i++) { // keep track of what order pegs are placed in. Dictionary <int, KeyValuePair <int, int> > pegInsertionDictionary = new Dictionary <int, KeyValuePair <int, int> >(); int pegInsertionKey = 1; // i will also denote how many pegs to place this level int pegsToPlace = i + 1; if (i % 2 == 0) // add center peg { pegsToPlace--; pegInsertionDictionary.Add(pegInsertionKey++, new KeyValuePair <int, int>(i, centerColumn)); } int rightPegsToPlace = pegsToPlace / 2; int leftPegsToPlace = rightPegsToPlace; // add pegs to right side every 2 columns for (int j = centerColumn + 1; j < columns; j += 2) { if (i % 2 == 0 && j == centerColumn + 1) { j++; //shift over to the right since we added to center } if (rightPegsToPlace > 0) { rightPegsToPlace--; pegInsertionDictionary.Add(pegInsertionKey++, new KeyValuePair <int, int>(i, j)); } } // add pegs to left side every 2 columns for (int j = centerColumn - 1; j >= 0; j -= 2) { if (i % 2 == 0 && j == centerColumn - 1) { j--; //shift over to the left since we added to center } if (leftPegsToPlace > 0) { leftPegsToPlace--; pegInsertionDictionary.Add(pegInsertionKey++, new KeyValuePair <int, int>(i, j)); } } // order will matter later when we assign names to the pegs, so order from lowest to highest column IEnumerable <KeyValuePair <int, KeyValuePair <int, int> > > query = pegInsertionDictionary.OrderBy(kvp => kvp.Value.Value); foreach (KeyValuePair <int, KeyValuePair <int, int> > kvp in query) { PlacePeg(kvp.Value.Value, kvp.Value.Key); } } AssignNamesToPegs(); }
private void DisplayPuzzle(PegPuzzle pegPuzzle) { double rows = _pegPuzzle.NValue; double columns = (_pegPuzzle.NValue * 2) - 1; _tblPegBoard.RowCount = (int)rows; _tblPegBoard.ColumnCount = (int)columns; int centerColumn = Convert.ToInt32(Math.Ceiling(columns/2)) - 1; for (int i = 0; i < rows; i++) { // keep track of what order pegs are placed in. Dictionary<int, KeyValuePair<int, int>> pegInsertionDictionary = new Dictionary<int, KeyValuePair<int, int>>(); int pegInsertionKey = 1; // i will also denote how many pegs to place this level int pegsToPlace = i + 1; if (i % 2 == 0) // add center peg { pegsToPlace--; pegInsertionDictionary.Add(pegInsertionKey++, new KeyValuePair<int,int>(i, centerColumn)); } int rightPegsToPlace = pegsToPlace / 2; int leftPegsToPlace = rightPegsToPlace; // add pegs to right side every 2 columns for (int j = centerColumn + 1; j < columns; j+=2) { if (i % 2 == 0 && j == centerColumn + 1) j++; //shift over to the right since we added to center if (rightPegsToPlace > 0) { rightPegsToPlace--; pegInsertionDictionary.Add(pegInsertionKey++, new KeyValuePair<int, int>(i, j)); } } // add pegs to left side every 2 columns for (int j = centerColumn-1; j >= 0; j -= 2) { if (i % 2 == 0 && j == centerColumn - 1) j--; //shift over to the left since we added to center if (leftPegsToPlace > 0) { leftPegsToPlace--; pegInsertionDictionary.Add(pegInsertionKey++, new KeyValuePair<int, int>(i, j)); } } // order will matter later when we assign names to the pegs, so order from lowest to highest column IEnumerable<KeyValuePair<int, KeyValuePair<int, int>>> query = pegInsertionDictionary.OrderBy(kvp => kvp.Value.Value); foreach (KeyValuePair<int, KeyValuePair<int, int>> kvp in query) { PlacePeg(kvp.Value.Value, kvp.Value.Key); } } AssignNamesToPegs(); }
static public List <GraphNode <Dictionary <KeyValuePair <int, int>, int> > > Search(PegPuzzle pegPuzzle) { Stack <GraphNode <Dictionary <KeyValuePair <int, int>, int> > > stack = new Stack <GraphNode <Dictionary <KeyValuePair <int, int>, int> > >(); //filo Dictionary <KeyValuePair <int, int>, int> rootState = pegPuzzle.Board; Dictionary <KeyValuePair <int, int>, int> goalState = pegPuzzle.Goal; Graph <Dictionary <KeyValuePair <int, int>, int> > moves = pegPuzzle.Moves; var rootNode = new GraphNode <Dictionary <KeyValuePair <int, int>, int> >(rootState); rootNode.Neighbors = new GraphNodeList <Dictionary <KeyValuePair <int, int>, int> >(); moves.Nodes.Add(rootNode); stack.Push(moves.Nodes.Root()); while (stack.Count != 0) { GraphNode <Dictionary <KeyValuePair <int, int>, int> > currentNode = stack.Pop(); bool isGoalState = currentNode.Value.DictionaryEqual(goalState); if (isGoalState) { stack.Clear(); List <GraphNode <Dictionary <KeyValuePair <int, int>, int> > > solutionList = new List <GraphNode <Dictionary <KeyValuePair <int, int>, int> > >(); while (currentNode.Parent != null) { solutionList.Add(currentNode); currentNode = currentNode.Parent; } solutionList.Add(pegPuzzle.Moves.Nodes.Root()); return(solutionList); } pegPuzzle.GenerateGraph(currentNode); if (currentNode == null || currentNode.Neighbors == null) { return(null); } for (int i = 0; i < currentNode.Neighbors.Count; i++) { stack.Push(currentNode.Neighbors.ElementAt(i)); currentNode.Neighbors.ElementAt(i).Parent = currentNode; } } return(null); }