#!/usr/bin/python3
# written by fabian zimmer & bastian greshake | released under cc-by 3.0
import random           # import all those strange standard libs
import csv              # i'm not sure we even use csv
import sys
import os

costmatrix = {"KK":[3,3],"KV":[0,5],"VK":[5,0],"VV":[1,1]}  # things you could win
strategies = {"t":{"start":"K","K":"K","V":"V"},"n":{1:"V",2:"V",3:"K"},"k":{1:"K",2:"K",3:"V"},"r":{1:random.choice("KV"),2:random.choice("KV"),3:random.choice("KV")},"c":{1:"K",2:"K",3:"K"},"d":{1:"V",2:"V",3:"V"}}
#code t = titfortat, n = nasty, k = kind,r = random, c = coop, d = defect 

def game(n_generation,t_generations,consecutive):       #lets start our game

    os.system("rm 50plays.txt")                         #remove old output file
    output = open("50plays.txt","a")                    #open output-file
    writer=csv.writer(output)                           #okay, we use csv

    titfortat = coops = defects = rands = kinds = nasties = list(range(1,n_generation+1)) #initialise population
    strat2pop = {"t":titfortat,"n":nasties,"d":defects,"c":coops,"r":rands,"k":kinds} #hack to be abled to iterate via strategies over populations
    timecounter = 0 #set timecounter to 0
    poplist = []
    popdict = {}

    while timecounter < t_generations:	# start the first generation
        prob = "" 
        for i in strat2pop:
            prob = prob + i*len(strat2pop[i])
        print(prob)
        win = {"t":0,"n":0,"k":0,"r":0,"c":0,"d":0}
        
        for i in strategies:			# phew, we got the first pop, strategies  and this stuff
                                        # lets start with first generation and first strategy

            for j in strat2pop[i]:
                enemy = random.choice(prob)
                randx = random.randrange(1,consecutive)
                fightcounter = 0
                behavcounter = 1
                
                while fightcounter < randx:
                    if enemy == "t":
                        if fightcounter == 0:
                            he = strategies[enemy]["start"]
                        else: 
                            he = strategies[enemy][me]
                    else:
                        he = strategies[enemy][behavcounter]

                    if i == "t":
                        if fightcounter == 0:
                            me = strategies[i]["start"]
                        else:
                            me = strategies[i][he]
                    else:    
                        me = strategies[i][behavcounter]
                    
                    win[i] = win[i] + costmatrix[me+he][0]
                    win[enemy] = win[enemy] + costmatrix[me+he][1]
                    
                    fightcounter += 1

                    if behavcounter < 3:
                        behavcounter += 1
                    else: 
                        behavcounter = 1
        
        summe = 0
        for k in win:
            summe = summe + win[k]
           
        for k in win:
            strat2pop[k] = list(range(1,int(((win[k]/summe)*n_generation*6)+1)))
            if strat2pop[k] == []:
                popdict[k] = 0
            else:    
                popdict[k] = strat2pop[k][-1]
                
        poplist.append(popdict.copy())
        timecounter += 1
    y=1
    for i in poplist:
        writer.writerow([y,str(i["c"]),str(i["d"]),str(i["t"]),str(i["k"]),str(i["n"]),str(i["r"])])
        y+=1
    output.close()

    os.system("gnuplot ~/Dropbox/Python/prisoners/prisoners_gnu.txt")
    os.system("feh ~/Dropbox/Python/prisoners/50plays.png")

game(int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3]))

