13 December 2015

CST205 Week 8

What advise you will give to future CSIT-Online students so their learning in this class can be effective and rewarding? Relax and be patient. Keep in mind that this is an introductory course. Many of the people taking this may be more knowledgeable about Python than you, don't let that dissuade you.

Python Code of Interest this week - Electronic Craps Game:
dice = [0, 0]
points = [10]

def gameEnd(strState):
  if "win" in strState:
    points[0] += 1
    printNow('You won and have ' + str(points[0]) + ' points now.')
    answer = requestString('YOU WIN!!!\nWould you like to play again (y/n)?').lower()
  else:
    points[0] -= 1
    printNow('You lose and have ' + str(points[0]) + ' points now.')
    if points[0] > 0:
     answer = requestString('Sorry, you lose.\nWould you like to play again?').lower()
    else:
      showInformation('Sorry, you lose.\nYou have run out of credits.')
      answer = "no"
  if "n" in answer: # check for quit playing situation
    printNow('Thanks for playing, you cash out with ' + str(points[0]) + ' points.')
    return false
  return true

def diceRoll():
  import random
  return random.randint(1, 6)

showInformation('Welcome to our Craps Game')
if points[0] > 0:
  game = true
else: # otherwise they can't play anymore
  showInformation('Sorry, you are out of credits.')
  game = false
while game == true:
  # First Roll
  printNow('Rolling dice...')
  dice[0] = diceRoll()
  dice[1] = diceRoll()
  printNow('You rolled a ' + str(dice[0]) + ' and a ' + str(dice[1]) + '.')
  point = dice[0] + dice[1]
  if point == 7 or point == 11:
    game = gameEnd("lose")
  elif point == 2 or point == 3 or point == 12:
    game = gameEnd("win")
  else:
    showInformation('The point is now: ' + str(point) + '.')
    # Subsequent Rolls
    totalDice = 0
    rollNumber = 0
    while totalDice != point:
      dice[0] = diceRoll()
      dice[1] = diceRoll()
      printNow('You rolled a ' + str(dice[0]) + ' and a ' + str(dice[1]) + '.')
      nextRoll = dice[0] + dice[1]
      rollNumber += 1
      if nextRoll == point:
        game = gameEnd("win")
      elif nextRoll == 7:
        game = gameEnd("lose")
        break
      else:
        showInformation('Roll #' + str(rollNumber) + ' is a ' + str(nextRoll) + '.\nNot quite - let\'s try again.')

08 December 2015

CST205 Week 7

The best thing that we have learned so far in this course is the use of dictionaries in Python (aka Hashes).

For additional fun I wrote an online headline aggregator (but didn't use a dictionary in it):

#'print onlineAggregator()' - it will ask you for directory to save online file before pulling headlines

def onlineAggregator():
  localPath = setMediaPath()
  import urllib
  urllib.urlretrieve('http://otterrealm.com/category/news/', localPath+'\\news.html')
  string = open(localPath+'\\news.html', 'r').read()
  newString = "*** Otter Realm Breaking News! ****\n"
  index = 0
  while index < len(string):
    start = string.find('<h3>', index)
    if start == -1:
      break
    end = string.find('</h3>', index)
    newString += '> ' + string[start + 4:end] + '\n'
    index = end + 5
  return newString
I know this could have been done without downloading the file, but the assignment was opening local files and parsing them.

01 December 2015

CST205 Week 6

RE: Silicone Valleys Race to Hack Happiness

I very much disagree with the idea that depression is on the rise - the basis of increased drug use is a misleading statistic. The fact is that these drugs are readily available now and were not just 10 years ago. The permeation of technology into our society is natural - it, like the drugs, is filling a niche. This vacuum is more likely caused by an increasing amount of free time.

In addition to working with my group on a text based game, I wrote my own:

# The Ultimate Adventure Game - Written by John Lester

power = true # global state of ships power
zombieNum = 0 # global number of nearby zombies
haveKeys = false # global state of keys held

def help(): # prints help statements based on global variables
  global power
  global zombieNum
  global haveKeys
  global locationCode
  if power is true: # print power status
    printNow('The power is on')
  else:
    printNow('The power is off')
  if zombieNum > 0: # print number of nearby zombies
    printNow('You are surrounded by ' + str(zombieNum) + ' zombie(s).')
  if haveKeys is true: # print if player has keys
    printNow('You are carrying keys to the Officers Quarters')
  if locationCode == 'O':
    printNow('You are in the Officers Quarters')
    if zombieNum >= 1:
      printNow('The only exit is the escape pod')
    else:
      printNow('The only exits are aft or the escape pod')
  elif locationCode == 'C':
    printNow('You are in the Commmand Center')
    printNow('There are exits fore and aft')
    if haveKeys is false:
      printNow('The fore exit is locked')
  elif locationCode == 'M':
    printNow('You are in the Mess Hall')
    printNow('The only exit is starboard')
    if haveKeys is False:
      printNow('You can grab the captains keys')
  elif locationCode == 'H':
    printNow('You are in the main hallway')
    printNow('There are exits fore, aft, port and starboard')
  elif locationCode == 'Q':
    printNow('You are in the Crews Quarters')
    printNow('The only exit is port')
  elif locationCode == 'E':
    printNow('You are in the Engine Room')
    printNow('The only exit is fore')
    printNow('There is a power switch to the left')
  else:
    printNow('You are lost...')
  return

def command():
  global power
  global haveKeys
  printNow(' ')
  printNow('----- Command Center -----')
  printNow('You find yourself in the command center of a submarine')
  printNow('The dead bodies of the crew are strewn about the floor everywhere.')
  if power is true:
    printNow('Around you the consoles are spitting sparks.')
  else:
    printNow('The dim emergency lighting barely illuminates the walkways.')
  if haveKeys is true:
    printNow('Forward of your position is a door that leads to the Officers Quarters.')
  else:
    printNow('Forward of your position is a locked door.')
  printNow('Aft of your position is a door that leads to the main hallway.')
  move = doAction()
  cls()
  if move == 'fore' and haveKeys is true:
    printNow('You use the key to open the Officers Quarters.')
    return 'O'
  elif move == 'aft':
    return 'H'
  elif move == 'help':
    help()
  elif move == 'exit':
    return 'X'
  else:
    printNow('You are unable to move that direction or perform that action')
  return 'C'

def hallway():
  global power
  printNow(' ')
  printNow('----- Hallway -----')
  printNow('You are in the central hallway of the submarine.')
  printNow('All the bulkheads are covered in blood')
  if power is true:
    printNow('The lights above you flicker')
  else:
    printNow('The emergency lighting barely shows you the way around.')
  printNow('Forward of your position is a door that leads to Command, aft leads to the Engine Room.')
  printNow('To port is a door that leads to the Mess Hall, to starboard leads to the Crew Quarters.')
  move = doAction()
  cls()
  if move == 'fore':
    return 'C'
  elif move == 'port':
    return 'M'
  elif move == 'star':
    return 'Q'
  elif move == 'aft':
    return 'E'
  elif move == 'help':
    help()
  elif move == 'exit':
    return 'X'
  else:
    printNow('You are unable to move that direction or perform that action')
  return 'H'

def engineRoom():
  global power
  printNow(' ')
  printNow('----- Engine Room ------')
  if power is true:
    printNow('A single light bulb illuminates the very loud room.')
    printNow('Batteries line both sides of the room and a spinning shaft runs out the back.')
  else:
    printNow('You are in a very dark and quiet room.')
    printNow('Batteries line both sides of the room and a greasy shaft runs out the back.')
  printNow('On the left wall is the main power switch.')
  move = doAction()
  cls()
  if move == 'fore':
    return 'H'
  elif move == 'help':
    help()
  elif move == 'exit':
    return 'X'
  elif move == 'pwr':
    if power is true:
      power = false
      printNow('The ship suddenly falls dark and quiet as it lurches to a halt.')
    else:
      power = true
      printNow('The room suddenly lights up and the shaft in the center begins to spin.')
  else:
    printNow('You are unable to move that direction or perform that action')
  return 'E'

def crewQuarters():
  global zombieNum
  printNow(' ')
  printNow('----- Crew Quarters -----')
  printNow('The room is lined with bunks and lockers.')
  printNow('The door behind you (port side) is the only exit.')
  printNow('An undead crewman, with guts hanging from his stomach, slowly lumbers toward you.')
  move = doAction()
  if move == 'port':
    zombieNum = 0
    return 'H'
  elif move == 'atk':
    printNow('You do not have a weapon!!!')
  elif move == 'help':
    help()
    return 'Q'
  elif move == 'exit':
    return 'X'
  else:
    printNow('You are unable to move that direction or perform that action.')
  zombieNum += 1
  if zombieNum == 2:
    printNow('The undead crewman gets closer.')
  elif zombieNum >= 3:
    cls()
    printNow('The undead crewman attacks you and bites out your throat!')
    printNow('You lay on the floor bleeding out...better luck next time.')
    return 'X'
  return 'Q'

def messHall():
  global power
  global haveKeys
  global zombieNum
  printNow(' ')
  printNow('----- Mess Hall -----')
  if power is false:
    zombieNum += 1
    if zombieNum >= 2:
      cls()
      printNow('The undead crewman attacks you and bites out your throat!')
      printNow('You lay on the floor bleeding out...better luck next time.')
      return 'X'
    else:
      printNow('As you enter the room an undead crewman jumps at you from the darkness.')
      printNow('You are knocked back into the hallway and barely get the door closed.')
    return 'H'
  zombieNum = 0
  printNow('You are in the dining room, tables and chairs are strewn about.')
  printNow('On the floor, surrounded by dead bodies, is the ships captain.')
  if haveKeys is false:
    printNow('A large ring of keys hangs from his belt.')
  move = doAction()
  cls()
  if move == 'star':
    return 'H'
  elif move == 'keys':
    haveKeys = true
    printNow('You grab the keys to the Officers Quarters off the dead captain.')
  elif move == 'atk':
    printNow('You do not have a weapon!!!')
  elif move == 'help':
    help()
  elif move == 'exit':
    return 'X'
  else:
    printNow('You are unable to move that direction or perform that action.')
  return 'M'

def officersQuarters():
  global power
  global zombieNum
  printNow(' ')
  printNow('----- Officers Quarters -----')
  printNow('You have reached the front of the ship.')
  printNow('On each side are the slots that hold escape pods. One remains available...')
  escape = requestString('Do you want to enter the pod and escape: ').lower()
  cls()
  if "help" in escape or "?" in escape:
    help()
    return 'O'
  elif "y" in escape:
    printNow(' ')
    printNow('----- Escape Pod -----')
    printNow('As you step into the escape pod, freedom just moments away -')
    printNow('an undead crewman attacks you from the darkness and bites your chest!')
    printNow('You are able to crush the dead crewmans skull and push the body away.')
    if power is true:
      printNow('You lay on the floor bleeding as the escape pod surfaces.')
      printNow('You feel weak and hungry for human brains and see shore through the portal.')
      printNow('Happy hunting...')
    else:
      printNow('You lay on the floor bleeding, never to leave this underwater grave.')
      printNow('You will never be able to satiate your hunger for human brains.')
    return 'X'
  zombieNum += 1
  if zombieNum <= 1:
    printNow('A dead crewman comes staggering through the door to the Command Center blocking that exit.')
  else:
    printNow('Your indecision has killed you. The dead crewman grabs you and begins pulling out your intestines.')
    return 'X'
  return 'O'

def doAction():
  global locationCode
  direction = requestString('What do you want to do: ').lower()
  if "fore" in direction or "north" in direction:
    return 'fore'
  elif "aft" in direction or "south" in direction:
    return 'aft'
  elif "port" in direction or "west" in direction:
    return 'port'
  elif "star" in direction or "east" in direction:
    return 'star'
  elif "exit" in direction or "quit" in direction:
    return 'exit'
  elif "switch" in direction or "turn " in direction or "power" in direction:
    return 'pwr'
  elif "attack" in direction:
    return 'atk'
  elif "keys" in direction:
    return 'keys'
  elif "help" in direction or "?" in direction:
    return 'help'
  return 'none'

def cls():
  for x in range(0, 15):
    printNow(' ')

cls()
printNow('You wake up in the center of a submarine command center.')
printNow('You can type help at any time for a list of commands')
locationCode = 'C'
while locationCode != 'X':
  if locationCode == 'O':
    locationCode = officersQuarters()
  elif locationCode == 'C':
    locationCode = command()
  elif locationCode == 'M':
    locationCode = messHall()
  elif locationCode == 'H':
    locationCode = hallway()
  elif locationCode == 'Q':
    locationCode = crewQuarters()
  elif locationCode == 'E':
    locationCode = engineRoom()
  else:
    printNow('You seem to be lost...sending you back to command')
    locationCode = 'C'