Contents

  1. mystery.py
  2. tictactoe2.py
  3. tictactoe.py
  4. twod_exercises.py

mystery.py 1/4

[
top][prev][next]
# Practice with 2D lists
# by Sara Sprenkle, 12.03.2007

def main():
    matrix = createMatrix()
    print "Before:"
    print matrix
    mystery(matrix)
    print "After:"
    print matrix

def mystery(a):
    """ "run" this on A, at right """
    for row in range( len(a) ):
	for col in range( len(a[0]) ):
	    if row == col:
		a[row][col] = 42
	    else:
		a[row][col] += 1

def createMatrix():
    a0 = range(1,5)
    a1 = range(5,9)
    a2 = range(9,13)
    a = []
    a.append(a0)
    a.append(a1)
    a.append(a2)
    return a

main()

tictactoe2.py 2/4

[
top][prev][next]
# Practice using 2D lists using Tic-Tac-Toe
# NOTE: Does not handle tie games cleanly
# by Sara Sprenkle, 12.03.2007

def main():
    board = createBoard()
    printBoard(board)
    
    symbol = "o"
    while not hasWon(board):
        if symbol == "x":
            symbol = "o"
        else:
            symbol = "x"
        print "Player", symbol,"'s turn: "
        row = input("Pick the row (0,2): ")
        col = input("Pick the col (0,2): ")
        
        pickPos(board, row, col, symbol)
        printBoard(board)
        
        # TODO: Need to handle ties!
        
    print "Game over!"
    print "Player", symbol, "won"

def createBoard():
    board = []
    for row in xrange(3):
        row = []
        for col in xrange(3):
            row.append(" ")
        board.append(row)
    return board

def printBoard(board):
    for row in xrange(2):
        for col in xrange(2):
            print "%1s |" % board[row][col],
        print "%1s" % board[row][2]
        print "-"*9
    for col in xrange(2):
        print "%1s |" % board[2][col],
    print "%1s" % board[2][2]
        

def pickPos(board, row, col, symbol):
    # Should handle that the position is already taken
    board[row][col] = symbol
    
def hasWon(board):
    # horizontal
    for row in xrange(3):
        if board[row][0] != " ":
            if board[row][0] == board[row][1] and board[row][1] == board[row][2]:
                print "Found horizontal", row
                return True

    # vertical
    for col in xrange(3):
        if board[0][col] != " ":
            if board[0][col] == board[1][col] and board[1][col] == board[2][col]:
                print "Found horizontal", row
                return True
            
    # diagonal left to right
    if board[0][0] != " " and board[0][0] == board[1][1] and board[1][1] == board[2][2]:
        return True

    if board[0][2] != " " and board[0][2] == board[1][1] and board[1][1] == board[2][0]:
        return True

    return False

main()

tictactoe.py 3/4

[
top][prev][next]
# Practice using 2D lists using Tic-Tac-Toe
# by Sara Sprenkle, 12.03.2007

def main():
    board = createBoard()
    printBoard(board)
    print
    pickPos(board, 1, 1, "x")
    printBoard(board)

    print
    pickPos(board, 0, 2, "o")
    printBoard(board)


def createBoard():
    board = []
    for row in xrange(3):
        row = []
        for col in xrange(3):
            row.append(" ")
        board.append(row)
    return board

def printBoard(board):
    for row in xrange(2):
        for col in xrange(2):
            print "%1s |" % board[row][col],
        print "%1s" % board[row][2]
        print "-"*9
    for col in xrange(2):
        print "%1s |" % board[2][col],
    print "%1s" % board[2][2]
        

def pickPos(board, row, col, symbol):
    # Should handle that position is already taken
    board[row][col] = symbol
   

main()

twod_exercises.py 4/4

[
top][prev][next]
# Practice with 2D Lists
# Sara Sprenkle, 12.03.2007

def main():
    rows = input("How many rows? ")
    columns = input("How many columns? ")

    print "Correct Matrix Creation:"
    matrix = create2DList(rows, columns)
    print matrix

    matrix[1][2] = 3
    print matrix

    print "Incorrect Matrix Creation:"
    matrix = noCreate2DList(rows, columns)
    print matrix

    matrix[1][2] = 3
    print matrix


def create2DList(rows, cols):
    twodlist = []
    for row in xrange(rows):
        row = []
        for col in xrange(cols):
            row.append(0)
        twodlist.append(row)
    return twodlist

def noCreate2DList(rows, cols):
    twodlist = []
    onerow = []
    for col in xrange(cols):
        onerow.append(0)
    for row in xrange(rows):
        twodlist.append(onerow)
        
    return twodlist

    

main()

Generated by GNU enscript 1.6.4.