diff --git a/Mobile Applications/Basic/TicTacToe Game/app/src/main/java/com/example/tictactoe/MainActivity.kt b/Mobile Applications/Basic/TicTacToe Game/app/src/main/java/com/example/tictactoe/MainActivity.kt index 264315986..5a490092a 100644 --- a/Mobile Applications/Basic/TicTacToe Game/app/src/main/java/com/example/tictactoe/MainActivity.kt +++ b/Mobile Applications/Basic/TicTacToe Game/app/src/main/java/com/example/tictactoe/MainActivity.kt @@ -4,132 +4,150 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button -import kotlinx.android.synthetic.main.activity_main.* +import android.widget.TextView +import androidx.appcompat.widget.Toolbar class MainActivity : AppCompatActivity(), View.OnClickListener { - var PLAYER = true - var TURN_COUNT = 0 + companion object { + const val PLAYER_X = 1 + const val PLAYER_O = 0 + const val NO_PLAYER = -1 + const val BOARD_SIZE = 3 + } - var boardStatus = Array(3){IntArray(3)} - lateinit var board: Array> + private var currentPlayer = PLAYER_X + private var turnCount = 0 + private var boardStatus = Array(BOARD_SIZE) { IntArray(BOARD_SIZE) } + private lateinit var boardButtons: Array> + private lateinit var displayTv: TextView + private lateinit var resetBtn: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - board = arrayOf( - arrayOf(button, button2, button3), - arrayOf(button4, button5, button6), - arrayOf(button7, button8, button9) + val toolbar: Toolbar = findViewById(R.id.my_toolbar) + setSupportActionBar(toolbar) + + supportActionBar?.title = "TicTacToe" + + // Initialize the buttons and TextView using findViewById + displayTv = findViewById(R.id.displayTv) + resetBtn = findViewById(R.id.resetBtn) + + // Array of buttons from the layout + boardButtons = arrayOf( + arrayOf(findViewById(R.id.button), findViewById(R.id.button2), findViewById(R.id.button3)), + arrayOf(findViewById(R.id.button4), findViewById(R.id.button5), findViewById(R.id.button6)), + arrayOf(findViewById(R.id.button7), findViewById(R.id.button8), findViewById(R.id.button9)) ) - for (i in board) { - for (button in i) { + + // Set onClickListener for each button + for (row in boardButtons) { + for (button in row) { button.setOnClickListener(this) } } - initializeBoardStatus() + + // Set the reset button action resetBtn.setOnClickListener { - PLAYER = true - TURN_COUNT = 0 - initializeBoardStatus() - updateDisplay("Player X Turn") + resetGame() } + + // Initialize board status + initializeBoardStatus() } private fun initializeBoardStatus() { - for(i in 0..2) { - for(j in 0..2) { - boardStatus[i][j] = -1 - board[i][j].isEnabled = true - board[i][j].text = "" + for (i in 0 until BOARD_SIZE) { + for (j in 0 until BOARD_SIZE) { + boardStatus[i][j] = NO_PLAYER + boardButtons[i][j].isEnabled = true + boardButtons[i][j].text = "" } } + updateDisplay("Player X's Turn") } - override fun onClick(view: View?) { - when(view?.id) { - R.id.button-> updateValue(row = 0, col = 0, player = PLAYER) - R.id.button2-> updateValue(row = 0, col = 1, player = PLAYER) - R.id.button3-> updateValue(row = 0, col = 2, player = PLAYER) - R.id.button4-> updateValue(row = 1, col = 0, player = PLAYER) - R.id.button5-> updateValue(row = 1, col = 1, player = PLAYER) - R.id.button6-> updateValue(row = 1, col = 2, player = PLAYER) - R.id.button7-> updateValue(row = 2, col = 0, player = PLAYER) - R.id.button8-> updateValue(row = 2, col = 1, player = PLAYER) - R.id.button9-> updateValue(row = 2, col = 2, player = PLAYER) + private fun resetGame() { + currentPlayer = PLAYER_X + turnCount = 0 + initializeBoardStatus() + } + override fun onClick(view: View?) { + when (view?.id) { + R.id.button -> processMove(0, 0) + R.id.button2 -> processMove(0, 1) + R.id.button3 -> processMove(0, 2) + R.id.button4 -> processMove(1, 0) + R.id.button5 -> processMove(1, 1) + R.id.button6 -> processMove(1, 2) + R.id.button7 -> processMove(2, 0) + R.id.button8 -> processMove(2, 1) + R.id.button9 -> processMove(2, 2) } - TURN_COUNT++ - PLAYER = !PLAYER - if(PLAYER) { - updateDisplay("Player X Turn") + + turnCount++ + currentPlayer = if (currentPlayer == PLAYER_X) PLAYER_O else PLAYER_X + + if (turnCount == BOARD_SIZE * BOARD_SIZE) { + updateDisplay("Game Draw") + disableBoard() } else { - updateDisplay("Player O Turn") + checkWinner() } - if (TURN_COUNT == 9) { - updateDisplay("GAME DRAW") + } + + private fun processMove(row: Int, col: Int) { + val value = if (currentPlayer == PLAYER_X) "X" else "O" + boardButtons[row][col].apply { + isEnabled = false + text = value } - checkWinner() + boardStatus[row][col] = currentPlayer + updateDisplay(if (currentPlayer == PLAYER_X) "Player O's Turn" else "Player X's Turn") } private fun checkWinner() { - //check horizontally - for (i in 0..2) { - if (boardStatus[i][0] == boardStatus[i][1] && boardStatus[i][0] == boardStatus[i][2]) { - if (boardStatus[i][0] == 1) - updateDisplay("PLAYER X WINNER") - else if (boardStatus[i][0] == 0) - updateDisplay("PLAYER O WINNER") - } - } - //check vertically - for (i in 0..2) { - if (boardStatus[0][i] == boardStatus[1][i] && boardStatus[0][i] == boardStatus[2][i]) { - if (boardStatus[0][i] == 1) - updateDisplay("PLAYER X WINNER") - else if (boardStatus[0][i] == 0) - updateDisplay("PLAYER O WINNER") - } - } - //check fist diagonal \ - if (boardStatus[0][0] == boardStatus[1][1] && boardStatus[0][0] == boardStatus[2][2]) { - if (boardStatus[0][0] == 1) - updateDisplay("PLAYER X WINNER") - else if (boardStatus[0][0] == 0) - updateDisplay("PLAYER O WINNER") - } - //check second diagonal / - if (boardStatus[0][2] == boardStatus[1][1] && boardStatus[0][2] == boardStatus[2][0]) { - if (boardStatus[1][1] == 1) - updateDisplay("PLAYER X WINNER") - else if (boardStatus[1][1] == 0) - updateDisplay("PLAYER O WINNER") + if (hasPlayerWon(PLAYER_X)) { + updateDisplay("Player X Wins!") + disableBoard() + } else if (hasPlayerWon(PLAYER_O)) { + updateDisplay("Player O Wins!") + disableBoard() } } - private fun updateDisplay(text: String) { - displayTv.text = text - if(text.contains("WINNER")) - disableButton() - } - private fun disableButton() { - for (i in board) { - for (button in i) { - button.isEnabled = false + private fun hasPlayerWon(player: Int): Boolean { + // Check rows and columns + for (i in 0 until BOARD_SIZE) { + if (isWinningLine(player, boardStatus[i][0], boardStatus[i][1], boardStatus[i][2]) || + isWinningLine(player, boardStatus[0][i], boardStatus[1][i], boardStatus[2][i]) + ) { + return true } } + + // Check diagonals + return (isWinningLine(player, boardStatus[0][0], boardStatus[1][1], boardStatus[2][2]) || + isWinningLine(player, boardStatus[0][2], boardStatus[1][1], boardStatus[2][0])) } - private fun updateValue(row: Int, col: Int, player: Boolean) { - val text = if(player) "X" else "O" - val value = if(player) 1 else 0 + private fun isWinningLine(player: Int, vararg positions: Int): Boolean { + return positions.all { it == player } + } - board[row][col].apply { - isEnabled = false - setText(text) - } + private fun updateDisplay(message: String) { + displayTv.text = message + } - boardStatus[row][col] = value + private fun disableBoard() { + for (row in boardButtons) { + for (button in row) { + button.isEnabled = false + } + } } -} \ No newline at end of file +} diff --git a/Mobile Applications/Basic/TicTacToe Game/app/src/main/res/layout/activity_main.xml b/Mobile Applications/Basic/TicTacToe Game/app/src/main/res/layout/activity_main.xml index ad0ee0896..97fca7e98 100644 --- a/Mobile Applications/Basic/TicTacToe Game/app/src/main/res/layout/activity_main.xml +++ b/Mobile Applications/Basic/TicTacToe Game/app/src/main/res/layout/activity_main.xml @@ -1,13 +1,29 @@ + + + + +