Returns the 4-character string of the animal for the piece at (x, y) on the board. This string will end with a ! if the piece is afraid of another animal on the board.
(board, x, y)
| 167 | |
| 168 | |
| 169 | def getAnimalStr(board, x, y): |
| 170 | """Returns the 4-character string of the animal for the piece at |
| 171 | (x, y) on the board. This string will end with a ! if the piece |
| 172 | is afraid of another animal on the board.""" |
| 173 | piece = board[(x, y)] |
| 174 | |
| 175 | for offsetX, offsetY in FEARED_ANIMAL_DIRECTIONS[piece]: |
| 176 | # Check the directions of the animal this piece is afraid of: |
| 177 | checkX, checkY = x, y # Start at the piece's location. |
| 178 | while True: |
| 179 | # The space check moves further in the current direction: |
| 180 | checkX += offsetX |
| 181 | checkY += offsetY |
| 182 | if not isOnBoard(checkX, checkY): |
| 183 | break # This space is off-board, so stop checking. |
| 184 | if board[(checkX, checkY)] == FEARED_PIECE[piece]: |
| 185 | return piece[0:-1] + '!' # This piece is afraid. |
| 186 | elif board[(checkX, checkY)] != EMPTY_SPACE: |
| 187 | break # Another animal is blocking any feared animals. |
| 188 | elif board[(checkX, checkY)] == EMPTY_SPACE: |
| 189 | continue # This space is empty, so keep checking. |
| 190 | return piece # This piece is not afraid. |
| 191 | |
| 192 | |
| 193 | def isOnBoard(x, y): |
no test coverage detected