Score Analytics on #flappybird with Python


I posted this as a hypothetical assignment for my Creative Technologies students at UU Magee for their Creative Coding modules but thought it can come here too.

What is the average #flappybird score? I can’t get past 3 points and gave up after a few attempts as I’m not your classic game player type. Players can share (brag) their scores to the rest of the world.

Fri Feb 07 10:17:09 +0000 2014|getting better ! :) I scored 32 pts in #flapflap!!! ->
Fri Feb 07 10:17:11 +0000 2014|OMG! I scored 44 pts in #flapflap!!! ->
Fri Feb 07 10:17:19 +0000 2014|OMG! I scored 28 pts in #flapflap!!! ->
Fri Feb 07 10:17:24 +0000 2014|OMG! I scored 5 pts in #flapflap!!! ->
Fri Feb 07 10:17:26 +0000 2014|OMG! I scored 9 pts in #flapflap!!! ->
Fri Feb 07 10:17:27 +0000 2014|OMG! I scored 16 pts in #flapflap!!! ->
Fri Feb 07 10:17:40 +0000 2014|OMG! I scored 10 pts in #flapflap!!! ->
Fri Feb 07 10:17:53 +0000 2014|OMG! I scored 21 pts in #flapflap!!! ->
Fri Feb 07 10:17:54 +0000 2014|OMG! I scored 27 pts in #flapflap!!! ->
Fri Feb 07 10:17:58 +0000 2014|OMG! I scored 33 pts in #flapflap!!! ->
Fri Feb 07 10:18:01 +0000 2014|OMG! I scored 3 pts in #flapflap!!! ->
Fri Feb 07 10:18:03 +0000 2014|OMG! I scored 9 pts in #flapflap!!! ->

As the output is pretty uniformed I set a SpringXD stream to pull the scores from Twitter as they happened.

With a quick hack around with Python (Java is my main language, Python is second for stuff around the edges) I came up with this:
#import the regular expressions library
import re
# create the function to calculate the average score
# all we do is divide the sum of the array by the number of elements in it
def getmean(scorearray):
 return sum(scorearray) / float(len(scorearray))
# function to get the minimum score
def getmin(scorearray):
 return min(scorearray)
# function to get the maximum score
def getmax(scorearray):
 return max(scorearray)
# create a regular expression to look for "123 pts" in the text
r = re.compile('([0-9]+) pts')
# open the file flappybird.out
file = open('flappybird.out')
# create an empty list array
scorearray = []
# open the file and iterate each line
for line in file:
 # extract the score from the line
 w = 0
 # if it borks on the line just ignore it
# create a new array from lowest to highest scores, we'll need this 
# for the interquartile mean.
sortedarray = sorted(scorearray)
# define a quartile count, total size of the array divided by 4
qcount = int(len(sortedarray)/4)
# print the min, max and mean scores 
print 'Min: ', getmin(scorearray)
print 'Max: ', getmax(scorearray)
print 'Avg: ', getmean(scorearray)
# print the interquartile range average (q2 + q3 scores)
# this eliminates the outliers and gives a more realistic mean
print 'IQM: ', getmean(sortedarray[qcount:qcount*3])

It’s not the most elegant thing in the world but it does work and for 20 minutes at lunchtime, well that’s all I really wanted…..

Min: 0
Max: 99999999999
Avg: 75570257.1496
IQM: 16.3323699422


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: