Score counting.
Score counting.

file:a/Marker.cpp -> file:b/Marker.cpp
--- a/Marker.cpp
+++ b/Marker.cpp
@@ -5,7 +5,7 @@
 int Marker::_count=0;
 
 Marker::Marker(Ogre::SceneManager *sceneMngr,Ogre::Vector3 position,Ogre::String type)
-  : _sceneMngr(sceneMngr)
+  : _sceneMngr(sceneMngr),_type(type)
 {   
   _count++;
   _markerEntity=sceneMngr->createEntity(type+Ogre::StringConverter::toString(_count),type+".mesh");
@@ -17,4 +17,5 @@
   _markerNode->roll(Ogre::Degree(30));
 }
 
+Ogre::String Marker::type() { return _type; }
 

file:a/Marker.h -> file:b/Marker.h
--- a/Marker.h
+++ b/Marker.h
@@ -8,8 +8,11 @@
 public:
   Marker(Ogre::SceneManager *sceneMngr,Ogre::Vector3 position,Ogre::String type);
 
+  Ogre::String type();
 private:
   static int _count;
+
+  Ogre::String _type;
 
   Ogre::SceneManager *_sceneMngr;
 

file:a/TTT.cpp -> file:b/TTT.cpp
--- a/TTT.cpp
+++ b/TTT.cpp
@@ -4,7 +4,10 @@
 
 TTT::TTT()
   : App()
-{ }
+{ 
+  _score["lemon"]=0;
+  _score["beer"]=0;
+}
 
 void TTT::setupScene()
 {
@@ -160,22 +163,27 @@
   code|=(codeforpos(pos.x)<<0);
   code|=(codeforpos(pos.y)<<3);
   code|=(codeforpos(pos.z)<<6);
-  printf("%d: %f %f %f\n",code,pos.x,pos.y,pos.z);
-  printf("count %d\n",_markers.count(code));
+  //printf("%d: %f %f %f\n",code,pos.x,pos.y,pos.z);
+  //printf("count %d\n",_markers.count(code));
 
   if(_markers.count(code)>0) 
-    {
-      printf("%p\n",_markers[code]);
-      return;}
+    return;
 
   Ogre::String type=_cursor->type();
   Marker *marker=new Marker(sceneMngr(),pos,type);
   _markers[code]=marker;
 
+  updateScore(type,code);
+
+  if(_score[type]>=2)
+    {
+      printf("%s WINS!!!!\n",type.c_str());
+    }
+
   if(type=="lemon")
     _cursor->setType("beer");
   else
-    _cursor->setType("lemon");
+  _cursor->setType("lemon");
 }
 
 unsigned int TTT::codeforpos(int x)
@@ -186,3 +194,64 @@
   return 0;
 }
 
+void TTT::updateScore(Ogre::String type,unsigned int code)
+{
+  unsigned int points=0;
+  //printf("updateScore\n");
+  for(std::map<unsigned int,Marker * >::const_iterator iter1=_markers.begin();
+      iter1!=_markers.end();
+      iter1++)
+    {
+      if(iter1->second->type()!=type) continue;
+      for(std::map<unsigned int, Marker * >::const_iterator iter2=_markers.begin();
+	  iter2!=_markers.end();
+	  iter2++)
+	{
+	  if(iter1->first==iter2->first) continue;
+	  if(iter2->second->type()!=type) continue;
+
+	  //printf("%d %d %d\n",iter1->first,iter2->first,code);
+	  unsigned int configuration=iter1->first|iter2->first|code;
+	  unsigned int sumx=sum3(configuration);
+	  unsigned int sumy=sum3(configuration>>3);
+	  unsigned int sumz=sum3(configuration>>6);
+	  //printf("sumx: %d\n",sumx);
+	  //printf("sumy: %d\n",sumy);
+	  //printf("sumz: %d\n",sumz);
+	  if(sumx==3 && ((sumy==1&&sumz==1) or (sumy==3&&sumz==1) or (sumy==1&&sumz==3)))
+	    {
+	      //printf("POINT!\n");
+	      points++;
+	    }
+	  if(sumy==3 && ((sumx==1&&sumz==1) or (sumx==3&&sumz==1) or (sumx==1&&sumz==3)))
+	    {
+	      //printf("POINT!\n");
+	      points++;
+	    }
+	  if(sumz==3 && ((sumy==1&&sumx==1) or (sumy==3&&sumx==1) or (sumy==1&&sumx==3)))
+	    {
+	      //printf("POINT!\n");
+	      points++;
+	    }
+	  if(sumz==3 && sumy==3 && sumx==3)
+	    {
+	      //printf("POINT!\n");
+	      points++;
+	    }
+	}
+    }
+  points/=2; // Double counted because of the way the points were handled.
+  printf("%s got %d points\n",type.c_str(),points);
+
+  _score[type]+=points;
+}
+
+unsigned int TTT::sum3(unsigned int code)
+{
+  unsigned int b1=code&1;
+  unsigned int b2=(code>>1)&1;
+  unsigned int b3=(code>>2)&1;
+  printf("%d: %d %d %d\n",code,b1,b2,b3);
+  return b1+b2+b3;
+}
+

file:a/TTT.h -> file:b/TTT.h
--- a/TTT.h
+++ b/TTT.h
@@ -29,6 +29,7 @@
   OgreBites::SdkCameraMan *_cameraMan;
   Cursor *_cursor;
   std::map<unsigned int,Marker * > _markers;
+  std::map<Ogre::String,unsigned int > _score;
 
   void createGrid();
   void createCamera();
@@ -36,6 +37,9 @@
   void place();
 
   unsigned int codeforpos(int x);
+  void updateScore(Ogre::String type,unsigned int code);
+  unsigned int sum3(unsigned int code);
+
 };
 
 #endif // TTT_H_