zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

test #30

Closed zzz6519003 closed 10 years ago

zzz6519003 commented 10 years ago

这个为了提高猜词准确性可能要用到传说中的机器学习的一些算法,感兴趣不过短时间估计做不出啥成果。 于是就用词频率暴力了。。

js可以很方便可以用jquery实现publisher/subscriber


    var addPlugin = function($) {
        /*! Tiny Pub/Sub - v0.7.0 - 2013-01-29
        * https://github.com/cowboy/jquery-tiny-pubsub
        * Copyright (c) 2013 "Cowboy" Ben Alman; Licensed MIT */
        var o = $({});

        $.subscribe = function() {
            o.on.apply(o, arguments);
        };

        $.unsubscribe = function() {
            o.off.apply(o, arguments);
        };

        $.publish = function() {
            o.trigger.apply(o, arguments);
        };
    }

核心逻辑就是

this.startGame = function() {
    $.subscribe('initGameDone', this.nextWord);
    $.subscribe('nextWordDone', this.guessWord);
    $.subscribe('nextWordError', this.checkTestFinished);
    $.subscribe('guessWordDone', this.checkWordCompleted);
    $.subscribe('guessWordFail', this.nextWord);
    $.subscribe('guessWordError', this.guessWord);
    $.subscribe('wordNotCompleted', this.guessWord);
    $.subscribe('wordCompleted', this.checkTestFinished);
    $.subscribe('testNotFinished', this.nextWord);
    $.subscribe('testFinished', this.getTestResults);
    $.subscribe('getTestResultsDone', this.submitTestResults);
    this.initGame();
}

每一个action都是类似的

 this.nextWord = function() {
  var word;
  positionWeGuess = 0;
  var postData = {
    "userId": userId,
    "action": action[1],
    "secret": secret
  };
  $.ajax({
    url: gameURL,
    type: 'POST',
    dataType: 'json',
    data: postData
  })
  .done(function(data) {
    word = data["word"];
    numberOfWordsTried = data["data"]["numberOfWordsTried"];
    numberOfGuessAllowedForThisWord = data["data"]["numberOfGuessAllowedForThisWord"];
    $.publish('nextWordDone');
  })
  .fail(function() {
    $.publish('nextWordError');
  })
};

猜完要检测是否达到条件

this.checkWordCompleted = function() {
  if (word.indexOf('\*') != -1) {
    $.publish('wordNotCompleted');
  } else {
    $.publish('wordCompleted');
  };
}

this.checkTestFinished = function() {
  if (numberOfWordsTried != 80) {
    $.publish('testNotFinished');
  } else {
    $.publish('testFinished');
  };
}