Ted - 1 year ago 50

Python Question

I am wondering how I can go about condensing these elif statements into a method of some sorts. I also don't know how to go about storing a chosen coordinate so that I can perform checks of surrounding coordinates. I know my code is nooby, but so am I, I learn better starting with the long way :)

Below is how I'm going about storing a coordinate inside a variable. (Not sure this is even the right way to do it yet...)

`grab = board[x][y]`

if(SjumpX == 'A1'):

grab = [0][0]

elif(SjumpX == 'A2'):

grab = [0][1]

elif(SjumpX == 'A3'):

grab = [0][2]

elif(SjumpX == 'A4'):

grab = [0][3]

elif(SjumpX == 'B1'):

grab = [1][0]

elif(SjumpX == 'B2'):

grab = [1][1]

elif(SjumpX == 'B3'):

grab = [1][2]

elif(SjumpX == 'B4'):

grab = [1][3]

elif(SjumpX == 'C1'):

grab = [2][0]

elif(SjumpX == 'C2'):

grab = [2][1]

elif(SjumpX == 'C3'):

grab = [2][2]

elif(SjumpX == 'C4'):

grab = [2][3]

SjumpX is the coordinate of the piece my player wants to grab, and DjumpX is the coordinate of the destination. My logic behind this is if the player enters a coordinate(ie A1 B2 C3...), I can then store that coordinate into the variable 'grab', then use that variable to test if the destination coordinate is empty, also if the coordinate between the two is the an opposing players piece.

Here is the board:

`1 2 3 4`

A - X O X

B X O - O

C O X O X

This where I'm checking that the "jumpable" destination coordinates are empty based on the current coordinates of my 'grab' variable. In this case 'A3' <==> grab = [0][2]

`if((grab[x][y-2] == '-' or grab[x][y+2] == '-' or grab[x-2][y] == '-' or grab[x+2][y] == '-')and`

(grab[x][y-1] == 'X' or grab[x][y+1] == 'X' or grab[x-1][y] == 'X' or grab[x+1][y] == 'X'):

My main Questions Are:

1- How do I condense my huge elif statement list?

2- What is the correct format/process to store a coordinate to perform checks on surrounding coordinate content?

3- How can I condense my if statement that checks to see if the destination coordinate is empty('-').

Answer Source

we can make a map then using it we can initialize the grap ie

```
field_map = {'A1':(0,0),'A2':(0,0)......}
if SjumpX in field_map.keys():
x,y = field_map[SjumpX]
grab = [x][y]
```

i think i helps