Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.39 KB | None | 0 0
  1. var randomInt: Int = 0
  2. var attemptCounter: Int = 0
  3. fun main(args: Array<String>) {
  4.  
  5.     gameTypeSelection()
  6.  
  7. }
  8. fun gameTypeSelection(){
  9.     println("Select type of game:\n 1. Human vs Human\n 2. Human vs AI\n 3. AI vs AI")
  10.     val type = readLine()?.toInt()
  11.     if (type == 1){
  12.         println("You have selected Human vs Human")
  13.         randomNumberImplementation()
  14.     }else if (type == 2){
  15.         println("You have selected Human vs AI")
  16.         randomNumberGenerator(true)
  17.     }else if (type == 3){
  18.         println("You have selected AI vs AI")
  19.         randomNumberGenerator(false)
  20.     }else{
  21.         println("You have selected wrong")
  22.         gameTypeSelection()
  23.     }
  24. }
  25. //for AI and human player 2.
  26. fun randomNumberGenerator(type: Boolean){
  27.     randomInt = (Math.random()*101).toInt()
  28.     println("This is random number $randomInt")//check
  29.     if (type){
  30.         numberGuessing()
  31.     }else{
  32.         numberGuessingForAI()
  33.     }
  34. }
  35. //for human player
  36. fun randomNumberImplementation(){
  37.     println("First player must enter number between 1 and 100")
  38.     randomInt = readLine()?.toInt()!!
  39.     if(randomInt in 1..100){
  40.         numberGuessing()
  41.     }else{
  42.         println("Error.")
  43.         randomNumberImplementation()
  44.     }
  45. }
  46. //for AI player
  47. fun numberGuessingForAI(){
  48.     var guessAI = (Math.random()*101).toInt()
  49.     println(guessAI)
  50.     guesValueCheckForAI(guessAI)
  51. }
  52. fun guesValueCheckForAI(value: Int){
  53.     if (value < randomInt){
  54.         attemptCounter++
  55.         numberGuessingForAI()
  56.     }else if (value > randomInt){
  57.         attemptCounter++
  58.         numberGuessingForAI()
  59.     }else if (value == randomInt){
  60.         println("Correct guess is $value")
  61.         guessCounter(false)
  62.     }
  63. }
  64. //for human player
  65. fun numberGuessing(){
  66.     println("Player guess number")
  67.     val ats = readLine()?.toInt()
  68.     if (ats != null) {
  69.         checkingGuess(ats)
  70.     }
  71. }
  72. fun checkingGuess(ats: Int){
  73.     if(ats < randomInt){
  74.         println("Is more")
  75.         guessCounter(true)
  76.     }else if(ats > randomInt){
  77.         println("Is less")
  78.         guessCounter(true)
  79.     }else if (ats == randomInt){
  80.         println("Correct guess is $ats")
  81.         guessCounter(false)
  82.     }
  83. }
  84. fun guessCounter(check: Boolean){
  85.     if(check){
  86.         attemptCounter++
  87.         numberGuessing()
  88.     }else{
  89.         attemptCounter++
  90.         println("Guessed $attemptCounter times")
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement