import kotlin.random.Random
fun main() {
val choices = arrayOf("Rock", "Paper", "Scissors")
val playerName = getPlayerName()
var playerScore = 0
var computerScore = 0
println("Hello $playerName, let's play Rock-Paper-Scissors!")
while (playerScore < 3 && computerScore < 3) {
val playerChoice = getPlayerChoice()
val computerChoice = choices.random()
println("$playerName chose $playerChoice")
println("Computer chose $computerChoice")
val roundWinner = determineRoundWinner(playerChoice, computerChoice)
if (roundWinner == playerName) {
playerScore++
println("$playerName wins the round!")
} else if (roundWinner == "Computer") {
computerScore++
println("Computer wins the round!")
} else {
println("It's a tie!")
}
println("Score: $playerName: $playerScore, Computer: $computerScore")
}
if (playerScore > computerScore) {
println("$playerName wins the game!")
} else {
println("Computer wins the game!")
}
}
fun getPlayerName(): String {
print("Please enter your name: ")
return readLine() ?: "Player"
}
fun getPlayerChoice(): String {
print("Rock, Paper, or Scissors? ")
var playerChoice = readLine()
while (playerChoice !in arrayOf("Rock", "Paper", "Scissors")) {
println("Invalid choice. Please choose Rock, Paper, or Scissors.")
print("Rock, Paper, or Scissors? ")
playerChoice = readLine()
}
return playerChoice
}
fun determineRoundWinner(playerChoice: String, computerChoice: String): String {
if (playerChoice == "Rock" && computerChoice == "Scissors" ||
playerChoice == "Scissors" && computerChoice == "Paper" ||
playerChoice == "Paper" && computerChoice == "Rock") {
return "Player"
} else if (playerChoice == "Rock" && computerChoice == "Paper" ||
playerChoice == "Scissors" && computerChoice == "Rock" ||
playerChoice == "Paper" && computerChoice == "Scissors") {
return "Computer"
} else {
return "Tie"
}
}