vendredi 13 décembre 2019

Programming transition matrix for chess pieces

Situation

I am currently trying to program the transition matrix for the king in the game of chess. We know that, on a 8x8 board, the probability of the king reaching a space j from a space i, where i,j = 1, 2, 3,..., 64 is

p(i,j) = 1/d(i) where d(i) # of adjacent spaces to i if i and j are adjacent


Attempt in R

My approach is exhaustive: to loop on all squares and increment a variable d equal to the number of adjacent squares whose belong to the set of all squares 1, 2,..., 64.

P = matrix(0, ncol = 64, nrow = 64)

for(i in 1:64){
  d <- 0

  if((i + 6) %in% 1:64){
    d <- d + 1
  } else if((i - 6) %in% 1:64){
    d <- d + 1
  } else if((i - 10) %in% 1:64){
    d <- d + 1
  } else if((i + 10) %in% 1:64){
    d <- d + 1
  } else if((i - 15) %in% 1:64){
    d <- d + 1
  } else if((i + 15) %in% 1:64){
    d <- d + 1
  } else if((i - 17) %in% 1:64){
    d <- d + 1
  } else if((i + 17) %in% 1:64){
    d <- d + 1
  }

  if((i + 6) %in% 1:64){
    P[i, i + 6] = 1/d
  } else if((i - 6) %in% 1:64){
    P[i, i - 6] = 1/d
  } else if((i - 10) %in% 1:64){
    P[i, i - 10] = 1/d
  } else if((i + 10) %in% 1:64){
    P[i, i + 10] = 1/d
  } else if((i - 15) %in% 1:64){
    P[i, i - 15] = 1/d
  } else if((i + 15) %in% 1:64){
    P[i, i + 15] = 1/d
  } else if((i - 17) %in% 1:64){
    P[i, i - 17] = 1/d
  } else if((i + 17) %in% 1:64){
    P[i, i + 17] = 1/d
  }

}

Problem

The variable d does not seem to increment as expected. It should have values between 3 (if the king is in the corners) or 8 (if the king is not in first or last rank or files). I seem to be getting only values of 1 for d. In other words, d does not seem to increment after the first if statement.

This can be seen if add print(d) after d is incremented.

Aucun commentaire:

Enregistrer un commentaire