---
# System prepended metadata

title: Learning Diary MAD
tags: [fh, MAD]

---

###### tags: `fh` `MAD`
# Learning Diary MAD
## 07.04.

### Kotlin Tutorial
### 1. What are the major differences between Java and Kotlin. Explain each and give an example?

* Kotlin allows to import a reference to a view into an active file which allows you to work with that view as part of the activity. That meany ViewByld methods dont have to be hand-written.
* In Kotlin, all types are non-nullable by default, which is neat since NullPointExceptions wont be as much as a anoyance/problem any more as they were in java. Trying to assign Null will leat to a fail upon compiling
* Kotlin has a function called Coroutines which are similar to threading/multithreading.It is used to get work done that would usually block the main thread for a time (screen is frozen) without doing just that. It makes a asynchronous code look synchronous.
* There are no checked exceptions in Kotlin which means that a try-catch statement is not required for the code to work.
* Kotlins class delegation allows for multi-inheritance.
* Data Classes in Kotlin make it easier to create classes which only exist for the purpouse of holding data. Getter Setter and other standard functions are automatically generated by the compiler.
* With smart casts Kotlin saves the developers time by automatically casting the right type when using the keyword "is-checks"
* Kotline doesnt require the developer to specify the type explicitly. Types will be assigned according to the entered value.
* Kotlin CAN be used either as procedural or functional programming language.

### 2. Implement the game from https://www.brainbashers.com/numbermaster.asp in Kotlin and copy your code here.

```
import kotlin.random.Random

fun main(args: Array<String>) {
    gamestart()
    val secNum = genSecretNumber()
    while (true) {
        if (playersTurn(intListToCharacterList(secNum))){
            break
        }else{
            continue
        }
    }
}

var attempts: Int = 0
fun gamestart() {
    println(
        "In this game you are supposed to guess a number with four digits. No digits repeat themselves. You have an " +
                "infinite amount of guesses but the shorter the better!\nAfter every attempt you will be told if there were " +
                "any correct numbers in your combination.\nAdditionally you get told if and how many numbers were in the" +
                " correct position, but not which one." +
                "\nGood luck and have fun :) !"
    )
}

fun genSecretNumber(): MutableList<Int> {
    var secNum = MutableList(4) {
        Random.nextInt(0, 9)
    }
    while (secNum[0] == 0 || secNum[1] == secNum[0] || secNum[1] == secNum[2] ||
        secNum[1] == secNum[3] || secNum[2] == secNum[0] || secNum[2] == secNum[3] ||
        secNum[3] == secNum[0]
    ) {
        secNum = MutableList(4) {
            Random.nextInt(0, 9)
        }
    }
    return secNum
}

fun intListToCharacterList(intList: MutableList<Int>): MutableList<String> {
    val strList = mutableListOf<String>()
    for (i in intList) {
        strList.add(i.toString())
    }
    return strList
}

//dieser Abschnitt dient nur dem Zweck zu prüfen ob auch wirklich nur Zahlen ohne 0 an erster Stelle sowie keinen
//sich wiederholenden Ziffern mit meiner Funktion entstehen
fun prüfGenSecretNumber() {
    val listOfNumbers = List(50) {
        genSecretNumber()
    }
    listOfNumbers.toString()
    for (i in listOfNumbers) {
        println("${i[0]}${i[1]}${i[2]}${i[3]}")
    }
}

fun checkSameNumber(enteredNumber: MutableList<String>, randomNumber: MutableList<String>): Int {
    val sameNumbersList = mutableListOf<Boolean>()
    var numberOfCorrectDigits: Int = 0
    for (i in 0..3) {
        if (randomNumber.contains(enteredNumber[i])) {
            sameNumbersList.add(true)
            numberOfCorrectDigits++
        } else {
            sameNumbersList.add(false)
        }
    }
    return numberOfCorrectDigits
}


fun checkSamePosition(enteredNumber: MutableList<String>, randomNumber: MutableList<String>): MutableList<Char> {
    val samePositionList = mutableListOf<Boolean>()
    for (i in 0..3) {
        if (enteredNumber[i] == randomNumber[i]) {
            samePositionList.add(true)
        } else {
            samePositionList.add(false)
        }
    }
    val answer = mutableListOf<Char>()
    for (i in samePositionList) {
        if (i) {
            answer.add('+')
        } else {
            answer.add('x')
        }
    }
    return answer
}


fun playersTurn(randomNumber: MutableList<String>): Boolean {
    println("Please enter your four digit guess:")
    var won: Boolean = false
    val guess: Int = Integer.valueOf(readLine())
    val guessList = mutableListOf<String>()
    for (i in guess.toString().asIterable()){
        guessList.add(i.toString())
    }
    attempts++
    //val guessArray = arrayOf(guess.toString()[0], guess.toString()[1], guess.toString()[2], guess.toString()[3])
    if (guessList == randomNumber) {
        won = true
        println(
            "Congratulations you guessed the number!\nIt was:" + randomNumber[0] + randomNumber[1] + randomNumber[2] + randomNumber[3] +
                    "\nIt took you " + attempts + " attempts!" +
                    "\nDo you want to play again? Enter yes or no:"
            //TODO nochmal spielen fkt/spielabbruch
        )
        return won
    }
    println("You have guessed " + checkSameNumber(guessList, randomNumber) + " Digits correctly!")
    println(checkSamePosition(guessList, randomNumber))
    return won
}
```






---
## Build your first App
### 1. Explain the terms Activity and Intent. Have you used them in the tutorial and if yes, for what purpose?
* Activity: user interface or whatever you can do with the user interface. When clicking on something a new interface opens with an INTENT
* Intent: is a event that is passed along with data from the first user interface to the other
### 2. Describe the main insights and difficulties you faced during the tutorial.
The most difficult part was handling the format of the random generated number and user input in a way so that it is easy to compare lateron. I figured if i do the hard work first ill have it easier later which was true.
My biggest "AHA"-Moment was when i realized that i could convert the user input which came as Integer to a array with this simple line:
>var guessArray = arrayOf(guess.toString()[0], guess.toString()[1], guess.toString()[2], guess.toString()[3])
the simple fact that i can treat a string like an array with a "[]" at the end was new to me.
Edit at a later time:
For some reason that didnt work so well. In the debugger when i was comparing the random generated number and the number entered by the player there was allways a missmatch because for some reason ascii code and character got compared instead of character to character (which i intended).
With some help i figured out a solution to my problem.

There is definitly room for improvement on this programm. For instance, some exceptions need to be handled and an option to actually close the game or play again is missing. But the game itself works.

---
## Open Questions
### Please list any questions you were not able to understand from the tutorials.