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.
int sixes etc. just seems fundamentally broken to me. That part should be a loop. The rest of the code is fine.
You could argue that there's "only" 6 of those variables, so it's fine, but if it went up to, say, "twenties" it would fit this sub perfectly. Looking again I guess the code isn't so bad, but that still makes me uncomfortable.
What's the limit on this kind of non-looped system before it's bad? 10? 20? 30?
Well, it could be super simplified by using a map (key: 1 to 6) -> (value: counter) to count the dice outputs. Then you get the final score with whichever rules you want with quick reads from the map, which I guess is the idea in this kata...
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.
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.
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.
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.
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.
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.
5
u/mintymyst Jun 20 '22
What color theme is this in? Looks great.
9
u/kristallnachte Jun 20 '22
In JS
3
u/[deleted] Jun 26 '22
What editor is this
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
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.
3
u/Lord-Sneakthief Jun 20 '22
TFW you're about 60% of the way to understanding arrays
3
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 21 '22
Image Transcription: Code
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
u/ConflictDifferent Jun 20 '22
They completely farkled the code up
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 ??