Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var randomInt: Int = 0
- var attemptCounter: Int = 0
- fun main(args: Array<String>) {
- gameTypeSelection()
- }
- fun gameTypeSelection(){
- println("Select type of game:\n 1. Human vs Human\n 2. Human vs AI\n 3. AI vs AI")
- val type = readLine()?.toInt()
- if (type == 1){
- println("You have selected Human vs Human")
- randomNumberImplementation()
- }else if (type == 2){
- println("You have selected Human vs AI")
- randomNumberGenerator(true)
- }else if (type == 3){
- println("You have selected AI vs AI")
- randomNumberGenerator(false)
- }else{
- println("You have selected wrong")
- gameTypeSelection()
- }
- }
- //for AI and human player 2.
- fun randomNumberGenerator(type: Boolean){
- randomInt = (Math.random()*101).toInt()
- println("This is random number $randomInt")//check
- if (type){
- numberGuessing()
- }else{
- numberGuessingForAI()
- }
- }
- //for human player
- fun randomNumberImplementation(){
- println("First player must enter number between 1 and 100")
- randomInt = readLine()?.toInt()!!
- if(randomInt in 1..100){
- numberGuessing()
- }else{
- println("Error.")
- randomNumberImplementation()
- }
- }
- //for AI player
- fun numberGuessingForAI(){
- var guessAI = (Math.random()*101).toInt()
- println(guessAI)
- guesValueCheckForAI(guessAI)
- }
- fun guesValueCheckForAI(value: Int){
- if (value < randomInt){
- attemptCounter++
- numberGuessingForAI()
- }else if (value > randomInt){
- attemptCounter++
- numberGuessingForAI()
- }else if (value == randomInt){
- println("Correct guess is $value")
- guessCounter(false)
- }
- }
- //for human player
- fun numberGuessing(){
- println("Player guess number")
- val ats = readLine()?.toInt()
- if (ats != null) {
- checkingGuess(ats)
- }
- }
- fun checkingGuess(ats: Int){
- if(ats < randomInt){
- println("Is more")
- guessCounter(true)
- }else if(ats > randomInt){
- println("Is less")
- guessCounter(true)
- }else if (ats == randomInt){
- println("Correct guess is $ats")
- guessCounter(false)
- }
- }
- fun guessCounter(check: Boolean){
- if(check){
- attemptCounter++
- numberGuessing()
- }else{
- attemptCounter++
- println("Guessed $attemptCounter times")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement