#!/usr/bin/env python
#
# while statement
#
import time
import datetime
import sys
import random

print "In this game a playing field will be randomly generated."
print "Your goal is to get to one of five $ signs at the bottom."
print "You will be timed and the number of turns you take will be counted."
print "GOOD LUCK!"

lineblank = "                    "
line1 = "***  *****  *   *   "
line2 = "*     *  ** * * ****"
line3 = "  ***   * **   *    "
line4 = " * *  *     *  * ** "
line5 = "   * *       * ** **"
line6 = "* *      * *** *    "
line7 = "  ** *      **    **"
line8 = "* * *  *   **   *   "
line9 = " *    *    *** *  * "
line10 = "  ****        ** *  "
line11 = "*   * * **     *  * "
line12 = "* *    *      * *** "
line13 = " **  ** *      *  * "
line14 = "  *    *** **  *    "
line15 = "   *      *  ** *** "
line16 = "   **      **  *  **"
line17 = "  *   *     * ***** "
line18 = "   * ***   ***      "
line19 = "  **      * *   *** "
line20 = "  * **    * *  **   "
lineend = "***$****$***$***$*$*"
hpos = 0 # horizontal position
vpos = 0  # vertical position
loseflag = 0
winflag = 0

origscreen = [] # screen without a "o"
def formscreen():
  len=20
  i = 0
  while i < len:
    if i == 0:
      origscreen.append(lineblank)
    if i == len-1:
      origscreen.append(lineend)
    else:
      randomline = random.randint(1,20)
      if randomline == 1:
        origscreen.append(line1)
      if randomline == 2:
        origscreen.append(line2)
      if randomline == 3:
        origscreen.append(line3)
      if randomline == 4:
        origscreen.append(line4)
      if randomline == 5:
        origscreen.append(line5)
      if randomline == 6:
        origscreen.append(line6)
      if randomline == 7:
        origscreen.append(line7)
      if randomline == 8:
        origscreen.append(line8)
      if randomline == 9:
        origscreen.append(line9)
      if randomline == 10:
        origscreen.append(line10)
      if randomline == 11:
        origscreen.append(line11)
      if randomline == 12:
        origscreen.append(line12)
      if randomline == 13:
        origscreen.append(line13)
      if randomline == 14:
        origscreen.append(line14)
      if randomline == 15:
        origscreen.append(line15)
      if randomline == 16:
        origscreen.append(line16)
      if randomline == 17:
        origscreen.append(line17)
      if randomline == 18:
        origscreen.append(line18)
      if randomline == 19:
        origscreen.append(line19)
      if randomline == 20:
        origscreen.append(line20)
    i = i+1
def printscreen():
  loseflag = 0
  winflag = 0
  screen = []
  mylen = len(origscreen)
  i = 0
  while (i < mylen):
       x = origscreen[i]
       screen.append(x)
       if i == vpos:
               if (x[hpos] == "*"):
                       loseflag = 1
               if (x[hpos] == "$"):
                       winflag = 1
               x = x[:hpos] + ("o") + x[hpos+1:]
               screen[i] = x
       i = i+1
  for s in screen:
       print s
  if loseflag == 1:
       print "You LOST! Your total time was "+str(totaltime)+" seconds and you moved "+str(turns)+" times before you failed miserably."
  if winflag == 1:
       print "You WON! Your total time was only "+str(totaltime)+" seconds and you moved "+str(turns)+" times. Did you beat your friends?"

totaltime = 0
formscreen()
i = 0
x = ""
turns = 0
while (loseflag == 0) and (winflag == 0):
   printscreen()
   mybeforetime = time.time()
   x = raw_input("Please hit u(up), d(down), l(left), r(right), or s(stop):")
   myaftertime = time.time()
   diff = myaftertime - mybeforetime
   print ("You hit the letter: ") + x
   if x == "u":
       vpos = vpos-1
   if x == "d":
       vpos = vpos+1
   if x == "l":
       hpos = hpos-1
   if x == "r":
       hpos = hpos+1
   totaltime = totaltime + diff
   print "Your time is at "+str(totaltime)+" seconds"
   turns = int(turns) + 1
   print "You have moved "+str(turns)+" times"
   i=i+1

print "Your time is"+str(totaltime)
