from random import randint from math import sqrt class AirLocation(object): def __init__(self, rows, x = None, y = None): self.rows=rows self.miles=0 if x is None and y is None: x = randint(rows*-1,rows) y = randint(rows*-1,rows) self.x=x self.y=y self.chart = {} for var1 in reversed(range(rows*-1,rows+1)): dict1 = {} for var2 in reversed(range(rows*-1,rows+1)): dict1[var2] = 0 self.chart[var1]=dict1 def print_chart_airport(self, airport): print (" "), for num in reversed(range(self.rows*(-1),self.rows + 1)): if num >= 0: print " "+(str(num)), else: print (str(num)), for num in reversed(range((self.rows)*(-1),(self.rows+1))): print if num >= 0: print " "+(str(num)), else: print (str(num)), for var in reversed(range((self.rows)*(-1),(self.rows+1))): if self.x == num and self.y == var: print " P", elif airport.x == num and airport.y == var: print " A", else: print " " + str(self.chart[num][var]), print print def print_chart(self): print (" "), for num in reversed(range(self.rows*(-1),self.rows + 1)): if num >= 0: print " "+(str(num)), else: print (str(num)), for num in reversed(range((self.rows)*(-1),(self.rows+1))): print if num >= 0: print " "+(str(num)), else: print (str(num)), for var in reversed(range((self.rows)*(-1),(self.rows+1))): if self.x == num and self.y == var: print " P", else: print " " + str(self.chart[num][var]), print print def fly(self, newX, newY): newX=self.x+newX newY=self.y+newY if newX>=self.rows*-1 and newX<=self.rows and newY>=self.rows*-1 and newY<=self.rows: self.miles+= sqrt(float((self.x-newX)**2)+float((self.y-newY)**2)) self.x=newX self.y=newY return True return False def distanceBetween(self, location): return sqrt(float((self.x-location.x)**2)+float((self.y-location.y)**2)) def distanceCoords(self, x, y): return sqrt(float((x)**2)+float((y)**2)) # Main def printHelp(debug, gridPrint, airplane, airport): if gridPrint: if debug: print airplane.print_chart_airport(airport) else: print airplane.print_chart() print "Welcome to the Airport Game!\n" while True: rows = int(raw_input("Enter the maximum row number (1-9): ")) if rows in range(1,10): break else: print "Invalid input" while True: initX = int(raw_input("Enter initial X position: ")) if initX<=rows and initX>=rows*-1: break else: print "Invalid input" while True: initY = int(raw_input("Enter initial Y position: ")) if initY<=rows and initX>=rows*-1: break else: print "Invalid input" while True: gridPrint = raw_input("Would you like to have the grid printed after each turn? (y/n): ") if gridPrint.lower() != 'y' or gridPrint.lower != 'n': break else: print "Invalid input" while True: debug = raw_input("Would you like to play in debug mode (where the airport is printed)? (y/n): ") if debug.lower() != 'y' or debug.lower != 'n': break else: print "Invalid input" if gridPrint == 'y': gridPrint = True else: gridPrint = False if debug == 'y': debug = True else: debug = False airplane = AirLocation(rows, initX, initY) dist = 0 while dist == 0: airport = AirLocation(rows) dist = airplane.distanceBetween(airport) flights = 0 dist = 0 #SET UP COMPLETE if debug: airplane.print_chart_airport(airport) else: airplane.print_chart() while True: print "Number of flights : "+ str(flights) print "Plane Location: (%s, %s)" %(str(airplane.x), str(airplane.y)) if debug: print "Airport Location: ("+str(airport.x)+", "+str(airport.y)+")" print "Distance just flown: "+str(dist)+" miles" print "Distance to airport: %.3g miles" %(airplane.distanceBetween(airport)) while True: newX = int(raw_input("What distance to fly in the x direction?: ")) newY = int(raw_input("What distance to fly in the y direction?: ")) if(airplane.fly(newX, newY)): break else: print "Invalid input" flights+=1 dist=airplane.distanceCoords(newX, newY) print if airplane.distanceBetween(airport) == 0: print "You won in "+str(flights)+" turns! You are within sight of the airport!" print "Airport Location: ("+str(airport.x)+", "+str(airport.y)+")" break printHelp(debug, gridPrint, airplane, airport)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question