Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way that I can parse a numpy array and see if there is a certain value?

I have a numpy array that I am using in order to store the position of checkers on a checker board. I am currently writing the logic in order to detect whether the player has won depending on if there are any enemy pieces on the board. Code is included, any help is appreciated. I guess I have to write more, the one sides checkers are stored as 1 and the other sides checkers is stored as 2. I want to see if there is any way to see if there are any ones left in the array or if there are any twos in the array, and exit the loop afterwards

# poop code :)

import numpy as np

# initial board state
positionOfCheckers = np.array([
    [0, 2, 0, 2, 0, 2, 0, 2],
    [2, 0, 2, 0, 2, 0, 2, 0],
    [0, 2, 0, 2, 0, 2, 0, 2],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [1, 0, 1, 0, 1, 0, 1, 0],
    [0, 1, 0, 1, 0, 1, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 0]])


# prints current checkerboard

def printCheckerBoard(position_of_checkers):
    posArrayX = 0
    posArrayY = 0
    checkerBoard = '  0  1  2  3  4  5  6  7\n0'

    for x in range(64):

        currentPiece = position_of_checkers[posArrayY, posArrayX]

        if currentPiece == 1:
            checkerBoard = checkerBoard + " O "

        elif currentPiece == 2:
            checkerBoard = checkerBoard + " o "

        else:
            checkerBoard = checkerBoard + "   "

        if posArrayX == 7:
            posArrayX = 0
            posArrayY += 1
            checkerBoard = checkerBoard + '\n' + str(posArrayY)

        else:
            posArrayX += 1

    return checkerBoard


# take move input and logic uppercase
def tmialu():
    movePieceX = int(input('Input X Value of Piece'))
    movePieceY = int(input('Input Y Value of Piece'))
    movePlaceX = int(input('Input X Value of Destination'))
    movePlaceY = int(input('Input Y Value of Destination'))

    # if piece is on square
    if positionOfCheckers[movePieceY, movePieceX] == 1:

        # if target square is empty
        if positionOfCheckers[movePlaceY, movePlaceX] == 0:

            # making sure that you are moving up and left
            if movePieceX - movePlaceX == 1 & movePieceY - movePlaceY == 1:

                print('cond satisfied')
                positionOfCheckers[movePieceY, movePieceX] = 0
                positionOfCheckers[movePlaceY, movePlaceX] = 1

            # making sure that you are moving up and right
            elif movePlaceX - movePieceX == 1 & movePieceY - movePlaceY == 1:

                print('cond satisfied')
                positionOfCheckers[movePieceY, movePieceX] = 0
                positionOfCheckers[movePlaceY, movePlaceX] = 1

        # else if the target piece is holding an enemy piece
        elif positionOfCheckers[movePlaceY, movePlaceX] == 2:

            # making sure its moving in a legal direction, up and left
            if movePieceX - movePlaceX == 1 & movePieceY - movePlaceY == 1:
                if positionOfCheckers[movePlaceY - 1, movePlaceX - 1] == 0:
                    print('cond satisfied')
                    positionOfCheckers[movePieceY, movePieceX] = 0
                    positionOfCheckers[movePlaceY, movePlaceX] = 0
                    positionOfCheckers[movePlaceY - 1, movePlaceX - 1] = 1

            # making sure its moving in a legal direction, up and right
            elif movePlaceX - movePieceX == 1 & movePieceY - movePlaceY == 1:
                if positionOfCheckers[movePlaceY - 1, movePlaceX + 1] == 0:
                    print('cond satisfied')
                    positionOfCheckers[movePieceY, movePieceX] = 0
                    positionOfCheckers[movePlaceY, movePlaceX] = 0
                    positionOfCheckers[movePlaceY - 1, movePlaceX + 1] = 1

    return positionOfCheckers

# take input and logic lowercase
def tmiall():
    movePieceX = int(input('Input X Value of Piece'))
    movePieceY = int(input('Input Y Value of Piece'))
    movePlaceX = int(input('Input X Value of Destination'))
    movePlaceY = int(input('Input Y Value of Destination'))

    # if piece is on square
    if positionOfCheckers[movePieceY, movePieceX] == 2:

        # if target square is empty
        if positionOfCheckers[movePlaceY, movePlaceX] == 0:

            # making sure that you are moving down and left
            if movePieceX - movePlaceX == 1 & movePlaceY - movePieceY == 1:

                print('cond satisfied')
                positionOfCheckers[movePieceY, movePieceX] = 0
                positionOfCheckers[movePlaceY, movePlaceX] = 2

            # making sure that you are moving down and right
            elif movePlaceX - movePieceX == 1 & movePlaceY - movePieceY == 1:

                print('cond satisfied')
                positionOfCheckers[movePieceY, movePieceX] = 0
                positionOfCheckers[movePlaceY, movePlaceX] = 2

        # else if the target piece is holding an enemy piece
        elif positionOfCheckers[movePlaceY, movePlaceX] == 1:

            # making sure its moving in a legal direction, up and left
            if movePieceX - movePlaceX == 1 & movePlaceY - movePieceY == 1:

                if positionOfCheckers[movePlaceY + 1, movePlaceX - 1] == 0:
                    print('cond satisfied')
                    positionOfCheckers[movePieceY, movePieceX] = 0
                    positionOfCheckers[movePlaceY, movePlaceX] = 0
                    positionOfCheckers[movePlaceY + 1, movePlaceX - 1] = 2

            # making sure it's moving in a legal direction, up and right
            elif movePlaceX - movePieceX == 1 & movePlaceY - movePieceY == 1:

                if positionOfCheckers[movePlaceY + 1, movePlaceX + 1] == 0:
                    print('cond satisfied')
                    positionOfCheckers[movePieceY, movePieceX] = 0
                    positionOfCheckers[movePlaceY, movePlaceX] = 0
                    positionOfCheckers[movePlaceY + 1, movePlaceX + 1] = 2


        return positionOfCheckers

while True:
  tmialu()
  print(printCheckerBoard())
  tmiall()
  print(printCheckerBoard())

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can just use the in operator to check.

1 in positionOfCheckers # True if there is a single 1 in array
2 in positionOfCheckers # True if there is a single 2 in array
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading