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

[둘리] 1, 2단계 오목 제출합니다. #26

Merged
merged 18 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
fe3ce07
docs: 오목 기능 목록 작성
tmdgh1592 Mar 14, 2023
ca4182c
feat: 1부터 15 위치를 가진 오목알 클래스 구현
tmdgh1592 Mar 14, 2023
e13e7da
feat: 중복 오목알 처리를 위한 오목알 일급 컬렉션 구현
tmdgh1592 Mar 14, 2023
fa5e2b9
docs: .gitkeep 파일 제거
tmdgh1592 Mar 14, 2023
0c398fd
feat: 오목알을 놓은 플레이어가 게임에서 이겼는지 확인하는 기능 구현
tmdgh1592 Mar 14, 2023
190d007
feat: 사용자가 오목알을 놓았을 때 상태를 반환하는 기능 구현
tmdgh1592 Mar 14, 2023
9d0c276
feat: 오목판에 돌을 올려놓는 기능 구현
tmdgh1592 Mar 15, 2023
606cfcf
feat: 플레이어의 오목알을 놓는 턴을 바꾸는 기능 구현
tmdgh1592 Mar 15, 2023
f96746e
refactor: 돌을 놓으면 새로운 상태를 가진 플레이어 반환하는 기능 구현
tmdgh1592 Mar 15, 2023
e5e8ce4
feat: 플레이어가 놓은 마지막 돌을 리턴하는 기능 구현
tmdgh1592 Mar 15, 2023
c472a31
refactor: 오목알을 놓을 수 있는지 확인하는 함수를 Players 클래스로 이동
tmdgh1592 Mar 15, 2023
f4f1b15
feat: 오목 게임 진행하는 기능 구현
tmdgh1592 Mar 15, 2023
28bb0cd
feat: 오목 게임 입력, 출력 화면 구현
tmdgh1592 Mar 15, 2023
f37f93b
feat: 오목 컨트롤러 구현
tmdgh1592 Mar 15, 2023
b1fcb5b
refactor: 도메인 패키지 분리
tmdgh1592 Mar 15, 2023
11b2666
fix: 중간에 오목알을 뒀을 때 승리 판정하지 않는 오류 수정
tmdgh1592 Mar 16, 2023
a0166e0
feat: 렌주룰 기능 추가 (미완성)
tmdgh1592 Mar 16, 2023
7eb0bc9
feat: 렌주룰 적용 (완성)
tmdgh1592 Mar 16, 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
feat: 렌주룰 기능 추가 (미완성)
  • Loading branch information
tmdgh1592 committed Mar 16, 2023
commit a0166e090c15e9a7251daf12a7c85cf6ddcce3a9
11 changes: 10 additions & 1 deletion src/main/kotlin/domain/player/BlackPlayer.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package domain.player

import domain.rule.RenjuRule
import domain.state.PlayerState
import domain.state.PlayingState
import domain.stone.Stone
import domain.stone.Stones

class BlackPlayer(state: PlayerState = PlayingState()) : Player(state) {
override fun putStone(stone: Stone): Player = BlackPlayer(state.add(stone))
override fun putStone(stone: Stone, otherStones: Stones): Player {
val blackStones = state.getAllStones()
if (!RenjuRule().check(blackStones, otherStones, stone)) {
return BlackPlayer(state.add(stone))
} else {
throw IllegalStateException("3-3입니다!")
}
}
}
5 changes: 4 additions & 1 deletion src/main/kotlin/domain/player/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import domain.position.Position
import domain.state.PlayerState
import domain.state.WinState
import domain.stone.Stone
import domain.stone.Stones

abstract class Player(protected val state: PlayerState) : Cloneable {
val isWin: Boolean = state is WinState

abstract fun putStone(stone: Stone): Player
abstract fun putStone(stone: Stone, otherStones: Stones): Player

fun canPlace(): Boolean = state !is WinState

Expand All @@ -18,5 +19,7 @@ abstract class Player(protected val state: PlayerState) : Cloneable {

fun getLastStone(): Stone = state.getLastStone()

fun getAllStones(): Stones = state.getAllStones()

public override fun clone(): Player = super.clone() as Player

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloneable 인터페이스를 구현한 이유는 무엇인가요?

어떠한 기능을 수행했다면 구현체에서 그 책임을 가져야 하지 않을까요?

}
4 changes: 2 additions & 2 deletions src/main/kotlin/domain/player/Players.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ data class Players private constructor(private val players: List<Player>) {

fun putStone(stoneColor: StoneColor, stone: Stone): Players {
if (stoneColor == StoneColor.BLACK) {
return Players(blackPlayer = getBlackPlayer().putStone(stone), whitePlayer = getWhitePlayer())
return Players(blackPlayer = getBlackPlayer().putStone(stone, getWhitePlayer().getAllStones()), whitePlayer = getWhitePlayer())
}
return Players(blackPlayer = getBlackPlayer(), whitePlayer = getWhitePlayer().putStone(stone))
return Players(blackPlayer = getBlackPlayer(), whitePlayer = getWhitePlayer().putStone(stone, getBlackPlayer().getAllStones()))
}

fun getBlackPlayer(): Player = players.first { it is BlackPlayer }
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/domain/player/WhitePlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package domain.player
import domain.state.PlayerState
import domain.state.PlayingState
import domain.stone.Stone
import domain.stone.Stones

class WhitePlayer(state: PlayerState = PlayingState()) : Player(state) {
override fun putStone(stone: Stone): Player = WhitePlayer(state.add(stone))
override fun putStone(stone: Stone, otherStones: Stones): Player = WhitePlayer(state.add(stone))
}
8 changes: 8 additions & 0 deletions src/main/kotlin/domain/rule/OmokRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package domain.rule

import domain.stone.Stone
import domain.stone.Stones

interface OmokRule {
fun check(blackStones: Stones, whiteStones: Stones, startStone: Stone): Boolean
}
154 changes: 154 additions & 0 deletions src/main/kotlin/domain/rule/RenjuRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package domain.rule

import domain.position.Position
import domain.stone.Stone
import domain.stone.Stones

class RenjuRule : OmokRule {
private var threeCount: Int = 0

override fun check(blackStones: Stones, whiteStones: Stones, startStone: Stone): Boolean {
return check33(blackStones, whiteStones, startStone)
}

private fun check33(blackStones: Stones, whiteStones: Stones, startStone: Stone): Boolean {
return checkAllDirections(blackStones, whiteStones, startStone)
}

private fun checkAllDirections(blackStones: Stones, whiteStones: Stones, startStone: Stone): Boolean {
val directions = listOf(RIGHT_DIRECTION, TOP_DIRECTION, RIGHT_TOP_DIRECTION, LEFT_BOTTOM_DIRECTION)

for (moveDirection in directions) {
val (forwardCount, forwardEmptyCount) =
findThree(blackStones, whiteStones, startStone.position, moveDirection, FORWARD_WEIGHT)
val (backCount, backEmptyCount) =
findThree(blackStones, whiteStones, startStone.position, moveDirection, BACK_WEIGHT)

// 만약 빈 칸이 2 미만이고, 같은 돌 개수가 무조건 3이면 3-3 가능성 ok
// 현재 방향과 3인지 여부를 확인한다.
println(moveDirection)
println("forwardCount : $forwardCount backCount : $backCount")
println("forwardEmptyCount : $forwardEmptyCount backEmptyCount : $backEmptyCount")
if (forwardCount + backCount - 1 == 3 && forwardEmptyCount + backEmptyCount <= 1) {
val (upDownDir, leftRightDir) = moveDirection

// 백돌 양쪽 합 6칸 이내에 2개 이상 있는지 확인한다.
// 닫혀 있으면 다른 방향 확인
println("ㅇㅇㅇ")
if (!isBlockedByWhiteStoneInSix(whiteStones, startStone.position.row, startStone.position.col, upDownDir, leftRightDir)) {
threeCount++
println("ㅇㅇㅇ2")
}
if (threeCount == 2) {
// 금수
println("금수!!")
return true
}
}
}
return false
}

private fun isBlockedByWhiteStoneInSix(
whiteStones: Stones,
row: Int,
col: Int,
upDownDir: Int,
leftRightDir: Int
): Boolean {
val (oneDirMoveCount, oneDirFound) = checkWhite(
whiteStones,
row + upDownDir,
col + leftRightDir,
upDownDir,
leftRightDir
)
val (otherDirMoveCount, otherDirFound) = checkWhite(
whiteStones,
row - upDownDir,
col - leftRightDir,
upDownDir,
leftRightDir
)

// 양 방향 6칸 이하에 각각 1개씩 있으면 참
if (oneDirMoveCount + otherDirMoveCount <= 6 && oneDirFound && otherDirFound) {
return true
}
return false
}

private fun checkWhite(
whiteStones: Stones,
row: Int,
col: Int,
upDownDir: Int,
leftRightDir: Int
): Pair<Int, Boolean> {
var (curRow, curCol) = Pair(row, col)
var moveCount = 1
while (!whiteStones.hasStone(Stone.of(curRow, curCol)) && moveCount <= 6) {
curRow += upDownDir
curCol += leftRightDir
moveCount++
if (whiteStones.hasStone(Stone.of(curRow, curCol))) {
return Pair(moveCount, true)
}
}
return Pair(moveCount, false)
}

private fun findThree(
blackStones: Stones,
whiteStones: Stones,
startPosition: Position,
direction: Pair<Int, Int>,
weight: Int = FORWARD_WEIGHT
): Pair<Int, Int> {
val (startRow, startCol) = Pair(startPosition.row, startPosition.col)
var sameStoneCount = 1
var emptyCount = 0
var (currentRow, currentCol) = Pair(startRow + direction.first * weight, startCol + direction.second * weight)

// 현재 탐색 방향에
// 흰 돌이 아니고, 범위 안에 있고
// 같은 돌의 개수가 3개 이하이고, 공백이 1개 이하일 때까지
while (inRange(currentRow, currentCol) && emptyCount <= 1 &&
sameStoneCount <= 3 && !whiteStones.hasStone(Stone.of(currentRow, currentCol))
) {
// 검은 돌이 있는지 확인한다.
if (blackStones.hasStone(Stone.of(currentRow, currentCol))) {
++sameStoneCount
}
// 빈 칸인지 확인한다.
if (!blackStones.hasStone(Stone.of(currentRow, currentCol)) &&
!whiteStones.hasStone(Stone.of(currentRow, currentCol))
) {
if (sameStoneCount == 3) break
++emptyCount
}
currentRow += direction.first * weight
currentCol += direction.second * weight
}

if (sameStoneCount == 1) emptyCount = 0

// println("현재 방향 : ${direction.first * weight} ${direction.second * weight}")
// println("검은 돌 : $sameStoneCount / 빈 칸 : $emptyCount")

return Pair(sameStoneCount, emptyCount)
}

private fun inRange(x: Int, y: Int) = x in Position.POSITION_RANGE && y in Position.POSITION_RANGE

companion object {

private val RIGHT_DIRECTION = Pair(1, 0)
private val TOP_DIRECTION = Pair(0, 1)
private val RIGHT_TOP_DIRECTION = Pair(1, 1)
private val LEFT_BOTTOM_DIRECTION = Pair(-1, -1)

private const val FORWARD_WEIGHT = 1
private const val BACK_WEIGHT = -1
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/domain/state/PlayerState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ abstract class PlayerState(protected val stones: Stones = Stones()) {
fun getPlaced(): List<Position> = stones.getPositions()

fun getLastStone(): Stone = stones.lastStone

fun getAllStones(): Stones = stones.copy()
}