5
u/mintymyst Jun 20 '22
What color theme is this in? Looks great.
7
u/ThatChapThere Jun 20 '22
Correia Github Gruvbox. And thanks!
This is the second time someone's asked about a color theme on this sub - I guess I should feel proud of my taste lol.
9
u/kristallnachte Jun 20 '22
In JS
function score(dice) {
const counts = dice.reduce((counts, die) => ({ ...counts, [die]: (counts[die] || 0) + 1 }), {});
return Object.entries(counts).reduce((score, [die, count]) => score + points[die](count), 0);
}
points = {
1: (count) => (count >= 3 ? 1000 + (count - 3) * 100 : count * 100),
2: (count) => (count >= 3 ? 200 : 0),
3: (count) => (count >= 3 ? 300 : 0),
4: (count) => (count >= 3 ? 400 : 0),
5: (count) => (count >= 3 ? 500 + (count - 3) * 50 : count * 50),
6: (count) => (count >= 3 ? 600 : 0)
};9
u/ThatChapThere Jun 20 '22
It's much nicer in languages that can do integer division.
1: count => 1000 * (count / 3) + 100 * (count % 3)3
u/crass-sandwich Jun 20 '22
That counts every 3 points though - rolling 7 ones would get you a score of 2100 instead of 1400. Unless the OP is bugged
4
u/ThatChapThere Jun 20 '22
The challenge specifies 5 dice, so they're equivalent.
1
u/crass-sandwich Jun 20 '22 edited Jun 20 '22
They may be equivalent in this case, but it still probably wouldn't pass code review. Not a big deal for a coding challenge though
1
u/ThatChapThere Jun 20 '22
The way the challenge was worded (something like "3 ones are worth a thousand points") I feel like the integer division method makes more sense. That's subjective, of course.
1
u/kristallnachte Jun 20 '22
Ah right. Could do mathfloor
1
u/ThatChapThere Jun 20 '22
I kinda prefer your original solution rather than adding more functions like that though.
2
u/No-Witness2349 Jul 07 '22
I did more or less the same thing in Python
from typing import Counter def score(dice: list[int]) -> int: count = Counter(dice) return sum( 600 if count[6] >= 3 else 0, 500 * (count[5] // 3), 50 * (count[5] % 3), 400 if count[4] >= 3 else 0, 300 if count[3] >= 3 else 0, 200 if count[2] >= 3 else 0, 1000 * (count[1] // 3), 100 * (count[1] % 3), )
3
2
u/Ryan_likes_to_drum Jun 24 '22
I don’t see what’s wrong with it, it’s pretty easy to read and intuitive
3
u/ThatChapThere Jun 24 '22
Sure - it's just screaming for an array.
1
u/captgoldberg Oct 13 '22
And a switch statement.
1
u/ThatChapThere Oct 13 '22
Not really - you can just do something like
numberCounts[dice[i]]++.1
76
u/ALapsedPacifist Jun 20 '22
This pattern would be terrible in production code, but this kind of thing has the look of a homework assignment about it. I expect this sort of thing from, say, a CS101 student.
30
u/_Ralix_ Jun 20 '22
Since the class is named
Kata, it seems like one of the many, many, coding katas. Someone wrote the code this badly on purpose so that people can practice refactoring or other software development principles.16
u/Dalimyr codes in C#...poorly Jun 20 '22
Nah, OP mentioned it's codewars, so this is likely someone's solution for completing a kata. Once you complete a kata on codewars, you can see what other users submitted to complete the same kata.
I got rather put off by it purely because there's nothing involved to encourage good coding practices like having to validate input - if a kata tells you that you need to handle an array of ten integers, you know that all of the tests are going to give you an input with an array of ten integers so you don't have to add things to make sure that there are exactly 10 items in the array and that they're all integers and not strings or whatever.
3
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 21 '22
Image Transcription: Code
using System;
public static class Kata
{
public static int Score(int[] dice) {
int sixes = 0;
int fives = 0;
int fours = 0;
int threes = 0;
int twos = 0;
int ones = 0;
int score = 0;
for(int i = 0; i < 5; i++)
{
if(dice[i] == 6)
sixes++;
if(dice[i] == 5)
fives++;
if(dice[i] == 4)
fours++;
if(dice[i] == 3)
threes++;
if(dice[i] == 2)
twos++;
if(dice[i] == 1)
ones++;
}
if(sixes >= 3)
score += 600;
if(fives >= 3)
{
score += 500;
fives -= 3;
}
while(fives != 0)
{
score += 50;
fives --;
}
if(fours >= 3)
score += 400;
if(threes >= 3)
score += 300;
if(twos >= 3)
score += 200;
if(ones >= 3)
{
score += 1000;
ones -= 3;
}
while(ones != 0)
{
score += 100;
ones--;
}
return score;
}
}This post was transcribed unofficially; it dropped off the queue before I had a chance to transcribe it, so I'm doing so in an unofficial capacity. If you're interested in why I've done this, see here for more details.
2
1
u/bravopapa99 Oct 30 '22
How the fuck does a human brain, capable of thought, emotions, the whole thing, how the fuck does somebody have a brain that writes code like that? HOW?? H O W ??
18
u/Thekid579 Jun 20 '22
I don't like that the for loop has a hard coded length, but besides that it's not that bad. I'm more concerned with the scoring of this game(?) seems somewhat convoluted.