Swapping two characters in a 2D array in python?
Question: So, I'm brand new to programming, and this is frustrating me! What I want to
do is be able to import a 4x8 text file, and turn the text into a 2D list so
that I can swap two characters. For example, if the imported text file looks
like this:

    
    
    OOOOOOOO
    OOOXOOOO
    OOOOOOOO
    OOOOOOOO
    

then I would like to be able to change the position of the X (the row/column
location) when user input is entered, such that an O will get put in its place
to preserve formatting. So, for exapmle, the program will prompt the user for
their input and if the user enters "up," then the X will move one space up.

    
    
    OOOXOOOO
    OOOOOOOO
    OOOOOOOO
    OOOOOOOO
    

I want it to repeatedly prompt for a new move after each time one is made, and
display the new grid each time (so you can see the X in its new position each
time you enter a movement).

This is all I have so far. I know I need to first find the X, but I really
don't know how to. I'm stuck here. Any help is appreciated.

    
    
    #Global variables for the movements
    UP = 8
    DOWN = 2
    RIGHT = 6
    LEFT = 4
    
    #Dimensions of grid
    ROWS = 4
    COLUMNS = 8
    
    def start():        
        filename = input("Enter the name of the Start Positions file: ")
        textFile = open(filename)
        aMaze = [line.strip() for line in textFile]
        for r in range(ROWS):
            for c in range(COLUMNS):
                print(aMaze[r][c], end="")
            print()
    
    def moveType():
        while (True):
            try:
                move = input("ENTER YOUR MOVE: ") 
            except ValueError:
                print("unimportant error message")
                continue
            if ((int(move)) in (DOWN, LEFT, RIGHT, UP)):
                playerMove(move)
                continue
            else:
                print("unimportant error message")
                continue            
        return(move)
    
    def playerMove(move):
        move = (int(move))
        if (move == DOWN):
            #need help here
        elif (move == UP):
            #and here
        elif (move == LEFT):
            #don't know what i'm doing
        elif (move == RIGHT):
            #also here
    
    start()
    moveType()
    


Answer: This is a perfect opportunity to learn about abstraction. To solve your
problem, think about the sub problems you could solve (with functions) that
would make your final problem easier.

In your specific instance, wouldn't it be easier to write a program to find
the Cartesian coordinates of where X is? With an (x,y) coordinate, you could
then make a function to turn that coordinate (likely stored as a tuple) into a
2d array where that coordinate is an X an everything else is a zero.

Hint 1:

    
    
    x =0
    y =0
    for row in numrows:
        for col in numcols:
            if array[row][col] == X
                y = row
                x = col
    

Hint 2:

    
    
    for row in numrows:
        for col in numcols:
            if col is x and row is y:
                place X
            else:
                place O
    

Note: if this were an application where you wanted to eek out every bit of
performance, you certainly would not need to iterate through your array every
time to find X. You could (and should) opt to store the location of X and then
use two accesses into your array to flip X's and O's. But seeing as this is
likely one of your first problems you are solving this is of course not a
concern.

Hope this helps! Good luck starting to code!

