2

To make a board game using a 2D numpy array, user to input a number and I need X & Y value of that number being returned in separate variables.

This is how I'd set up the array

board = np.array(["01","02","03","04","05","06","07","08",
                      "09","10","11","12","13","14","15","16",
                      "17","18","19","20","21","22","23","24",
                      "25","26","  ","@@","29","30","31","32",
                      "33","34","35","@@","  ","38","39","40",
                      "41","42","43","44","45","46","47","48",
                      "49","50","51","52","53","54","55","56",
                      "57","58","59","60","61","62","63","64"])
board = board.reshape(8,8)

1 Answer 1

5

It could be done in many ways. One of it could be as follows:

value = input("Enter the board value")  # This allows the user to enter the value

found = np.squeeze(np.where(value == board))  # This numpy function finds where the array is equal to the board value

if found.size > 0:  # checks if the found value is not empty
    print('The value is present in the board at {}, {}'.format(found[0], found[1]))
else:
    print('Enter a valid input')
Sign up to request clarification or add additional context in comments.

3 Comments

@WilliamKavanagh Glad to know. However, it would throw an unclear result for a double occurence of the @@. If you want that as a special purpose case, you can add it as else if (in python: elif) case. Do you want me to take care of that? or you want to take it as a learning process and figure that out yourself. I'd suggest the later ;)
Due to the nature of the array I know it wont throw a double array but thank you for the offer. Im taking it as a learning experience anyway because I feel im going to be using numpy alot
in this code something would have to go very wrong for a double occurence but thank you for the offer. I think il take it as a learning experience for now. Dont wanna become to reliant on this website

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.