Skip to content

Instantly share code, notes, and snippets.

@thomas-louvigne
Last active September 10, 2016 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomas-louvigne/34e6f076c248f709371c18b37ee630d4 to your computer and use it in GitHub Desktop.
Save thomas-louvigne/34e6f076c248f709371c18b37ee630d4 to your computer and use it in GitHub Desktop.
Damier en JS
function buildDamier(taille)
{
// On présupose que le damier est carré,
// C'est à dire que son nombre de colone est égale à son nombre de ligne
var ligne = new Array(taille);
for ( let i = 0 ; i < taille ; i++)
{
let colonne = new Array(taille);
ligne[i] = colonne; // A chaque ligne, on ajoute les colonnes
for (let j = 0; j < taille; j++)
{
// Dans chaque case on test de savoir si on est sur un numéro de case pair ou impair
// Pour cela, on additionne de sa position dans les lignes et dans les colonnes.
ligne[i][j] = ( ((i+j)%2)== 0 ? 1 : -1);
}
}
return(ligne);
};
console.log(buildDamier(9));
@sp4ce
Copy link

sp4ce commented Sep 10, 2016

Salut! vivent les damiers: https://jsfiddle.net/bkooa7rc/2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment