```----------------------------------------------------------------------------------
GARDEN ROUTE DEFCON GROUP DC2744
www.dc2744.org, dc2744@eocs.co.za, dc2744@dc2744.org
==================================================================================
----------------------------------------------------------------------------------
Practical Exercises at DCG Meetups
----------------------------------------------------------------------------------
Scraper (Bash & Python):
Create a script that scrapes a site for info

The code:

[code]
...
[/code]

----------------------------------------------------------------------------------
Towers Of Hanoi (Python):
Create the game and a program that solves the puzzel and
prints out the pattern to solve it.

The code:

[code]
#!/usr/bin/python

print "Towers Of Hanoi"

stand=[[0,0,1],
[0,0,2],
[0,0,3]
]
move_count=0

def display_game():
d=["   |   ",
"  -|-  ",
" --|-- ",
"---|---"]
print stand
game="""
p1p1 p2p1 p3p1
p1p2 p2p2 p3p2
p1p3 p2p3 p3p3
"""+"="*25
for pg in range(0,len(stand)):
for loc in range(0,3):
game=game.replace("p"+str(pg+1)+"p"+str(loc+1),d[stand[pg][loc]])
print game

def get_the_disk(p,l):
return stand[p-1][l-1]

def something_above(fp,fl,tp,tl):
# check if a disk is on top of the disk being moved
if fl > 1:
if stand[fp-1][fl-2] != 0:
print "something above disk, can't move"
return True
else:
return False
else:
return False

def smaller_below(fp,fl,tp,tl):
# check if a bigger disk is being moved onto a smaller one
m=True
if tl < 3:
for i in range(tl-1,3):
if stand[fp-1][fl-1] > stand[tp-1][i] and stand[tp-1][i] != 0:
print "can't put bigger disk on top of smaller disk"
m=True
break
else:
m=False
else:
m=False
return m

def change_disk(fp,fl,tp,tl):
if something_above(fp,fl,tp,tl) == False:
if smaller_below(fp,fl,tp,tl) == False:
if stand[tp-1][tl-1] == 0:
stand[tp-1][tl-1] = stand[fp-1][fl-1]
stand[fp-1][fl-1] = 0
for i in range(tl-1,3):
if stand[tp-1][i] == 0:
stand[tp-1][i] = stand[tp-1][i-1]
stand[tp-1][i-1] = 0
else:
print "that space is taken"

display_game()

while True:
move_count=move_count+1
print "move : " + str(move_count)
peg=0
location=0
disk=0

while disk == 0:
print "no disk"
peg=int(input("select a peg:"))
location=int(input("select a location:"))
if peg < 4 and location < 4:
disk=get_the_disk(peg,location)
else:
print "invalid selection"

npeg=0
nlocation=0
next_location=0

npeg=int(input("select a dest peg:"))
nlocation=int(input("select a dest location:"))
if npeg < 4 and nlocation < 4:
next_location=get_the_disk(npeg,nlocation)
else:
print "invalid selection"

print "disk : " + str(disk)
print "next_location : " + str(next_location)

if next_location == 0:
print "empty location, valid move"

if disk > next_location:
print "bigger disk on smaller"

if disk == next_location:
print "same disk"

if disk < next_location:
print "smaller disk on bigger, valid move"

change_disk(peg,location,npeg,nlocation)

display_game()

[/code]

----------------------------------------------------------------------------------
Tic Tac Toe (Python):
Create the game using python. Incomplete.

The code:

[code]
#!/usr/bin/python

#draw a table

print "TicTacToe"

#draw a line through matching (O)s or (X)s thats in line
#has to be 3 in a row

table= """
1|2|3
4|5|6
7|8|9
"""

player1="0"
player2="X"
end=False

positions=["1","2","3","4","5","6","7","8","9"]

def draw_table():
print table

def determin_winner():
endd=False
print "detirmin winner:"
print positions
print positions
print positions
if positions == "X" and positions == "X" and positions == "X":
print "X wins"
endd=True
elif positions == "X" and positions == "X" and positions == "X":
print "X wins"
endd=True
elif positions == "X" and positions == "X" and positions == "X":
print "X wins"
endd=True
elif positions == "X" and positions == "X" and positions == "X":
print "X wins"
endd=True
elif positions == "X" and positions == "X" and positions == "X":
print "X wins"
endd=True
elif positions == "X" and positions == "X" and positions == "X":
print "X wins"
endd=True
else:
endd=False
return endd

draw_table()
player=""
pp=str(input("choose player 0) 0 or 1) X"))

if pp == "0":
player=player1
else:
player=player2

while end == False:
p=input("choose position:")
table = table.replace(str(p),player)
positions[p-1]=player
end=determin_winner()
draw_table()

[/code]

----------------------------------------------------------------------------------
==================================================================================
```