Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. import argparse
  2. import json
  3. import api
  4.  
  5.  
  6. def analyser_commande():
  7.     parser = argparse.ArgumentParser(description="Jeu Quoridor - phase 1")
  8.     parser.add_argument('idul', help='IDUL du joueur')  
  9.     parser.add_argument(
  10.     '-l', '--lister', action = 'store_true',
  11.     help="Lister les identifiants de vos 20 dernières parties."
  12.     )
  13.     return parser.parse_args()
  14.  
  15.  
  16. print(analyser_commande())
  17.  
  18.  
  19. def afficher_damier_ascii(dico):
  20. """Description:
  21.    Une fonction qui permet d'afficher un damier complet."""
  22.     res = f'Légende: 1={dico["joueurs"][0]["nom"]}, 2={dico["joueurs"][1]["nom"]}' + '\n' + '   ' + '-'*35 + '\n'
  23.     for y in range(18, 1, -1):
  24.         if y % 2 == 0:
  25.             res += str(int(y / 2)) + ' | '
  26.             for x in range(0, 8, 1):
  27.                 if dico["joueurs"][0]["pos"] == [x + 1, y / 2]:
  28.                     res += '1   '
  29.                 elif dico["joueurs"][1]["pos"] == [x + 1, y / 2]:
  30.                     res += '2   '
  31.                 else:
  32.                     mur_présent2 = False
  33.                     mur_présent5 = False
  34.                     for mur_vertical in dico["murs"]["verticaux"]:
  35.                         if [x + 2, (y) // 2] == mur_vertical:
  36.                             mur_présent2 = True
  37.                         if [x + 2, ((y) // 2)-1] == mur_vertical:
  38.                             mur_présent5 = True
  39.                     if mur_présent2 == True:
  40.                         res += '. | '
  41.                     elif mur_présent5 == True:
  42.                         res += '. | '
  43.                     else:
  44.                         res += '.   '
  45.             if dico["joueurs"][0]["pos"] == [9, y]:
  46.                 res += '1 ' + '|'
  47.             elif dico["joueurs"][1]["pos"] == [9, y]:
  48.                 res += '2 ' + '|'
  49.             else:
  50.                 res += '. ' + '|'                
  51.         else:
  52.             res += '\n' + '  |'
  53.             for x in range(1, 10):
  54.                 if x != 9:
  55.                     murs_présent4 = False
  56.                     murs_présent3 = False
  57.                     for mur_horizontal in dico["murs"]["horizontaux"]:
  58.                         if [x - 1, (y + 1) // 2] == mur_horizontal:
  59.                             murs_présent3 = True
  60.                         if mur_horizontal == [x, (y + 1) // 2]:
  61.                             murs_présent4 = True
  62.                     if murs_présent4 == True:
  63.                         res += '-' * 4
  64.                     elif murs_présent3 == True:
  65.                         mur_présent6 = False
  66.                         for mur_vertical in dico["murs"]["verticaux"]:
  67.                             if [x + 1, (y) // 2] == mur_vertical:
  68.                                 mur_présent6 = True
  69.                         if mur_présent6 == True:
  70.                             res += '---|'
  71.                         else:
  72.                             res += '--- '
  73.                     else:
  74.                         mur_présent1 = False
  75.                         for mur_vertical in dico["murs"]["verticaux"]:
  76.                             if [x + 1, (y) // 2] == mur_vertical:
  77.                                 mur_présent1 = True
  78.                         if mur_présent1 == True:
  79.                             res += '   |'
  80.                         else:
  81.                             res += ' ' * 4
  82.                 else:
  83.                     res += '   |' + '\n'
  84.     res += '\n' + '--|-----------------------------------' + '\n' + '  | 1   2   3   4   5   6   7   8   9'
  85.     return res
  86.  
  87.  
  88. if __name__ == '__main__':
  89.     analyser = analyser_commande()
  90.     if analyser.lister:
  91.         print(api.lister_parties(analyser.idul))
  92.     else:
  93.         afficher_damier_ascii(api.débuter_partie(analyser.idul)[1])
  94.         id_partie = api.débuter_partie(analyser.idul)[0]
  95.         pas_victoire = True
  96.         while pas_victoire:
  97.             type_coup = input("entrer le type de coup (D, MH ou MV)")
  98.             position = input("entrer la position sous forme de tuple")
  99.             choix_jeu = True
  100.             while choix_jeu:
  101.                 try:
  102.                     afficher_damier_ascii(api.jouer_coup(id_partie, type_coup, position))
  103.                     choix_jeu = False
  104.                 except StopIteration as fin:
  105.                     pas_victoire = False
  106.                     choix_jeu = False
  107.                     print(f'Victoire de {fin}')
  108.                 except RuntimeError as re:
  109.                     choix_jeu = False
  110.                     print(re)
  111.  
  112.  
  113. print(afficher_damier_ascii({
  114.     "joueurs": [
  115.         {"nom": "idul", "murs": 7, "pos": [5, 5]},
  116.         {"nom": "automate", "murs": 3, "pos": [3, 8]}
  117.     ],
  118.     "murs": {
  119.         "horizontaux": [[4, 4], [2, 6], [3, 8], [5, 8], [7, 8]],
  120.         "verticaux": [[6, 2], [4, 4], [2, 6], [7, 5], [7, 7]]
  121.     }
  122. }
  123. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement