Skip to content
Open
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# android-omok-precourse
# android-omok-precourse

1. **게임 보드 초기화**:
- 15x15 크기의 게임 보드

2. **돌 놓기 기능**:
- 사용자가 클릭한 위치에 돌을 놓음
- 흑돌과 백돌

3. **승리 조건 체크**:
- 가로, 세로, 대각선 방향으로 다섯 개 이상의 연속된 돌이 있는지 확인
- 다섯 개 이상의 돌이 연속된 경우 해당 플레이어가 승리

4. **게임 재시작 기능**:
- 게임이 종료된 후 보드를 초기화하고 새로운 게임을 시작

10 changes: 9 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>








mma<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

Expand Down
79 changes: 76 additions & 3 deletions app/src/main/java/nextstep/omok/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import android.widget.TableLayout
import android.widget.TableRow
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import androidx.appcompat.app.AlertDialog

class MainActivity : AppCompatActivity() {

private val boardSize = 15
private var currentPlayer = "black"
private val boardState = Array(boardSize) { Array(boardSize) { "" } }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -16,8 +22,75 @@ class MainActivity : AppCompatActivity() {
board
.children
.filterIsInstance<TableRow>()
.flatMap { it.children }
.filterIsInstance<ImageView>()
.forEach { view -> view.setOnClickListener { view.setImageResource(R.drawable.black_stone) } }
.forEachIndexed { rowIndex, row ->
row.children.filterIsInstance<ImageView>().forEachIndexed { colIndex, imageView ->
imageView.setOnClickListener {
if (boardState[rowIndex][colIndex].isEmpty()) {
boardState[rowIndex][colIndex] = currentPlayer
imageView.setImageResource(
if (currentPlayer == "black") R.drawable.black_stone else R.drawable.white_stone
)
if (checkWin(rowIndex, colIndex)) {
showGameOverDialog("$currentPlayer wins!")
} else {
currentPlayer = if (currentPlayer == "black") "white" else "black"
}
}
}
}
}
}

private fun checkWin(row: Int, col: Int): Boolean {
val directions = listOf(
listOf(Pair(0, 1), Pair(0, -1)), // horizontal
listOf(Pair(1, 0), Pair(-1, 0)), // vertical
listOf(Pair(1, 1), Pair(-1, -1)), // diagonal down-right and up-left
listOf(Pair(1, -1), Pair(-1, 1)) // diagonal down-left and up-right
)

for (direction in directions) {
var count = 1
for (dir in direction) {
var r = row
var c = col
while (true) {
r += dir.first
c += dir.second
if (r in 0 until boardSize && c in 0 until boardSize && boardState[r][c] == currentPlayer) {
count++
if (count >= 5) return true
} else {
break
}
}
}
}
return false
}

private fun showGameOverDialog(message: String) {
AlertDialog.Builder(this)
.setTitle("Game Over")
.setMessage(message)
.setPositiveButton("OK") { _, _ ->
resetBoard()
}
.show()
}

private fun resetBoard() {
currentPlayer = "black"
for (i in 0 until boardSize) {
for (j in 0 until boardSize) {
boardState[i][j] = ""
}
}
val board = findViewById<TableLayout>(R.id.board)
board.children.filterIsInstance<TableRow>().forEach { row ->
row.children.filterIsInstance<ImageView>().forEach { imageView ->
imageView.setImageResource(0)
}
}
}
}