Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import os
  2. import random
  3. import time
  4. import subprocess
  5. import math
  6. from subprocess import Popen
  7. from random import randint, shuffle
  8. import re
  9. import sys
  10.  
  11. def get_command(seed):
  12.     c = "./Game "
  13.     for i in range(0,4):
  14.         c += PLAYERS[i] + " "
  15.     c += "-s "
  16.     c += str(seed)
  17.     c += " -i default.cnf "
  18.     c += "2>&1 > /dev/null | grep 'got score'"
  19.     return c
  20.  
  21.  
  22. random.seed(time.time())
  23.  
  24. PLAYERS = sys.argv[1:5]
  25. punt = [0,0,0,0]
  26. guany = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  27. n = int(sys.argv[5])
  28. count = 0
  29.  
  30. try:
  31.   for i in range(0,n):
  32.    
  33.     com = get_command(random.randrange(10000))
  34.     p = Popen(com, shell=True, stdout=subprocess.PIPE)
  35.     used = [0,0,0,0]
  36.     llista = [0,0,0,0]
  37.     for j,line in enumerate(p.stdout.readlines()):
  38.       llista[j] += int(re.findall("\d+", str(line))[-1])
  39.  
  40.  
  41.     maxim = 0
  42.     jugador = 1
  43.     for j in range(0,4):
  44.       punt[j] += llista[j]
  45.       if maxim < llista[j]:
  46.           jugador = j
  47.           maxim = llista[j]
  48.     guany[0][jugador] += 1
  49.     used[jugador] = 1
  50.     print(str(i+1) + ". " + " "*(len(str(n))-len(str(i+1))) + "Ha guanyat el jugador " + str(jugador+1) + ": " + PLAYERS[jugador])
  51.     sys.stdout.flush()
  52.     maxim = 0
  53.     jugador = 1
  54.     for j in range(0,4):
  55.       if maxim < llista[j] and used[j] == 0:
  56.           jugador = j
  57.           maxim = llista[j]
  58.     guany[1][jugador] += 1
  59.     used[jugador] = 1
  60.  
  61.     maxim = 0
  62.     jugador = 1
  63.     for j in range(0,4):
  64.       if maxim < llista[j] and used[j] == 0:
  65.           jugador = j
  66.           maxim = llista[j]
  67.     guany[2][jugador] += 1
  68.     used[jugador] = 1
  69.  
  70.     maxim = 0
  71.     jugador = 1
  72.     for j in range(0,4):
  73.       if maxim < llista[j] and used[j] == 0:
  74.           jugador = j
  75.           maxim = llista[j]
  76.     guany[3][jugador] += 1
  77.     used[jugador] = 1
  78.     count = count+1
  79.  
  80. except KeyboardInterrupt:
  81.     print()
  82.  
  83.  
  84.  
  85. print ("")
  86.  
  87. nom = ["", "", "", ""]
  88. mitja = [0.0,0.0,0.0,0.0]
  89. for i in range(0,4):
  90.     nom[i] = sys.argv[i + 1]
  91.     while len(nom[i]) < 12:
  92.         nom[i] += str(" ")
  93.     for j in range(0,4):
  94.         mitja[i] += float(j + 1)*float(guany[j][i])/float(count)
  95.  
  96. for i in range(0,4):
  97.   print("Jugador "+str(i + 1) + " " + nom[i] + ": " + str(punt[i]//count) + " " + str(guany[0][i]) + " "\
  98. + str(guany[1][i]) + " " + str(guany[2][i]) + " " + str(guany[3][i]) + " %.2f" % (mitja[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement