private void gameButton_Click(object sender, EventArgs e) { try { if (!StoneCollection.Any()) { MessageBox.Show("Stone collection empty"); return; } stoneBindingSource.EndEdit(); List<int> values = StoneCollection.Select(obj => obj.Value).ToList(); GameService gameService = new GameService(); Combination firstCombination = new Combination(); firstCombination.ValuesCollection = values; firstCombination.RemovedValue = 0; firstCombination.Level = 0; firstCombination.Player = Player.None; firstCombination.CombinationList = gameService.PrepareTree(firstCombination); firstCombination.GameCombination = gameService.CurrentGameCombination(firstCombination.Player, firstCombination.CombinationList); TurnResult combination = firstCombination.GameCombination == TurnResult.Winning ? TurnResult.Losing : TurnResult.Winning; PrepareTree(firstCombination); MessageBox.Show(combination.ToString()); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void PrepareTree(Combination combination) { movesTreeView.Nodes.Clear(); TreeNode parentNode = new TreeNode(); string valuesCollection = "Collections at start "; foreach (int value in combination.ValuesCollection) { valuesCollection += string.Format("{0},", value); } parentNode.Text = valuesCollection.TrimEnd(','); AddNode(parentNode, combination); movesTreeView.Nodes.Add(parentNode); movesTreeView.ExpandAll(); }
public List<Combination> PrepareTree(Combination parrentCombination) { List<Combination> combinationCollection = new List<Combination>(); List<List<KeyValuePair<int, List<int>>>> pertumations = new List<List<KeyValuePair<int, List<int>>>>(); foreach (int value in parrentCombination.ValuesCollection) { if (value > 0) pertumations.Add(PermutationManager.Permutate(value, parrentCombination.ValuesCollection)); } foreach (List<KeyValuePair<int, List<int>>> permutation in pertumations) { foreach (KeyValuePair<int, List<int>> newValues in permutation) { Combination currentCombination = new Combination(); currentCombination.ValuesCollection = newValues.Value; currentCombination.RemovedValue = newValues.Key; currentCombination.Player = ChangePlayer(parrentCombination.Player); currentCombination.Level = parrentCombination.Level + 1; currentCombination.CombinationList = PrepareTree(currentCombination); if (currentCombination.CombinationList.Any()) { currentCombination.GameCombination = CurrentGameCombination(currentCombination.Player, currentCombination.CombinationList); } else { currentCombination.GameCombination = TurnResult.Winning; } combinationCollection.Add(currentCombination); if(currentCombination.GameCombination == TurnResult.Winning) { return combinationCollection; } } } return combinationCollection; }
private void OnNewMove(Combination combination) { if (NewMove != null) NewMove(combination); }
private void AddNode(TreeNode parentNode, Combination combination) { foreach (Combination childCombination in combination.CombinationList) { TreeNode childNode = new TreeNode(); childNode.Text = childCombination.ToString(); AddNode(childNode, childCombination); parentNode.Nodes.Add(childNode); } }