kotlin
Copy code
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
kotlin
Copy code
// Declaring UI elements
private lateinit var instructionText: TextView
private lateinit var guessInput: EditText
private lateinit var submitGuessButton: Button
private lateinit var resultText: TextView
private lateinit var playAgainButton: Button
kotlin
Copy code
instructionText = findViewById(R.id.instructionText)
guessInput = findViewById(R.id.guessInput)
submitGuessButton = findViewById(R.id.submitGuessButton)
resultText = findViewById(R.id.resultText)
playAgainButton = findViewById(R.id.playAgainButton)
kotlin
Copy code
submitGuessButton.setOnClickListener {
val userGuess = guessInput.text.toString().toIntOrNull()
if (userGuess != null) {
playGame(userGuess)
} else {
resultText.text = "Please enter a valid number!"
resultText.visibility = View.VISIBLE
}
}
playAgainButton.setOnClickListener {
resetGame()
}
kotlin
Copy code
private fun playGame(userGuess: Int) {
val computerGuess = Random.nextInt(1, 101)
val userDelta = kotlin.math.abs(50 - userGuess)
val computerDelta = kotlin.math.abs(50 - computerGuess)
val winner = when {
userDelta < computerDelta -> "User wins! (User: $userGuess, Computer: $computerGuess)"
computerDelta < userDelta -> "Computer wins! (User: $userGuess, Computer: $computerGuess)"
else -> "It's a tie! (User: $userGuess, Computer: $computerGuess)"
}
resultText.text = winner
resultText.visibility = View.VISIBLE
playAgainButton.visibility = View.VISIBLE
}
private fun resetGame() {
guessInput.text.clear()
resultText.text = ""
resultText.visibility = View.GONE
playAgainButton.visibility = View.GONE
}
kotlin
Copy code
// src/main/java/com/example/guessgame/MainActivity.kt
package com.example.guessgame
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
// Declaring UI elements
private lateinit var instructionText: TextView
private lateinit var guessInput: EditText
private lateinit var submitGuessButton: Button
private lateinit var resultText: TextView
private lateinit var playAgainButton: Button
// Random number for the game
private var targetNumber = Random.nextInt(1, 101)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initializing UI elements
instructionText = findViewById(R.id.instructionText)
guessInput = findViewById(R.id.guessInput)
submitGuessButton = findViewById(R.id.submitGuessButton)
resultText = findViewById(R.id.resultText)
playAgainButton = findViewById(R.id.playAgainButton)
// Setting up click listener for the submit button
submitGuessButton.setOnClickListener {
val userGuess = guessInput.text.toString().toIntOrNull()
if (userGuess != null) {
playGame(userGuess)
} else {
resultText.text = "Please enter a valid number!"
resultText.visibility = View.VISIBLE
}
}
// Setting up click listener for the play again button
playAgainButton.setOnClickListener {
resetGame()
}
}
// Method to handle the game logic
private fun playGame(userGuess: Int) {
val computerGuess = Random.nextInt(1, 101)
val userDelta = kotlin.math.abs(50 - userGuess)
val computerDelta = kotlin.math.abs(50 - computerGuess)
val winner = when {
userDelta < computerDelta -> "User wins! (User: $userGuess, Computer: $computerGuess)"
computerDelta < userDelta -> "Computer wins! (User: $userGuess, Computer: $computerGuess)"
else -> "It's a tie! (User: $userGuess, Computer: $computerGuess)"
}
resultText.text = winner
resultText.visibility = View.VISIBLE
playAgainButton.visibility = View.VISIBLE
}