CS 100 & CS 102

CS 102, lab 3, part 1.  We'll make a small change or two.  Read in 10 integer values, and print out whether these 10 values are in ascending order (e.g. -3, 0, 12, 13, 29, 33, 50, 101, 120, 205), descending order (e.g. the reverse of what I just showed) or neither.  To do this, we need 3 variables:  lastval, the last value read in, newval, the next value read in, and order.  Order will be -1 if the values so far are in descending order, 1 for ascending, and 0 for neither.   No two numbers will be the same.

lastval = input("enter first value:")
newval = input("enter 2nd value:")
if lastval < newval:
       order = 1     #i.e. ascending order for the first two values
else:
       order = -1   #i.e. descending order for the first two values

for i in range(9):    #i.e. go through the loop 8 times
       lastval = newval       #we know the tentative order, grab the next value
       newval = input("enter new value:")
      #now for the heavy lifting!  is the order still correct?
      if order == 1  and newval < lastval:      
                #i.e. we had ascending order previously, but the newest value is less
                #than the previous one--so out-of-order
                order = 0      #our numbers will not be in order

      if order == -1 and newval > lastval:
                order = 0    #same idea

if order == 1:
        print "ascending order"
elif order == -1:
        print "descending order"
else:
        print "no order"

-----------------------------------------------------------
MONOPOLY of sorts.
We'll invent a simple version.  You start at GO, each of the 4 sides of the board has 10 squares.  How many turns will it take to reach or pass GO again?  No buying, selling, etc.  Roll 2 dice, doubles lets you roll again, and unlike Monopoly, you won't go to Jail if you roll doubles 3 times in a row.

We really do not need to worry about a 4-sided board here--the only concern will be that if we roll 2 dice, how many turns will it take for the total on all the dice rolls to reach 40 or more (4 sides, 10 squares per side, 40 brings you back to GO again).  Later on we might spice things up a bit--e.g. if you land on square # 23, you pay $25, square 31 nets you $75, reaching GO again, $200, etc.

The variables we need:  square:  this starts with 0, and as we roll the dice, we add the dice total to square--this is where we are on the board.  turncount:  how many turns we've taken so far.  We can start this off at 1 as we do our first dice roll.

We also need import random.  random.randrange(1,7) returns a random number between 1 and 6--a roll of one die.

square = 0
turncount = 1   #this gets us ready to roll!

while square < 40:
       die1 = random.randrange(1,7)
       die2 = random.randrange(1,7)     #our 2 dice
       square = square + die1 + die2       #move forward
       if die1 != die2:
                turncount = turncount + 1       #turncount increases if we fail to roll a double

print "to pass GO took",turncount, "turns"
------------------------

to spice it up:
we add the variable money = 0
and  we can add to the while loop:
       if square == 23:
              money = money - 25
      if square == 31:
              money = money +75
and when we leave the loop--having passed GO,
money = money + 200
and we have also:
print "money = $",money
------------------
there are plenty of ways to spice things up further!
----------------------------------------------
SNAKES and LADDERS
This is a very old game--100 squares.  Roll 2 dice.  If you land on the lower end of a ladder, climb up the ladder to the square on the other end--e.g. the lower end of a ladder is at square 12, the upper end at square 53.  If your dice roll lands you on square 12, you advance to square 53.  Snakes do the opposite.  If you land on the top end of a snake, you get carried down to the lower end--e.g.  from square 41 to square 23.  Doubles do not let you roll again this turn.  How many rolls does it take to to reach square 100 or beyond?  Note that it could be a very large number--you could be very unlucky!  The code would be similar to Monopoly, but without the doubles code, and we would see code such as:

         if square == 12:
                  square = 53  #i.e. a ladder
        if square == 41:
                 square = 23   #i.e. a snake.

to see examples of snakes and ladders, a good source is www.boardgamegeek.com   and type in snakes and ladders in the search box.