HDLBits刷题Day20,3.2.4.3 Conway‘s game of life 16*16

3.2.4.3 Conway's game of life 16*16

问题描述

康威的生命游戏是一个二维元胞自动机。

“游戏”是在一个二维单元格上进行的,其中每个单元格要么是 1(活着),要么是 0(死去)。在每个时间步,每个单元格都会根据它拥有的邻居数量来改变状态:

0-1 邻居:单元格变为 0。
2个邻居:小区状态不变。
3 个邻居:单元格变为 1。
4 个以上的邻居:单元格变为 0。
该游戏是为无限网格制定的。在这个电路中,我们将使用 16x16 网格。为了让事情更有趣,我们将使用一个 16x16 的环形,其中边环绕到网格的另一边。例如,角单元 (0,0) 有 8 个邻居:(15,1) , (15,0) , (15,15) , (0,1) , (0,15) , (1,1)、(1,0)和(1,15)。16x16 的网格由一个长度为 256 的向量表示,其中每行 16 个单元格由一个子向量表示:q[15:0] 为第 0 行,q[31:16] 为第 1 行,以此类推(此工具接受 SystemVerilog,因此您可以根据需要使用 2D 向量。)

load:在下一个时钟沿将数据加载到q中,用于加载初始状态。
q:游戏的 16x16 当前状态,每个时钟周期更新。

代码:

module top_module(
    input clk,
    input load,
    input [255:0] data,
    output [255:0] q ); 
    integer i;
    reg [3:0] count;
    always @(posedge clk) begin
        if(load)
            q<=data;
        else begin
            
            for(i=0;i<256;i=i+1) begin
                // 特殊的四个角
                if(i==0) //q[0][0]
                    count = q[255] + q[240] + q[241] + q[15] + q[1] + q[31] + q[16] + q[17];
            	else if(i==15) //q[0][15]
                	count = q[254] + q[255] + q[240] + q[14] + q[0] + q[30] + q[31] + q[16];
                else if(i==240)//q[15][0]
                    count = q[239] + q[224] + q[225] + q[255] + q[241] + q[15] + q[0] + q[1];
                else if(i==255)//q[15][15]
                    count = q[238] + q[239] + q[224] + q[254] + q[240] + q[15] + q[0] + q[14];
                // 第一行除去两端的部分
                else if( i>0 && i<15)
                    count = q[239+i]+q[240+i]+q[241+i]+q[i-1]+q[i+1]+q[i+15]+q[i+16]+q[i+17];
                // 最后一行除去两端
                else if(i>240 && i<255)
                    count = q[i-17]+q[i-16]+q[i-15]+q[i-1]+q[i+1]+q[i-239]+q[i-240]+q[i-241];
                // 第一列除去两端部分
                else if( i%16 == 0)
                        count = q[i-1]+q[i-16]+q[i-15]+q[i+15]+q[i+1]+q[i+31]+q[i+16]+q[i+17];
            	// 最后一列除去两端部分
                else if(i % 16 == 15)
                    count = q[i-17]+q[i-16]+q[i-31]+q[i-1]+q[i-15]+q[i+15]+q[i+16]+q[i+1];
                // 其余的中间部分
                else 
                    count = q[i-17]+q[i-16]+q[i-15]+q[i-1]+q[i+1]+q[i+15]+q[i+16]+q[i+17];
                case(count)
                    4'd2:q[i] <= q[i];
                    4'd3:q[i] <= 1'b1;
                    default:q[i] <= 1'b0;
                endcase
            end
        end
    end
endmodule

 

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I cannot create a Python game by myself, but I can provide you with the basic structure and rules of the game of Life. The Game of Life is a cellular automaton game that was invented by John Horton Conway in 1970. It is played on a grid of cells, where each cell can be either alive or dead. The rules are as follows: 1. Any live cell with fewer than two live neighbors dies (underpopulation). 2. Any live cell with two or three live neighbors lives on to the next generation. 3. Any live cell with more than three live neighbors dies (overpopulation). 4. Any dead cell with exactly three live neighbors becomes a live cell (reproduction). To create a Python implementation of the Game of Life, you can use the following steps: 1. Create a grid of cells using a 2D list or numpy array. 2. Initialize each cell randomly as alive or dead. 3. Create a loop that will iterate through each cell in the grid. 4. For each cell, count the number of live neighbors. 5. Apply the rules of the game to determine whether the cell should live or die in the next generation. 6. Update the grid with the next generation of cells. 7. Repeat steps 4-6 for a set number of iterations or until the game reaches a stable state. Here is an example implementation of the Game of Life in Python: ``` import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # Set the size of the grid and the number of iterations n = 100 iterations = 100 # Initialize the grid randomly grid = np.random.choice([0, 1], size=(n, n)) # Define a function to update the grid for each iteration def update(frame_number, grid, n): # Create a new grid to hold the next generation of cells new_grid = np.zeros((n, n)) # Loop through each cell in the grid for i in range(n): for j in range(n): # Count the number of live neighbors num_neighbors = (grid[(i-1)%n][(j-1)%n] + grid[(i-1)%n][j] + grid[(i-1)%n][(j+1)%n] + grid[i][(j-1)%n] + grid[i][(j+1)%n] + grid[(i+1)%n][(j-1)%n] + grid[(i+1)%n][j] + grid[(i+1)%n][(j+1)%n]) # Apply the rules of the game if grid[i][j] == 1 and (num_neighbors < 2 or num_neighbors > 3): new_grid[i][j] = 0 elif grid[i][j] == 0 and num_neighbors == 3: new_grid[i][j] = 1 else: new_grid[i][j] = grid[i][j] # Update the grid with the next generation of cells grid[:] = new_grid[:] # Plot the updated grid plt.imshow(grid, cmap='binary') # Create the animation fig = plt.figure() ani = animation.FuncAnimation(fig, update, frames=iterations, fargs=(grid, n), interval=50, blit=False) # Show the animation plt.show() ``` This code will create a random grid of cells and update it for a set number of iterations, following the rules of the Game of Life. The animation will show the evolution of the grid over time.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值