Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Solution to the previous quiz: '8/KppppPPk/8/8/8/8/8/8'. As always, let's analyze the script line by line. First we define a string fen with the value "8/KPPppppppk/8/8/8/8/8/8/8/8/8/8 w - - 0 1" and you may wonder, what the hell is this? Well, although it doesn't look like it, it's a representation of a chess position. It is not a diagram, but it is a format that is readable by a chess program and contains all the essential elements that describe a chess position.
These elements are extracted from the FEN in the following line p, t, r1, r2, o, m = fen.split(). Here we have used the split method and then unpacked the content in the variables p (piece arrangement), t (turn), r1 (castling possibilities), r2 (square for the en passant pawn; I should have named this variable differently), o (number of half moves to apply the fifty move rule) and m (number of full moves since the game started).
These are all attributes of a chess position, but for this script we are only interested in the first one, p, that is, the piece arrangement itself.
What we basically do is to apply the replace method to replace the characters "P" in the string with the character "p", but by specifying the third numerical argument of replace, we are telling it to do the replacement only for that number of characters. Since in this case we said 2, only two "P" characters will be replaced. That explains why the value of new is '8/KppppPPPPk/8/8/8/8/8/8/8/8/8/8'.
I know, it might be annoying for a human to read that string, but it's not impossible. By the way, the capital P stands for white pawn, while the small p stands for black pawn, so with this operation what we did was to replace two white pawns by two black ones.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Bienvenido a mi nueva serie de Quizzes de Programaci贸n, en la cual podr谩s poner a prueba tus conocimientos y habilidades sobre programaci贸n y desarrollo de software de una manera sencilla y divertida. Si quieres aprender m谩s sobre ella visita mi blog aqu铆 en Hive y el primer post donde la present茅.
Soluci贸n al quiz anterior: '8/KppppPPk/8/8/8/8/8/8'. Como siempre, analicemos el script l铆nea por l铆nea. Primero definimos la cadena fen con el valor "8/KPPppppppk/8/8/8/8/8/8/8/8/8/8 w - - 0 1" y te preguntar谩s, 驴qu茅 carajos es esto? Pues aunque no lo parezca, es una representaci贸n de una posici贸n de ajedrez. No es un diagrama, pero es un formato que es legible por un programa de ajedrez y contiene todos los elementos esenciales que describen una posici贸n de ajedrez.
Estos elementos son extra铆dos del FEN en la siguiente l铆nea p, t, r1, r2, o, m = fen.split(). Aqu铆 hemos utilizado el m茅todo split y luego descomprimimos el contenido en las variables p (disposici贸n de piezas), t (turno), r1 (posibilidades de enroque), r2 (casilla para el pe贸n al paso; deber铆a haber nombrado esta variable de otra manera), o (n煤mero de medios movimientos para aplicar la regla de los cincuenta movimientos) y m (n煤mero de movimientos completos desde que comenz贸 la partida).
Todos estos son atributos de una posici贸n de ajedrez, pero para este script s贸lo nos interesa el primero, p, es decir, la disposici贸n de las piezas en s铆.
Lo que b谩sicamente hacemos es aplicar el m茅todo replace para reemplazar los caracteres "P" en la cadena con el car谩cter "p" , pero al especificar el tercer argumento num茅rico de replace, le estamos diciendo que reemplace solo esa cantidad de caracteres. Como en este caso dijimos 2, solo se reemplazar谩n dos caracteres "P". Eso explica por qu茅 el valor de new es '8/KppppPPPPk/8/8/8/8/8/8/8/8/8/8'.
Lo s茅, puede resultar molesto para un humano leer esa cadena, pero no es imposible. Por cierto, la P may煤scula representa un pe贸n blanco, mientras que la p min煤scula representa un pe贸n negro, entonces con esta operaci贸n lo que hicimos fue sustituir dos peones blancos por dos negros.
Si quieres bloguear sobre contenido inform谩tico y de programaci贸n, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.