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 28bb0cd59841850f221d69b1df1039481489369a
5 changes: 5 additions & 0 deletions src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import controller.OmokController

fun main() {
OmokController().start()
}
12 changes: 12 additions & 0 deletions src/main/kotlin/listener/OmokStartEndEventListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package listener

import domain.Position
import domain.StoneColor
import domain.player.Players

interface OmokStartEndEventListener {
fun onStartTurn(stoneColor: StoneColor, position: Position)
fun onEndTurn(players: Players)
fun onStartGame()
fun onEndGame(stoneColor: StoneColor)
}
9 changes: 9 additions & 0 deletions src/main/kotlin/listener/OmokTurnEventListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package listener

import domain.Position
import domain.StoneColor

interface OmokTurnEventListener {
fun onTakeTurn(stoneColor: StoneColor): Position
fun onNotPlaceable()
}
12 changes: 12 additions & 0 deletions src/main/kotlin/view/BoardModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package view

import domain.Position

object BoardModel {
private const val RANGE_MIN = 'A'
private const val RANGE_MAX = 'O'

private val range = (RANGE_MIN..RANGE_MAX).toList()
fun getString(position: Position) = range[position.col] + (Position.POSITION_RANGE.max() - position.row).toString()
fun getColInt(col: String) = range.indexOf(col.toCharArray()[0]) + 1
}
13 changes: 13 additions & 0 deletions src/main/kotlin/view/ColorModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package view

import domain.StoneColor

object ColorModel {
private const val BLACK = "흑"
private const val WHITE = "백"

fun getString(color: StoneColor): String = when (color) {
StoneColor.BLACK -> BLACK
StoneColor.WHITE -> WHITE
}
}
42 changes: 42 additions & 0 deletions src/main/kotlin/view/InputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package view

import domain.Position
import domain.Position.Companion.POSITION_RANGE
import domain.StoneColor
import listener.OmokTurnEventListener

class InputView : OmokTurnEventListener {
override fun onTakeTurn(stoneColor: StoneColor): Position = askPosition()

override fun onNotPlaceable() {
reAskPosition(CANT_PLACE_STONE_ERROR_MESSAGE)
}

private fun askPosition(): Position {
print(ASK_POSITION_MESSAGE)
val input = readln()
if (input.length !in POSITION_INPUT_RANGE)
return reAskPosition(INVALID_FORMAT_ERROR_MESSAGE)

val col = BoardModel.getColInt(input.first().toString())
val row = input.substring(ROW_INPUT_SIZE).toIntOrNull()
if (row == null || row !in POSITION_RANGE || col !in POSITION_RANGE)
return reAskPosition(INVALID_FORMAT_ERROR_MESSAGE)
Comment on lines +19 to +25

Choose a reason for hiding this comment

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

InputView가 로직에 대한 처리를 너무 자세하게 알고 있지는 않을까요?
행과, 열에 해당하는 위치를 만드는 것은 Position 객체가 해야할 책임으로 볼 수 있지 않을까요?

논리적인 오류가 아니라면 null을 반환하도록 만들어 볼 수 있겠습니다.

https://medium.com/@galcyurio/kotlin%EC%97%90%EC%84%9C%EC%9D%98-%EC%98%88%EC%99%B8-%EC%B2%98%EB%A6%AC-%EB%B0%A9%EB%B2%95-48a5cd94a4e6

return Position(row, col)
}

private fun reAskPosition(message: String): Position {
println(message)
return askPosition()
}

companion object {
private const val ASK_POSITION_MESSAGE = "위치를 입력하세요: "
private const val INVALID_FORMAT_ERROR_MESSAGE = "포맷에 맞지 않는 입력값입니다."
private const val CANT_PLACE_STONE_ERROR_MESSAGE = "해당 위치에는 오목알을 둘 수 없습니다."
private const val MIN_POSITION_INPUT_SIZE = 2
private const val MAX_POSITION_INPUT_SIZE = 3
private val POSITION_INPUT_RANGE = MIN_POSITION_INPUT_SIZE..MAX_POSITION_INPUT_SIZE
private const val ROW_INPUT_SIZE = 1
}
}
82 changes: 82 additions & 0 deletions src/main/kotlin/view/OutputView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package view

import domain.Position
import domain.StoneColor
import domain.player.Players
import listener.OmokStartEndEventListener

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 = '○'
}
}