Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. #imports
  2. import os,sys,random,time
  3.  
  4. #var/tabs
  5. player = 0
  6. comp = 0
  7. play = False
  8. comp_memories = []
  9. AI = False
  10. p_score = 0
  11. c_score = 0
  12.  
  13.  
  14. #functions
  15. def playerInput():
  16.     global player,comp_memories,AI,play
  17.     player = 0
  18.     com = input("Entrée votre commande : ")
  19.     if com == "help":
  20.         print("Commande disponible = help , ai (on/off ai), exit (ferme le programme), score (affiche le score), p (joue pierre), f (joue feuille) , c (joue cisseaux)")
  21.     elif com == "ai":
  22.         if not AI:
  23.             AI = True
  24.             print("AI = ON")
  25.         elif AI:
  26.             AI = False
  27.             print("AI = OFF")
  28.     elif com == "exit":
  29.         sys.exit()
  30.     elif com == "score":
  31.         print("Comp :"+str(c_score)+" - "+str(p_score)+": Player")
  32.     elif com == "p":
  33.         player = 1
  34.         print("p")
  35.         play = True
  36.         comp_memories.insert(0,1)
  37.     elif com == "f":
  38.         player = 2
  39.         print("f")
  40.         play = True
  41.         comp_memories.insert(0,2)
  42.     elif com == "c":
  43.         player = 3
  44.         print("c")
  45.         play = True
  46.         comp_memories.insert(0,3)
  47.     else:
  48.         print("Commande inconue fait help")
  49.  
  50. def compRandom():
  51.     global comp
  52.     seed = time.time()
  53.     random.seed(seed)
  54.     i = random.randrange(3)
  55.     if i == 0:
  56.         comp = 1
  57.     elif i == 1:
  58.         comp = 2
  59.     elif i == 2:
  60.         comp = 3
  61.     print(comp)
  62.  
  63. def compAI():
  64.     global comp,comp_memories
  65.     p = 0
  66.     f = 0
  67.     c = 0
  68.     total = 0
  69.     if len(comp_memories) != 0:
  70.         compRandom()
  71.     elif len(comp_memories) > 0:
  72.         for i in comp_memories:
  73.             if i == 1:
  74.                 p+=1
  75.                 total+=1
  76.             elif i == 2:
  77.                 f+=1
  78.                 total+=1
  79.             elif i == 3:
  80.                 c+=1
  81.                 total+=1
  82.         p_percent = (p/total)*100
  83.         f_percent = (f/total)*100
  84.         c_percent = (c/total)*100
  85.         random.seed(time.time())
  86.         i = random.random()*100
  87.         nbr = []
  88.         x = 100
  89.         for x in nbr:
  90.             if x <= p_percent:
  91.                 nbr.insert(x, 2)
  92.             elif p_percent < x <= p_percent + f_percent:
  93.                 nbr.insert(x, 3)
  94.             elif p_percent + f_percent < x <= c_percent:
  95.                 nbr.insert(x, 1)
  96.         comp = nbr[i]
  97.  
  98. def game():
  99.     global player,comp,p_score,c_score
  100.     if player == 1:
  101.         if comp == 2:
  102.             print("Computer win !!")
  103.             c_score += 1
  104.         elif comp == 3:
  105.             print("Player win !!")
  106.             p_score += 1
  107.         elif comp == 1:
  108.             print("Egalite !!")
  109.  
  110.     elif player == 2:
  111.         if comp == 1:
  112.             print("Player win !!")
  113.             p_score += 1
  114.         elif comp == 3:
  115.             print("Computer win !!")
  116.             c_score += 1
  117.         elif comp == 2:
  118.             print("Egalite !!")
  119.  
  120.     elif player == 3:
  121.         if comp == 1:
  122.             print("Computer win !!")
  123.             c_score += 1
  124.         elif comp == 2:
  125.             print("Player win !!")
  126.             p_score += 1
  127.         elif comp == 3:
  128.             print("Egalite !!")
  129.  
  130. #MainCode
  131. while True:
  132.     playerInput()
  133.     if AI and play:
  134.         compAI()
  135.         game()
  136.         play = False
  137.     elif not AI and play:
  138.         compRandom()
  139.         game()
  140.         play = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement