HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

"Dice on a Yacht" game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamediceyacht

Problem

I wrote a small program to address some assignment tasks related to making a game called 'Dice on a Yacht'. I sent the code to be reviewed by a company I was applying for, and I have not gotten the job (as I had expected shortly after submitting code and thinking over it further). I realize that I need to start preparing more for my next job and so I have started reading material related to algorithm design and analysis (which I suspect may be my strongest weak point) and as well as design patterns and language specific material (mostly C# but also C++).

This is written in Unity C# (the job was for a mobile Unity position). I'm just curious what issues you can find and what recommendations there are regarding the code or anything else.

For more info on the assignment itself: I was tasked with writing two functions getScore() and getSuggestions(). getScore should retrieve the appropriate score for an array that is passed in, and the final score depends on what category is also passed in (i.e small straight, four of a kind, twos, etc. Check out Wikipedia for scoring rules). I wrote a function getDifferences which simply gets the differences between elements and adds them into an array that is then used by three other non-numerical categories for calculating score. Not too much code was expected to be written, but I figured the getDifferences function was a nice way for dealing with the special categories in a reusable manner. The getSuggestions function is one I spent less time on, and is definitely one that would need more improvement.

```
using UnityEngine;
using System.Collections;

public class AssessmentExam: MonoBehaviour {

public enum Category{
Ones,
Twos,
Threes,
Fours,
Fives,
Sixes,
Sevens,
Eights,
ThreeOfAKind,
FourOfAKind,
FullHouse,
SmallStraight,
LargeStraght,
AllDifferent,
Chance,
AllSame
}

// Use t

Solution

I agree that the getSuggestions method has significant room for improvement. The major thing I notice is the method name is plural, yet the method only returns a single item. I would return a collection of suggestions and order them by score or something like that.

Why doesn't your code handle all the categories?

Method names should be UpperCamelCase. That is, GetScore, GetSuggestions, and GetDifferences.

The spacing is messed up in many places. For example here:

}
    }
        return score;
}


The braces should all be indented by four more spaces, and return score; should not be indented more than the above brace.

array isn't a very descriptive name. I'd go with something like DieNumbers.

Most of your loops can be replaced with LINQ, which is more concise, safer, and easier to read, in my opinion. For example:

for(int i = 0; i < array.Length; i++){
    if(array[i] == intCategory){
        score += intCategory;
    }
}


Can be replaced with:

score = array.Where(x => x == intCategory).Sum();

Code Snippets

}
    }
        return score;
}
for(int i = 0; i < array.Length; i++){
    if(array[i] == intCategory){
        score += intCategory;
    }
}
score = array.Where(x => x == intCategory).Sum();

Context

StackExchange Code Review Q#105492, answer score: 3

Revisions (0)

No revisions yet.