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.')

No comments: