Word Chain Game Program in Python- Part 1


Need to design and implement two related programs using Python programming language:-

  1. “wordchain.py”, a CLI program that allows the user(s) to play a word game. Every time the game is played, some data about it is stored in a text file.
  2. “logviewer.py”, a GUI program that lets the user view the information in the text file and uses it to determine some gameplay statistics.


Word Chain Game Main Program

“wordchain.py” is a program with a Command-Line Interface (CLI).

This program will implement a simple word game which can be played by multiple users who take turns on the same computer, although you will be playing it alone as you write and test your code.

The game begins by asking how many people wish to play, and then prompting you to enter a name for each of the players. The game will then continually cycle through each player and ask them to enter a word that matches the following criteria:-

The word must start with the letter that the previous word ended in. e.g. If the previous word was “duck”, the next word must start with “k”.  For the first word of the game, a random letter of the alphabet is chosen.

The game will randomly select whether the word must be a noun, a verb or an adjective.

The word must not have been previously used in this game.

If the player enters a word that matches all of the criteria, the length of the “word chain” increases by one and the game moves on to the next player. For example, if the game asked you to enter a verb starting with “t”, then “try” would be valid (as long as it had not been used earlier in the game).

The goal of the game is to obtain the longest word chain by continually entering valid words. It is

not intended to be competitive or require quick thinking – there are no winners or losers, and no

time limit for entering a word. You are likely to get bored of playing before you break the chain.

Once the word chain is broken (which ends the game), the program should add “log” of the game to a text file named “logs.txt”. Use the “json” module to read and write data from/to the text file in JSON format (see Reading 7.1). Each log of a game should be a dictionary consisting of three keys:-

“players”: an integer of the number of players, e.g. 3
“names”: a list of strings of the player names, e.g. [‘Andrea’, ‘Beth’, ‘Carol’]
“chain”: an integer of the final chain length of the game, e.g. 21

The logs should be stored in a list, resulting in the file containing a list of dictionaries. The example to the right demonstrates a list of two logs. After adding the log to the text file, the program ends.


Log Viewer GUI Program

“logviewer.py” is a program with a Graphical User Interface (GUI).

We will use the “tkinter” module to create the GUI.

We will use the “tkinter.messagebox”, and “json” modules.

This program uses the data from the “logs.txt” file. The program should load all of the data from the file once only – when the program begins. The program simply allows the user to view the logs created by “wordchain.py” as well as some basic statistical information.

The user can press the “Next Log” button to advance through the logs until they reach the last log.

Once they have reached the last log, clicking the button should display a “no more logs” messagebox.

Clicking the “Show Stats” button should show a messagebox that displays the total number of

games, average number of players, and maximum chain length of all logged games.



We will cover the solution in two parts. Here is part 1 which is the GUI Log Viewer program. We will release the part 2 soon with the Main Word Chain Program.

For executing this program, we have used a dummy json file with sample data.

We are providing this program first, as it is easier to do and has less logic and more GUI things:-

We are using TKinter Python library for making the UI components of this program.



import tkinter
import tkinter.messagebox
import json
from tkinter import *

class ProgramGUI:   

    def init(self, master):
       self.master = master
       master.title(“WordChain Log Viewer”)
       self.label = Label(master, text=””)
       self.frame = tkinter.Frame(root)
       self.lognumbertext = StringVar()
       self.lognumberlabel = tkinter.Label(root, textvariable=self.lognumbertext)  

       self.playerstext = StringVar()
       self.playerslabel = tkinter.Label(root, textvariable=self.playerstext)
       self.chainlengthtext = StringVar()
       self.chainlength = tkinter.Label(root, textvariable=self.chainlengthtext)

           with open(‘logs.txt’) as jsondata:
               self.logs = json.load(jsondata)
       except Exception as e:
           messagebox.showerror(“Error”, “Missing/Invalid File”)

           self.nextlogbutton = tkinter.Button(self.frame, text=”Next Log”,  fg=”white”,bg=”blue”,state=”disabled”,
           self.showstatsbutton = tkinter.Button(self.frame,  text=”Show Stats”,  fg=”white”,bg=”blue”,state=”disabled”, command=self.show_stats)

           self.nextlogbutton = tkinter.Button(self.frame,  text=”Next Log”,  fg=”white”,bg=”blue”, command=self.show_log)
           self.showstatsbutton = tkinter.Button(self.frame,  text=”Show Stats”,  fg=”white”,bg=”blue”,command=self.show_stats)


       self.nextlog = 0
       self.totalplayers = 0
       self.lognumberslist = []
       self.numofplayerslist = []
       self.playersrecordslist = []
       self.chainlengthlist = []
       self.numofdictionariesinfile = len(self.logs)       

       for i in range(0, self.numofdictionariesinfile):
           numofplayers = len(self.logs[i][‘names’])
           aplayernamestring = ‘(‘           

           for j in range(0, len(self.logs[i][‘names’])):
                          aplayernamestring = aplayernamestring + self.logs[i][‘names’][j]
                          aplayernamestring = aplayernamestring + ‘,’
                          self.totalplayers = self.totalplayers + 1                         

           aplayernamestring = aplayernamestring.rstrip(‘,’)
           aplayernamestring = aplayernamestring + ‘)’


    def show_log(self):         

       if(self.nextlog >= self.numofdictionariesinfile):
           messagebox.showerror(“End of file”, “No more logs to show”)

       self.lognumbertext.set(‘Log #:  ‘ + str(self.lognumberslist[self.nextlog] + 1))
       self.playerstext.set(‘Players:  ‘+ str(self.numofplayerslist[self.nextlog]) + ‘ ‘ + str(self.playersrecordslist[self.nextlog]))
       self.chainlengthtext.set(‘Chain Length:  ‘+ str(self.chainlengthlist[self.nextlog]))
       self.nextlog = self.nextlog + 1

           messagebox.showinfo(“Alert”, “Only one log found. Navigation and statistics disabled.”)

    def show_stats(self):
       messagebox.showinfo(“WordChain Statistics”, “Number of Games: ” + str(self.numofdictionariesinfile) + “\n” + “Average Players: ” + str(round((self.totalplayers / self.numofdictionariesinfile), 1)) + “\n” + “Max Chain: ” + str(max(self.chainlengthlist)))


root = Tk()
root.minsize(400, 150)
gui = ProgramGUI(root)


Powered by Facebook Comments

Be the first to comment

Leave a Reply

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