Professional Documents
Culture Documents
def read_string_list_from_file(the_file):
'''
CODE PROVIDED TO INCORPORATE
<file-name including extension .txt>(String) --> List of strings
Assumptions:
1) the_file is in the same directory as this program
2) the_file contains one planet data per line
3) after the data of each planet in the file there is a return (
so that the next planet is in the next line), and also
there is (one single) return after the last planet
in the_file
to call or invoke this function:
listStrings = read_string_list_from_file(<file-name.txt in quotes>)
using the data provided the call to this function would be:
listStrings = read_string_list_from_file("planetsData1.txt")
'''
fileRef = open(the_file,"r") # opening file to be read
localList=[]
for line in fileRef:
string = line[0:len(line)-1] # eliminates trailing '\n'
# of each line
localList.append(string) # adds string to list
fileRef.close()
#........
#print ("\n JUST TO TRACE, the local list of strings is:\n")
#for element in localList:
# print (element)
#print ()
#........
return localList
def file():
file = input("\nType the name of the board file including '.txt' or type d f
or default :")
while (file.lower() != "d" and file.find(".txt") == -1):
file = input("You did not type the file name correctly. Try again: ")
if file.lower() == "d":
res = "planetsData1.txt"
else:
res = file
return res
def create_lists_board(listStrings):
'''
Assumptions:
1) The listStrings parameter will have a list with the strings,
2) each string corresponds to the data for one planet
3) de string for each planet is: civlevel-fuel-rocks
4) civleve, fuel and rocks are integer numbers each
'''
# RECOMMENDED THAT YOU IMPLEMENT THIS
#
# your code will process the parameter list and return 3 lists:
# one list for the civilization level in each planet,
# one list for the fuel liters in each planet
# one list for the rock specimens in each planet
civlevel = []
fuel = []
rocks = []
for i in range(len(listStrings)):
civlevel += [listStrings[i][0]]
fuel += [listStrings[i][1]]
rocks += [listStrings[i][2]]
return [civlevel,fuel,rocks]
def display_board(planet_number,lst,pos,python_planet):
result = "Planet# CivLevel Fuel Rocks \n"
for i in range(len(lst)):
result += str(planet_number[i])
for k in range(len(lst[i])):
if len(str(lst[i][k])) == 1:
result += " " + str(lst[i][k])
else:
result += " " + str(lst[i][k])
if python_planet == i and python_planet > 0:
result += " <===== Python Planet "
if planet_number[i] == pos:
result += " <===== Astronaut Position\n"
else:
result += "\n"
print (result)
return
def show_board(title):
#RECOMMENDED
#This will be useful to show the board each time that
#the borad needs to be shown to the user.
#the parameter title woudl allow that the function is called
#at differnt times and the title so indicates: exmple: after creation",
#before turn number xxx, etc
print ("\nShowing board... " + title)
print ("\n The board at this point contains...\n")
# your code...
return display_board(planet_number,listOfString,position,python_planet)
def convert_to_list(a):
res = []
for i in range(len(a)):
lst = []
st = ""
for k in range(len(a[i])):
if a[i][k].isdigit() and not(k == (len(a[i])-1)):
st += a[i][k]
elif k == (len(a[i])-1):
st += a[i][k]
lst.append(int(st))
else:
lst.append(int(st))
st = ""
res += [lst]
return res
def planet_num(a):
res = []
for i in range(len(a)):
res += [i]
return res
def replace_rocks(planet_rocks,listOfString):
for i in range(len(planet_rocks)):
listOfString[i][2] = planet_rocks[i]
listOfString[0] = [0,0,0]
return
def mild_explosions(listOfString,planet_lst,planet_rocks):
global mild_count
planet_position = 0
random = (r.randint(1,len(planet_lst)*5))
for i in range(len(planet_lst)-1):
if (random == (i+1)):
print("\nOooooh! A mild or amazing explosion is happening in planet
# " + str(i+1) + " \n the board will have more rock specimens!")
mild_count += 1
planet_position = (i+1)
for i in range(planet_position):
for k in range(planet_position,i,-1):
planet_rocks[i] = planet_rocks[i] + planet_rocks[k]
replace_rocks(planet_rocks,listOfString)
if (planet_position > 0):
print("\n\nOh oh! An amazing explosion occured in planet # " + str(plane
t_position) + "\n")
show_board("after mild explosion")
return planet_rocks
def amazing_explosions(listOfString,planet_lst,planet_fuel,planet_rocks,pos,pyth
on_planet,):
global amazing_count
planet_position = 0
engame = 0
check = 0
random = (r.randint(1,len(planet_lst)*5))
for i in range(len(planet_lst)-1):
if (random == (i+1)):
print("\nOooooh! A mild or amazing explosion is happening in planet
# " + str(i+1) + " \n the board will have more rock specimens!")
amazing_count += 1
planet_position = (i+1)
for i in range(planet_position):
for k in range(planet_position,i,-1):
planet_rocks[i] = planet_rocks[i] + planet_rocks[k]
replace_rocks(planet_rocks,listOfString)
if planet_position > 0 and planet_position != pos and planet_position < pos
and planet_position < python_planet:
pos -=1
python_planet -=1
del planet_rocks[planet_position]
del listOfString[planet_position]
planet_lst = planet_num(listOfString)
print("\n\nOh oh! An amazing explosion occured in planet # " + str(plane
t_position) + "\nThis planet will dissapear!\nand the board shrunk\nbut the astr
onaut was not there and it did not affect his position...")
check += 1
elif planet_position == pos:
engame = 1
elif planet_position > 0 and planet_position != pos and planet_position > po
s and planet_position > python_planet:
del planet_rocks[planet_position]
del listOfString[planet_position]
planet_lst = planet_num(listOfString)
print("\n\nOh oh! An amazing explosion occured in planet # " + str(plane
t_position) + "\nThis planet will dissapear!\nand the board shrunk\nbut the astr
onaut was not there and it did not affect his position...")
check += 1
elif planet_position > 0 and planet_position != pos and planet_position < po
s and planet_position > python_planet:
pos -=1
del planet_rocks[planet_position]
del listOfString[planet_position]
planet_lst = planet_num(listOfString)
print("\n\nOh oh! An amazing explosion occured in planet # " + str(plane
t_position) + "\nThis planet will dissapear!\nand the board shrunk\nbut the astr
onaut was not there and it did not affect his position...")
check += 1
elif planet_position > 0 and planet_position != pos and planet_position > po
s and planet_position < python_planet:
python_planet -=1
del planet_rocks[planet_position]
del listOfString[planet_position]
planet_lst = planet_num(listOfString)
print("\n\nOh oh! An amazing explosion occured in planet # " + str(plane
t_position) + "\nThis planet will dissapear!\nand the board shrunk\nbut the astr
onaut was not there and it did not affect his position...")
check += 1
return [planet_rocks,planet_lst,python_planet,pos,endgame,check]
def drawPlanet(lis):
t.pencolor(determinePlanetOutlineColour(lis))
t.fillcolor(determinePlanetFillColour(lis))
t.begin_fill()
for i in range(2):
t.forward(50)
t.left(90)
t.forward(100)
t.left(90)
t.end_fill()
def drawSpace():
t.penup()
t.forward(70)
t.pendown()
def drawAllPlanets(lis):
t.speed(0)
startPos(len(lis))
for i in range(len(lis)):
drawPlanet(lis[i])
drawSpace()
def drawAstro(planets, pos, fuel):
t.penup()
t.fillcolor(determineShipColour(fuel))
t.backward(70*(planets-pos))
t.right(90)
t.forward(150)
t.left(90)
t.forward(25)
t.pencolor("black")
t.pendown()
t.begin_fill()
t.circle(25)
t.end_fill()
def startPos(n):
t.penup()
space = 0
if((n%2)==0):
space = n/2
t.backward((space*50)+((space-1)*20)+10)
else:
space = n//2
t.backward((space*50)+(space*20)+25)
t.pendown()
'''space = (n-1)*50 + 25
t.backward(space)
t.pendown()'''
def determinePlanetFillColour(lis):
colour = (0, 0, 0)
value = lis[2] * 17
if(value < 1):
colour = (0, 0, 0)
elif(value < 255):
colour = (0, value, 0)
elif(value < 455):
colour = ((value - 255), 255, (value - 255))
else:
colour = (200, 255, 200)
return colour
def determinePlanetOutlineColour(lis):
colour = ""
if(lis[1] > 0):
colour = "orange"
else:
colour = "black"
return colour
def determineShipColour(fuel):
colour = ""
if(fuel < 1):
colour = "red"
elif(fuel < 10):
colour = "yellow"
else:
colour = "green"
return colour
def clearBoard():
t.reset()
#Planets is the matrix of planets, pos is the position of the player,
#fuel is the amount of fuel the player has
def drawBoard(planets, pos, fuel):
t.colormode(255)
t.pensize(5)
drawAllPlanets(planets)
drawAstro(len(planets), pos, fuel)
#========================================================================#
import random as r
import turtle as t
import math as m
fuel_amount = 1
game_count = 0
win_count = 0
mild_count = 0
amazing_count = 0
max_turns = 1000000
turn_counter = 1
position = 0
endgame = 0
rock_counter = []
listStrings = read_string_list_from_file("planetsData1.txt")
listOfString = convert_to_list(listStrings)
yes_no = start_game()
planet_number = planet_num(listOfString)
planet_civ_level = (create_lists_board(listOfString))[0]
planet_fuel = (create_lists_board(listOfString))[1]
planet_rocks = (create_lists_board(listOfString))[2]
python_planet = 0
check = 0
if yes_no == "y":
show_board("just created")
python_planet = int(input("Which position should python planet be? (0-" + str(le
n(planet_number)-1) + ") 0 has no affect: "))
while not(str(python_planet).isdigit()) or python_planet <0 or python_planet > (
len(planet_number)-1):
if not(python_planet.isdigit):
python_planet = int(input("That is not a positive integer. Please try ag
ain: "))
else:
python_planet = int(input("That value is not within the valid range. Ple
ase try again: "))
if python_planet == 0:
python_planet = int(-1)
while yes_no.lower() == "y":
while turn_counter <= int(max_turns) and int(fuel_amount) > 0:
if turn_counter == 1:
data = file()
listStrings = read_string_list_from_file(data)
listOfString = convert_to_list(listStrings)
planet_number = planet_num(listOfString)
planet_civ_level = (create_lists_board(listOfString))[0]
planet_fuel = (create_lists_board(listOfString))[1]
planet_rocks = (create_lists_board(listOfString))[2]
display_board(planet_number,listOfString,position, python_planet)
name, civ_lvl, fuel_amount, max_turns, amaz_expl, mild_expl, positio
n = game_info()
planet_number = planet_num(listOfString)
planet_civ_level = (create_lists_board(listOfString))[0]
planet_fuel = (create_lists_board(listOfString))[1]
planet_rocks = (create_lists_board(listOfString))[2]
display_board(planet_number,listOfString,position, python_planet)
turn(name, civ_lvl, position, fuel_amount, max_turns, mild_expl, amaz_ex
pl, listOfString, rock_counter)
if amaz_expl.lower() == "y" and position > 0:
result = amazing_explosions(listOfString,planet_number,planet_fuel,p
lanet_rocks,position,python_planet)
python_planet = result[2]
position = result[3]
planet_number = result[1]
planet_rocks = result[0]
endgame = result[4]
check = result[5]
if check == 1:
show_board("after amazing explosion")
mild_expl = "n"
elif amaz_expl.lower() == "y" and position == 0:
result = amazing_explosions(listOfString,planet_number,planet_fuel,p
lanet_rocks,position,python_planet)
planet_number = result[1]
planet_rocks = result[0]
endgame = result[4]
check = result[5]
if check == 1:
show_board("after amazing explosion")
mild_expl = "n"
if mild_expl.lower() == "y":
mild_explosions(listOfString, planet_number, planet_rocks)
position = movement(planet_number, position)
clearBoard()
drawBoard(listOfString,position,fuel_amount)
if position != python_planet:
fuel_amount = encounter(fuel_amount, civ_lvl, position, planet_civ_l
evel)
if int(fuel_amount) > 0:
rock_counter = rock_collection(position, rock_counter, planet_ro
cks)
else:
game_count += 1
yes_no, turn_counter, fuel_amount, position, rock_counter = end_
of_game(name, civ_lvl, position, fuel_amount, rock_counter, max_turns, turn_coun
ter, python_planet)
else:
print("WOW! The astronaut reached Python Planet and the game is now
over!")
game_count += 1
win_count += 1
yes_no, turn_counter, fuel_amount, position, rock_counter = end_of_g
ame(name, civ_lvl, position, fuel_amount, rock_counter, max_turns, turn_counter,
python_planet)
if turn_counter == int(max_turns):
game_count += 1
yes_no, turn_counter, fuel_amount, position, rock_counter = end_of_g
ame(name, civ_lvl, position, fuel_amount, rock_counter, max_turns, turn_counter,
python_planet)
if endgame == 1:
game_count += 1
yes_no, turn_counter, fuel_amount, position, rock_counter = end_of_g
ame(name, civ_lvl, position, fuel_amount, rock_counter, max_turns, turn_counter,
python_planet)
turn_counter += 1
end_of_all_games(game_count, win_count)
newList = binaryList(planet_rocks)
num = toBase10(newList)
print("Which converted to decimal is "+ str(num) + ".")
print("Goodbye!")