-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fe3ce07
docs: 오목 기능 목록 작성
tmdgh1592 ca4182c
feat: 1부터 15 위치를 가진 오목알 클래스 구현
tmdgh1592 e13e7da
feat: 중복 오목알 처리를 위한 오목알 일급 컬렉션 구현
tmdgh1592 fa5e2b9
docs: .gitkeep 파일 제거
tmdgh1592 0c398fd
feat: 오목알을 놓은 플레이어가 게임에서 이겼는지 확인하는 기능 구현
tmdgh1592 190d007
feat: 사용자가 오목알을 놓았을 때 상태를 반환하는 기능 구현
tmdgh1592 9d0c276
feat: 오목판에 돌을 올려놓는 기능 구현
tmdgh1592 606cfcf
feat: 플레이어의 오목알을 놓는 턴을 바꾸는 기능 구현
tmdgh1592 f96746e
refactor: 돌을 놓으면 새로운 상태를 가진 플레이어 반환하는 기능 구현
tmdgh1592 e5e8ce4
feat: 플레이어가 놓은 마지막 돌을 리턴하는 기능 구현
tmdgh1592 c472a31
refactor: 오목알을 놓을 수 있는지 확인하는 함수를 Players 클래스로 이동
tmdgh1592 f4f1b15
feat: 오목 게임 진행하는 기능 구현
tmdgh1592 28bb0cd
feat: 오목 게임 입력, 출력 화면 구현
tmdgh1592 f37f93b
feat: 오목 컨트롤러 구현
tmdgh1592 b1fcb5b
refactor: 도메인 패키지 분리
tmdgh1592 11b2666
fix: 중간에 오목알을 뒀을 때 승리 판정하지 않는 오류 수정
tmdgh1592 a0166e0
feat: 렌주룰 기능 추가 (미완성)
tmdgh1592 7eb0bc9
feat: 렌주룰 적용 (완성)
tmdgh1592 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: 렌주룰 기능 추가 (미완성)
- Loading branch information
commit a0166e090c15e9a7251daf12a7c85cf6ddcce3a9
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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입니다!") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloneable 인터페이스를 구현한 이유는 무엇인가요?
어떠한 기능을 수행했다면 구현체에서 그 책임을 가져야 하지 않을까요?