Skip to content

Programming Assignment(PA) 3: Squared-X


In PA-2, you created the game Squared. In PA-3, you will create Squared-X, a rewrite with similar rules and gameplay, but a new implementation and a partially color user interface.


Changes to the game

Along with the letters for the game, you are given an ideal solution, which is the smallest set of words that use all the letters.

The primary change to the gameplay in this version is that instead of having a maximum number of tries to solve the puzzle, you are given a maximum number of words (valid words), to solve the puzzle. So invalid words do not count.

  • If you use up the maximum words, but have not finished all the letters, you lose.
  • If you finish all the letters with the maximum number of words, you score 50.
  • If you finish all the letters with the same number of words as the ideal solution, you score 100.
  • If you finish all the letters with more words than the ideal solution but less than the maximum words, you score 75.


The full game will be implemented in a single file,, and you will be given the file with docstrings and some completed functions.

This version will not require NLTK. The set of valid words will be provided in a file. As with the previous version, a file with the game board letters will also be provided to the program, but now it also includes the ideal solution on the last line of the file.

In Squared, most functions had several parameters that represented the state of the game. In Squared-X, a single dictionary will store the full game state.

            "valid": a set initialized from words_file 
                     as described in the docstring
            "previous": initialized to an empty list
            "sides": a list of lists initialized from 
                     letters_file as described in the docstring
            "all": a set initialized from letters_file 
                   as described in the docstring
            "used": initialized to an empty set
        "ideal": a list of words from letters_file 
                 that represent the ideal solution
        "status": a status message initialized to "" (empty str)

You will write/modify 5 functions:

  • initialize_game_state(words_file, letters_file) This function takes 2 input files and uses them to initialize the game state. It reads in the valid words and the board letters and initializes all the other game information. The letters file will have the same format as it did in the previous version, but it will contain a 5th line which will list the ideal solution for this set of letters as words separated by hyphens.
  • update_game_state(word, valid, game_state) Updates the game state dictionary for the given word and the result of checking the word's validity.
  • is_word_valid(word, game_state) Checks the given word's validity. This function must do exactly what it did in the previous version, except that all the information needed to determine the word's validity is now stored in the game state dictionary. The status message is recorded in the dictionary rather than being returned.
  • get_score(game_state) This function determines if the game has been won yet, and whether or not the ideal solution has been found. It has changed from the previous version, because it uses MAX_WORDS rather than MAX_ATTEMPTS, and it uses the game state dictionary.
  • display_board(game_state) In the previous version, the print_board function was given. You must write this new version to use the game state dictionary, plus print the letters in color, using the colorama module.


The colorama module helps with this. You will need to load the module into your thonny environment. The necessary imports are already made in the file. A separate function sets the background color and prints the game board. In display_board you must create and return a single string that represents the board. Used letters are displayed in the color CYAN and unused letters are displayed in the color WHITE. To color a letter, you precede it with a colorama foreground constant, like so:

print(Fore.CYAN + 'a') will print a letter 'a' in the color cyan.

s = Fore.WHITE + 'b' + Fore.CYAN + 'a' creates a string "ba" where 'b' is white and 'a' is cyan.

NOTE: The color markers are very hard to read in gradescope. When this function is tested, all your WHITE markers will be replaced with a '*' and all your CYAN markers will be replaced with a '#', in order to make the output easier to read. This function is worth just 3 points in gradescope.

Provided Files

This file is the one you created in PA-2 - the list of all English words that are valid for Squared. You may test with this file or create a smaller file.

This file contains the letters for the sides of the square, in the same format as previously, in the order: top, left, right, bottom. It also contains the ideal solution for this set of sides (in order, separated by hyphens -). For example:


This file contains the required imports and constants for the program, as well as the docstrings for the functions that you must write. It also contains 2 completed functions:

play_squaredx_game: This function uses the functions that you will write to play the game. You can use it to check if your functions are working as intended.

print_board_color: This function sets the background color to black, prints your color board string (from the display_board function), and then resets all the colors to the default settings.


This assignment is worth 50 points.

You are limited to a maximum of 25 submissions into Gradescope

Programming Assignment Attribution

In the module docstring for, provide a short statement describing any assistance that you received on this assignment, either from another student, an online source, an AI-enabled tool, or any other source.