Home > Back-end >  How would I search a single row in a numpy array to see if it has a specified value?
How would I search a single row in a numpy array to see if it has a specified value?

Time:09-03

I am programming a simple checkers game and to see if either side's pieces have reached the other side I want to check if the row has a specific value. Included are the pieces' definitions: 1 and 2 are pawns, 3 and 4 are kings, and 0 is a blank space.

positionOfCheckers = np.array([
    [0, 0, 0, 2, 0, 2, 0, 2],
    [2, 0, 1, 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]])

pieceDict = {
  0:"   ",
  1:" o ",
  2:" x ",
  3:" O ",
  4:" X "
}

CodePudding user response:

IIUC, you want to transform the pawns in kings if they reached the other side?

Assuming 1 is bottom.

You can use simple indexing:

positionOfCheckers[0, positionOfCheckers[0] == 1] = 3
positionOfCheckers[-1, positionOfCheckers[-1] == 2] = 4

example input (modified):

positionOfCheckers = np.array([
    [0, 1, 0, 2, 0, 2, 0, 2],
    [2, 0, 1, 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, 2, 0]])

output:

array([[0, 3, 0, 2, 0, 2, 0, 2],
       [2, 0, 1, 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, 4, 0]])

Visual output:

print('\n'.join(map(''.join, np.vectorize(pieceDict.get)(positionOfCheckers))))

    O     x     x     x 
 x     o     x     x    
    x     x     x     x 
                        
                        
 o     o     o     o    
    o     o     o     o 
 o     o     o     X    

CodePudding user response:

import numpy as np

# note I added a 1 and 2 to the opposite sides of the board.
position_of_checkers = np.array([
    [1, 0, 0, 2, 0, 2, 0, 2],
    [2, 0, 1, 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, 2]])

piece_dict = {
  0:"   ",
  1:" o ",
  2:" x ",
  3:" O ",
  4:" X "
}

# goal 1:  check if any 'o' players are on the top row
# goal 2:  check if any 'x' players are on the bottom row

o_value = 1
O_value = 3
x_value = 2
X_value = 4

if o_value in position_of_checkers[0, :]:
    print("There is an \'o\' pawn on the other side!")
    
    # now do stuff here that you have found there is a pawn
    # such as replacing the pawn to be a king
    mask = position_of_checkers[0, :] == o_value
    position_of_checkers[0, :] = np.where(mask, O_value, position_of_checkers[0, :])
    
if x_value in position_of_checkers[7, :]:
    print("There is an \'x\' pawn on the other side!")
    
    # now do stuff here that you have found there is a pawn
    # such as replacing the pawn to be a king
    mask = position_of_checkers[7, :] == x_value
    position_of_checkers[7, :] = np.where(mask, X_value, position_of_checkers[7, :])

print(position_of_checkers)

There are multiple ways to go about it, @mozway 's answer is shorter code as well. Here's another way of going about it.

  • Related