I didn't look at the rest of the code, but this looks like simulated annealing.
T is a temperature parameter that regulates the randomness you allow in the search. The lower
T gets, the less likely it is that a step that increases the energy (the function to be minimized) is accepted. The
if statement implements the Metropolis algorithm for bringing the system into statistical equilibrium at temperature
[Edit in response to the comments:]
There's more stuff in the directory containing the source code, including the input file for
digstat (distat.log) and a short explanation (playn.txt) (which by the way would have told you that the code performs simulated annealing). On comparing the input file for
digstat with other digram frequency data, it looks like these are natural logarithms of scaled digram frequencies, rounded to integers. (The scaling has no effect on the algorithm since it merely adds a constant to all scores.)
The code you quote computes the part of the Kullback–Leibler divergence that depends on the current key. This is the relative entropy between the digram frequencies corresponding to the current key and the actual English diagram frequencies. The part $\sum p_i\log p_i$ that involves only the actual digram frequencies and doesn't depend on the digram frequencies corresponding to the current key is omitted, since it would cancel out in the energy differences used in the Metropolis algorithm.