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

[스캇] 3, 4단계 오목 제출합니다. #42

Merged
merged 28 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e0f830f
feat: set up the android project
woowahan-pjs Mar 14, 2023
237283c
feat: add main activity and resources
woowahan-pjs Mar 21, 2023
7aa3aea
refactor(): Domain 폴더 옮기기
chws0508 Mar 22, 2023
6e32058
refactor(Stones): 마지막 바둑돌의 위치를 찾는 책임을 바둑돌들 객체에게 넘김 및 불필요한 매서드 제거
chws0508 Mar 22, 2023
d0d4909
refactor(Rule): 렌주 룰 업데이트
chws0508 Mar 22, 2023
c123268
refactor(Board): 룰을 보드에게 주입하는 형식으로 보드와 렌주룰 의존성 제거
chws0508 Mar 22, 2023
dea789c
refactor(): 보드게임에게 현재 턴을 관리하는 책임을 넘김, 바둑돌을 추가하고 승자를 판단하는 방식으로 수정
chws0508 Mar 22, 2023
98f5f88
refactor(): resetGame 기능 구현 및, Controller에서 While문을 사용하도록 수정
chws0508 Mar 23, 2023
f49262a
feat(): step3 구현
chws0508 Mar 23, 2023
8ab1e81
refactor(): 오류 수정
chws0508 Mar 23, 2023
829523b
refactor(): 데이터베이스 적용을 위해 getStone 을 public으로 수정
chws0508 Mar 25, 2023
fe96c33
feat(): 데이터베이스 틀 작성
chws0508 Mar 25, 2023
0c91ca9
refactor(): 컨버터 기능 클래스 구현
chws0508 Mar 25, 2023
dd828a7
feat(): 데이터베이스 관리 클래스 구현
chws0508 Mar 25, 2023
13ee782
feat(): 데이터베이스 적용 및 안드로이드에 차례 보여주는 화면 추가
chws0508 Mar 25, 2023
1ac4ef8
feat(): 콘솔도 돌아가도록 수정
chws0508 Mar 25, 2023
a5f639c
chore(): ktLint Check
chws0508 Mar 25, 2023
64fb165
refactor(OmokGame): 현재 턴을 private set으로 외부에서 변경 제어, 게임 생성 시 현재 턴을 결정할…
chws0508 Mar 26, 2023
74476df
refactor(Stones): 중복으로 돌을 놓을 수 없게 제한
chws0508 Mar 26, 2023
ce42547
refactor(OmokGame): omokGame이 게임 진행상태를 가지도록 수정, 돌을 성공적으로 놓았는지 반환하는 함수 추가
chws0508 Mar 26, 2023
8f7335d
refactor(Controller): 컨트롤러 수정
chws0508 Mar 26, 2023
86a7d55
refactor(OmokDb): 데이터베이스 수정
chws0508 Mar 26, 2023
646767c
refactor(MainActivity): MainActivity 수정
chws0508 Mar 26, 2023
29696aa
refactor(): 안쓰는 함수 삭제
chws0508 Mar 26, 2023
a24a999
refactor(): 현재 턴 관리 중복 코드 제거
chws0508 Mar 27, 2023
d4410b3
refactor(): 상태를 Finished 로 변경하는 것을 private 으로 설정
chws0508 Mar 27, 2023
2d1ed8f
refactor(): OmokDbManager 삭제,
chws0508 Mar 27, 2023
d5a943e
refactor(): 중복 돌 검사 체크 함수 분리
chws0508 Mar 27, 2023
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
Prev Previous commit
Next Next commit
refactor(): 보드게임에게 현재 턴을 관리하는 책임을 넘김, 바둑돌을 추가하고 승자를 판단하는 방식으로 수정
  • Loading branch information
chws0508 committed Mar 22, 2023
commit dea789ca15f93315557d56fdb063d9da79143398
6 changes: 4 additions & 2 deletions domain/src/main/kotlin/Controller.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import domain.Board
import domain.OmokGame
import domain.RenjuRuleAdapter
import view.InputView
import view.OutputView

class Controller {
fun run() {
OutputView.printStart()
val omokGame = OmokGame(Board())
val winnerColor = omokGame.getWinnerColor(OutputView::printCurrentState, InputView::inputPosition)
val omokGame = OmokGame(Board(rule = RenjuRuleAdapter()))
val winnerColor =
omokGame.getWinnerColorPhase(OutputView::printCurrentState, InputView::inputPosition)
OutputView.printResult(winnerColor, omokGame.board)
}
}
31 changes: 19 additions & 12 deletions domain/src/main/kotlin/domain/OmokGame.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package domain

class OmokGame(val board: Board) {
fun getWinnerColor(showCurrentState: (Board) -> Unit, getPosition: () -> Position): Color {
val stone = getStone(showCurrentState, getPosition)
val winnerColor = judgeWinner(stone)
var currentColor: Color = INITIAL_COLOR
fun getWinnerColorPhase(
getCurrentResult: (OmokGame) -> Unit,
getPosition: () -> Position
): Color {
val stone = getStone(getCurrentResult, getPosition)
board.placeStone(stone)
return winnerColor ?: return getWinnerColor(showCurrentState, getPosition)
return board.getWinnerColor() ?: getWinnerColorPhase(getCurrentResult, getPosition)
}

private fun getStone(showCurrentState: (Board) -> Unit, getPosition: () -> Position): Stone {
showCurrentState(board)
val stone = Stone(board.getCurrentTurn(), getPosition())
private fun getStone(showCurrentState: (OmokGame) -> Unit, getPosition: () -> Position): Stone {
showCurrentState(this)
val stone = Stone(currentColor, getPosition())
if (!board.isEmpty(stone)) return getStone(showCurrentState, getPosition)
currentColor = nextColor(currentColor)
return stone
}

private fun judgeWinner(stone: Stone): Color? {
when {
board.isBlackWin(stone) -> return Color.BLACK
board.isWhiteWin(stone) -> return Color.WHITE
private fun nextColor(color: Color): Color {
return when (color) {
Color.BLACK -> Color.WHITE
Color.WHITE -> Color.BLACK
}
return null
}

companion object {
private val INITIAL_COLOR = Color.BLACK
}
}
25 changes: 11 additions & 14 deletions domain/src/main/kotlin/view/OutputView.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package view

import domain.Board
import domain.Color
import domain.Position
import domain.Stones
import domain.*

object OutputView {

fun printCurrentState(board: Board) {
printBoard(board)
printTurn(board.getCurrentTurn())
printLastPosition(board.getLastPosition())
fun printCurrentState(omokGame: OmokGame) {
printBoard(omokGame.board)
printTurn(omokGame.currentColor)
printLastPosition(omokGame.board.stones.getLastStone())
}

fun printStart() {
println("오목 게임을 시작합니다.")
}

fun printBoard(board: Board) {
private fun printBoard(board: Board) {
val customBoard = generateCustomBoard(board.stones)
customBoard.forEachIndexed { y, colors ->
print("${Board.getSize() - y} ".padStart(4, ' '))
Expand All @@ -40,19 +37,19 @@ object OutputView {
println()
}

fun printTurn(color: Color) {
private fun printTurn(color: Color) {
when (color) {
Color.BLACK -> print("흑의 차례입니다.")
Color.WHITE -> print("백의 차례입니다.")
}
}

fun printLastPosition(position: Position?) {
if (position == null) {
private fun printLastPosition(stone: Stone?) {
if (stone == null) {
println()
return
}
println(" (마지막 돌의 위치: ${AlphabetCoordinate.convertAlphabet(position.x)}${position.y})")
println(" (마지막 돌의 위치: ${AlphabetCoordinate.convertAlphabet(stone.position.x)}${stone.position.y})")
}

fun printResult(color: Color, board: Board) {
Expand All @@ -68,7 +65,7 @@ object OutputView {
MutableList(Board.getSize()) { 0 }
}
stones.values.forEach {
if (it.isBlack()) {
if (it.color== Color.BLACK) {
initBoard[Board.getSize() - it.position.y][it.position.x] = BLACK
} else {
initBoard[Board.getSize() - it.position.y][it.position.x] = WHITE
Expand Down
46 changes: 24 additions & 22 deletions domain/src/test/kotlin/domain/OmokGameTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ internal class OmokGameTest {
val board = generateBlackWinOmokBoard()
val omokGame = OmokGame(board)
// when
val actual = omokGame.getWinnerColor({}, { Position(1, 6) })
val actual = omokGame.getWinnerColorPhase({}) { Position(1, 6) }
val expected = Color.BLACK
// then
Assertions.assertThat(actual).isEqualTo(expected)
}

private fun generateBlackWinOmokBoard(): Board {
val board = Board().apply {
placeStone(Stone(getCurrentTurn(), 1, 2))
placeStone(Stone(getCurrentTurn(), 2, 2))
placeStone(Stone(getCurrentTurn(), 1, 3))
placeStone(Stone(getCurrentTurn(), 2, 3))
placeStone(Stone(getCurrentTurn(), 1, 4))
placeStone(Stone(getCurrentTurn(), 2, 4))
placeStone(Stone(getCurrentTurn(), 1, 5))
placeStone(Stone(getCurrentTurn(), 4, 8))
val board = Board(rule = RenjuRuleAdapter()).apply {
placeStone(Stone(Color.BLACK, 1, 2))
placeStone(Stone(Color.WHITE, 2, 2))
placeStone(Stone(Color.BLACK, 1, 3))
placeStone(Stone(Color.WHITE, 2, 3))
placeStone(Stone(Color.BLACK, 1, 4))
placeStone(Stone(Color.WHITE, 2, 4))
placeStone(Stone(Color.BLACK, 1, 5))
placeStone(Stone(Color.WHITE, 4, 8))
}
return board
}
Expand All @@ -40,23 +40,24 @@ internal class OmokGameTest {
val board = generateWhiteWinOmokBoard()
val omokGame = OmokGame(board)
// when
val actual = omokGame.getWinnerColor({}, { Position(1, 6) })
omokGame.currentColor = Color.WHITE
val actual = omokGame.getWinnerColorPhase({}) { Position(1, 6) }
val expected = Color.WHITE
// then
Assertions.assertThat(actual).isEqualTo(expected)
}

private fun generateWhiteWinOmokBoard(): Board {
val board = Board().apply {
placeStone(Stone(getCurrentTurn(), 2, 2))
placeStone(Stone(getCurrentTurn(), 1, 2))
placeStone(Stone(getCurrentTurn(), 2, 3))
placeStone(Stone(getCurrentTurn(), 1, 3))
placeStone(Stone(getCurrentTurn(), 2, 4))
placeStone(Stone(getCurrentTurn(), 1, 4))
placeStone(Stone(getCurrentTurn(), 2, 5))
placeStone(Stone(getCurrentTurn(), 1, 5))
placeStone(Stone(getCurrentTurn(), 2, 10))
val board = Board(rule = RenjuRuleAdapter()).apply {
placeStone(Stone(Color.BLACK, 2, 2))
placeStone(Stone(Color.WHITE, 1, 2))
placeStone(Stone(Color.BLACK, 2, 3))
placeStone(Stone(Color.WHITE, 1, 3))
placeStone(Stone(Color.BLACK, 2, 4))
placeStone(Stone(Color.WHITE, 1, 4))
placeStone(Stone(Color.BLACK, 2, 5))
placeStone(Stone(Color.WHITE, 1, 5))
placeStone(Stone(Color.BLACK, 2, 10))
}
return board
}
Expand All @@ -67,7 +68,8 @@ internal class OmokGameTest {
val board = generateWhiteWinOmokBoard()
val omokGame = OmokGame(board)
// when
val actual = omokGame.getWinnerColor({}, { getPosition() })
omokGame.currentColor = Color.WHITE
val actual = omokGame.getWinnerColorPhase({}) { getPosition() }
val expected = Color.WHITE
// then
Assertions.assertThat(actual).isEqualTo(expected)
Expand Down