u=1;
f[u_,q_]:=u*q;
L=20;
i=40;
n=50;
dx=2L/i;
dt=dx/2;
q0[x_]:=Module[{},If[x<-L+17||x>L-17,Return[0],Return[1]]];
grid=Range[-L,L,dx];
F[a_,b_]:=Module[{dfa,dfb,x,q},
dfa=D[f[u,x],x]/.x->a;
dfb=D[f[u,x],x]/.x->b;
(*Print[{dfa,dfb}];*)
If[dfa>0&&dfb>0,Return[f[u,a]]];
If[dfa<0&&dfb<0,Return[f[u,b]]];
]
Plot[q0[x],{x,-L,L}]

Q={};
AppendTo[Q,{}];
For[j=1,j<=i,j++,AppendTo[Q[[1]],q0[grid[[j]]]]];
Q[[1]];
For[k=2,k≤n,k++,
AppendTo[Q,{}];
AppendTo[Q[[k]],0];
{
For[j=2,j<i,j++,
{
AppendTo[Q[[k]],Q[[k-1,j]]-(dt/dx)(F[Q[[k-1,j]],Q[[k-1,j+1]]]-F[Q[[k-1,j-1]],Q[[k-1,j]]])]
}
]
AppendTo[Q[[k]],0];
}
]
Animate[ListPlot[Q[[k]],Joined->True,PlotRange->{{0,40},{0,1.5}}],{k,1,n,1}]