Automate the Wiki-Link Game with Python

…and add a fun random twist!

Timo Kats
Geek Culture

--

Photo by Luke Chesser on Unsplash

The Wiki-Link game is a game on Wikipedia where the player starts on a random page and progresses by continuously clicking on the Nth link that a page has (with N being a random number that the player chooses beforehand). By doing this, the player will be redirected from one page to another until a page has no links to click on (called a short-page ending) or links to a page that has been visited before (infinite loop ending). The amount of pages that were visited before this happens are the final score.

Sounds boring? Well according to Wikipedia it “is designed to waste time and life”, so playing it with friends during the weekend wouldn’t be my recommendation either. However, if you’re still reading this you’re probably a programmer, and programmers generally don’t get invited to parties during the weekend anyways. Instead, we write Python scripts. (or is this just me?)

Therefore this article will focus on automating Wiki-Link using Python. More specifically, this program will research a very interesting characteristic that this game has, which is that when N=1 (i.e. always click on the first link of every page) the game ends at the Wikipedia page of “Philosophy” roughly 97% of the time.

Figure 1: All Roads Lead to “Philosophy” from Xefer

What this research doesn’t show however, is whether this result stems from only using the first link of every page or from Wikipedia’s structure as a whole. Hence this article aims to redo this research with a random N (i.e. selecting a random link every page). By doing this we can discard the element of what link we used and immediately get an overview of Wikipedia’s structure as a whole. So let’s get started!

Creating the program

The first step in creating this program would be to get the data from Wikipedia into our environment. This can be done with a multitude of webscraping tools but for this program we selected the wikipedia API from Python. With this API we can access all Wikipedia pages based on a search term (that a user normally puts in the search bar).

However, some search terms can refer to multiple pages. For example, the search term “New York” can refer to the page of “New York (state)” or the page of “New York City”. This is called a Disambiguation Error. To deal with this error our program always selects the first (most likely) option.

try:
p = wikipedia.page(page)
except wikipedia.DisambiguationError as e:
s = options[0]
p = wikipedia.page(s)

Next, after selecting the page we need to extract the available links. This is possible through adding the links attribute to the name of the page. From these links we then select a random link to further the Wiki-Link game with.

link = random.choice(p.links)

After selecting a link we check whether it is valid and leads to a page where we can continue our game. If this is true, the game progresses to the next page. If not, the game is over and the final score is presented.

try:
p = wikipedia.page(link)
except wikipedia.exceptions.PageError:
print('game over. Your score:', score)
exit(0)
wiki_link(link, score + 1)

The complete version of this code can be found in the appendix down below along with a link to repl.it where you can try this program yourself!

Results

Finally, after creating the program we can start graphing its results. To do this we run the program three times with the same starting page and graph the pages it follows. The two different starting points we experiment with in this article are computer programming and data science (very fitting…).

Figure 2: Pages visited with computer programming as a starting point

Firstly the pages that were visited with computer programming as a starting point. So far there’s absolutely no pattern to be observed. Apart from the fact that all the visited pages were programming related.

Figure 3: Pages visited with data science as a starting point

Secondly the pages that were visited with data science as a starting point. Just like the previous figure, there’s no real patter to be observed. The only thing Figures 2 and 3 have in common is that they both have similar lengths.

In conclusion

The program that was made for this article generally works and probably lends itself very well to those that are still interested in “wasting time and life” (as the Wiki-Link game intents). Therefore, a link to a working repl of this program is given down below in the appendix.

As far as the research goes. The program didn’t find any resemblance of a pattern using random links. Hence, it seems as if that characteristic is partly inherent to using the first link only instead of just Wikipedia. However, if the experiment can be expanded to a larger scale maybe different patterns could still be found.

Appendix

--

--