#!/usr/bin/python
'''
lunar_lander.py
basic lunar lander simulation based on constraints found at:
http://www.cs.carleton.edu/faculty/dmusican/cs111s10/lunarlander.html
'''
import sys
class LunarLander(object):
'Lunar lander object'
def __init__(self):
self.altitude = 10
self.velocity = 4
self.fuel = 2
def getVelocity(self):
return self.velocity
def getAltitude(self):
return self.altitude
def getFuel(self):
return self.fuel
def thrust(self, fuel):
if fuel > self.fuel:
self.velocity -= fuel * 4
self.fuel = 0
self.velocity -= fuel * 4
self.fuel -= fuel
def tick(self, fuel):
self.thrust(fuel)
self.velocity += 2
self.altitude -= self .velocity
#check for landing or crash
if self.altitude <= 0 and self.velocity < 5:
print 'skrrttt~ Houston, we have landed. ~skrrtt~ Victory! \'Murica! Fuck yeah!'
self.restart()
elif self.altitude <= 0 and self.velocity > 4:
print 'Oh no, you crashed!'
self.restart()
def report(self):
print '\nAlt = %d Vel = %d Fuel = %d' % (self.getAltitude(),
self.getVelocity(),
self.getFuel())
def restart(self):
restart = raw_input('Replay? Y/N >')
if restart.lower() == 'y':
main()
sys.exit('game over!')
def main():
print 'Welcome to Lunar Lander!'
lander = LunarLander()
running = True
while running:
lander.report()
x = 0
x = input('input thrust: ')
lander.tick(x)
main()
Love At First Byte
a code chronicle.
Tuesday, December 11, 2012
Lunar Lander
Just made a lunar lander game thingy based on some of the constraints found at http://www.cs.carleton.edu/faculty/dmusican/cs111s10/lunarlander.html. /shruggies, took about 10 minutes.
codejournal
compiling binaries in the UNIX environment with gcc compiler... from the *nix command line! woot.
vim commands
-i just realized i ought to write my blogposts in vim from now on....
UNIX terminal stuff
permission groups, directory bits
the command I should never ever use: "rm -rf /" aka "recursively remove everything from the contents of root"
gcc complier stuff
makefiles
all:
clean:
etc
makefiles are utility scripts that are used by gcc to compile / clean your binaries in a specified way
gcc looks for these files (in order): GNUMakefile, Makefile, makefile
can call with a certain makefile in mind, e.g., makefile -f MyMakeFile <file to make>
vim commands
:q quit, save
:w save
:q! quit without saving
i : insert mode
esc : command mode
h, j, k, l : movement in command mode
w, e : skip to the beginning/end of next word
f, n : find next instance of the word, goto Next instance
A : move to end of line and insert mode (A = append?)
$ : move to end of line
gg, G : move to beginning/end of the file
-i just realized i ought to write my blogposts in vim from now on....
UNIX terminal stuff
permission groups, directory bits
the command I should never ever use: "rm -rf /" aka "recursively remove everything from the contents of root"
./executable : execute file in the current directory?
rm - remove file
mkdir - make directory
cp file file2 - make copy of file to file2name
ls -la - list directory -l = long (permissions), -a = all
touch <filename.ext> : create file
vim <filename optional> : enter vim, edit file or create file
cat : output contents of file
| : (pipes) -> redirect stdout of one program to stdin or another prog1 stdout | prog2 stdin
chmod (a+x) : change permissions... ??
chown : change owners
sudo : "superuser do" -> execute program with root authority [not safe]
gcc complier stuff
makefiles
all:
clean:
etc
makefiles are utility scripts that are used by gcc to compile / clean your binaries in a specified way
gcc looks for these files (in order): GNUMakefile, Makefile, makefile
can call with a certain makefile in mind, e.g., makefile -f MyMakeFile <file to make>
Wednesday, October 24, 2012
Progress. Whoa..
It's been a bit of a hiatus, but I've been at it the whole time. I'm finally starting to feel comfortable writing code. That sounds a bit silly when I say it out loud; I've been reading and writing code on a daily basis for several weeks now. And yet, I never felt a sense of mastery over the concept I was trying to model, or the solution I was trying to implement. I've been unable to craft 100% exactly what I want into a set of precise instructions, though I've caught a glimpse of what that feels like here and there.
So there's this set of math-oriented programming challenges on the interwebs, called Project Euler. I'm (slowly) completing them, one-by-one. A maths wizard I am not, by any means, and I constantly feel like I'm in over my head when working some of these problems. They get progressively harder in succession, and with every one of these I knock down, inevitable fist pumping ensues.
Here's problem #5:
Anyway, I haven't yet reached a solution for this one, but I'm close. Thus far, I've been able to write a small program to tackle the smaller problem: producing the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. This gives me the ability to test a small-scale solution before I blow it up for the actual problem (all digits 1-20).
In the first part, I'm trying to determine a starting number to begin testing for divisibility. I ended up multiplying all the numbers in the range of divisors and counting down from there. Why? I wanted to begin with a number that I knew already had to be divisible by all the members of the divisor set.... plus that was just the first method I could think of. This part could really use some work, generating a better guess is essential to having a fast iterative algorithm.
The second loop increments my guess counter, g, and then in a nested loop iterates through each element d in divisors, checking to see if they divide evenly. This is accomplished through the modulus operator (%), which instead of returning the result of a division, returns the remainder only. If a number passes all checks, I then store it in the variable ans, continuing on to see if I can find a smaller number.
I've been able to get it to spit out the number 2520 given a divisor list of the numbers 1 through 10, so I know it works. My current problem is when I try divisors from 11 to 20 my initial guess is too high. IDLE is giving me an error, I think the Python list object is unable to hold a range of numbers 670,442,572,800 digits long, and I wouldn't expect it to. That's quite big.
So I've gotta figure a workaround for that, but confidence abounds! Happy coding.
So there's this set of math-oriented programming challenges on the interwebs, called Project Euler. I'm (slowly) completing them, one-by-one. A maths wizard I am not, by any means, and I constantly feel like I'm in over my head when working some of these problems. They get progressively harder in succession, and with every one of these I knock down, inevitable fist pumping ensues.
Here's problem #5:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Now the cool thing about Euler problems is they are open-ended in their implementation. You can use any language, any model, and any algorithm, so long as you reach the correct solution. I really like this method of learning, as opposed to a problem set like pyschools. Pyschools does a lot of hand-holding in helping frame the problem and shape your program. Euler does not care about how you solve the problem in the slightest, and I think being 'stranded' in this way forces you to pull up your bootstraps and design your solution from the ground-up. I think us beginner programmers face an uneasiness once we step out of Tutorial Land and into Outer Problem Space. The only way to overcome this obstacle is to grab the nearest handhold and start climbing.
divisors = [2,3,4,5,6,7,8,9,10]
def euler_5(*divs):
start = divs[0]
for d in range(1, len(divs) + 1):
start *= d
g = 0
for n in range(start, 0, -divs[0]):
g += 1
check = 0
for d in divs:
check += n % d
if check == 0:
ans = n
print ans, g
euler_5(*divisors)
In the first part, I'm trying to determine a starting number to begin testing for divisibility. I ended up multiplying all the numbers in the range of divisors and counting down from there. Why? I wanted to begin with a number that I knew already had to be divisible by all the members of the divisor set.... plus that was just the first method I could think of. This part could really use some work, generating a better guess is essential to having a fast iterative algorithm.
The second loop increments my guess counter, g, and then in a nested loop iterates through each element d in divisors, checking to see if they divide evenly. This is accomplished through the modulus operator (%), which instead of returning the result of a division, returns the remainder only. If a number passes all checks, I then store it in the variable ans, continuing on to see if I can find a smaller number.
I've been able to get it to spit out the number 2520 given a divisor list of the numbers 1 through 10, so I know it works. My current problem is when I try divisors from 11 to 20 my initial guess is too high. IDLE is giving me an error, I think the Python list object is unable to hold a range of numbers 670,442,572,800 digits long, and I wouldn't expect it to. That's quite big.
So I've gotta figure a workaround for that, but confidence abounds! Happy coding.
Thursday, October 11, 2012
Githuh?
Trying to get the basic shell of my poke_clone working, not coming along as easy as I expected. In the process of trying to get it up on git so I can spam r/learnpython questions about my source, I found this little intro tut on how Git actually works as a VCS: Getting-Started-Git-Basics.
Tuesday, October 9, 2012
Scratchlist #1
So this is my scratchlist, which is a bunch of mental throw-up about stuff I'm working on, learning, and stuff I'm planning on learning next. Might as well write it down and keep track.
project: search engine
In the process oflaunching learning how to launch my basic search engine from Udacity CS101. Need to work on receiving and handling queries via forms and familiarize with Google AppEngine's framework. I do see a problem: I don't really know what I should use for my search corpus. Ideally, this would be some set of pages whose links eventually terminate, but I can't have my web crawler crawling an infinite # of links. I think I will have to implement some sort of 'depth-limiter' into the crawler to establish a stopping point for link-indexing. How? I don't know.
project: text-based Pokemon game clone
Main objective is practicing OOP-basics in Python. Perhaps this can also be a good app to experiment on later when I learn some socket-handling and want to make it into a multiplayer game. That'd really be cool, but I'll have to put that one on the back-burner right now.
project: blog platform
Developing in tandem with CS253 Web Applications class from udacity.com. This is a great project to learn database interaction with. I mean, a blog post is essentially a string of text, stored in a table somewhere, should be easy as pie, right? WRONG.
setting up my new domain!
I took the plunge and shelled out for my own domain. Probably a bit premature, but for now at least I have my own little playground.
the Command Line
I feel like science dog when I open terminal. Forcing myself to do accomplish as much as I know how to do with a CLI instead of gui's... the main reason for this is so many tutorial and educational resources out there basically expect such knowledge, and I don't feel comfortable just entering sudo commands without knowing what effect they'll have.
project: search engine
In the process of
project: text-based Pokemon game clone
Main objective is practicing OOP-basics in Python. Perhaps this can also be a good app to experiment on later when I learn some socket-handling and want to make it into a multiplayer game. That'd really be cool, but I'll have to put that one on the back-burner right now.
project: blog platform
Developing in tandem with CS253 Web Applications class from udacity.com. This is a great project to learn database interaction with. I mean, a blog post is essentially a string of text, stored in a table somewhere, should be easy as pie, right? WRONG.
setting up my new domain!
I took the plunge and shelled out for my own domain. Probably a bit premature, but for now at least I have my own little playground.
the Command Line
I feel like science dog when I open terminal. Forcing myself to do accomplish as much as I know how to do with a CLI instead of gui's... the main reason for this is so many tutorial and educational resources out there basically expect such knowledge, and I don't feel comfortable just entering sudo commands without knowing what effect they'll have.
Java
This is another thing I want to learn in order to better understand what I'm learning (huh? paradox). Lately I've come across a couple treasure troves of knowledge, all taught in java, which is a strictly-typed language unlike Python, which is dynamically-typed.
check out TheCherno's awesome gamedev tutorial series: http://www.youtube.com/watch?v=GFYT7Lqt1h8
check out TheCherno's awesome gamedev tutorial series: http://www.youtube.com/watch?v=GFYT7Lqt1h8
Tuesday, October 2, 2012
ಠ_ಠ
I don't understand Github.
edit: I sort of understand Github.
edit: I sort of understand Github.
Tuesday, September 25, 2012
I am science dog.
I'm taking the cs253 web development course on udacity. Coincidentally, the lectures are being delivered by Steve Huffman, one of the founders of reddit! He's really flying through the content, I'm struggling to keep up with a lot of the discussion. Steve tends to glaze over details that I get hung up on -- it's like following a kid through a narrow corridor. He keeps pausing, beckoning at you to follow, but the space gets ever narrower. Case in point, google app engine's request handlers, lambda functions, template variables, and more. My brain gets snagged on these and I have to take the time to research each one of them in turn, making sure I understand just to keep up with the exploding lexicon.
I am Science Dog. |
As discouraging as my pace has been, it's been motivating to hear from one of the people who shaped the internet into what it is today. One thing I really appreciate is Steve uses examples of things he learned while building his many web applications as lessons of what to do and what not to do. And I'm sure that at one point, he was just as clueless as I am. I hope.
Subscribe to:
Posts (Atom)