我是python的初学者。我的问题是必须通过简单交叉路口的两辆汽车之间的相互作用。我已经用FIACRE语言获得了所需的答案。我已经用这种语言实现了一个6 x 6的网格,如下所示:
a= [[1,1,1,1,1,1],
[1,1,0,2,1,1],
[1,3,0,0,0,1],
[1,0,0,0,0,1],
[1,1,0,0,1,1],
[1,1,1,1,1,1]]
其中1代表人行道,0代表街道,而2和3代表汽车。我已经获得了如下结果列表:m1,m2,m3,m4,m5和m6。每个代表一种运动。
#(m,n) and (y,x) are the coordinates for each car
# this is how I implemented in FIACRE, not in python
m1: if a[y][x]=2 and a[y][x-1]=0; then a[y][x-1]:=2; a[y][x]:=0; x:=x-1;
m2: if a[y][x]=2 and a[y+1][x]=0; then a[y+1][x]:=2; a[y][x]:=0; y:=y+1;
m3: if a[y][x]=2 and a[y][x+1]=0; then a[y][x+1]:=2; a[y][x]:=0; x:=x+1;
m4: if a[m][n]=3 and a[m][n+1]=0; then a[m][n+1]:=3; a[m][n]:=0; n:=n+1;
m5: if a[m][n]=3 and a[m-1][n]=0; then a[m-1][n]:=3; a[m][n]:=0; m:=m-1;
m6: if a[m][n]=3 and a[m+1][n]=0; then a[m+1][n]:=3; a[m][n]:=0; m:=m+1;
我已经开始实现python代码:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# here I import the .txt file with the m steps obtained in FIACRE
steps = pd.read_table("C:\\Users\\...\\ file.txt")
nrows, ncols = 6,6 #number of rows and columns
# Make a 6x6 grid...
image = np.array([[1,1,1,1,1,1],
[1,1,0,3,1,1],
[1,2,0,0,0,1],
[1,0,0,0,0,1],
[1,1,0,0,1,1],
[1,1,1,1,1,1]])
plt.matshow(image)
plt.show()
我期望的是一个代码,该代码实现了必须更改数组(图像)的步骤。然后,对每个步骤中获得的数组进行动画处理,以模拟汽车的运动,并保存为gif。