Advertisement
mmouhib

Untitled

Oct 8th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import os
  2. os.system('cls')
  3.  
  4. #saisie des indices du reine
  5. def saisie():
  6.     while 1:
  7.         x = input()
  8.         if x.isdigit():
  9.             x = int(x)
  10.             if x >= 0 and x < 8:
  11.                 return x
  12.             else:
  13.                 print("! valeur doit etre entre 0 et 7")
  14.         else:
  15.             print('! valeur doit etre entre un entier')
  16.  
  17.  
  18. #remplir la table par '-'
  19. def init_board():
  20.     board = []
  21.     for i in range(8):
  22.         sub_board = []
  23.         for j in range(8):
  24.             sub_board.append('-')
  25.         board.append(sub_board)
  26.     return board
  27.  
  28. #afficher la table
  29. def board_printer(board):                                                              
  30.     print('-' * 27)
  31.     #le role du variable 'start' est de connaitre si on
  32.     #est sur le debut du ligne pour ajouter le '|'
  33.     start  = 1
  34.     for sub_board_one in board:
  35.         for sub_board_two in sub_board_one:
  36.             if start:
  37.                 print('|', sub_board_two, ' ', end = '')
  38.                 start = 0
  39.             else:
  40.                 print(sub_board_two, ' ', end = '')
  41.         print('|')
  42.         start = 1
  43.     print('-' * 27)
  44.  
  45. #remplir l'axe de X et Y d'indice du reine
  46. def plus(board,l,c):
  47.     for i in range(0,8):
  48.         if not i == c:
  49.             board[l][i] = 'X'
  50.     for i in range(0,8):
  51.         if not i == l:
  52.             board[i][c] = 'X'
  53.  
  54.  
  55. #remplir le diagonal d'indice du reine
  56. def diagonal(board,l,c):
  57.     #top left
  58.     l1,c1 = l-1,c-1
  59.     while l1 >= 0 and c1 >= 0:
  60.         board[l1][c1] = 'X'
  61.         l1 -= 1
  62.         c1 -= 1
  63.  
  64.     #right down
  65.     l1,c1 = l+1,c+1
  66.     while l1 < 8 and c1 < 8:
  67.         board[l1][c1] = 'X'
  68.         l1 += 1
  69.         c1 += 1
  70.  
  71.     #top right
  72.     l1,c1 = l-1,c+1
  73.     while l1 >= 0 and c1 < 8:
  74.         board[l1][c1] = 'X'
  75.         l1 -= 1
  76.         c1 += 1
  77.  
  78.     #down left
  79.     l1,c1 = l+1,c-1
  80.     while l1 < 8 and c1 >= 0:
  81.         board[l1][c1] = 'X'
  82.         l1 += 1
  83.         c1 -= 1
  84.            
  85.  
  86. def main():
  87.     print("saisie N° du ligne")
  88.     l = saisie()
  89.     print("saisie N° du colonne")
  90.     c = saisie()
  91.     os.system('cls')
  92.     board = init_board()
  93.     board[l][c] = 'R'
  94.     plus(board,l,c)
  95.     diagonal(board,l,c)
  96.     board_printer(board)
  97.  
  98.  
  99. if __name__ == '__main__':
  100.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement