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

Question:

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  7n0'

    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())
Asked By: GWBrickner

||

Answers:

You can do 2 in positionOfCheckers and this will return true if there is a 2 in the array, otherwise it will return false.

Answered By: Alec Petersen

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
Answered By: tsusdere
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.