#!/usr/bin/env python

import os
import sys
import requests
import re
from bot import RandomBot

def get_new_game_state(serverHost, key, numberOfTurns = '20', mode='training'):
    if(mode=='training'):
        params = { 'key': key, 'turns': numberOfTurns}
        r = requests.post(serverHost + '/api/training', params)

        if(r.status_code == 200):
            return r.json()
        else:
            print("Error when creating the game")
            print(r.text)
    else:
        pass

def move(url, direction):
    r = requests.post(url, {'dir': direction})
    return r.json()

def start(serverHost, key, bot, numberOfGames = 20):

    def play(state, gamesPlayed = 0):
        if (state['game']['finished']):
            gamesPlayed += 1
            print('Game finished: %d/%d' % (gamesPlayed, numberOfGames))

            if(gamesPlayed < numberOfGames):
                print('asking a new game')
                state = get_new_game_state(serverHost, key)
                play(state, gamesPlayed)
        else:
            url = state['playUrl']
            direction = bot.move(state)
            newState = move(url, direction)

            print("Playing turn %d with direction %s" % (state['game']['turn'], direction))
            play(newState, gamesPlayed)

    state = get_new_game_state(serverHost, key)
    print("Start: " + state['viewUrl'])
    play(state)

if __name__ == "__main__":
    if (len(sys.argv) > 3):
        start(sys.argv[1], sys.argv[2], RandomBot(), int(sys.argv[3]))
    else:
        print("Usage: %s <server> <key> <number-of-games-to-play>" % (sys.argv[0]))
        print('Example: %s http://localhost:9000 mySecretKey 20' % (sys.argv[0]))

@@ -111,33 +111,34 @@
 ame_state(server
-H
+_h
 ost, key, number
@@ -133,27 +133,29 @@
  key, number
-OfT
+_of_t
 urns = '20',
@@ -249,11 +249,13 @@
 mber
-OfT
+_of_t
 urns
@@ -288,17 +288,18 @@
 t(server
-H
+_h
 ost + '/
@@ -604,25 +604,26 @@
 start(server
-H
+_h
 ost, key, bo
@@ -627,27 +627,29 @@
  bot, number
-OfG
+_of_g
 ames = 20):%0A
@@ -670,25 +670,26 @@
 state, games
-P
+_p
 layed = 0):%0A
@@ -741,25 +741,26 @@
        games
-P
+_p
 layed += 1%0A 
@@ -803,25 +803,26 @@
 %25d' %25 (games
-P
+_p
 layed, numbe
@@ -818,27 +818,29 @@
 ayed, number
-OfG
+_of_g
 ames))%0A%0A    
@@ -851,25 +851,26 @@
     if(games
-P
+_p
 layed %3C numb
@@ -875,11 +875,13 @@
 mber
-OfG
+_of_g
 ames
@@ -967,33 +967,34 @@
 ame_state(server
-H
+_h
 ost, key)%0A      
@@ -1016,25 +1016,26 @@
 state, games
-P
+_p
 layed)%0A     
@@ -1133,17 +1133,18 @@
      new
-S
+_s
 tate = m
@@ -1276,17 +1276,18 @@
 play(new
-S
+_s
 tate, ga
@@ -1289,17 +1289,18 @@
 e, games
-P
+_p
 layed)%0A%0A
@@ -1340,9 +1340,10 @@
 rver
-H
+_h
 ost,

Pythonize variable names