Skip to content

Des œuvres d'art

Objectifs didactiques

  • Gérer plusieures boucles imbriqués
  • Structuration du code

Dans la programmation d'algorithmes, souvent il est nécessaire d'utiliser plusieurs boucles imbriquées. Une façon classique d'exercer cela est d'essayer de produire des petits "œuvres d'art".

Le carré

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
fun main(args: Array<String>) {
    val sizeSquare = 10
    for (y in 1..sizeSquare) {
        for (x in 1..sizeSquare) {
            print("*")
        }
        println()
    }
}

Voici une alternative pour la formulation des boucles de type for :

fun main(args: Array<String>) {
    val sizeSquare = 10
    for (y in 0 until sizeSquare) { // itération de 0 à sizeSquare-1 !
        for (x in 0 until sizeSquare) {
            print("*")
        }
        println()
    }
}

Le triangle

*
**
***
****
*****
******
*******
fun main(args: Array<String>) {
    val sizeTriangle = 7
    for (y in 1..sizeTriangle) {
        for (x in 1..y) {
            print("*")
        }
        println()
    }
}

Variante en structurant le code avec une fonction supplémentaire:

Code
fun printCharNTimes(c:Char, n:Int) {
    for (x in 1..n) {
        print(c)
    }
}

fun main(args: Array<String>) {
    val sizeTriangle = 7
    for (n in 1..sizeTriangle) {
        printCharNTimes('*', n)
        println()
    }
}

Les dents de scie

*
**
***
****
*****
*
**
***
****
*****
*
**
***
****
*****
Code
fun main(args: Array<String>) {
    val numberOfTeeth = 3
    val sizeOfTooth = 5
    for (n in 1..numberOfTeeth) {
        for (y in 1..sizeOfTooth) {
            for (x in 1..y) {
                print("*")
            }
            println()
        }
    }
}

Le diamant

     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *
Code
fun main(args: Array<String>) {
    val size = 5
    for (y in 1..size) {
        for (x in 1..size-y) {
            print(" ")
        }
        for (x in 1 until 2*y) {
            print("*")
        }
        println()
    }
    for (y in size-1 downTo 1) {
        for (x in 1..size-y) {
            print(" ")
        }
        for (x in 1 until 2*y) {
            print("*")
        }
        println()
    }
}
Code (structuré avec fonctions supplémentaires)
fun printDiamondLine(nbrSpaces: Int, nbrStars: Int) {
    printCharNTimes(' ', nbrSpaces)
    printCharNTimes('*', nbrStars)
    println()
}

fun main(args: Array<String>) {
    val sizeDiamond = 6
    for (y in 1..sizeDiamond) {         // first half of diamond
        printDiamondLine(sizeDiamond-y, (2*y)-1)
    }
    for (y in sizeDiamond-1 downTo 1) { // second half
        printDiamondLine(sizeDiamond-y, (2*y)-1)
    }
}

Le sapin de Noël

    *
   ***
    *
   ***
  *****
    *
   ***
  *****
 *******
    *
   ***
  *****
 *******
*********
Code
fun main(args: Array<String>) {
    val size = 4
    for (n in 1..size) {
        for (y in 1..n+1) {
            for (x in 1..size+1-y) {
                print(" ")
            }
            for (x in 1 until y*2) {
                print("*")
            }
            println()
        }
    }
}
     *
    ***
   *****
    ***
   *****
  *******
   *****
  *******
 *********
  *******
 *********
***********
Code
fun main(args: Array<String>) {
    val size = 3
    for (n in 2..size+2) {
        for (y in n-2..n) {
            for (x in 1..size+2-y) {
                print(" ")
            }
            for (x in 0..y*2) {
                print("*")
            }
            println()
        }
    }
}

Le damier

|||---|||---|||
|||---|||---|||
|||---|||---|||
---|||---|||---
---|||---|||---
---|||---|||---
|||---|||---|||
|||---|||---|||
|||---|||---|||
---|||---|||---
---|||---|||---
---|||---|||---
|||---|||---|||
|||---|||---|||
|||---|||---|||
Code
fun main(args: Array<String>) {
    val size = 5
    val square = 3
    for (y in 1..size) {
        for (i in 1..square) {
            for (x in 1..size) {
                if ((x + y) % 2 == 0) {
                    for (j in 1..square) {
                        print("|")
                    }
                } else {
                    for (j in 1..square) {
                        print("-")
                    }
                }
            }
            println()
        }
    }
}
@@@...@@@...@@@
@@@...@@@...@@@
@@@...@@@...@@@
...@@@...@@@...
...@@@...@@@...
...@@@...@@@...
@@@...@@@...@@@
@@@...@@@...@@@
@@@...@@@...@@@
...@@@...@@@...
...@@@...@@@...
...@@@...@@@...
@@@...@@@...@@@
@@@...@@@...@@@
@@@...@@@...@@@
///\\\///\\\///
///\\\///\\\///
///\\\///\\\///
\\\///\\\///\\\
\\\///\\\///\\\
\\\///\\\///\\\
///\\\///\\\///
///\\\///\\\///
///\\\///\\\///
\\\///\\\///\\\
\\\///\\\///\\\
\\\///\\\///\\\
///\\\///\\\///
///\\\///\\\///
///\\\///\\\///