Advertisement
Guest User

Untitled

a guest
Sep 12th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.63 KB | None | 0 0
  1. var gameWon = 0
  2. var gamePlayed = 0
  3.  
  4. fun tirage(changerDePorte: Boolean) {
  5.     gamePlayed++
  6.  
  7.     val car = portes.random()
  8.     val choice = portes.random()
  9.     if (!changerDePorte) {
  10.         if (car == choice) {
  11.             println("T'as choisi la bonne porte du premier coup. Bravo !")
  12.             gameWon++
  13.         } else {
  14.             println("Oh non t'es nul, t'aurais du changer d'avis :(")
  15.         }
  16.     } else {
  17.         val porteOuverte = openDoor(car, choice)
  18.         val newChoice = portes.toMutableSet().apply {
  19.             remove(choice)
  20.             remove(porteOuverte)
  21.         }
  22.         assert(newChoice.size == 1)
  23.         if (newChoice.first() == car) {
  24.             gameWon++
  25.             println("T'as choisi la mauvaise porte, mais tu t'es ravisé.. Bravo !")
  26.         } else {
  27.             println("Oh non t'es nul, t'aurais pas du changer d'avis :(")
  28.         }
  29.     }
  30. }
  31.  
  32. fun main(args: Array<String>) {
  33.     println("Voulez vous changer tout le temps de porte (0) ou tout le temps garder la même (1) ?")
  34.     val choix = readLine()!!.toInt()
  35.     println("\nCombien de tirages ?")
  36.     val nombreTirage = readLine()!!.toInt()
  37.     for(i in 0 until nombreTirage) {
  38.         tirage(choix == 0)
  39.     }
  40.     println("\nNombre de parties jouées $gamePlayed\n Nombre de parties gagnées : $gameWon\nRatio : ${gameWon.toFloat()/ gamePlayed.toFloat()}")
  41. }
  42.  
  43. fun openDoor(car: Int, choice: Int): Int {
  44.     return portes.toMutableSet().apply {
  45.         remove(car)
  46.         remove(choice)
  47.     }.random()
  48. }
  49.  
  50. const val PORTE_1 = 0
  51. const val PORTE_2 = 1
  52. const val PORTE_3 = 2
  53.  
  54. val portes = setOf(PORTE_1, PORTE_2, PORTE_3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement