forked from woowacourse/kotlin-omok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputView.kt
84 lines (72 loc) · 4.43 KB
/
OutputView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package view
import domain.player.Players
import domain.position.Position
import domain.stone.StoneColor
import listener.OmokStartEndEventListener
import view.model.BoardModel
import view.model.ColorModel
class OutputView : OmokStartEndEventListener {
override fun onStartGame() {
printStart()
}
override fun onEndGame(stoneColor: StoneColor) {
printWinner(stoneColor)
}
override fun onStartTurn(stoneColor: StoneColor, position: Position) {
printTurn(stoneColor, position)
}
override fun onEndTurn(players: Players) {
printOmokBoard(players)
}
private fun printStart() {
println(START_MESSAGE)
println(EMPTY_BOARD)
println(TURN_MESSAGE.format(ColorModel.getString(StoneColor.BLACK)))
}
private fun printOmokBoard(players: Players) {
val board = EMPTY_BOARD.toMutableList()
players.getBlackPlayer().getPositions().forEach {
board[calculateIndex(it)] = BLACK_STONE
}
players.getWhitePlayer().getPositions().forEach {
board[calculateIndex(it)] = WHITE_STONE
}
println(board.joinToString(separator = ""))
}
private fun calculateIndex(position: Position): Int = 721 + 3 * position.col - 48 * position.row
private fun printTurn(stoneColor: StoneColor, position: Position) {
print(TURN_MESSAGE.format(ColorModel.getString(stoneColor)))
println(LAST_STONE_POSITION_MESSAGE.format(BoardModel.getString(position)))
}
private fun printWinner(stoneColor: StoneColor) {
println(GAME_END_MESSAGE)
println(WINNER_MESSAGE.format(ColorModel.getString(stoneColor)))
}
companion object {
private const val START_MESSAGE = "오목 게임을 시작합니다."
private const val TURN_MESSAGE = "%s의 차례입니다. "
private const val LAST_STONE_POSITION_MESSAGE = "(마지막 돌의 위치: %s)"
private const val GAME_END_MESSAGE = "게임이 종료되었습니다."
private const val WINNER_MESSAGE = "%s의 승리입니다."
private val EMPTY_BOARD: String = """
| 15 ┌──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┐
| 14 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 13 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 12 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 11 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 10 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 9 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 8 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 7 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 6 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 5 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 4 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 3 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 2 ├──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
| 1 └──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┘
| A B C D E F G H I J K L M N O
""".trimMargin()
private const val BLACK_STONE = '●'
private const val WHITE_STONE = '○'
}
}