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 15, 2023
commit f4f1b159768e558f3c0a542f6cb33f85a019cd1f
7 changes: 5 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## 오목

### Domain
- [ ] 흑돌이 먼저 시작한다.
- [ ] 한 플레이어라도 승리할 때까지 차례를 번갈아가면서 돌을 놓는다.
- [x] 흑, 백 플레이어를 가지고 있다
- [x] 흑돌이 먼저 시작한다.
- [ ] 게임의 진행 여부는 `PlayerState`가 결정한다.
- [x] 오목알은 자신의 위치를 알고 있다.
- [x] `x, y` 위치는 `1부터 15`로 제한된다.
- [x] 중복되는 위치의 오목알을 가질 수 없다.
Expand All @@ -14,11 +17,11 @@
- [x] 오목알을 놓은 플레이어가 게임에서 이겼는지 확인한다.
- [x] 사용자는 특정 위치에 내 돌이 있는지 확인한다.
- [x] 사용자는 마지막 돌의 위치를 알고 있다.
- [ ] 게임의 진행 여부는 `PlayerState`가 결정한다.
- [x] 오목알을 놓으면 상대방의 차례가 된다.

### Input
- [ ] 오목알을 놓을 위치를 입력받는다.

### Output
- [ ] 오목알의 위치를 입력받기 전에 오목판을 출력한다.
- [ ] 마지막 돌의 위치를 출력한다.
15 changes: 10 additions & 5 deletions src/main/kotlin/Board.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import Position.Companion.POSITION_RANGE
import player.Player
import player.Players

class Board(private val players: Players) {
constructor(blackPlayer: Player, whitePlayer: Player) : this(Players(blackPlayer, whitePlayer))

fun putStone(turn: Turn, stone: Stone): Player? {
fun putStone(stoneColor: StoneColor, stone: Stone): Board? {
if (players.canPlace(stone)) {
return players.putStone(turn, stone)
return Board(players.putStone(stoneColor, stone))
}
return null
}

private fun makeEmptyBoard(): List<MutableList<BoardState>> =
List(POSITION_RANGE.max()) { MutableList(POSITION_RANGE.max()) { BoardState.EMPTY } }
fun getPlayers(): Players = players.copy()

fun isRunning(): Boolean = players.isRunning

fun getWinner(): Player = players.getWinner()

// private fun makeEmptyBoard(): List<MutableList<BoardState>> =
// List(POSITION_RANGE.max()) { MutableList(POSITION_RANGE.max()) { BoardState.EMPTY } }
}
3 changes: 0 additions & 3 deletions src/main/kotlin/BoardState.kt

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/kotlin/Stone.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data class Stone private constructor(val position: Position) {
companion object {
private val STONES = POSITION_RANGE.map { row -> POSITION_RANGE.map { col -> Stone(Position(row, col)) } }

fun of(row: Int, col: Int) = STONES[row - 1][col - 1]
fun of(row: Int, col: Int): Stone = STONES[row - 1][col - 1]
fun of(position: Position): Stone = STONES[position.row - 1][position.col - 1]
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/Turn.kt → src/main/kotlin/StoneColor.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
enum class Turn {
enum class StoneColor {
BLACK, WHITE;

fun next(): Turn = when (this) {
fun next(): StoneColor = when (this) {
BLACK -> WHITE
WHITE -> BLACK
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/game/Omok.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package game

import Board
import Stone
import StoneColor
import listener.OmokEventListener
import player.BlackPlayer
import player.WhitePlayer

class Omok(private val eventListener: OmokEventListener) {
private val board = Board(BlackPlayer(), WhitePlayer())

fun run() {
eventListener.onStartGame()
var currentStoneColor: StoneColor = StoneColor.BLACK
var currentBoard: Board = board
do {
currentBoard = takeTurn(currentBoard, currentStoneColor)
eventListener.onEndTurn(currentBoard.getPlayers())
currentStoneColor = currentStoneColor.next()
} while (currentBoard.isRunning())

eventListener.onEndGame(board.getWinner())
}

private fun takeTurn(board: Board, stoneColor: StoneColor): Board {
val newStone = Stone.of(eventListener.onTakeTurn(stoneColor))
return board.putStone(stoneColor, newStone) ?: return takeTurn(board, stoneColor)
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/listener/OmokEventListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package listener

import Position
import StoneColor
import player.Player
import player.Players

interface OmokEventListener {
fun onStartGame()
fun onTakeTurn(stoneColor: StoneColor): Position
fun onEndTurn(positions: Players)
fun onEndGame(player: Player)
}
2 changes: 0 additions & 2 deletions src/main/kotlin/player/BlackPlayer.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package player

import BoardState
import Stone
import state.PlayerState
import state.PlayingState

class BlackPlayer(state: PlayerState = PlayingState()) : Player(state) {
override val boardState: BoardState = BoardState.BLACK
override fun putStone(stone: Stone): Player = BlackPlayer(state.add(stone))
}
12 changes: 8 additions & 4 deletions src/main/kotlin/player/Player.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package player

import BoardState
import Position
import Stone
import state.PlayerState
import state.WinState

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

abstract fun putStone(stone: Stone): Player

fun canPlace(): Boolean = state !is WinState

fun isPlaced(stone: Stone): Boolean = state.hasStone(stone)

fun getPlaced(): List<Position> = state.getPlaced()
fun getPositions(): List<Position> = state.getPlaced()

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

public override fun clone(): Player = super.clone() as Player
}
24 changes: 19 additions & 5 deletions src/main/kotlin/player/Players.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package player

import Stone
import Turn
import StoneColor

class Players private constructor(private val players: Map<Turn, Player>) {
constructor(black: Player, white: Player) : this(mapOf(Turn.BLACK to black, Turn.WHITE to white))
data class Players private constructor(private val players: List<Player>) {
val isRunning: Boolean
get() = players.all { it.canPlace() }

fun putStone(turn: Turn, stone: Stone): Player? = players[turn]?.putStone(stone)
constructor(blackPlayer: Player, whitePlayer: Player) : this(listOf(blackPlayer.clone(), whitePlayer.clone()))

fun canPlace(stone: Stone): Boolean = players.values.none { it.isPlaced(stone) }
fun putStone(stoneColor: StoneColor, stone: Stone): Players {
if (stoneColor == StoneColor.BLACK) {
return Players(blackPlayer = getBlackPlayer().putStone(stone), whitePlayer = getWhitePlayer())
}
return Players(blackPlayer = getBlackPlayer(), whitePlayer = getWhitePlayer().putStone(stone))
}

fun getBlackPlayer(): Player = players.first { it is BlackPlayer }

fun getWhitePlayer(): Player = players.first { it is WhitePlayer }

fun getWinner(): Player = players.first { it.isWin }

fun canPlace(stone: Stone): Boolean = players.none { it.isPlaced(stone) }
}
2 changes: 0 additions & 2 deletions src/main/kotlin/player/WhitePlayer.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package player

import BoardState
import Stone
import state.PlayerState
import state.PlayingState

class WhitePlayer(state: PlayerState = PlayingState()) : Player(state) {
override val boardState: BoardState = BoardState.WHITE
override fun putStone(stone: Stone): Player = WhitePlayer(state.add(stone))
}
1 change: 1 addition & 0 deletions src/main/kotlin/state/PlayerState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ abstract class PlayerState(protected val stones: Stones = Stones()) {
fun hasStone(stone: Stone): Boolean = stones.hasStone(stone)

fun getPlaced(): List<Position> = stones.getPositions()

fun getLastStone(): Stone = stones.lastStone
}
4 changes: 2 additions & 2 deletions src/test/kotlin/BoardTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class BoardTest {
val blackPlayer: Player = BlackPlayer(PlayingState(Stones(ONE_ONE_STONE)))
val whitePlayer: Player = WhitePlayer(PlayingState(Stones(ONE_TWO_STONE)))
val board = Board(blackPlayer, whitePlayer)
assertThat(board.putStone(Turn.BLACK, ONE_THREE_STONE)).isInstanceOf(Player::class.java)
assertThat(board.putStone(StoneColor.BLACK, ONE_THREE_STONE)).isInstanceOf(Player::class.java)
}

@Test
fun `특정 위치에 돌을 놓지 못하면 null을 반환한다`() {
val blackPlayer: Player = BlackPlayer(PlayingState(Stones(ONE_ONE_STONE)))
val whitePlayer: Player = WhitePlayer(PlayingState(Stones(ONE_TWO_STONE)))
val board = Board(blackPlayer, whitePlayer)
assertThat(board.putStone(Turn.BLACK, ONE_TWO_STONE)).isNull()
assertThat(board.putStone(StoneColor.BLACK, ONE_TWO_STONE)).isNull()
}
}
9 changes: 9 additions & 0 deletions src/test/kotlin/OmokTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import game.Omok
import org.junit.jupiter.api.Test

class OmokTest {
@Test
fun `한 플레이어라도 승리할 때까지 차례를 번갈아가면서 돌을 놓는다`() {
val omok = Omok()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource

class TurnTest {
class StoneColorTest {
@CsvSource("WHITE, BLACK", "BLACK, WHITE")
@ParameterizedTest
fun `상대방 차례를 반환한다`(myTurn: Turn, expected: Turn) {
val actual = myTurn.next()
fun `상대방 차례를 반환한다`(myStoneColor: StoneColor, expected: StoneColor) {
val actual = myStoneColor.next()
assertThat(actual).isEqualTo(expected)
}
}