Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. def splitting(board): #<--splitting board on 2D matrix
  2.     for row in range(len(board)):
  3.          board[row]=list(board[row])
  4.     print(board)
  5.     return board
  6.  
  7. def find_char(matrix,char): #<-- looking for position of jaffar's pawn
  8.     jaffars_pawn = [(index, row.index(char)) for index, row in enumerate(matrix) if char in row]
  9.     print(jaffars_pawn)
  10.     return jaffars_pawn #<-- return as tupel in list, in case of many pawns but only one in signle row
  11.    
  12. def attack(jaffar_position,board): #<- main function which ger points when Jaffar's pawn are able to beat Aladdin pawn
  13.     board = splitting(board)
  14.     len_board = len(board)
  15.     y , x = jaffar_position[0]
  16.     points = 0
  17.     play = True
  18.     while play: #<-loop until next beat dosen't occur
  19.         if ((x+2<len_board-1 or x-2 >= 0) and y-2>=0): #<- chceking that attack jaffar's pawn dosen't move to the outside of board
  20.            
  21.             if (board[y-1][x+1] == "X" and not board[y-2][x+2] == "X"):#<- checking if jaffar's pawn can attack up-right direction
  22.                 points+=1
  23.                 x , y = x + 2, y - 2
  24.                 continue
  25.            
  26.             if (board[y-1][x-1] == "X" and not board[y-2][x-2] == "X"): #<- checking if jaffar's pawn can attack up-left direction
  27.                 points+=1 #<- counting points
  28.                 x ,y = x - 2, y - 2 #<- move jaffar's pawn
  29.                 continue
  30.            
  31.             else: play=False #<- when Jaffar's pawn can't attack next Aladdin's pawn
  32.            
  33.         else: play=False
  34.        
  35.     return points
  36.    
  37.    
  38. def solution(B):
  39.     jaffar_char="O"
  40.     return attack(find_char(splitting(B),jaffar_char),B)
  41.  
  42. A =['.X.....',
  43.    '..X.X..',
  44.    '.X..X..',
  45.    '....X..',
  46.    '...X...',
  47.    '..X.X.X',
  48.    '...O...']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement