# Reward discounting in reinforcement learning for a Pong game

I am trying to understand how to train a neural network to win a Pong game using reinforcement learning, by following the blog post Spinning up a Pong AI with deep reinforcement learning.

The environment is provided by Gym AI. It gives the AI a reward of 1 if the opponent misses the ball, and a reward of -1 if it misses the ball.

I am confused about how reward discounting works in this context. This is the function that the blog post used:

def discount_rewards(r, gamma):
""" take 1D float array of rewards and compute discounted reward """
r = np.array(r)
discounted_r = np.zeros_like(r)
# we go from last reward to first one so we don't have to do exponentiations
for t in reversed(range(0, r.size)):
if r[t] != 0: running_add = 0 # if the game ended (in Pong), reset the reward sum
running_add = running_add * gamma + r[t] # the point here is to use Horner's method to compute those rewards efficiently