PrisonersDilemma/helper_functions.py

68 lines
2.1 KiB
Python
Raw Permalink Normal View History

2024-04-26 21:11:23 +02:00
import statistics
2024-04-26 21:45:42 +02:00
def analyze_results(game_data,game_nr):
2024-04-26 20:01:54 +02:00
defect_str="X "
coorperate_str="O "
2024-04-26 21:45:42 +02:00
spacer_str=""*len(game_data)
lines=["Game "+str(game_nr)+":",spacer_str]
2024-04-26 20:01:54 +02:00
str1="Player 1: "
str2="Player 2: "
p_1_points=0
p_2_points=0
for i in game_data:
#visualize
if i[0]:
str1+=coorperate_str
elif not i[0]:
str1+=defect_str
if i[1]:
str2+=coorperate_str
elif not i[1]:
str2+=defect_str
#award points
if i[0] and i[1]:
p_1_points+=3
p_2_points+=3
elif i[0] and i[1]==False:
p_1_points+=0
p_2_points+=5
2024-04-26 22:39:24 +02:00
elif i[1] and i[0]==False:
p_1_points+=5
p_2_points+=0
2024-04-26 20:01:54 +02:00
elif i[0]==False and i[1]==False:
p_1_points+=1
p_2_points+=1
lines.append(str1+" Points:"+str(p_1_points))
lines.append(str2+" Points:"+str(p_2_points))
lines.append(spacer_str)
#print the results
for i in lines:
print(i)
2024-04-26 20:37:23 +02:00
return p_1_points,p_2_points
2024-04-26 20:01:54 +02:00
def play_game(games_array,func1,func2,how_many_times):
for i in range(how_many_times):
choice_1=func1(games_array,0)
choice_2=func2(games_array,1)
games_array.append([choice_1,choice_2])
2024-04-26 20:37:23 +02:00
def analyze_scores(score_table):
average_score_p_1=0
2024-04-26 21:11:23 +02:00
all_p1_scores=[]
all_p2_scores=[]
2024-04-26 20:37:23 +02:00
average_score_p_2=0
p1_sum=0
p2_sum=0
for i in score_table:
p1_sum+=i[0]
2024-04-26 21:11:23 +02:00
all_p1_scores.append(i[0])
2024-04-26 20:37:23 +02:00
p2_sum+=i[1]
2024-04-26 21:11:23 +02:00
all_p2_scores.append(i[1])
2024-04-26 20:37:23 +02:00
average_score_p_1=p1_sum/len(score_table)
average_score_p_2=p2_sum/len(score_table)
2024-04-26 21:11:23 +02:00
print(str(average_score_p_1)+" AVG Player 1 Score |"+str(statistics.median(all_p1_scores))+" Median Player 1 Score")
print(str(average_score_p_2)+" AVG Player 2 Score |"+str(statistics.median(all_p2_scores))+" Median Player 1 Score")
2024-04-26 22:39:24 +02:00
print(str(round(p1_sum/(p1_sum+p2_sum),2))+ " Player 1 Percent of Points")
print(str(round(p2_sum/(p1_sum+p2_sum),2))+ " Player 2 Percent of Points")