Building Premagic Game

by Kardi Teknomo

In this tutorial, we will build a simple premagic game. Through this tutorial, you will learn the following:

What is Premagic Game?

Premagic game is a simple solitaire board game created based on my paper "Premagic and Ideal Flow Matrices". The player would change the value of the cells such that the sum of rows is exactly the same as the sum of columns. The challenge is to win the game in minimum number of cell changes.

premagic

Clearly, the game of size N can be solved at maximum $N^{2}-N$ number of cell changes. The player would lose if the number of changes is more than the maximum number of cell change. loss

Simple solution to win the game is to make symmetric matrix, or constant matrix. win

Let us start. First, we importing the necessary modules.

Before we enter the development of the game, I need to explain several concepts that will be used in the game development:

  1. Natural Matrix
  2. Premagic Matrix

Natural Matrix

Natural matrix is a matrix of ordered natural number. There are two ways to create natural matrix:

  1. Fill the matrix by columns
  2. Fill the matrix by rows

Given an index $i$ (1 to $mR \cdot mC$) and matrix size (i.e. maximum number of rows $mR$ or maximum number of columns $mC$), we can find row and column associated with the index.

The index start from 1 up to $mR \cdot mC$, which is the matrix size.

Natural matrix filled by columns

The formula is as follow:

$ r = r=mod(\left ( i-1 \right ),mR)+1 $

$ c = \left \lceil \frac{i}{mR} \right \rceil $

$ i = mR\cdot \left ( c-1 \right )+r $

The following codes show the natural matrix filled by columns.

Natural matrix filled by rows

The formula is as follow:

$ r = \left \lceil \frac{i}{mC} \right \rceil $

$ c = mod(\left ( i-1 \right ),mC)+1 $

$ i = mC\cdot \left ( r-1 \right )+c $

The following codes show the natural matrix filled by rows.

Premagic

A matrix is called premagic if and only if the sum of rows is exactly the same of the sum of columns. This paper on "Premagic and Ideal Flow Matrices" shows an easy way to test if a matrix is premagic:

$\textbf{A}\cdot \textbf{j}=\textbf{A}^{T}\cdot \textbf{j}$

Graphical User Interface of the Game

Now we will develop the graphical user interface of the board game.

First Version

First we will generate the layout programmatically. To do so, we create the layout as string, then we use eval() Python function to convert the string into code.

In the code below, we will use the layout function to generate the layout programmatically and then show.

The result of the first version layout would be shown below. firstVersion

Second Version

In the second version of layout, we will change the layout to accept event (by setting enable_events=True) and we add key such that we can call them uniquely later.

The following layout is copy of first version layout, add enable_events=True in the entry input text and then add key to all elements.

In the second version of the game program, we copy the first version of the game above and then add more code to initialize and in the while loop section.

The result of the second version would be similar to the picture below.

secondVersion

That's all. Enjoy the game and extend the game with your own additional optional features.

Visit www.Revoledu.com for more tutorials in Data Science

Copyright © 2021 Kardi Teknomo

Permission is granted to share this notebook as long as the copyright notice is intact.