0

I'm creating genius square in python(genius square is a board game where you fit tetris-like pieces into a square with circles in certain positions). I'm trying to find a way to convert the input to placing the circles on the grid. This is my current code

circles = input('circle positions? For example:A6-B4-F1...')
circle_list = circles.split('-')
circle_one = circle_list[0]
circle_two = circle_list[1]
circle_three = circle_list[2]
circle_four = circle_list[3]
circle_five = circle_list[4]
circle_six = circle_list[5]
circle_seven = circle_list[6]
if circle_one[0] == 'A':
    turtle.setpos(0,-50)
    if circle_one[1] == '1':
        turtle.setpos(25,-50)
        turtle.circle(25)

this would continue for all 5 letters and 5 numbers

Dylan Ayre
  • 11
  • 1
  • 2
    You don't really need `circle_one` etc variables, it's enough to loop through `circle_list` using `for`-loops. Also, if you convert `A6`, `B4` etc to integer coords, you can calculate the destination coords using the same formula. – bereal Aug 22 '22 at 10:46
  • 2
    Yes, there is a more efficient way. What way that would be would require you to describe what the code is supposed to do better. – matszwecja Aug 22 '22 at 10:48
  • 1
    it can be more useful to keep it as `circle_list` and use `circle_list[0][0] == "A"` – furas Aug 22 '22 at 12:13

1 Answers1

0

I don't know what you try to do but more useful is to keep it as circle_list because you can use for-loop to repeate code on every item in this list. And list may have any number of elements.

circle_list = circles.split('-')

for item in circle_list:

    if item[0] == 'A':
        turtle.setpos(0,-50)
        if item[1] == '1':
            turtle.setpos(25,-50)
            turtle.circle(25)

EDIT:

your input data looks like coordinates in grid and you could use dictionary to convert string A1 to values (25, -50)

circle_list = circles.split('-')

all_ys = {'A': -50, 'B': -25}
all_xs = {'1':  25, '2':  50}

for item in circle_list:

    y = all_ys.get(item[0])  # gives value from dict or `None`
    x = all_xs.get(item[1])  # gives value from dict or `None`

    if (x is None) or (y is None):
        print('wrong coordinates:', item)
    else:
        turtle.setpos(x,y)
        turtle.circle(25)
furas
  • 134,197
  • 12
  • 106
  • 148