import java.util.*

/**********************************************
 * Simple game made by Frekvens1
 * - Version 1.1
 * - Self explaining code
 * - Made for newer developers to learn more


 * Changelog


 * 1.1:
 * - Bug fixes
 * - Added more inputs
 * - Increased readability
 * - Optimized some functions
 * - Added changelog and credits


 * Credits - Thanks for testing my code!


 * Shlvam Rawat & Sasha_Kuprii
 * - Shortened the inputs to "r, p, s"

 * Ivan
 * - Made me convert to switch, as its more correct usage.

 * Gabriel Carvalho
 * - Bug hunter: "NoSuchElementException" when input was null

 * Alexandre da Costa Leite
 * - Bug hunter: Changing from && to || in function check for win
 * - Important change if converting this to multiplayer


 * Uploaded 19.07.2016 (DDMMYYYY)
 * Last updated 04.02.2017 (DDMMYYYY)
 */

fun main(args: Array<String>) {

    try {
        val sc = Scanner(System.`in`)

        if (sc.hasNext()) { //Checks for null values

            val userInput = quickFormat(sc.next())
            sc.close()

            if (isValid(userInput)) {
                game(userInput)

            } else {
                displayInputs()
            }
        } else {
            displayInputs() //Null value, displaying correct inputs
        }

    } catch (e: Exception) {
        e.printStackTrace()
    }

}


fun game(user: String) {
    val computer = computerResults()

    print("$user vs $computer\n")

    if (user.equals(computer, ignoreCase = true)) {
        print("Stalemate! No winners.")
    } else {

        if (checkWin(user, computer)) {
            print("You won against the computer!")
        } else {
            print("You lost against the computer!")
        }
    }
}

fun computerResults(): String {

    val types = arrayOf("rock", "paper", "scissors")

    val rand = Random()
    val computerChoice = rand.nextInt(3)

    return types[computerChoice]
}

fun isValid(input: String): Boolean {


    when (input.toLowerCase()) {

        "rock" -> return true

        "paper" -> return true

        "scissors" -> return true

        else -> return false
    }
}

fun checkWin(user: String, opponent: String): Boolean {

    if (!isValid(user) || !isValid(opponent)) {
        return false
    }

    val rock = "rock"
    val paper = "paper"
    val scissors = "scissors"

    if (user.equals(rock, ignoreCase = true) && opponent.equals(scissors, ignoreCase = true)) {
        return true
    }

    if (user.equals(scissors, ignoreCase = true) && opponent.equals(paper, ignoreCase = true)) {
        return true
    }

    if (user.equals(paper, ignoreCase = true) && opponent.equals(rock, ignoreCase = true)) {
        return true
    }

    return false
    //If no possible win, assume loss.
}

/**********************************************
 * Libraries
 */

fun displayInputs() {
    //One place to edit it all!
    print("Invalid user input!\nWrite rock, paper or scissors!")
}

fun print(text: String) {
    //Makes printing text easier
    println(text)
}

fun quickFormat(input: String): String {
    //Just some quick function to shorten inputs.

    var output = input

    when (input.toLowerCase()) {

        "r" -> output = "rock"

        "p" -> output = "paper"

        "s" -> output = "scissors"

        "scissor" -> output = "scissors"
    }

    return output
}
