Skip to content

Commit

Permalink
Show actual score
Browse files Browse the repository at this point in the history
  • Loading branch information
nneonneo committed Mar 31, 2014
1 parent da361cf commit ec43bbe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
9 changes: 6 additions & 3 deletions 2048.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def find_best_move(m):
board = to_c_board(m)

print_board(to_val(m))
print "Current approx. score:", sum(_to_score(c) for row in m for c in row)

scores = pool.map(score_toplevel_move, [(board, move) for move in xrange(4)])
bestmove, bestscore = max(enumerate(scores), key=lambda x:x[1])
Expand Down Expand Up @@ -88,7 +87,6 @@ def rungame(args):
while 1:
state = gamectrl.get_status()
if state == 'ended':
print "Game over."
break
elif state == 'won':
time.sleep(0.75)
Expand All @@ -99,9 +97,14 @@ def rungame(args):
move = find_best_move(board)
if move < 0:
break
print "%010.6f: Move %d: %s" % (time.time() - start, moveno, movename(move))
print "%010.6f: Score %d, Move %d: %s" % (time.time() - start, gamectrl.get_score(), moveno, movename(move))
gamectrl.execute_move(move)

score = gamectrl.get_score()
board = gamectrl.get_board()
maxval = max(max(row) for row in to_val(board))
print "Game over. Final score %d; highest tile %d." % (score, maxval)

if __name__ == '__main__':
import sys
rungame(sys.argv[1:])
32 changes: 25 additions & 7 deletions gamectrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def execute(self, cmd):

def get_status(self):
''' Check if the game is in an unusual state. '''
return self.execute(
'''
return self.execute('''
var messageContainer = document.querySelector(".game-message");
if(messageContainer.className.search(/game-over/) !== -1) {"ended"}
else if(messageContainer.className.search(/game-won/) !== -1) {"won"}
Expand All @@ -61,11 +60,12 @@ def continue_game(self):
self.execute('document.querySelector(".keep-playing-button").click();')

def send_key_event(self, action, key):
return self.execute(
'''var keyboardEvent = document.createEvent("KeyboardEvent");'''
'''var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";'''
'''keyboardEvent[initMethod]("%s", true, true, window, false, false, false, false, %d, 0);'''
'''(document.body || document).dispatchEvent(keyboardEvent);''' % (action, key))
return self.execute('''
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod]("%s", true, true, window, false, false, false, false, %d, 0);
(document.body || document).dispatchEvent(keyboardEvent);
''' % (action, key))

class Fast2048Control(Generic2048Control):
''' Control 2048 by hooking the GameManager and executing its move() function.
Expand All @@ -90,6 +90,9 @@ def setup(self):

self.execute('GameManager.prototype.isGameTerminated = _func_tmp;')

def get_score(self):
return self.execute('GameManager._instance.score')

def get_board(self):
grid = self.execute('GameManager._instance.grid')

Expand Down Expand Up @@ -127,6 +130,21 @@ def setup(self):
}
''')

def get_score(self):
score = self.execute('''
var scoreContainer = document.querySelector(".score-container");
var scoreText = '';
var scoreChildren = scoreContainer.childNodes;
for(var i = 0; i < scoreChildren.length; ++i) {
if(scoreChildren[i].nodeType == Node.TEXT_NODE) {
scoreText += scoreChildren[i].textContent;
}
}
scoreText;
''')

return int(score)

def get_board(self):
res = self.execute(
'''
Expand Down

0 comments on commit ec43bbe

Please sign in to comment.