[dasher] Move searching for target in game mode from DasherModel to DasherNode



commit 8759d4af1b51bce760a7c1554419188f9c4a938e
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Sat Aug 22 17:23:26 2009 +0100

    Move searching for target in game mode from DasherModel to DasherNode
    and AlphabetManager.

 ChangeLog                          |    2 +
 Src/DasherCore/AlphabetManager.cpp |   13 +++++++
 Src/DasherCore/AlphabetManager.h   |    2 +
 Src/DasherCore/DasherModel.cpp     |   68 +++++------------------------------
 Src/DasherCore/DasherNode.cpp      |    7 ++++
 Src/DasherCore/DasherNode.h        |    8 ++++
 Src/DasherCore/NodeManager.h       |    7 ++++
 7 files changed, 49 insertions(+), 58 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 636f6a0..fe56156 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,8 @@
 	* Move iOffset field into DasherNode (now m_iOffset).
 	* Remove SControlData structure and use its single member directly.
 	* AlphabetManager.cpp: Move node creation outside of if statement.
+	* Move searching for target in game mode from DasherModel to DasherNode
+	  and AlphabetManager.
 
 2009-08-21  Alan Lawrence <acl33 inf phy cam ac uk>
 
diff --git a/Src/DasherCore/AlphabetManager.cpp b/Src/DasherCore/AlphabetManager.cpp
index 069bfee..ddd8eb7 100644
--- a/Src/DasherCore/AlphabetManager.cpp
+++ b/Src/DasherCore/AlphabetManager.cpp
@@ -125,6 +125,19 @@ CDasherNode *CAlphabetManager::GetRoot(CDasherNode *pParent, int iLower, int iUp
   return pNewNode;
 }
 
+bool CAlphabetManager::GameSearchNode(CDasherNode *pNode, string strTargetUtf8Char) {
+  if (pNode->GetFlag(NF_SUBNODE)) {
+    if (pNode->GameSearchChildren(strTargetUtf8Char)) {
+      pNode->SetFlag(NF_GAME, true);
+      return true;
+    }
+  } else if (m_pNCManager->GetAlphabet()->GetText(static_cast<SAlphabetData *>(pNode->m_pUserData)->iSymbol) == strTargetUtf8Char) {
+    pNode->SetFlag(NF_GAME, true);
+    return true;
+  }
+  return false;
+}
+
 void CAlphabetManager::PopulateChildren( CDasherNode *pNode ) {
   PopulateChildrenWithSymbol( pNode, -2, 0 );
 }
diff --git a/Src/DasherCore/AlphabetManager.h b/Src/DasherCore/AlphabetManager.h
index 55b418d..cc71be8 100644
--- a/Src/DasherCore/AlphabetManager.h
+++ b/Src/DasherCore/AlphabetManager.h
@@ -86,6 +86,8 @@ namespace Dasher {
 
     virtual void SetFlag(CDasherNode *pNode, int iFlag, bool bValue);
 
+    virtual bool GameSearchNode(CDasherNode *pNode, std::string strTargetUtf8Char);
+    
     struct SAlphabetData {
       symbol iSymbol;
       int iPhase;
diff --git a/Src/DasherCore/DasherModel.cpp b/Src/DasherCore/DasherModel.cpp
index 7fd9af7..a50533a 100644
--- a/Src/DasherCore/DasherModel.cpp
+++ b/Src/DasherCore/DasherModel.cpp
@@ -693,66 +693,18 @@ void CDasherModel::Push_Node(CDasherNode *pNode) {
 
   GameMode::CDasherGameMode* pTeacher = GameMode::CDasherGameMode::GetTeacher();
   if(m_bGameMode && pNode->GetFlag(NF_GAME) && pTeacher )
-    {
-      std::string strTargetUtf8Char;
-      
-      strTargetUtf8Char = pTeacher->GetSymbolAtOffset(pNode->m_iOffset + 1);
-      
-      CDasherNode::ChildMap::iterator i, j;
-      // Check if this is the last node in the sentence...
-      if(strTargetUtf8Char == "GameEnd")
-	{
-	  pNode->SetFlag(NF_END_GAME, true);
-	  goto multibreak;
-	}
-      CAlphabetManager::SAlphabetData * pAlphabetData;
-      // ...if it is not then find which child is next in line.
-      for(i = pNode->Children().begin(); i != pNode->Children().end(); i++)
-	{
-	  // Only look for children who are the same type of node as pNode is (i.e. alphabet)
-	  if((*i)->m_pNodeManager != pNode->m_pNodeManager) continue;
-	  
-	  // Look at the children of the group nodes while we search
-	  if((*i)->GetFlag(NF_SUBNODE))
-	    {
-	      for(j = (*i)->Children().begin(); j != (*i)->Children().end(); j++)
-		{
-		  std::string strNodeUtf8Char;
-		  CDasherNode * pTempNode = (*j);
-		  
-		  pAlphabetData = static_cast<CAlphabetManager::SAlphabetData *>(pTempNode->m_pUserData);
-		  strNodeUtf8Char = m_pNodeCreationManager->GetAlphabet()->GetText(pAlphabetData->iSymbol);
-		  
-		  if(strNodeUtf8Char == strTargetUtf8Char)
-		    {
-		      (*j)->SetFlag(NF_GAME, true);
-		      (*i)->SetFlag(NF_GAME, true);
-		      goto multibreak;
-		    }
-		}
-	    }
-	  else
-	    {
-	      std::string strNodeUtf8Char;
-	      CDasherNode * pTempNode = (*i);
-
-	      pAlphabetData = static_cast<CAlphabetManager::SAlphabetData *>(pTempNode->m_pUserData);
-	      strNodeUtf8Char = m_pNodeCreationManager->GetAlphabet()->GetText(pAlphabetData->iSymbol);
-
-	      if(strNodeUtf8Char == strTargetUtf8Char)
-		{
-		  (*i)->SetFlag(NF_GAME, true);
-		  goto multibreak;
-		}	 
-	    }
-	}
-      // If this line runs then we have a target character which is not in our current alphabet.
-      // So let's give up!
-      pNode->SetFlag(NF_END_GAME, true);
+  {
+    std::string strTargetUtf8Char(pTeacher->GetSymbolAtOffset(pNode->m_iOffset + 1));
       
-    multibreak:
-      ; // A lonely semicolon is the null statement - it does nothing.
+    // Check if this is the last node in the sentence...
+    if(strTargetUtf8Char == "GameEnd")
+	    pNode->SetFlag(NF_END_GAME, true);
+	  else if (!pNode->GameSearchChildren(strTargetUtf8Char)) {
+      // Target character not found - not in our current alphabet?!?!
+      // Let's give up!
+      pNode->SetFlag(NF_END_GAME, true); 
     }
+  }
   ////////////////////////////
   
 
diff --git a/Src/DasherCore/DasherNode.cpp b/Src/DasherCore/DasherNode.cpp
index a8ad1fa..d1a920a 100644
--- a/Src/DasherCore/DasherNode.cpp
+++ b/Src/DasherCore/DasherNode.cpp
@@ -219,3 +219,10 @@ int CDasherNode::MostProbableChild() {
   
   return iMax;
 }
+
+bool CDasherNode::GameSearchChildren(string strTargetUtf8Char) {
+  for (ChildMap::iterator i = Children().begin(); i != Children().end(); i++) {
+    if ((*i)->m_pNodeManager->GameSearchNode((*i), strTargetUtf8Char)) return true;
+  }
+  return false;
+}
diff --git a/Src/DasherCore/DasherNode.h b/Src/DasherCore/DasherNode.h
index fa14f99..586a782 100644
--- a/Src/DasherCore/DasherNode.h
+++ b/Src/DasherCore/DasherNode.h
@@ -223,6 +223,14 @@ class Dasher::CDasherNode:private NoClones {
   ///
   void Delete_children();
   /// @}
+  
+  ///
+  /// Sees if a *child* / descendant of the specified node (not that node itself)
+  /// represents the specified character. If so, set the child & intervening nodes'
+  /// NF_SUBNODE flag, and return true; otherwise, return false.
+  ///
+  bool GameSearchChildren(std::string strTargetUtf8Char);
+  
 
   /// \todo Make private, read only access?
   // leave public, or implement a get method - pconlon
diff --git a/Src/DasherCore/NodeManager.h b/Src/DasherCore/NodeManager.h
index a51b169..c745403 100644
--- a/Src/DasherCore/NodeManager.h
+++ b/Src/DasherCore/NodeManager.h
@@ -114,6 +114,13 @@ namespace Dasher {
     virtual void SetFlag(CDasherNode *pNode, int iFlag, bool bValue) {};
 
     virtual void SetControlOffset(CDasherNode *pNode, int iOffset) {};
+    
+    ///
+    /// See if this node, or *if an NF_SUBNODE* a descendant (recursively),
+    /// represents the specified alphanumeric character; if so, set it's NF_GAME flag and
+    /// return true; otherwise, return false.
+    ///
+    virtual bool GameSearchNode(CDasherNode *pNode, std::string strTargetUtf8Char) {return false;}
 
   private:
     int m_iID;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]