added controller for models to work

This commit is contained in:
Wyatt J. Miller 2020-09-17 18:14:02 -04:00
parent e8bbc08fab
commit 8d1004a84d

View File

@ -1,16 +1,17 @@
package com.wyattjmiller.tictactoeapp package com.wyattjmiller.tictactoeapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.GridLayout import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.gridlayout.widget.GridLayout
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity(), View.OnClickListener {
// Private member variables per the layout
// Private variables per the layout
private lateinit var mGameboardGridLayout: GridLayout private lateinit var mGameboardGridLayout: GridLayout
private lateinit var mGameboardButtonOne: Button private lateinit var mGameboardButtonOne: Button
private lateinit var mGameboardButtonTwo: Button private lateinit var mGameboardButtonTwo: Button
@ -23,22 +24,210 @@ class MainActivity : AppCompatActivity() {
private lateinit var mGameboardButtonNine: Button private lateinit var mGameboardButtonNine: Button
private lateinit var mGameStatsButton: Button private lateinit var mGameStatsButton: Button
private lateinit var mResetGameButton: Button private lateinit var mResetGameButton: Button
private lateinit var mGameStatDialog: DialogFragment
private lateinit var mGameStatusTextView: TextView
// Private variables - other variables // Private variables - other variables
private lateinit var mResetDialog: DialogFragment private lateinit var mAllButtonList: List<Button>
private var mGameWon: GameOption = GameOption.No
private var mGameboard = Gameboard()
private var mStats = Stats()
enum class GameOption {
Won,
Cats,
No
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
Log.d("LIFECYCLE", "onCreate invoked!")
// dark mode, for those who need it
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
// initialize member variables from the layout
mGameboardGridLayout = findViewById(R.id.gameboardGridLayout)
mGameboardButtonOne = findViewById(R.id.gameboardButton1)
mGameboardButtonTwo = findViewById(R.id.gameboardButton2)
mGameboardButtonThree = findViewById(R.id.gameboardButton3)
mGameboardButtonFour = findViewById(R.id.gameboardButton4)
mGameboardButtonFive = findViewById(R.id.gameboardButton5)
mGameboardButtonSix = findViewById(R.id.gameboardButton6)
mGameboardButtonSeven = findViewById(R.id.gameboardButton7)
mGameboardButtonEight = findViewById(R.id.gameboardButton8)
mGameboardButtonNine = findViewById(R.id.gameboardButton9)
mGameStatusTextView = findViewById(R.id.gameStatusTextView)
// set the onClickListeners, this method would be easier for me
mGameboardButtonOne.setOnClickListener(this)
mGameboardButtonTwo.setOnClickListener(this)
mGameboardButtonThree.setOnClickListener(this)
mGameboardButtonFour.setOnClickListener(this)
mGameboardButtonFive.setOnClickListener(this)
mGameboardButtonSix.setOnClickListener(this)
mGameboardButtonSeven.setOnClickListener(this)
mGameboardButtonEight.setOnClickListener(this)
mGameboardButtonNine.setOnClickListener(this)
// initialize the list of buttons
mAllButtonList = listOf(mGameboardButtonOne, mGameboardButtonTwo, mGameboardButtonThree,
mGameboardButtonFour, mGameboardButtonFive, mGameboardButtonSix,
mGameboardButtonSeven, mGameboardButtonEight, mGameboardButtonNine)
this.startGame()
}
override fun onStart() {
Log.d("LIFECYCLE", "onStart invoked!")
super.onStart()
}
override fun onResume() {
Log.d("LIFECYCLE", "onResume invoked!")
super.onResume()
}
override fun onPause() {
Log.d("LIFECYCLE", "onPause invoked!")
super.onPause()
}
override fun onStop() {
Log.d("LIFECYCLE", "onStop invoked!")
super.onStop()
}
override fun onDestroy() {
Log.d("LIFECYCLE", "onDestroy invoked!")
super.onDestroy()
}
override fun onClick(view: View?) {
when (view!!.id) {
R.id.gameboardButton1 -> {
this.checkGameState(this.mGameboardButtonOne)
}
R.id.gameboardButton2 -> {
this.checkGameState(this.mGameboardButtonTwo)
}
R.id.gameboardButton3 -> {
this.checkGameState(this.mGameboardButtonThree)
}
R.id.gameboardButton4 -> {
this.checkGameState(this.mGameboardButtonFour)
}
R.id.gameboardButton5 -> {
this.checkGameState(this.mGameboardButtonFive)
}
R.id.gameboardButton6 -> {
this.checkGameState(this.mGameboardButtonSix)
}
R.id.gameboardButton7 -> {
this.checkGameState(this.mGameboardButtonSeven)
}
R.id.gameboardButton8 -> {
this.checkGameState(this.mGameboardButtonEight)
}
R.id.gameboardButton9 -> {
this.checkGameState(this.mGameboardButtonNine)
}
}
} }
fun clickResetGame(view: View?) { fun clickResetGame(view: View?) {
// dialog appears for conformation (gotta get to)
for (buttonList in mAllButtonList) {
buttonList.text = ""
}
mGameboardGridLayout.visibility = View.VISIBLE
mGameWon = GameOption.No
mGameboard.resetGame()
val updatedText = mGameboard.updateGameText()
mGameStatusTextView.text = updatedText
} }
fun clickGameStats(view: View?) { fun clickGameStats(view: View?) {
// dialog appears for game stats
}
private fun checkGameState(button: Button) {
if (mGameWon != GameOption.No) { return } else {
if (button.text != "") { return } else {
val buttonText = mGameboard.placePlayerPiece()
button.text = buttonText
val winner = isGameWon()
if (winner == GameOption.Won || winner == GameOption.Cats) {
mGameWon = winner
mGameboardGridLayout.visibility = View.INVISIBLE
}
mGameboard.updateState(mGameWon)
val updatedText = mGameboard.updateGameText()
mGameStatusTextView.text = updatedText
mGameboard.getNextPlayerPiece()
}
}
}
private fun isGameWon(): GameOption {
// horizontal win scenarios
if ((mGameboardButtonOne.text == "X" && mGameboardButtonTwo.text == "X" && mGameboardButtonThree.text == "X") ||
(mGameboardButtonOne.text == "O" && mGameboardButtonTwo.text == "O" && mGameboardButtonThree.text == "O")) {
return GameOption.Won
}
if ((mGameboardButtonFour.text == "X" && mGameboardButtonFive.text == "X" && mGameboardButtonSix.text == "X") ||
(mGameboardButtonFour.text == "O" && mGameboardButtonFive.text == "O" && mGameboardButtonSix.text == "O")) {
return GameOption.Won
}
if ((mGameboardButtonSeven.text == "X" && mGameboardButtonEight.text == "X" && mGameboardButtonNine.text == "X") ||
(mGameboardButtonSeven.text == "O" && mGameboardButtonEight.text == "O" && mGameboardButtonNine.text == "O")) {
return GameOption.Won
}
// vertical win scenarios
if ((mGameboardButtonOne.text == "X" && mGameboardButtonFour.text == "X" && mGameboardButtonSeven.text == "X") ||
(mGameboardButtonOne.text == "O" && mGameboardButtonFour.text == "O" && mGameboardButtonSeven.text == "O")) {
return GameOption.Won
}
if ((mGameboardButtonTwo.text == "X" && mGameboardButtonFive.text == "X" && mGameboardButtonEight.text == "X") ||
(mGameboardButtonTwo.text == "O" && mGameboardButtonFive.text == "O" && mGameboardButtonEight.text == "O")) {
return GameOption.Won
}
if ((mGameboardButtonThree.text == "X" && mGameboardButtonSix.text == "X" && mGameboardButtonNine.text == "X") ||
(mGameboardButtonThree.text == "O" && mGameboardButtonSix.text == "O" && mGameboardButtonNine.text == "O")) {
return GameOption.Won
}
// diagonal win scenarios
if ((mGameboardButtonOne.text == "X" && mGameboardButtonFive.text == "X" && mGameboardButtonNine.text == "X") ||
(mGameboardButtonOne.text == "O" && mGameboardButtonFive.text == "O" && mGameboardButtonNine.text == "O")) {
return GameOption.Won
}
if ((mGameboardButtonSeven.text == "X" && mGameboardButtonFive.text == "X" && mGameboardButtonThree.text == "X") ||
(mGameboardButtonSeven.text == "O" && mGameboardButtonFive.text == "O" && mGameboardButtonThree.text == "O")) {
return GameOption.Won
}
// cats game scenario
if (mGameboardButtonOne.text != "" && mGameboardButtonTwo.text != "" && mGameboardButtonThree.text != "" &&
mGameboardButtonFour.text != "" && mGameboardButtonFive.text != "" && mGameboardButtonSix.text != "" &&
mGameboardButtonSeven.text != "" && mGameboardButtonEight.text != "" && mGameboardButtonNine.text != "") {
return GameOption.Cats
}
return GameOption.No
}
private fun startGame() {
mGameboard.startGame()
} }
} }