- Magic Square Game
The objective of the site is to create a game that will present magic squares for the user to solve.
In developing the site, I decided to dynamically generate the magic squares; this introduced some complexity in the structure of the JavaScript code but overall led to a more robust game with an extremely large number (773,584,182) of possible permutations for the number of magic squares to solve.
As a site user:
- I want a site that is easy to use and navigate
- I want a site that provides visual cues and feedback depending on my actions
- I want a site that offers varying difficulty levels and provides guidance if stuck
- I want a site that works across different devices and platforms
As a site owner:
- I want to ensure that the site has a distinctive visual identity
- I want to develop a site with a clear call to action
- I want to ensure that the Magic Square game is easy to play and provides assistance to the user if they are stuck
- I want to ensure that the user base is as wide as possible and that the site works across different platforms and screen sizes
- I want the ability to dynamically generate the magic squares as opposed to using a library of magic squares to serve to the user to solve
Before building the site, I mapped out the following wireframes using wireframe.cc. My objective was to target a simple design and to ensure that the intention of each page was clear to the user.
On implementing the actual design, I made one minor change - the three control buttons (Instructions, Settings, Play Game) were moved out of the game container box. Instead I decided to place these three buttons under the site title.
I felt that this led to an improved user experience, with a clear separation between the game and the controls used to move between the different game sections.
JavaScript was used to achieve the following functionality:
- To dynamically create the values for the Magic Square that the user will solve
- To control user responses to button presses and to validate user inputs
An array of length n can be transformed into a magic square with row and column of size root n if the difference between each consecutive element in the array is a fixed constant k. (for example, with n=9 and k=0 the sequence [1,2,3,4,5,6,7,8,9] forms a solvable magic square of size 3x3)
In order to algorithmically generate a magic square, the following inputs are required:
- A sequence of
nintegers, with a fixed differencekbetween subsequent integers - An algorithm for mapping the values such that they form a magic square. The De la Loubère (or siamese) method is proposed as the algorithm for completing the magic square.

In order to generate the magic square, the following functions were defined:
SetSquareDiff(): This function generates a random value for the differencek; the range for the valuekis defined by the difficulty level that the user selects. There are 3 potential values forkfor each difficulty level with the default difficulty level being easy. For instance the values ofkfor easy can fall in the range of[0,2]; the exact value is chosen randomly for the selected difficulty level.genSquare()): This function generates an array of lengthnwith each consecutive value in the array increasing by a fixed constantk. The valuenis defined by the square size selected by the user; the default grid size is3x3yielding a value ofnequal to 9makeSquare(): For an inputted array, this function applies the De la Loubère method to arrange the values into a Magic Square. The function creates a matrix of sizeroot nbyroot nbefore returning a flattened array of lengthnfillSquare()This function takes an integer value representing the size of the square and performs the following:- Modifies the HTML code to create the gird for the magic square
- Populates the square values in the HTML code using the array ordered by the
makeSquare()function - Randomly removes
root nvalues from the completed magic square and replaces them with an input box for completion by the player of the game (e.g. for a square of size 7x7, 7 values are randomly removed for completion by the user)
The fixed constant k is randomly generated with 3 possible values for each difficulty level. In addition the values that are removed for completion by the user are also randomly selected. This lends to a large number of potential game combinations as follows:
- Small Grid size: 3 difficulty levels by 3 potential values for
kby 84 combinations for the removed values = 756 possible game combinations - Medium Grid size: 3 difficulty levels by 3 potential values for
kby 53,130 combinations for the removed values = 478,170 game combinations - Large Grid size: 3 difficulty levels by 3 potential values for
kby 85,900,584 combinations for the removed values = 773,584,182 game combinations
In order to interact with the site JavaScript and jQuery are used to provide the following functionality:
- Store the size and difficulty levels as selected by the user
- To change the color of the setting buttons once the user makes a selection
- To modify the HTML code to create the magic square
- To hide sections of the website depending on the user selection
In selecting the fonts for the site I had two objectives:
- Select a title font that could be used in place of a logo
- Select a font that would compliment the title font while retaining legibility
The site fonts were chosen using Google Fonts. The fonts chosen were Monoton for the title and Titillium Web for the body content.
A broad color palette was chosen for the site with a range of complimenting colors chosen.
In choosing the color palette, my objective was to select a color palette that would create a strong visual identity as well as suggest a playful, fun game.
The following section provides an overview of the site features and design, with screenshots providing a visual overview of each feature.
-
Introduction Container
- The Introduction pane welcomes the user to the site and features a clear call to action.
- I originally planned a site logo, however ultimately I used a text logo; this logo animates when the user hovers over it and this feature helps to create a clear sense of identity for the site
-
Instructions Container
- The Instructions pane provides the following detail:
- Historical information on Magic Squares
- Instructions on how to solve the Magic Square
- A image providing a graphical visualisation of how to solve the game
-
Settings Container
- The Setting pane has the following features:
- Each button has a shadow on hover by the user
- The size and difficulty buttons change color on selection
- Text underneath the button provides context on the difficulty and size settings
-
Game Container
- The Game screen displays the Magic Square with the missing values for input by the user
- The Hint button is used to generate Hints for the user; there are four possible levels of Hint which are triggered by the number of times the user clicks on the Hint button
- On clicking the Submit button, there are two possible responses:
- The Success alert displays if the user correctly completes the magic square
- The Incorrect alert displays if the user incorrectly completes the magic square
-
Responsive Design
- The site has been designed to be fully responsive for screen sizes from
280pxupwards. For screen sizes less than525pxin width, the container collapses and spans the width of the screen
Overall, I am satisfied with the functionality and feature set of the developed site. In terms of additional features, I believe the game could be further enhanced through the addition of a scoreboard feature.
- Scoreboard This would provide users with the ability to view their scores; I would propose implementing a timer, with the user assigned a scored based on how quickly they managed to solve the Magic Square. The feature would require the use of local storage to record the user name and score and allow multiple users to play against each other.
Testing was completed across a number of platforms as follows:
- The site was tested on Chrome, Firefox and Safari
- Platform specific testing was completed on Windows and Linux desktop environments. Mobile testing was completed on iOS
- The site responsiveness was tested across both Apple phones and tables. In addition, responsiveness was tested using Chrome Dev Tools, wth screen sizes from
280px(Galaxy Fold) upwards tested.
Performance was tested using Lighthouse in Chrome Dev Tools. No significant issues were reported for either Desktop of Mobile
- HTML
- No errors were reported when running the code through the official W3C validator
- CSS
- No errors were found when passing through the official Jigsaw validator
- A number of warnings were triggered; however these related to the use of vendor extensions to enable the animation of the main title (e.g.
-moz-text-fill-color)
- JavaScript
- No errors were found when passing through the official Jshint validator.
- The following screenshot summarises the output:

The following bugs were encountered during the development and the following fixes were implemented:
Bug: As part of the hint alert box, the maximum and minimum missing values are displayed to the user. Initially, instead of displaying the min and max values, a NaN value was returned for the minimum and maximum values.
Fix: This bug was resolved by implementing the fix proposed by the following link. It is not possible to apply the min and max functions directly to a list in JavaScript; rather the list must first be destructed.
Bug: If the user selected a Hint and then restarted the game or changed the settings, it was not possible to generate a new Hint; this was despite the fact that the hide styling had been removed from the Hint alert.
Fix: On reviewing the code, it was determined that it was necessary to also remove the style attribute of the Hint alert in addition to removing the hide styling. A similar fix was required for the Wrong answer alert.
Bug: On testing the website on Firefox, it was noted that the Main Title had an underline on hover; this underline did not appear on Google Chrome which was the primary browser on which the site was developed.
Fix: This was resolved by adding an explicit text-decoration styling of none to the Title on hover.
There are no known bugs present in the final site deployment.
- To support site testing, the completed Magic Square is logged to the DOM for each generated Magic Square. This was designed to support testing of the site; I would propose removing this logging in the final version of the deployed site.
The site was deployed using GitHub pages to the following location: link
- The following guide was used to create the text gradients link
- The following StackOverFlow post was used to help implement the
fadeInandfadeOutfunctionality of thecontainer-introdiv: link - Sample code was leveraged from the following CodePen example to create the animated gradient for the title link
- The following function was used to generate the random values for the value
kwhich represents the increment in the magic square array values: link - The following paper was used to better understand the algorithm to generate the Magic Square: link
- The following code examples were used to help create the alert banners that were used for the hint, correct answer and wrong answer messages: link
- The Wikipedia page on the siamese method for providing the gif used to illustrate the method: link
- HTML
- CSS
- JavaScript
- jQuery
- VScode
All coding was completed in VS Code. - wireframe.cc
This site was used to build the wireframes for the site layout. - cdnjs
cdnjs was used as the reference for the jQuery JavaScript library. - coolors.co
Potential site palettes were tested with Coolors. - gauger.io
This website was used to generate the favicon using an icon from Font Awesome. - Am I Responsive?
For rendering the device preview image - https://ecotrust-canada.github.io/
For generating the formatted table of contents in markdown - Google Fonts
Used to provide the custom fonts for the site
- CodeInstitute modules on HTML, CSS and JavaScript.
- My mentor for his suggestions and feedback on the project.
- My friends who helped test the logic and design of the website.
- My teachers who first introduced me to Magic Squares and the symmetries that exist in mathematics.














