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

[부산대 Android_박정훈] 미션 제출합니다. #34

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
refactor: Reduce start method indent depth
- Reduce indent depth by using initImgOpacity and setBoard functions
  • Loading branch information
Pjhn committed Jun 9, 2024
commit cff82a61b66a2bf0150c60740f49a3380d5d4d26
73 changes: 41 additions & 32 deletions app/src/main/java/nextstep/omok/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -34,6 +34,47 @@ class MainActivity : AppCompatActivity() {
MutableList(boardSize) { Stone.EMPTY }
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
start()
}

private fun start(){
val imgWhite = findViewById<ImageView>(R.id.img_white)
val imgBlack = findViewById<ImageView>(R.id.img_black)
val board = findViewById<TableLayout>(R.id.board)
val restartBtn = findViewById<Button>(R.id.restart_btn)

initImgOpacity(imgWhite,imgBlack)

setBoard(imgWhite,imgBlack,board)

restartBtn.setOnClickListener {
init(board)
}
}
private fun initImgOpacity(imgWhite: ImageView, imgBlack: ImageView) {
imgWhite.alpha = 0.2f
imgBlack.alpha = 1.0f
}

private fun setBoard(imgWhite: ImageView, imgBlack: ImageView,board: TableLayout){
board
.children
.filterIsInstance<TableRow>()
.flatMap { it.children }
.filterIsInstance<ImageView>()
.forEachIndexed { idx, view ->
var row = idx / boardSize
var col = idx % boardSize

view.setOnClickListener {
placeStone(view, row, col, imgWhite, imgBlack)
}
}
}

private fun placeStone(
view: ImageView,
row: Int,
@@ -144,39 +185,7 @@ class MainActivity : AppCompatActivity() {
hideResultScreen()
}

private fun start(){
val imgWhite = findViewById<ImageView>(R.id.img_white)
val imgBlack = findViewById<ImageView>(R.id.img_black)
val board = findViewById<TableLayout>(R.id.board)
val restartBtn = findViewById<Button>(R.id.restart_btn)

imgWhite.alpha = 0.2f
imgBlack.alpha = 1.0f

board
.children
.filterIsInstance<TableRow>()
.flatMap { it.children }
.filterIsInstance<ImageView>()
.forEachIndexed { idx, view ->
var row = idx / boardSize
var col = idx % boardSize

view.setOnClickListener {
placeStone(view, row, col, imgWhite, imgBlack)
}
}

restartBtn.setOnClickListener {
init(board)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

start()

}
}