0

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())
James K
  • 3,692
  • 1
  • 28
  • 36

2 Answers2

0

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

0

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
tsusdere
  • 51
  • 3
  • ["In" operator for numpy arrays?](https://stackoverflow.com/questions/39452843/in-operator-for-numpy-arrays) – wwii Aug 31 '22 at 22:37