0

For a small project battleship py3.5:

def maingame_sp():

    global board
    board = [['.' for i in range (0,10)] \
                  for j in range (0,10)]

def coordinates(board):
    x_ax = input(str("Give coordinate x"))

    y_ax = input(int("Give coordinate y"))

I am trying to build a version of battleship where I can input coordinates like A1 to the board and replacing the "." with a marker for misses and hits.

Can anyone give me a little help on how to access multiple lists inside a lists generated by the forloop range to replace an item using the input x- and y-axis?

3
  • Is A in A1 part of the x or the y-axis? Commented Mar 13, 2017 at 10:02
  • 1
    Welcome to StackOverflow. Your question needs a few changes to become acceptable. First, show what have you tried. Second, your question needs to be more clear. Do you want the input to be the coordinates in numbers, line x_ax and y_ax as in your code, or like A1 as in your text? Commented Mar 13, 2017 at 10:02
  • I was trying to figure out how i input coordinates like A1, how i do this i really dont know so i was hoping the community could help me point me in the right way. Commented Mar 14, 2017 at 13:51

2 Answers 2

1

You would have to do it like this:

def maingame_sp():

    global board
    board = [['.' for i in range (0,10)] \
                  for j in range (0,10)]

def coordinates(board):
    x_ax = input(str("Give coordinate x"))
    y_ax = input(int("Give coordinate y"))

    try:
        board[ord(x_ax)-ord('A')][ord(y_ax)-1] = "-"
    except IndexError:
        print("Wrong input")

If the axis are printing the wrong way, swap the square-brakets order:

        board[ord(y_ax)-1][ord(x_ax)-ord('A')] = "-"
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for sharing !
0

if i understand, you want access to a cell in a 2d array.
>>> a = ['a', 'b', 'c', ['1', '2']]
>>> a[3][1]
'2' `

2 Comments

Ah, i think i should be able to slam something together. What i do not fully understand however, how the list looks like from the printed array, is it like 1 list with 10 list inside it, to print a 10x10 grid ? So accessing J10 would be index 10/10 if im correct. ( x and y coordinates are printed at index -1 on the list )
for a grid who start at A1 and end at J10, A1 is at board[0][0] and J10 is at board[9][9]

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.