Skip to content

Feature/task08#7

Open
fpandyz wants to merge 31 commits intomainfrom
feature/task08
Open

Feature/task08#7
fpandyz wants to merge 31 commits intomainfrom
feature/task08

Conversation

@fpandyz
Copy link
Owner

@fpandyz fpandyz commented May 13, 2021

No description provided.

Aleksandr Shinkarev added 30 commits May 13, 2021 18:02

case class Frame(
number: Int,
throws: List[Int] = List(),

Choose a reason for hiding this comment

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

better use List.empty - it doesn't allocate object


case class Frame(
number: Int,
throws: List[Int] = List(),

Choose a reason for hiding this comment

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

throws - bad name, it can be an issue if used in JAVA code

) {
def score = throws.sum;

def isStrike = if (throws.isEmpty) false else throws(0) == 10;

Choose a reason for hiding this comment

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

better use def isStrike = throws.nonEmpty && throws.head == 10 (without unneccesary if\else)

Copy link
Owner Author

Choose a reason for hiding this comment

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

agree

def score = throws.sum;

def isStrike = if (throws.isEmpty) false else throws(0) == 10;
def isSpare = if (throws.length < 2) false else throws(0) + throws(1) == 10;

Choose a reason for hiding this comment

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

Same as previous comment.
Also, the best approach is prevent creating empty lists while parsing(probably create some domain object or add validation while parsing - both cases are ok)

class Game(input: String) {
def score(): Int = {
@tailrec
def calculateScore(

Choose a reason for hiding this comment

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

Алгоритм рабочий, но выглядит ОЧЕНЬ императивно.
В целом задача может быть решена двумя способами - 1) с разбиением на доменные объекты и классы. Тогда первым действием программы должны идти парсинг и валидация и создание непротиворечивого дерева объектов, которое уже потом в цикле обрабатывается на основе правил. В этом случае множество проверок типа isEmpty и т.д отпадают. Например у нас есть объекты Frame, которые содержат список Ball(енум Strike, Spare, Regular) и т.д.
2) Функциональный стиль - когда ты рекурсивно идешь по строке, откусывая по символу за раз. Тут конечно надо подумать как учитывать бонусные фреймы и баллы за страйки, но в этом и суть:)
Чтобы немного навести на мысль - посмотри реализация на хаскелле, там не должно быть сложно для понимания - https://github.com/cskeppstedt/haskell-bowling-kata/blob/master/src/Bowling.hs

Copy link
Owner Author

Choose a reason for hiding this comment

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

Я конечно подумаю ещё, но мне пока не понятна разница между императивным и декларативным. Если можешь статью какую посоветовать, то было бы хорошо.
Кажется, что можно сделать как то иначе, сохраняя рекурсию, но получится ли хоть немного легче в понимании пока не знаю, может и можно, но сложнова-то пока :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants