# Move  moves a marker up, down, left, right, on a ``square'' game board.
# Inputs:  a sequence of moves,  u,d,l,r
# Outputs: the status of the game board after each move

# Data structure:  a list models the ``square'' board, which is printed
#   one ``row'' per line on the display
ROWSIZE = 3
BOARDSIZE = ROWSIZE * ROWSIZE
board = BOARDSIZE * ["_"]  # the game board

Xposition = BOARDSIZE / 2
board[Xposition] = "X"   # place X marker in middle of board

while True :
    # invariant:  board  remembers current position of X marker

    # print board:
    count = 0;
    while count < len(board) :
        if count % ROWSIZE == 0 :
            print
        print board[count],
        count = count + 1
    print

    # ask player for next move:
    move = raw_input("Move (u,d,l,r): ")

    OKmove = True 
    # process the move:
    if move == "l" :
        new_position = Xposition - 1
        if Xposition % ROWSIZE == 0 :
            OKmove = False
    elif move == "r" :
        new_position = Xposition + 1
        if new_position % ROWSIZE == 0 :
            OKmove = False
    elif move == "u" :
        new_position = Xposition - ROWSIZE
        if new_position < 0 :
            OKmove = False
    elif move == "d" :
        new_position = Xposition + ROWSIZE
        if new_position >= BOARDSIZE :
            OKmove = False
    else :
        OKmove = False

    if OKmove :
        board[new_position] = "X"
        board[Xposition] = "_"
        Xposition = new_position
    else :
        print "Illegal move"


# Finished.
