import re
from random import random, randint

# Football simulation.
tn = 32
Teams = [0]*tn
Records = [0]*tn
fd = open("nflstandings.txt","r")  # open file for reading

i = 0
while i<tn:
    line = fd.readline().split() # creates array of words (as strings)
    Teams[i] = line[len(line)-1] # team name Jets, Giants, etc
    Teams[i] += " "*abs(10-len(Teams[i])) # fix name string length
    i +=1
#while
#print(Teams)


fd.readline()  # table header - skip
#print(line)

i = 0  # loop to read win-loss-tie records for each cooresponding team i
while i<tn:
    line = fd.readline().split()
    Records[i] = (int(line[0]),int(line[1]),int(line[2])) # (W,L,T) record
    i+=1
#while i

fd.close()  # close input file
#print(Records)

######### Simulate #########################

# this simulation was written after most teams played 4 games, with 12 more
# to play:

gamesleft = 12  # games left

def gamesplayed(i):  # games play so far
    (w,l,t) = Records[i]
    return w+l+t
#gamesplayed

# permute Teams, Records, make sure first gamesleft teams played enough games
i = 0
while i<tn-1:
    r = randint(i,tn-1)
    while (i<=gamesleft and gamesplayed(r)!=16-gamesleft): r = randint(i,tn-1)
    Teams[i],Teams[r] = Teams[r],Teams[i]
    Records[i],Records[r] = Records[r],Records[i]
    i += 1
#permutation loop

##################### now use TG and RC instead of Teams and Records
#print(Teams[:gamesleft+1])
TG = Teams[:gamesleft+1]  # first gamesleft values of array
RC = Records[:gamesleft+1]
    
print("teams to play: ",TG)

# Typical football scores (for random selection of scores)
Scores =[0,3,6,7,10,13,14,16,17,20,21,23,24,27,28,30,31,34,35,37,38,41,42,44,45,47,48,49,52,54,55,56,59]

def biasrand(m):  # gives biased random number with average m , 0<m<1:
    if (m<=0 or m >=1): return random()
    r = random()   # gives uniform distribution number 0<=r<1
    return r**(1/m - 1)  # definite integral from 0 to 1 = m
#biasrand


def wpercentage(i):  # winning percentage of team i, to .001 
  (ws,ls,ts) = RC[i]
  gp = ws+ls+ts # games played
  wp = 0.0 # default winning percentage
  if (gp>0): wp = (ws+ts*0.5)/gp
  return wp
#wpercentage

# sets min and max winning percentage as allowed by simulation
minwp,maxwp = 0.1,0.9

def play(i,k):  #Team[i] plays Team[k], returns string report
    wpi = wpercentage(i) # get winning percentages of teams i and k
    wpk = wpercentage(k) 
    wpi = min(max(wpi,minwp),maxwp) # give every team a chance
    wpk = min(max(wpk,minwp),maxwp)
    si = biasrand(wpi) # random number with average wpi,wpk
    sk = biasrand(wpk)
    scorei = Scores[int(si*len(Scores))] # pick score base on biased random num
    scorek = Scores[int(sk*len(Scores))]
    (wi,li,ti) = RC[i]  # current records
    (wk,lk,tk) = RC[k]    
    if (scorei>scorek):  # team i wins
       RC[i] = (wi+1,li,ti)
       RC[k] = (wk,lk+1,tk)
    elif(scorek>scorei):  # team k wins
       RC[i] = (wi,li+1,ti)
       RC[k] = (wk+1,lk,tk)
    else:                 # tie
       RC[i] = (wi,li,ti+1)
       RC[k] = (wk,lk,tk+1)
    report = TG[i]+" "+str(scorei)+" - "+TG[k]+" "+str(scorek)
    return report
#play

# simulation loop:
i =0
while i<len(TG)-1:
    k = i+1
    while k<len(TG):
        result = play(i,k)
        print(result)
        k += 1
    #while k
    i += 1
# while i



############# insertion sort
# calculate winning percentage array:
WPC = [0]*len(TG)
i = 0
while i<len(TG):
    WPC[i] = wpercentage(i)
    i += 1
#while

# sort
def simplesort(A,B=[],C=[]):
    i = 0
    while i<len(A)-1: # find ith largest item in A, swap to front:
      mi = i # index of largest
      k = i+1
      while k<len(A):
          if A[k]>A[mi]: mi=k
          k += 1
      # while k
      # at this point, mi points to ith largest item
      A[i],A[mi] = A[mi],A[i] # swap to location i
      if (len(B)==len(A)):
          B[i],B[mi] = B[mi],B[i] # swap to location i
      if (len(C)==len(A)):
          C[i],C[mi] = C[mi],C[i] # swap to location i          
      i += 1
#simplesort

simplesort(WPC,TG,RC) # sort teams by winning percentage in decreasing order


######## Standings:
print("--------------\nFinal Standings:")
i = 0
while i<len(TG):
    (ws,ls,ts) = RC[i]
    wp = int(wpercentage(i)*1000 + 0.5)/1000
    print(TG[i]+": \t"+str(ws)+"-"+str(ls)+"-"+str(ts)+" \t"+str(wp))
    i+= 1
#standings loop    
