//attempts to guess groupings for ans public void GuessGroup(ShAnswer ans) { string anstxt = System.Text.RegularExpressions.Regex.Replace(ans.Text, "\\W", "").ToLower(); foreach (ShGroup grp in ans.Group.Question.Groups) { if (grp == ans.Group) { continue; } if (System.Text.RegularExpressions.Regex.Replace(grp.Text, "\\W", "").ToLower() == anstxt) { ans.ChangeGroup(grp); return; } } //didn't find any but let's try individual answers foreach (ShGroup grp in ans.Group.Question.Groups) { if (grp == ans.Group) { continue; } foreach (ShAnswer ans2 in grp.Answers) { if (System.Text.RegularExpressions.Regex.Replace(ans2.Text, "\\W", "").ToLower() == anstxt) { ans.ChangeGroup(grp); return; } } } }
//nicely add a player, using new_answers public ShPlayer NiceAddPlayer(string new_name, string[] new_answers) { ShPlayer newP = new ShPlayer(this, new_name); Players.Add(newP); for (int iques = 0; iques < Questions.Count; iques++) { //get text for new answers string newAnsTxt = "(blank)"; if (iques < new_answers.Length) { if (new_answers[iques] != "") { newAnsTxt = new_answers[iques]; } } ShGroup newG = new ShGroup(Questions[iques], newAnsTxt); Questions[iques].Groups.Add(newG); ShAnswer newA = new ShAnswer(newG, newP, newAnsTxt); newG.Answers.Add(newA); newP.Answers.Add(newA); } return(newP); }
//Constructor2 //initialize with an array of questions, players, answers public ShGame(string[] new_questions, string[] new_players, string[,] new_answers) { if (new_answers.GetLength(0) != new_questions.Length || new_answers.GetLength(1) != new_players.Length) { throw new Exception("Answer list must be size [num questions, num players]"); } Questions = new List <ShQuestion>(new_questions.Length); Players = new List <ShPlayer>(new_players.Length); Questions.AddRange(new_questions.Select(txt => new ShQuestion(this, txt))); Players.AddRange(new_players.Select(txt => new ShPlayer(this, txt))); for (int iques = 0; iques < new_questions.Length; iques++) { for (int iplayer = 0; iplayer < new_players.Length; iplayer++) { ShGroup new_group = new ShGroup(Questions[iques], new_answers[iques, iplayer]); Questions[iques].Groups.Add(new_group); ShAnswer new_answer = new ShAnswer(new_group, Players[iplayer], new_answers[iques, iplayer]); new_group.Answers.Add(new_answer); Players[iplayer].Answers.Add(new_answer); } } }
//nicely add a player, setting answers blank public ShPlayer NiceAddPlayer(string new_name) { ShPlayer newP = new ShPlayer(this, new_name); Players.Add(newP); for (int iques = 0; iques < Questions.Count; iques++) { ShGroup newG = new ShGroup(Questions[iques], "(blank)"); Questions[iques].Groups.Add(newG); ShAnswer newA = new ShAnswer(newG, newP, "(blank)"); newG.Answers.Add(newA); newP.Answers.Add(newA); } return(newP); }
//nicely add a question, giving each player a blank answer //and making one group public ShQuestion NiceAddQuestion(string qtxt) { ShQuestion newQ = new ShQuestion(this, qtxt); Questions.Add(newQ); if (Players.Count > 0) { ShGroup newG = new ShGroup(newQ, "(blank)"); newQ.Groups.Add(newG); foreach (ShPlayer ply in Players) { ShAnswer newA = new ShAnswer(newG, ply, "(blank)"); ply.Answers.Add(newA); newG.Answers.Add(newA); } } return(newQ); }
//right click menu - set group name to this answer private void RCM_answer_use_as_group_name_Click(object sender, EventArgs e) { TreeNode clicked_node = RCM_answer.Tag as TreeNode; if (clicked_node == null) { return; } ShAnswer ans = clicked_node.Tag as ShAnswer; if (ans == null) { return; } ans.Group.Text = ans.Text; sheep_modified = true; SetTextForAllTreenodes(); }
//right click menu - set bonus score for this answer private void RCM_answer_bonus_Click(object sender, EventArgs e) { TreeNode clicked_node = RCM_answer.Tag as TreeNode; if (clicked_node == null) { return; } ShAnswer ans = clicked_node.Tag as ShAnswer; if (ans == null) { return; } InputText IP = new InputText(); IP.Text = "Bonus Score"; IP.label1.Text = "Enter bonus score for " + ans.Player.Name + ":"; IP.textBox1.Text = ans.AnswerBonus.ToString(); IP.StartPosition = FormStartPosition.CenterParent; IP.ShowDialog(); if (IP.DialogResult == DialogResult.OK) { if (IP.textBox1.Text.Trim() == "") { ans.AnswerBonus = 0; } try { ans.AnswerBonus = Convert.ToInt32(IP.textBox1.Text); } catch { } sheep_modified = true; SetTextForAllTreenodes(); } }
//right click menu - create a new group with this answer private void RCM_move_to_new_group_Click(object sender, EventArgs e) { TreeNode clicked_node = RCM_answer.Tag as TreeNode; if (clicked_node == null) { return; } ShAnswer ans = clicked_node.Tag as ShAnswer; if (ans == null) { return; } ShGroup newGroup = ans.Group.Question.StartNewGroup(ans.Text); ans.ChangeGroup(newGroup); TreeNode prevParent = clicked_node.Parent; TreeNode newParent = treeView1.Nodes.Add("b"); newParent.Tag = newGroup; prevParent.Nodes.Remove(clicked_node); newParent.Nodes.Add(clicked_node); newParent.Expand(); //if prevParent is empty, delete it if (prevParent.Nodes.Count == 0) { prevParent.Remove(); } sheep_modified = true; SetTextForAllTreenodes(); }
private string TextForTreeNode(ShAnswer ans) { return(ans.Text + " - " + ans.Player.Name + (ans.AnswerBonus == 0?"":" (" + (ans.AnswerBonus > 0?"+":"") + ans.AnswerBonus + ")")); }
//main drag/drop function private void treeView1_DragDrop(object sender, DragEventArgs e) { //stop sorting while dragging treeView1.TreeViewNodeSorter = null; Point cp = treeView1.PointToClient(new Point(e.X, e.Y)); TreeNode destNode = treeView1.GetNodeAt(cp); //don't continue if not a valid node if (!e.Data.GetDataPresent(typeof(TreeNode))) { return; } // treeView1.BeginUpdate(); TreeNode movingNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); TreeNode prevParent, newParent; ShQuestion curQuestion = sg.Questions[cur_q_index]; //different code depending on what type of thing we're dragging/dragging to if (movingNode.Tag.GetType() == typeof(ShAnswer) && destNode.Tag.GetType() == typeof(ShAnswer)) { //moving an answer to another answer ShAnswer ansToMove = (ShAnswer)movingNode.Tag; ShAnswer destAnswer = (ShAnswer)destNode.Tag; ansToMove.ChangeGroup(destAnswer.Group); prevParent = movingNode.Parent; newParent = destNode.Parent; prevParent.Nodes.Remove(movingNode); newParent.Nodes.Add(movingNode); } else if (movingNode.Tag.GetType() == typeof(ShAnswer) && destNode.Tag.GetType() == typeof(ShGroup)) { //moving an answer to another group ShAnswer ansToMove = (ShAnswer)movingNode.Tag; ShGroup destGroup = (ShGroup)destNode.Tag; ansToMove.ChangeGroup(destGroup); prevParent = movingNode.Parent; newParent = destNode; prevParent.Nodes.Remove(movingNode); newParent.Nodes.Add(movingNode); } else if (movingNode.Tag.GetType() == typeof(ShGroup) && destNode.Tag.GetType() == typeof(ShAnswer)) { //moving a group to an answer ShGroup grpToMove = (ShGroup)movingNode.Tag; ShAnswer destAnswer = (ShAnswer)destNode.Tag; grpToMove.MergeToGroup(destAnswer.Group); prevParent = movingNode; newParent = destNode.Parent; List <TreeNode> ansNodes = new List <TreeNode>(prevParent.Nodes.Cast <TreeNode>()); foreach (TreeNode nod in ansNodes) { prevParent.Nodes.Remove(nod); newParent.Nodes.Add(nod); } } else if (movingNode.Tag.GetType() == typeof(ShGroup) && destNode.Tag.GetType() == typeof(ShGroup)) { //moving a group to a group ShGroup grpToMove = (ShGroup)movingNode.Tag; ShGroup destGroup = (ShGroup)destNode.Tag; grpToMove.MergeToGroup(destGroup); prevParent = movingNode; newParent = destNode; List <TreeNode> ansNodes = new List <TreeNode>(prevParent.Nodes.Cast <TreeNode>()); foreach (TreeNode nod in ansNodes) { prevParent.Nodes.Remove(nod); newParent.Nodes.Add(nod); } } else { treeView1.EndUpdate(); return; } //if prevParent is empty, delete it if (prevParent.Nodes.Count == 0) { prevParent.Remove(); } SetTextForAllTreenodes(); sheep_modified = true; treeView1.EndUpdate(); }
//read data from xmlreader public void ReadFromXML(XmlReader xr) { Questions.Clear(); Players.Clear(); Method = ShMethod.Sheep; Rounding = ShRoundingType.None; #region xrread while (xr.Read()) { if (xr.IsStartElement()) { switch (xr.Name) { case "ScoringMethod": Method = (ShMethod)Enum.Parse(typeof(ShMethod), xr.ReadElementString()); break; case "Rounding": Rounding = (ShRoundingType)Enum.Parse(typeof(ShRoundingType), xr.ReadElementString()); break; case "Question": int qindex = Convert.ToInt32(xr["GameIndex"]); while (Questions.Count < qindex + 1) { Questions.Add(new ShQuestion(this, "(blank)")); } Questions[qindex].Text = xr.ReadElementString(); break; case "Player": int pindex = Convert.ToInt32(xr["GameIndex"]); decimal start_score = Convert.ToDecimal(xr["StartScore"]); while (Players.Count < pindex + 1) { Players.Add(new ShPlayer(this, "(blank)", start_score)); } Players[pindex].Name = xr.ReadElementString(); break; case "Group": //assuming that question and player have already been //completely read in as they should be at the start //of the xml file int group_q_index = Convert.ToInt32(xr["QuestionIndex"]); bool tempcorrect = Convert.ToBoolean(xr["Correct"]); decimal tempgroupbonus = Convert.ToDecimal(xr["GroupBonus"]); var tempgroupbonustype = (ShBonusType)Enum.Parse(typeof(ShBonusType), xr["BonusType"]); ShGroup newGroup = new ShGroup(this.Questions[group_q_index], ""); newGroup.Correct = tempcorrect; newGroup.GroupBonus = tempgroupbonus; newGroup.BonusType = tempgroupbonustype; Questions[group_q_index].Groups.Add(newGroup); XmlReader subxr = xr.ReadSubtree(); while (subxr.Read()) { if (subxr.IsStartElement()) { switch (subxr.Name) { case "Text": newGroup.Text = subxr.ReadElementString(); break; case "Answer": int ans_p_index = Convert.ToInt32(subxr["PlayerIndex"]); decimal tempansbonus = Convert.ToDecimal(subxr["AnswerBonus"]); var tempansbonustype = (ShBonusType)Enum.Parse(typeof(ShBonusType), xr["BonusType"]); string anstext = xr.ReadElementString(); ShAnswer newAns = new ShAnswer(newGroup, Players[ans_p_index], anstext); newAns.AnswerBonus = tempansbonus; newAns.BonusType = tempansbonustype; newGroup.Answers.Add(newAns); Players[ans_p_index].Answers.Add(newAns); break; } } } break; } } } #endregion }