Reputation: 65
I am running into an issue while I am iterating through my 2D array. I am going through my 4X3 grid and changing the requested coordinate from one character to another, rather "deleting" 'B' or 'W' and instead leaving nothing( the ' - ' character). I wont get into the niddy griddy of the code, but what I can say that I am currently working with this grid:
B W B W <= trying => A1 A2 A3 A4 W B W B to get these to B1 B2 B3 B4 B W B W <=correspond=> C1 C2 C3 C4
B = black game pieces W = white game pieces
Here is a snip of code so you can see where I am going with it, then I will post what I want to happen.
removeB = input("~BLACK Player, remove one of your 'B's by typing in the coordinance: ") print("") removeW = input("~WHITE Player, remove one of your 'W's by typing in the coordinance: ") print("") newgrid = copy_grid(board) for r in range(numrows): for c in range(numcols): if(removeB == 'A1'): newgrid[r][c] = '-' elif(removeB == 'A2'): newgrid[r][c+1] = '-' elif(removeB == 'A3'): newgrid[r][c+2] = '-' elif(removeB == 'A4'): newgrid[r][c+3] = '-' elif(removeB == 'B1'): newgrid[r+1][c] = '-' etc....etc...etc...' if(removeW == 'A1'): newgrid[r-1][c-1] = '-' elif(removeW == 'A2'): newgrid[r-1][c-1] = '-' elif(removeW == 'A3'): newgrid[r-1][c+1] = '-' elif(removeW == 'A4'): newgrid[r-1][c+2] = '-' elif(removeW == 'B1'): newgrid[r][c-1] = '-' etc....etc...etc...
You will see that once I go into the 2nd if statement that I i change the formatting of the newgrid coordinate checking as far as index values. I did this for testing purposes as I now know that I receive the same error message for both methods. Hence my confusion.... Also, I know that I will run into an issue because inside both if/else statements, I am checking a location even if it is not that player's Color. I will get to this once I get the coordinates to cooperate.
Here is the output that I want when removeB= A1 and removeW = C4 :
- W B W W B W B B W B -
Here is the error message :
Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> initial() File "C:\Users\Ted\Desktop\Lab3.1Final.py", line 84, in initial newgrid[r][c] = '-' IndexError: list index out of range
Line 84 is the line right after the very first if(removeB == 'A1') executes and it goes to change the value at the desired coordinates.
Please let me know if I should submit any other code pertaining to this question if it would help in providing the best answers. Thanks in advance!
Upvotes: 0
Views: 69
Reputation: 96277
You can go about this much more simply:
In [1]: grid = [['B','W','B','W'],['W','B','W','B'],['B','W','B','W']]
In [2]: def show_grid(grid):
...: print(*(" ".join(row) for row in grid),sep='\n')
...:
In [3]: def delete_at_coordinate(grid,coordinate):
...: row = ord(coordinate[0]) - 65
...: col = int(coordinate[1]) - 1
...: grid[row][col] = '-'
...:
In [4]: removeB = 'A1'
In [5]: removeA = 'C4'
In [6]: show_grid(grid)
B W B W
W B W B
B W B W
In [7]: delete_at_coordinate(grid,removeB)
In [8]: delete_at_coordinate(grid,removeA)
In [9]: show_grid(grid)
- W B W
W B W B
B W B -
Upvotes: 1