1

I have the following matrix and I am trying to replace item {6} or any other.

cells = str(input("cache cache: "))

matrix = """
---------
| {0} {1} {2} |
| {3} {4} {5} |
| {6} {7} {8} |
---------
""".format(*cells)
print(matrix)

coordinates = [
                ("1", "3"), ("2", "3"), ("3", "3"),
                ("1", "2"), ("2", "2"), ("3", "2"),
                ("1", "1"), ("2", "1"), ("3", "1")
]


given_coordinates = input("Enter the coordinates: ").split(", ")
t_given_coordinates = tuple(given_coordinates)


for x in coordinates:
    if x == t_given_coordinates:
        print(matrix.format(*cells.replace(matrix[6], "X")))

The user is supposed to enter the coordinates that relate to the matrix, for example: The given matrix is given in the first input: X_X_O____ From here, if the user inputs coordinates (1, 1), item {6} from matrix gets replaced by 1 X. I don't know how to proceed further, I'm relatively new to Python and I can't figure out how.

2 Answers 2

2

There are two things you should do with this:

  • Create a function that prints the current playing field, given the cells. So, you can print it whenever you want.
  • Change the item of the cells list, which corresponds to the entered coordinates.

In the example below I used your code to look up the index of the cell you need to change by adding cellnum = coordinates.index(t_given_coordinates). This line defines a new integer variable that holds the cell number according to your map. You may then use this index to change the content of this cell and redraw the complete thing.

cells = ['-']*9

def print_matrix(cells):
    matrix = """
    ---------
    | {0} {1} {2} |
    | {3} {4} {5} |
    | {6} {7} {8} |
    ---------
    """.format(*cells)
    print(matrix)

coordinates = [
                ("1", "3"), ("2", "3"), ("3", "3"),
                ("1", "2"), ("2", "2"), ("3", "2"),
                ("1", "1"), ("2", "1"), ("3", "1")
]


print_matrix(cells)

end = False
while not end:
    try:
        given_coordinates = input("Enter the coordinates: ").split(", ")
        t_given_coordinates = tuple(given_coordinates)
        cellnum = coordinates.index(t_given_coordinates)
        cells[cellnum] = "X"
        print_matrix(cells)
    except:
        print("That was an invalid input")
        end = True


The output will look somewhat like that:

    ---------
    | - - - |
    | - - - |
    | - - - |
    ---------
    
Enter the coordinates: 1, 1

    ---------
    | - - - |
    | - - - |
    | X - - |
    ---------
    
Enter the coordinates: 2, 3

    ---------
    | - X - |
    | - - - |
    | X - - |
    ---------
    
Enter the coordinates: hello world
That was an invalid input

But, if I may suggest a slight change:

  • The initial filling of the cells could already be the cell indices. So, the user just has to enter the index of the next cell to fill, which is a single integer. That's much less prone to raise errors. So, when the field is empty, it just reads:
---------
| 0 1 2 |
| 3 4 5 |
| 6 7 8 |
---------
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This is what I was looking for!
0

I would represent your choises as simple flat list and calculate the position inside this list from the tuples you get as inputs like so:

def print_list(l):
    """Takes a list of 9 elements and prints them in a 3x3 matrix
    replacing None with empty spaces."""
    matrix = """
---------
| {0} {1} {2} |
| {3} {4} {5} |
| {6} {7} {8} |
---------
"""

    print(matrix.format(*(what or " " for what in l)))


# use a list to store the taken fields
choices = ["X", None, None,
           "X", "O", "O",
            None, None, None]

print_list(choices)


c = ("3","1")  # the tuple your input gave you
# create numbers from it
c = list(map(int,c))
# calculate list position
pos = (c[0]-1)*3+c[1]-1
# fill correct letter in
choices [pos] = "X"

print_list(choices)

to get

---------
| X     |
| X O O |
|       |
--------- 

---------
| X     |
| X O O |
| X     |
---------

Obviously you would need to

  • check if the position in the list is still None to avoid overwriting X by O (or reverse)
  • change the letter to be written to X or O (whoever ones turn it is)
  • determine if a winning move was made
  • ...

See f.e. Search in duckduckgo.com for python + tic tac to on stackoverflow.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.