feb 12
clarify this week's assignment
The idea is to set up a realistic framework for a software project, using the Tron challenge as the problem to be solved.
1. Investigate code project hosting sites. Pick one; register; start in.
2. Download and get a tron robot running.
3. Put one of the tron bots into the project host site as "v0.01".
4. Write down a proposal on the project hosting site (wiki or docs or wherever) describing what you're going to do for the next month.
If there's time at the end of class,
we can log in and create a software project together
to see how this works. Or someone can do a demo next week.
Tron framework
The infrastructure includes :
- They suppl a java engine to run two robots against each other.
- (And cs now has jdk1.6 installed, so it'll run on cs.)
- Communication to/from robots is through read/print text.
- Game is displayed on command line as ascii text.
This runs on cs, but there are a few
things to be aware of.
cs.marlboro.edu Caveat 1 :
- The python example bots and tron.py presumes python2.5
- because they use the "any()" construct.
- But cs only has python2.4.
- However, the code rewrite required to fix that is simple.
cs.marlboro.edu Caveat 2 :
- The default PATH on cs doesn't include the current directory.
- Therefore, you can't just type "MyTronBot.py" and have it run.
- Instead, you'd need to type "./MyTronBot.py".
- So that's what you need to tell the java engine.
All this is in /var/www/cs/htdocs/courses/spring2010/programming/tron
which you can read but not edit; copy anything you want to
a directory under your home directory, or start from scratch.
$ ssh user@cs.marlboro.edu
password: *****
$ cd /var/www/cs/htdocs/courses/spring2010/programming/tron
$ ls -F
python/ python_starter_package.zip
You can see the code in this folder : tron .
The python_starter_package.zip is what came from
the waterloo folks. I unpacked it with
"unzip python_starter_package.zip" to python/ ;
however, I've made some changes and added a few things,
so the two don't match any more.
$ cd python
$ ls -F
MyTronBot.py*
engine/
example_bots/
freebot.py*
maps/
northbot.py*
randbot.py*
run*
test-java/ Jim: java compile/jar/run test
tron.py Jim: modified tron module for python2.4
tron.pyc compiled version of tron.py
wallbot.py* Jim: modified for python2.4
To see it work,
$ ./run
which is the same as
$ java -jar engine/Tron.jar \
maps/empty-room.txt \
"./MyTronBot.py" \
"./wallbot.py"
Discuss how all this fits together,
how the java stuff works (see test-java/ and .java, .class, .jar)
Or you can just work on your own machines.
(I had much less trouble getting everything
to work on my laptop, an up to date Mac OS X;
it's java and python were consistent with
the downloaded code, and it all just ran
as advertised.)
Issues to discuss :
- How can you debug a bot?
- Will "print" work ??
- What else is there?
- How can you make sure it's running fast enough?
- Does it maintain state between moves?
- Does it matter?
- How can you tell?
- What board information might you need?
- And how can you get at that?
- Strategies?
- ...
If there's time, look at the
tron.py file and the Board API.
Or put that off until next week.
generators
A generator is a function that remembers the point
in the function body where it last returned. Calling
a generator function a second (or nth) time jumps into
the middle of the function, with all local variables
intact from the last invocation.
So any function with a "yield" in it evaluates
to a "generator", which is sort of like an object.
To use the generator, either
- turn it into a list with "list(generator)"
- loop over it with "for x in generator:"
- test for membership (carefully) with "x in generator"
- get the next generated element with generator.next()
Generators always generate a sequence of things,
each of which is "returned" by a yield statement.
But the function doesn't actually finish when it
hits yield; instead, it suspends itself, and
then continues on when the next element of the sequence
is requested, until it hits yield again.
"""
An example of a python generator,
used to find Fibonacci numbers and
solve problem 2 from projecteuler.net.
$ python fibonacci.py
The sum of the even fibonacci numbers
less than 4000000 is 4613732.
Jim Mahoney | Feb 10 2010 | GPL
"""
def fibonacci(n):
""" Generate the terms in the Fibonacci sequence less than n.
>>> list(fibonacci(10))
[0, 1, 1, 2, 3, 5, 8]
>>> total = 0
>>> for i in fibonacci(10): total += i
>>> total
20
"""
# based on fibonacci_generator.py from
# http://en.literateprograms.org/Fibonacci_numbers_(Python)
a, b = 0, 1
yield a # [0,
while b < n:
yield b # 1, 1, 2, ... ]
a, b = b, a + b
def ProjectEulerProblem2():
n = int(4e6)
answer = sum([f for f in fibonacci(n) if f % 2 == 0])
print "The sum of the even fibonacci numbers "
print "less than %i is %i." % (n, answer)
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
ProjectEulerProblem2()
One of the advantages of using generators
is that it only spits out things as needed.
That means, for example, that you could use
a generator to represent the list of even numbers.
def evens():
x = 0
while True:
yield x
x += 2
However: use with caution; contains a number
of pitfalls for the unwary.
>>> 10 in evens()
True
>>> 5 in evens()
# ... infinite loop ... (Never gets to end so can't give False.)
>>> e = evens()
>>> 10 in e
True
>>> 6 in e
# ... another infinite loop ... (This generator is up to 12.)
Having fun yet? How about this :