From 4423b96c2897cbf41fde12d2dc37ce4f8d009247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 20:43:09 +0900 Subject: [PATCH 01/16] feat:README function --- README.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 08a8c8a..1e07170 100644 --- a/README.md +++ b/README.md @@ -1 +1,31 @@ -# android-omok-precourse \ No newline at end of file +# android-omok-precourse + +## 프로젝트 개요 +- 오목게임을 안드로이드 앱으로 구현한 프로젝트. + +## 기능목록 +1. UI 초기화 +- 설정 초기화 +2. 클릭 리스너 설정하기 +- 오목 판의 클릭 리스너 재설정 +3. 돌 놓기 기능 +- 오목판에 돌 배치하기 +4. 차례 변경 기능 +- 하얀 돌과 검은 돌 차례 바꾸기 +5. 승리 여부 표시 +- 5개 이상의 돌이 놓여 게임이 끝났음을 알리고 재시작 +6. 오목판 그리기 +- View 파일을 만들어 오목판 그리기 +- 해당 판은 기존 xml 파일 활용 +7. 돌 그리기 +- View 파일 안에 하얀 돌, 검은 돌 그리기 +- 해당 돌은 기존 xml 파일 활용 +8. 돌 놓는 이벤트 처리하기 +- 오목판 위에 돌 놓는 것 처리하기 +- 오목판 위에 돌을 놓음으로써 뒤의 오목판 이미지가 사라지지 않도록 설정하는 것 주의하기 +9. 돌 놓기 기능 +- 8의 기능 처리 +10. 승리 조건 체크하기 +- 같은 색의 돌이 가로,세로,대각선 중에서 하나라도 5개 이상이 되었을 때 게임 종료 +- 해당 돌이 같은 색인지, 사이의 같은 돌의 개수 체크하기 +- 승리 조건 판단 \ No newline at end of file From 9e29c357208398b1a6f44174a05a84437bbf7f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 20:47:35 +0900 Subject: [PATCH 02/16] fix:README function --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1e07170..4d059fd 100644 --- a/README.md +++ b/README.md @@ -28,4 +28,8 @@ 10. 승리 조건 체크하기 - 같은 색의 돌이 가로,세로,대각선 중에서 하나라도 5개 이상이 되었을 때 게임 종료 - 해당 돌이 같은 색인지, 사이의 같은 돌의 개수 체크하기 -- 승리 조건 판단 \ No newline at end of file +- 승리 조건 판단 +11. 레이아웃 작성하기 +- android_main.xml +12. test 돌리기 +- test code 작성 \ No newline at end of file From da407a19d8e297a761dba8c24e4a737c0453cff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 20:53:35 +0900 Subject: [PATCH 03/16] Initialized Omok Board Game --- .../main/java/nextstep/omok/MainActivity.kt | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/nextstep/omok/MainActivity.kt b/app/src/main/java/nextstep/omok/MainActivity.kt index e6cc7b8..d96f32c 100644 --- a/app/src/main/java/nextstep/omok/MainActivity.kt +++ b/app/src/main/java/nextstep/omok/MainActivity.kt @@ -1,23 +1,63 @@ package nextstep.omok import android.os.Bundle +import android.widget.FrameLayout import android.widget.ImageView 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 game = OmokGame(boardSize) + private var currentTurn = Stone.BLACK + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + startNewGame() + } + private fun startNewGame() { val board = findViewById(R.id.board) - board - .children - .filterIsInstance() - .flatMap { it.children } - .filterIsInstance() - .forEach { view -> view.setOnClickListener { view.setImageResource(R.drawable.black_stone) } } + board.removeAllViews() + initializeBoard(board) + setupBoardClickListeners(board) + } + + private fun initializeBoard(board: TableLayout) { + for (i in 0 until boardSize) { + val tableRow = TableRow(this) + for (j in 0 until boardSize) { + val frameLayout = createBoardCell(i, j) + tableRow.addView(frameLayout) + } + board.addView(tableRow) + } + } + + private fun createBoardCell(row: Int, col: Int): FrameLayout { + val frameLayout = FrameLayout(this).apply { + layoutParams = TableRow.LayoutParams(70, 70) + } + val boardImageView = ImageView(this).apply { + layoutParams = FrameLayout.LayoutParams( + FrameLayout.LayoutParams.MATCH_PARENT, + FrameLayout.LayoutParams.MATCH_PARENT + ) + setImageResource(getCellDrawable(row, col)) + } + val stoneImageView = ImageView(this).apply { + layoutParams = FrameLayout.LayoutParams( + FrameLayout.LayoutParams.MATCH_PARENT, + FrameLayout.LayoutParams.MATCH_PARENT + ) + setImageResource(android.R.color.transparent) // 초기에는 투명한 이미지로 설정 + } + frameLayout.addView(boardImageView) + frameLayout.addView(stoneImageView) + return frameLayout } -} +} \ No newline at end of file From d1c0ddd533b9b24cf2970dc9494e4ba48edb87eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 20:57:33 +0900 Subject: [PATCH 04/16] feat:click setting --- .../main/java/nextstep/omok/MainActivity.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/src/main/java/nextstep/omok/MainActivity.kt b/app/src/main/java/nextstep/omok/MainActivity.kt index d96f32c..e0ed817 100644 --- a/app/src/main/java/nextstep/omok/MainActivity.kt +++ b/app/src/main/java/nextstep/omok/MainActivity.kt @@ -60,4 +60,22 @@ class MainActivity : AppCompatActivity() { frameLayout.addView(stoneImageView) return frameLayout } + + private fun setupBoardClickListeners(board: TableLayout) { + board.children + .filterIsInstance() + .flatMap { it.children } + .filterIsInstance() + .forEachIndexed { index, frameLayout -> + val x = index % boardSize + val y = index / boardSize + setupCellClickListener(frameLayout, x, y) + } + } + + private fun setupCellClickListener(frameLayout: FrameLayout, x: Int, y: Int) { + frameLayout.setOnClickListener { + handleBoardClick(x, y, frameLayout.getChildAt(1) as ImageView) + } + } } \ No newline at end of file From 663e02b82251f0d5ad996ce551ccb311b66d7755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 20:58:30 +0900 Subject: [PATCH 05/16] feat: placeStone in Omok Board --- app/src/main/java/nextstep/omok/MainActivity.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/src/main/java/nextstep/omok/MainActivity.kt b/app/src/main/java/nextstep/omok/MainActivity.kt index e0ed817..1e3a594 100644 --- a/app/src/main/java/nextstep/omok/MainActivity.kt +++ b/app/src/main/java/nextstep/omok/MainActivity.kt @@ -78,4 +78,19 @@ class MainActivity : AppCompatActivity() { handleBoardClick(x, y, frameLayout.getChildAt(1) as ImageView) } } + + private fun handleBoardClick(x: Int, y: Int, stoneImageView: ImageView) { + if (game.placeStone(x, y)) { + updateStoneImage(stoneImageView) + if (game.checkWin(x, y)) { + showGameOverDialog() + } else { + switchTurn() + } + } + } + + private fun updateStoneImage(stoneImageView: ImageView) { + stoneImageView.setImageResource(if (currentTurn == Stone.BLACK) R.drawable.black_stone else R.drawable.white_stone) + } } \ No newline at end of file From 3e9100b1b0e41e37a263c2c38caf669ee2a7dcbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 20:59:25 +0900 Subject: [PATCH 06/16] feat: turn the step with white and black --- app/src/main/java/nextstep/omok/MainActivity.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/java/nextstep/omok/MainActivity.kt b/app/src/main/java/nextstep/omok/MainActivity.kt index 1e3a594..e05b887 100644 --- a/app/src/main/java/nextstep/omok/MainActivity.kt +++ b/app/src/main/java/nextstep/omok/MainActivity.kt @@ -93,4 +93,8 @@ class MainActivity : AppCompatActivity() { private fun updateStoneImage(stoneImageView: ImageView) { stoneImageView.setImageResource(if (currentTurn == Stone.BLACK) R.drawable.black_stone else R.drawable.white_stone) } + + private fun switchTurn() { + currentTurn = if (currentTurn == Stone.BLACK) Stone.WHITE else Stone.BLACK + } } \ No newline at end of file From 499c7458f03fb309d18c8d421c7abc003dc672c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:01:41 +0900 Subject: [PATCH 07/16] feat : notice the end of the game and restart --- app/src/main/java/nextstep/omok/MainActivity.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/main/java/nextstep/omok/MainActivity.kt b/app/src/main/java/nextstep/omok/MainActivity.kt index e05b887..3cfab03 100644 --- a/app/src/main/java/nextstep/omok/MainActivity.kt +++ b/app/src/main/java/nextstep/omok/MainActivity.kt @@ -97,4 +97,17 @@ class MainActivity : AppCompatActivity() { private fun switchTurn() { currentTurn = if (currentTurn == Stone.BLACK) Stone.WHITE else Stone.BLACK } + + private fun showGameOverDialog() { + AlertDialog.Builder(this) + .setTitle("게임 종료") + .setMessage("게임이 종료되었습니다. 새로운 게임을 시작하세요.") + .setPositiveButton("OK") { _, _ -> + game = OmokGame(boardSize) + currentTurn = Stone.BLACK + startNewGame() + } + .setCancelable(false) + .show() + } } \ No newline at end of file From bef30e6637ecb6a11ca665d005dc130e73637008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:03:10 +0900 Subject: [PATCH 08/16] fix:README function --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4d059fd..60fd7af 100644 --- a/README.md +++ b/README.md @@ -15,21 +15,22 @@ 5. 승리 여부 표시 - 5개 이상의 돌이 놓여 게임이 끝났음을 알리고 재시작 6. 오목판 그리기 -- View 파일을 만들어 오목판 그리기 - 해당 판은 기존 xml 파일 활용 -7. 돌 그리기 +7. 오목판 배치하 +- view 파일 활용 +8. 돌 그리기 - View 파일 안에 하얀 돌, 검은 돌 그리기 - 해당 돌은 기존 xml 파일 활용 -8. 돌 놓는 이벤트 처리하기 +9. 돌 놓는 이벤트 처리하기 - 오목판 위에 돌 놓는 것 처리하기 - 오목판 위에 돌을 놓음으로써 뒤의 오목판 이미지가 사라지지 않도록 설정하는 것 주의하기 -9. 돌 놓기 기능 -- 8의 기능 처리 -10. 승리 조건 체크하기 +10. 돌 놓기 기능 +- 9의 기능 처리 +11. 승리 조건 체크하기 - 같은 색의 돌이 가로,세로,대각선 중에서 하나라도 5개 이상이 되었을 때 게임 종료 - 해당 돌이 같은 색인지, 사이의 같은 돌의 개수 체크하기 - 승리 조건 판단 -11. 레이아웃 작성하기 +12. 레이아웃 작성하기 - android_main.xml -12. test 돌리기 +13. test 돌리기 - test code 작성 \ No newline at end of file From 68e644f409dff9e866ca2f002e9f8fb373d4bd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:03:56 +0900 Subject: [PATCH 09/16] feat: draw the omok board --- app/src/main/java/nextstep/omok/MainActivity.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/src/main/java/nextstep/omok/MainActivity.kt b/app/src/main/java/nextstep/omok/MainActivity.kt index 3cfab03..a1a1f2a 100644 --- a/app/src/main/java/nextstep/omok/MainActivity.kt +++ b/app/src/main/java/nextstep/omok/MainActivity.kt @@ -110,4 +110,18 @@ class MainActivity : AppCompatActivity() { .setCancelable(false) .show() } + + private fun getCellDrawable(row: Int, col: Int): Int { + return when { + row == 0 && col == 0 -> R.drawable.board_top_left + row == 0 && col == boardSize - 1 -> R.drawable.board_top_right + row == boardSize - 1 && col == 0 -> R.drawable.board_bottom_left + row == boardSize - 1 && col == boardSize - 1 -> R.drawable.board_bottom_right + row == 0 -> R.drawable.board_top + row == boardSize - 1 -> R.drawable.board_bottom + col == 0 -> R.drawable.board_left + col == boardSize - 1 -> R.drawable.board_right + else -> R.drawable.board_center + } + } } \ No newline at end of file From 6edfbc9262374e381fc11513968d06248d21d0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:05:01 +0900 Subject: [PATCH 10/16] omok game board on the view --- app/src/main/java/nextstep/omok/GameView.kt | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/src/main/java/nextstep/omok/GameView.kt diff --git a/app/src/main/java/nextstep/omok/GameView.kt b/app/src/main/java/nextstep/omok/GameView.kt new file mode 100644 index 0000000..5119f09 --- /dev/null +++ b/app/src/main/java/nextstep/omok/GameView.kt @@ -0,0 +1,30 @@ +package nextstep.omok + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.view.MotionEvent +import android.view.View + +class GameView(context: Context) : View(context) { + private val boardSize = 15 + private val cellSize = 70f + private val boardPaint = Paint().apply { color = Color.BLACK } + private val blackPaint = Paint().apply { color = Color.BLACK } + private val whitePaint = Paint().apply { color = Color.WHITE } + private val game = OmokGame(boardSize) + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + drawBoard(canvas) + drawStones(canvas) + } + + private fun drawBoard(canvas: Canvas) { + for (i in 0 until boardSize) { + canvas.drawLine(i * cellSize, 0f, i * cellSize, boardSize * cellSize, boardPaint) + canvas.drawLine(0f, i * cellSize, boardSize * cellSize, i * cellSize, boardPaint) + } + } +} \ No newline at end of file From 90767a2747607eadc7872f74fc204f679f6b647f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:05:40 +0900 Subject: [PATCH 11/16] draw the stones on the view --- app/src/main/java/nextstep/omok/GameView.kt | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/src/main/java/nextstep/omok/GameView.kt b/app/src/main/java/nextstep/omok/GameView.kt index 5119f09..afeb82d 100644 --- a/app/src/main/java/nextstep/omok/GameView.kt +++ b/app/src/main/java/nextstep/omok/GameView.kt @@ -27,4 +27,28 @@ class GameView(context: Context) : View(context) { canvas.drawLine(0f, i * cellSize, boardSize * cellSize, i * cellSize, boardPaint) } } + + private fun drawStones(canvas: Canvas) { + for (i in 0 until boardSize) { + for (j in 0 until boardSize) { + when (game.getStone(i, j)) { + Stone.BLACK -> canvas.drawCircle( + i * cellSize + cellSize / 2, + j * cellSize + cellSize / 2, + cellSize / 2, + blackPaint + ) + + Stone.WHITE -> canvas.drawCircle( + i * cellSize + cellSize / 2, + j * cellSize + cellSize / 2, + cellSize / 2, + whitePaint + ) + + else -> {} + } + } + } + } } \ No newline at end of file From 5f9a8ac8aaa9929b38cc59094fcdc99fe790ced9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:07:08 +0900 Subject: [PATCH 12/16] feat:event the stones on the board in view --- app/src/main/java/nextstep/omok/GameView.kt | 24 ++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/nextstep/omok/GameView.kt b/app/src/main/java/nextstep/omok/GameView.kt index afeb82d..a40197a 100644 --- a/app/src/main/java/nextstep/omok/GameView.kt +++ b/app/src/main/java/nextstep/omok/GameView.kt @@ -33,22 +33,26 @@ class GameView(context: Context) : View(context) { for (j in 0 until boardSize) { when (game.getStone(i, j)) { Stone.BLACK -> canvas.drawCircle( - i * cellSize + cellSize / 2, - j * cellSize + cellSize / 2, - cellSize / 2, - blackPaint + i * cellSize + cellSize / 2, j * cellSize + cellSize / 2, cellSize / 2, blackPaint ) - Stone.WHITE -> canvas.drawCircle( - i * cellSize + cellSize / 2, - j * cellSize + cellSize / 2, - cellSize / 2, - whitePaint + i * cellSize + cellSize / 2, j * cellSize + cellSize / 2, cellSize / 2, whitePaint ) - else -> {} } } } } + + override fun onTouchEvent(event: MotionEvent): Boolean { + if (event.action == MotionEvent.ACTION_DOWN) { + val x = (event.x / cellSize).toInt() + val y = (event.y / cellSize).toInt() + if (game.placeStone(x, y)) { + invalidate() + } + return true + } + return super.onTouchEvent(event) + } } \ No newline at end of file From 30f24ef6db017008cf1bf83536d71a7ca518a28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:08:33 +0900 Subject: [PATCH 13/16] feat : the function of placing the stones --- app/src/main/java/nextstep/omok/OmokGame.kt | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/src/main/java/nextstep/omok/OmokGame.kt diff --git a/app/src/main/java/nextstep/omok/OmokGame.kt b/app/src/main/java/nextstep/omok/OmokGame.kt new file mode 100644 index 0000000..0e87c9b --- /dev/null +++ b/app/src/main/java/nextstep/omok/OmokGame.kt @@ -0,0 +1,28 @@ +package nextstep.omok + +enum class Stone { + EMPTY, BLACK, WHITE +} + +class OmokGame(private val boardSize: Int) { + private val board: Array> = Array(boardSize) { Array(boardSize) { Stone.EMPTY } } + private var currentTurn = Stone.BLACK + + fun getStone(x: Int, y: Int): Stone { + return if (x in 0 until boardSize && y in 0 until boardSize) { + board[x][y] + } else { + Stone.EMPTY + } + } + //돌 놓는 동작 성공 여부 + fun placeStone(x: Int, y: Int): Boolean { + if (x !in 0 until boardSize || y !in 0 until boardSize || board[x][y] != Stone.EMPTY) { + return false + } + board[x][y] = currentTurn + val isWin = checkWin(x, y) + currentTurn = if (currentTurn == Stone.BLACK) Stone.WHITE else Stone.BLACK + return true + } +} \ No newline at end of file From e9d49ee50b4c6048592948538a1668b9b16e63fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:10:29 +0900 Subject: [PATCH 14/16] feat: the function of success standard or not --- app/src/main/java/nextstep/omok/OmokGame.kt | 35 ++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nextstep/omok/OmokGame.kt b/app/src/main/java/nextstep/omok/OmokGame.kt index 0e87c9b..b03fede 100644 --- a/app/src/main/java/nextstep/omok/OmokGame.kt +++ b/app/src/main/java/nextstep/omok/OmokGame.kt @@ -15,6 +15,7 @@ class OmokGame(private val boardSize: Int) { Stone.EMPTY } } + //돌 놓는 동작 성공 여부 fun placeStone(x: Int, y: Int): Boolean { if (x !in 0 until boardSize || y !in 0 until boardSize || board[x][y] != Stone.EMPTY) { @@ -25,4 +26,36 @@ class OmokGame(private val boardSize: Int) { currentTurn = if (currentTurn == Stone.BLACK) Stone.WHITE else Stone.BLACK return true } -} \ No newline at end of file + + fun checkWin(x: Int, y: Int): Boolean { + val directions = listOf( + Pair(1, 0), Pair(0, 1), Pair(1, 1), Pair(1, -1) + ) + val currentStone = board[x][y] + for ((dx, dy) in directions) { + var count = 1 + count += countStonesInDirection(x, y, dx, dy, currentStone) + count += countStonesInDirection(x, y, -dx, -dy, currentStone) + if (count >= 5) { + return true + } + } + return false + } + + private fun countStonesInDirection(x: Int, y: Int, dx: Int, dy: Int, stone: Stone): Int { + var count = 0 + var nx = x + dx + var ny = y + dy + while (nx in 0 until boardSize && ny in 0 until boardSize && board[nx][ny] == stone) { + count++ + nx += dx + ny += dy + } + return count + } +} + + + + From 20b8259a533fe6f1f3f49e78cdc0c2f3cabab7f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:11:42 +0900 Subject: [PATCH 15/16] feat: activity_main_layout --- app/src/main/res/layout/activity_main.xml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d809fc8..7db355e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,13 +1,16 @@ + android:layout_height="match_parent" + android:orientation="vertical" + android:gravity="center"> + @@ -1180,4 +1183,4 @@ - + \ No newline at end of file From 706786f4774780a049d07e900913f10565631886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EC=A7=80=EC=98=88?= Date: Sat, 8 Jun 2024 21:12:40 +0900 Subject: [PATCH 16/16] feat:test code --- .../test/java/nextstep/omok/OmokGameTest.kt | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/src/test/java/nextstep/omok/OmokGameTest.kt diff --git a/app/src/test/java/nextstep/omok/OmokGameTest.kt b/app/src/test/java/nextstep/omok/OmokGameTest.kt new file mode 100644 index 0000000..72bc5d5 --- /dev/null +++ b/app/src/test/java/nextstep/omok/OmokGameTest.kt @@ -0,0 +1,56 @@ +package nextstep.omok + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.DisplayName + +class OmokGameTest { + + private val boardSize = 15 + + @Test + @DisplayName("빈 위치에 돌을 놓을 수 있는지 테스트") + fun testPlaceStone() { + val game = OmokGame(boardSize) + assertTrue(game.placeStone(0, 0)) // 빈 곳에 돌을 놓을 수 있어야 함 + assertFalse(game.placeStone(0, 0)) // 이미 돌이 있는 곳에 다시 놓을 수 없어야 함 + } + + @Test + @DisplayName("정해진 위치에 돌이 놓였는지 테스트") + fun testGetStone() { + val game = OmokGame(boardSize) + game.placeStone(0, 0) + assertEquals(Stone.BLACK, game.getStone(0, 0)) + game.placeStone(1, 1) + assertEquals(Stone.WHITE, game.getStone(1, 1)) + } + + @Test + @DisplayName("빈 위치 확인 테스트") + fun testIsEmpty() { + val game = OmokGame(boardSize) + assertTrue(game.getStone(0, 0) == Stone.EMPTY) + game.placeStone(0, 0) + assertFalse(game.getStone(0, 0) == Stone.EMPTY) + } + @Test + @DisplayName("현재 턴 변경 테스트") + fun testSwitchTurn() { + val game = OmokGame(boardSize) + game.placeStone(0, 0) + assertEquals(Stone.BLACK, game.getStone(0, 0)) + game.placeStone(1, 1) + assertEquals(Stone.WHITE, game.getStone(1, 1)) + } + @Test + @DisplayName("오목판 초기화 테스트") + fun testInitializeBoard() { + val game = OmokGame(boardSize) + for (i in 0 until boardSize) { + for (j in 0 until boardSize) { + assertEquals(Stone.EMPTY, game.getStone(i, j)) + } + } + } +}