Setup¶

In [1]:
from math import floor as mF
import matplotlib
from matplotlib import colormaps as cm
import matplotlib.colors as mCm
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle as mpR
from matplotlib.animation import FuncAnimation as FA
import numpy as np
import pandas as pd

Constants & Functions¶

In [2]:
def getViridis(n):
    """
    Get a viridis color sequence.

    Parameters:
    n - Number of colors.
    
    Returns:
    List of n hex codes as strings.
    """
    
    return([mCm.to_hex(cm['viridis_r'].resampled(n).colors[v]) for v in range(n)])


#Viridis.
colors = getViridis(7)


#Clear axes.
def clearAx(ax):
    
    """
    Clear axes.

    Parameters:
    ax - Current ax.
    
    Returns:
    ax - Updated ax.
    """    
    
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)
    ax.set_xticks([])
    ax.set_yticks([])
    return(ax)





def chessboardHops(N, L, initial, G) :
    """
    Determine the states at the end of each generation from 1 to G
    for an NxN chessboard given initial point I and various ways to
    'hop' in list L.

    Parameters:
    N - Dimension of chessboard.
    L - List of ordered pairs, each ordered pair representing a scalar.
    initial - Ordered pair.  Consequently, the first generation.
    G - Number of generations to iterate.
    
    Returns:
    generations - List of lists of ordered pairs.
    """    
    
    Generations = {initial : 1}
    generations = [[initial]]
    
    g = 1
    while G - g > 0 :
        
        current_generation = []
        
        for square in [sq for sq, gen in Generations.items() if gen == g]  :
            
            sx = square[0]
            sy = square[1]
            
            for hop in L :
                
                hx = hop[0]
                hy = hop[1]
                
                #Right.
                pos_x = sx + hx
                #Left.
                neg_x = sx - hx
                #Top.
                pos_y = sy + hy
                #Bottom.
                neg_y = sy - hy
                
                #Hop to the top right.
                if (pos_x <= N-1) and (pos_y <= N-1) and ((pos_x, pos_y) not in Generations.keys()) :
                    Generations[(pos_x, pos_y)] = g+1
                    current_generation.append((pos_x, pos_y))
                
                #Hop to the bottom right.
                if (pos_x <= N-1) and (0 <= neg_y) and ((pos_x, neg_y) not in Generations.keys()) :
                    Generations[(pos_x, neg_y)] = g+1
                    current_generation.append((pos_x, neg_y))
                
                #Hop to the top left.
                if (0 <= neg_x) and (pos_y <= N-1) and ((neg_x, pos_y) not in Generations.keys()) :
                    Generations[(neg_x, pos_y)] = g+1
                    current_generation.append((neg_x, pos_y))
                
                #Hop to the bottom left.
                if (0 <= neg_x) and (0 <= neg_y) and ((neg_x, neg_y) not in Generations.keys()) :
                    Generations[(neg_x, neg_y)] = g+1 
                    current_generation.append((neg_x, neg_y))
        
        #Generation complete.
        generations.append(current_generation)
        g += 1
        
    return(generations)
      
    



def plotHelper(fig, option, initial, g, save) :
  
    ax = fig.add_subplot(xlim = (-0.55, 19.55),
                         ylim = (-0.55, 13.55))
    fig.subplots_adjust(left = 0.0,
                        bottom = 0,
                        right = 1,
                        top = 1)
     
    #Remove axes and ticks.
    ax = clearAx(ax)

    #Grid.
    #Horizontal
    for y in range(15) :    
        ax.plot([-0.5, 13.5],
                [y-0.5, y-0.5],
                c = 'k',
                lw = 3,
                zorder = 2)
    #Vertical
    for x in range(15) :    
        ax.plot([x-0.5, x-0.5],
                [-0.5, 13.5],
                c = 'k',
                lw = 3,
                zorder = 2)
        
    if option == 'Fiddler' :
        
        squares = [(2, 1), (9, 5), (10, 13), (3, 9)]
        for k in range(4) :
            ax.add_patch(mpR((squares[k][0] - 0.5, squares[k][1] - 0.5),
                             width = 1,
                             height = 1,
                             facecolor = colors[k],
                             zorder = 1))
        fig.savefig('2026.05.01Fiddler.png')
        
    else : 
        
        #[(6, 6) - 7,
        # (6, 3) - 6
        # (6, 0) - 7,
        # (5, 1) - 6
        # (3, 3) - 6,
        #  (0, 0) - 7,]   
        G = chessboardHops(14, [(1, 8), (4, 7), (7, 4), (8, 1)], initial, g)

        for j in range(g) :

            for square in G[j] :
                ax.add_patch(mpR((square[0] - 0.5, square[1] - 0.5),
                                 width = 1,
                                 height = 1,
                                 facecolor = colors[j],
                                 zorder = 1))


        text = [("Initial :", (17, 11), 'k', 32),
                ("(" + str(initial[0]) + ", " + str(initial[1]) + ")", (17, 9), colors[0], 56),
                ("Hops :", (17, 6), 'k', 32),
                (g-1, (17, 4), colors[g-1], 56)]

        for t in text :
            plt.annotate(text = t[0],
                         xy = t[1],
                         c = t[2],
                         size = t[3],
                         ha = 'center',
                         va = 'center')
            
        if save :
            fig.savefig('2026.05.01EC.png')
In [3]:
fig = plt.figure(figsize = (10, 7))
plotHelper(fig, 'Fiddler', (0,0), 0, False)
In [4]:
#Animate 
def hopTheBoard() :
    
    fig = plt.figure(figsize = (9, 6.3))
    fig.set_tight_layout(True)
    
    def animate(i):
        
        plt.clf()
        
        if i <= 6 :
            plotHelper(fig, "EC", (6, 6), i+1, False)

        elif i <= 12 :
            plotHelper(fig, "EC", (6, 3), i-6, False)
        
        elif i == 13 :
            plotHelper(fig, "EC", (5, 4), i-12, True)
        
        elif i <= 18 :
            plotHelper(fig, "EC", (5, 4), i-12, False)
            
        elif i <= 25 :
            plotHelper(fig, "EC", (6, 0), i-18, False)   
            
        elif i <= 31 :
            plotHelper(fig, "EC", (5, 1), i-25, False)
            
        elif i <= 37 :
            plotHelper(fig, "EC", (4, 2), i-31, False)
            
        elif i <= 43 :
            plotHelper(fig, "EC", (3, 3), i-37, False)

        elif i <= 49 :
            plotHelper(fig, "EC", (2, 1), i-43, False)            

        elif i <= 56 :
            plotHelper(fig, "EC", (0, 0), i-49, False)
                       

    #Run animation.
    anim = FA(fig, animate,
              frames = 57,
              interval = 800)   

    #Save animation.
    anim.save('2026.05.01EC.mp4'); 
In [5]:
hopTheBoard()

Fiddler¶

Rohan Lewis¶

2026.05.04¶