Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring code for better readability #1395 #1458

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<Button>>
private var currentPlayer = PLAYER_X
private var turnCount = 0
private var boardStatus = Array(BOARD_SIZE) { IntArray(BOARD_SIZE) }
private lateinit var boardButtons: Array<Array<Button>>
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
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_200"
android:elevation="4dp"
app:titleTextColor="@color/white"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:titleTextColor="@android:color/white"/>

<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_height="match_parent">

<TextView
android:id="@+id/displayTv"
android:layout_width="wrap_content"
Expand All @@ -31,6 +47,7 @@
android:textSize="50sp"
android:textColor="@android:color/black"
android:layout_weight="1"
app:backgroundTint="@color/teal_200"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
Expand All @@ -40,11 +57,13 @@
android:layout_marginLeft="8dp"
android:textColor="@android:color/black"
android:layout_width="0dp"
app:backgroundTint="@color/teal_200"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button3"
android:textSize="50sp"
app:backgroundTint="@color/teal_200"
android:textColor="@android:color/black"
android:layout_width="0dp"
android:layout_height="match_parent"
Expand All @@ -55,19 +74,22 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:backgroundTint="@color/teal_200"
android:orientation="horizontal">

<Button
android:id="@+id/button4"
android:textSize="50sp"
android:textColor="@android:color/black"
android:layout_width="0dp"
app:backgroundTint="@color/teal_200"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button5"
android:textSize="50sp"
android:layout_marginRight="8dp"
app:backgroundTint="@color/teal_200"
android:layout_marginLeft="8dp"
android:textColor="@android:color/black"
android:layout_width="0dp"
Expand All @@ -76,8 +98,10 @@
<Button
android:id="@+id/button6"
android:textSize="50sp"
app:backgroundTint="@color/teal_200"
android:textColor="@android:color/black"
android:layout_width="0dp"

android:layout_height="match_parent"
android:layout_weight="1" />

Expand All @@ -94,6 +118,7 @@
android:textSize="50sp"
android:textColor="@android:color/black"
android:layout_width="0dp"
app:backgroundTint="@color/teal_200"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
Expand All @@ -103,13 +128,15 @@
android:layout_marginLeft="8dp"
android:textColor="@android:color/black"
android:layout_width="0dp"
app:backgroundTint="@color/teal_200"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button9"
android:textSize="50sp"
android:textColor="@android:color/black"
android:layout_width="0dp"
app:backgroundTint="@color/teal_200"
android:layout_height="match_parent"
android:layout_weight="1" />

Expand All @@ -127,4 +154,5 @@
android:textSize="24sp"
app:backgroundTint="@color/teal_200" />

</LinearLayout>
</LinearLayout>
</LinearLayout>
Loading