implementing value-iteration algorithm