import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity(), View.OnClickListener {
private val button: Array<Array<Button?>> =
Array<Array<Button?>>(size = 3) { arrayOfNulls<Button>(3) }
private var player1Turn = true
private var roundCount = 0
private var player1Points = 0
private var player2Points = 0
private lateinit var textviewP1: TextView
private lateinit var textviewP2: TextView
private var playerFirst = ""
private var playerSecond = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textviewP1 = findViewById(R.id.p1_textView)
textviewP2 = findViewById(R.id.p2_textVew)
getPlayerName()
for (i in 0..2) {
for (j in 0..2) {
val buttonId = "button_$i$j"
val resId = resources.getIdentifier(
buttonId,
"id", packageName
)
button[i][j]!!.setOnClickListener(this)
}
}
val resetBtn = findViewById<Button>(R.id.resetBtn)
resetBtn.setOnClickListener { resetGame() }
}
private fun resetGame() {
player1Points = 0
player2Points = 0
upDatePointText()
resetBord()
}
private fun getPlayerName() {
val infalter = LayoutInflater.from(this@MainActivity)
val plView = infalter.inflate(R.layout.player_info,null)
val player1Name = plView.findViewById<EditText>(R.id.firstP)
val player2Name = plView.findViewById<EditText>(R.id.secondP)
val plDialog = AlertDialog.Builder(this@MainActivity)
plDialog.setView(plView)
plDialog.setPositiveButton("Add"){
dialog,_->
if (player1Name.text.toString().isEmpty()
&& player2Name.text.toString().isEmpty()
){
playerFirst = "Player1"
playerSecond = "Player2"
textviewP1.text = "$playerFirst"
textviewP2.text = "$playerSecond"
}
else{
playerFirst = player1Name.text.toString()
playerSecond = player2Name.text.toString()
textviewP1.text = "$playerFirst : 0"
textviewP2.text = "$playerSecond : 0"
}
}
plDialog.setNeutralButton("Cancel"){
dialog,_->
playerFirst = "Player1"
playerSecond = "Player2"
textviewP1.text = "$playerFirst"
textviewP2.text = "$playerSecond"
}
plDialog.create()
plDialog.show()
}
override fun onClick(v: View?) {
if (!(v as Button).text.toString().equals("")) {
return
}
if (player1Turn) {
(v as Button).text = "X"
v.setTextColor(resources.getColor(R.color.love_sky))
} else {
(v as Button).text = "0"
v.setTextColor(resources.getColor(R.color.purple))
}
roundCount++
if (checkForWin()){
if (player1Turn){
player1Win()
}
else{
player2Win()
}
}
else if (roundCount == 9){
draw()
}
else{
player1Turn = !player1Turn
}
}
private fun draw() {
AlertDialog.Builder(this)
.setTitle("Draw !!")
.setMessage("The game is drawn, Please play again...!!")
.setPositiveButton("ok"){dialog,_->
resetBord()
}
.setNeutralButton("Cancel"){dialog,_->resetBord()}
.show()
}
private fun player1Win() {
player2Points++
AlertDialog.Builder(this@MainActivity)
.setTitle("Congratulation!!!")
.setIcon(R.drawable.trophyy)
.setMessage("Congratulation $playerFirst, You won!!!")
.create()
.show()
upDatePointText()
resetBord()
}
private fun player2Win() {
player1Points++
AlertDialog.Builder(this@MainActivity)
.setTitle("Congratulation!!!")
.setIcon(R.drawable.trophyy)
.setMessage("Congratulation $playerSecond, You won!!!")
.create()
.show()
upDatePointText()
resetBord()
}
private fun resetBord() {
for (i in 0..2){
for (j in 0..2){
button[i][j]!!.text = ""
}
}
roundCount = 0
player1Turn = false
}
private fun upDatePointText() {
textviewP1.text = "$playerFirst : $player1Points"
textviewP2.text = "$playerSecond : $player1Points"
}
private fun checkForWin(): Boolean {
val field = Array(3){ arrayOfNulls<String>(3)}
for (i in 0..2){
for (j in 0..2){
field[i][j]=button[i][j]!!.text.toString()
}
}
for (i in 0..2){
if (field[i][0]==field[i][1]
&& field[i][0]== field[i][2]
&& field[i][0]!=""){
return true
}
}
for (i in 0..2){
if (field[0][i] == field[1][i]
&& field[0][i] == field[2][i]
&& field[0][i] !=""){
return true
}
}
if (field[0][0] ==field[1][1]
&& field[0][0] == field[2][2]
&& field[0][0] !=""){
return true
}
return if (field[0][2] == field[1][1]
&& field[0][2] == field[2][0]
&& field[0][2] !=""){
true
}
else false
}
override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
super.onSaveInstanceState(outState, outPersistentState)
outState.putInt("roundCount",roundCount)
outState.putInt("player1Points",player1Points)
outState.putInt("player2Points",player2Points)
outState.putBoolean("player1Turn",player1Turn)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
super.onRestoreInstanceState(savedInstanceState)
roundCount = savedInstanceState.getInt("roundCount")
player1Points = savedInstanceState.getInt("player1Points")
player2Points = savedInstanceState.getInt("player2Points")
player1Turn = savedInstanceState.getBoolean("player1Turn")
startActivity(intent)
}
}
MANIFEST
<?xml version="1.0" encoding="utf-8"?><uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApple"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
GRADULES
plugins {
id ‘com.android.application’
id ‘org.jetbrains.kotlin.android’
}
android {
compileSdk 32
defaultConfig {
applicationId "com.miztachi.myapple"
minSdk 19
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}