public static function db() {
    if (self::$db == NULL) {
			self::$db = new SQLiteDatabase(":memory:"); //sqlite_open(":memory:");
      self::$db->createFunction('REGEXP', array('cw_sqlite','cw_sqlite_regex'), 2);
      self::loaddb(self::$db);
		}
		return self::$db;
  }
  function putItAllTogether() {
    $ocr = new cw_ocr;
    $w = new cw_words;
    $sql = new cw_sqlite;
    $starttime = time();
    $word='';
    $exceptions=0;
    $notready=true;
    while ($notready) {
      print "\n Waiting for game screen. ".((time()) - ($starttime))." Seconds elapsed.";
      try {
        $filename = $ocr->screenGrab();
        $ocr->patternTest($filename, CLOCKWORDS_ROOT."match_image.pat");
        $notready = false;
        @unlink($filename);
        print "\n Game screen found, let's play!";
      } catch (Exception $e) {
        $notready = true;
        @unlink($filename);
      }
      usleep(500000);
    }
    $stillplaying=0;
    while ($stillplaying < 10) {
      $filename = $ocr->screenGrab();

      try {
        
        if (empty($GLOBALS[CLOCKWORDS_CROP_X])) { $result = $ocr->setCoords($filename); }
        try {
          dpr(array('$stillplaying'=>$stillplaying,'$filename'=>$filename));
          $result = $ocr->patternTest($filename, CLOCKWORDS_ROOT."match3.pat");
          //$stillplaying = ($stillplaying > 0) ? $stillplaying-1 : 0;
        } catch (Exception $e) {
          dpr($e->getMessage());
          //$stillplaying++;
          usleep(500000);
          try {
            $result = $ocr->patternTest($filename, CLOCKWORDS_ROOT."level.pat");
            $stillplaying = 100;
          } catch (Exception $e) {
            //game has not ended...
          }
          }
        try {
          
          $desired = $ocr->doOCR($filename);
          dpr(array('$desired',$desired));
          try {
            //$word = $w->getWord($desired);
            $word = $sql->getAWord($desired);
            dpr(array('$word',$word));
            try {
              $this->type($word);
              $exceptions = ($exceptions > 0) ? $exceptions-1 : $exceptions;
            } catch (Exception $e) { throw $e; }
          } catch (Exception $e) { throw $e; }
        } catch (Exception $e) { throw $e; }
      } catch (Exception $e) { dpr(array($e->getMessage(),$e->getFile(),$e->getLine()));$exceptions++; }
      if ($exceptions > 10) {
        print("\n   too many exceptions!\n");break;
        }
      $sql->removeWord($word);
      $len = strlen($word);
      if ($len > 5) { usleep($len . "00000"); }//the longer the word, the longer we need to wait to reload letters
      
    }
    //die("\n   ".CLOCKWORDS_RUN_TIME." seconds are up\n");
    $this->cleanup();
  }
 public function setUp() {
   $this->cw_sqlite = cw_sqlite::instance();
   $test_cw_words = $this->cw_sqlite->cw_words();
   $this->TOTAL_WORDS = count($test_cw_words->allWords);
 }