-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
108 lines (92 loc) · 3.64 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import tron_db
import tron
from Header import header,footer,validate_form
test_map = """
#########
# #
# 1 #
# #
#########
"""
#~ from worker import TS_MU
import worker
class CodeUpLoad(webapp.RequestHandler):
def post(self):
s = header();
name = self.request.get("name")
text = self.request.get("text").replace('\r\n','\n')
py = tron_db.Program.all().filter("name", name).get()
if not py:
py = tron_db.Program( name=name )
py.text = text
py.reset(worker.TS_MU)
try :
#~ compile(py.code,py.title,'exec')
game = tron.Game()
game.readmap(test_map)
game.add_player( py.text, py.name )
game.run()
if not game.errors[0]:
py.put()
s += "SUCCESS ! "+py.name+" is going to play soon !"
else:
s += "FAIL! "+py.name+" caused an error: '" + game.errors[0] + "\'"
except Exception, e:
s += "FAIL! "+py.name+" caused an exception: '" + str(e) + "\'"
#raise
s += footer()
self.response.out.write( s )
class CodeUpForm(webapp.RequestHandler):
def get(self):
res = header("submit a bot to the competition")
res += validate_form(1)
res += """
<br>
<table width=90%><tr><td>
<form name='upload' action='/up/load' method=post enctype='multipart/form-data' onsubmit='return valid();'>
<!--form name='upload' action='/up/load' method=post enctype='multipart/form-data'-->
<textarea name='text' rows='30' cols='60' title='your code goes here'>def turn(board,r,c): """+'\n '+"""return "N"</textarea>
<br>
<input name='name' title='botname'>
<input type='submit' value=' upload your bot '>
</td><td valign=top>
<br>
First of all: it's <a href='http://python.org'>Python</a> only, sorry for that<p>
A single function 'turn(board,r,c)' is called in each step,<br>
you are given the board(as a row-column grid[][]), and the position of your bot.<br>
you return the direction of your next move, one of ["N","E","S","W"] <p>
you'll die, if you<ul><li> hit a wall('#')<li>walk onto your own tail, or into another player('1','2','3',etc)</ul>
note, that since your code is running in a separate namespace,<br>
you'll have to address non-const global vars as 'global'<p>
some snippets:
<pre>
// roundabout
t = 0
DIRS=["N","E","S","W"]
def turn(board,r,c):
global t
t += 1
t %= 4
return DIRS[t]
STEPS={'N':[-1,0],'S':[1,0],'E':[0,-1],'W':[0,1]};
def walk(r,c,dir):
return r+STEPS[dir][0], c+STEPS[dir][1]
def passable(board,r,c):
if r<0 or c<0 or (r >= len(board)) or (c >= len(board[0])) or board[r][c] != ' ':
return False
</pre>
</td></tr></table><br>
"""
res += footer()
self.response.out.write(res)
def main():
application = webapp.WSGIApplication([
('/up/form', CodeUpForm),
('/up/load', CodeUpLoad),
], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()