Cloud Stack Ninja

This is my code for the project. I have to write the updateemergence function. We need to change the color of the dots to blue using a hypothesis. Mine is that when a red dot is surrounded by more than 40% of blue dots, it changes to blue. But how do I write it?

import tkinter from random import randint

class Emergence:

def __init__(self, gridsize = 10, gridtype = 0):
    """ initialize the emergence grid
    """
    if gridsize > 10:
        print("grid size is reduced to the max size 10")
        self.gridsize = 10
    else:
        self.gridsize = gridsize # number of grid points in each dimension
    
    self.dotsize = 10 # the diameter for each dot
    self.xspace = 40 # the space between two adjacent dots
    self.yspace = self.xspace
    self.numberofcolors = 2 # could be more than 2
    self.TOTALGRIDRATIO = 0.8
    self.gridcolors = [[0 for _ in range(self.gridsize)] for _ in range(self.gridsize)] # to save the colors for each point
    self.gridwidth = 10
    self.gridheight = 10
    self.canvaswidth = 0 
    
    # at this point, we only have one example 10x10 grid
    # if user requires an example grid with size smaller than 10,
    # we will use the top left subsquare of the 10x10 example
    if gridtype == 0 : # use the example grid
        examplegridcolors = [[0, 1, 0, 0, 0, 1, 0, 0, 1, 1], #row 1
                 [1, 1, 0 ,0, 0, 1, 0, 0, 0, 1],  #row 2
                 [0, 0, 0, 1, 1, 0, 1, 0, 0, 1],  #row 3
                 [1, 0, 0, 1, 0, 0, 0, 1, 0, 1],  #row 4
                 [1, 1, 1, 1, 1, 0, 0, 0, 0, 1],  #row 5
                 [0, 0, 1, 0, 0, 1, 1, 0, 0, 1],  #row 6
                 [0, 1, 1, 0, 0, 0, 1, 1, 0, 1],  #row 7
                 [0, 0, 0, 1, 0, 1, 1, 0, 0, 1],
                 [0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
                 [0, 0, 0, 1, 0, 1, 1, 0, 1, 1]]
                 
        self.gridcolors = examplegridcolors  
        
    else:
    # initialize the grid using random numbers
        for i in range(self.gridsize):
            for j in range(self.gridsize):
                self.gridcolors[i][j] = randint(0, self.numberofcolors - 1)
                
    # initialize the GUI window here. 
    # create the main window
    self.mainwindow = tkinter.Tk() 
    # initialize the canvas to None. 
    self.canvas = None
   
# use this function for debugging purposes   
def printGrid(self):
    """ prints the gridcolors in text
    """
    for i in range(self.gridsize):
        for j in range(self.gridsize):
            print(self.gridcolors[i][j] + "\t" , end="")
        print()

# this function make the GUI window 
def showindow(self):
    self.mainwindow.title("Emergence Grid")
    self.mainwindow.geometry("500x500") # default size of the window
    self.mainwindow.resizable(0, 0)
    self.mainwindow.bind('<Button-1>', self.onMouseClick) # bind the onMouseClick to the left mouse click <Button-1>
    # make the application ready to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event till the window is not closed.
    self.mainwindow.mainloop()
    self.drawGrid()

def computeGridLayout(self):
    """ The computeGridLayout method computes the coordinates of the dots
    """
    # compute width and height of the grid
    self.gridwidth = self.gridsize * (self.dotsize + self.xspace)
    self.canvaswidth = self.gridwidth / self.TOTALGRIDRATIO 
    # create canvas
    if self.canvas == None :
        self.canvas = tkinter.Canvas(self.mainwindow, height = self.canvaswidth, width = self.canvaswidth)
    else:
        self.canvas.delete("all") # clear the canvas

    self.canvas.pack() 
    
def drawGrid(self):
    """ draw the grid dots on canvas
    """

    # put your code here. 
    r = self.dotsize //2
    topLeftCorner_x = (self.canvaswidth - self.gridwidth) //2 # make the grid to be on center of canvas
    topLeftCorner_y = topLeftCorner_x

    for i in range(self.gridsize):
        for j in range(self.gridsize):
        # compute the coordinates of grid
            x = topLeftCorner_x + j * self.xspace
            y = topLeftCorner_y + i * self.yspace

            if self.gridcolors[i][j] == 0 :
                color = "blue"
            else:
                color = "red"

            self.canvas.create_oval(x-r, y-r, x+r, y+r, fill=color) # draw a circle


           # self.canvas.create_oval(50, 50, 80, 80, fill="green") # draw a circle

def updateEmergenceGrid(self):
    """ The updateEmergenceGrid function updates grid based on emergence rules
    """
    # put your code here
    same_color = 0
    for i in range(self.gridsize):
        for j in range(self.gridsize):
          #  while self.gridcolors[i][j] == 1:
            if self.gridcolors[i][j] == 1:
                count = chck_neighbor[i][j]
                
                for r in range(count):
                    for c in range(count):
                        if 



    pass  # placeholder for body of the method
   

def onMouseClick(self, event):
    """ The onMouseClick event handler will call everytime user clicks on main form.
    """
    self.computeGridLayout()
    self.drawGrid() 
    self.updateEmergenceGrid()

def in_bounds(self, row, col):
    """ Checks if the row and column are in bounds of the grid
        returns True when in bounds and False when not
    """
    if 0 < row < self.gridsize and 0 < col  < self.gridsize:
        return True
    else:
        return False

def chck_neighbor(self, row, col):
    """ Checks the neighbors of a dot with a particular row and col
    """
    for dr in [-1, 0, 1]:
        for dc in [-1, 0, 1]:
            
            num_ngbr = 0
            neighbor_row = row + dr
            neighbor_col = col + dc
            
            if self.in_bounds(neighbor_row, neighbor_col):
               # neighbor = self.gridsize[neighbor_row][neighbor_col]
                num_ngbr += 1                
        #return neighbor
    return num_ngbr

if name == "main":

e = Emergence(10,0)
e.showindow()


Read more here: https://stackoverflow.com/questions/64404360/we-have-to-change-the-color-of-grid-dots-using-emergence-pattern

Content Attribution

This content was originally published by ryka at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: