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 this for initialization
void Start () {
//Example Code to test out the functions in Unity
//int[] arr = {2,2,2,2,7};//{0,2,4,6,3};
//Debug.Log (getScore(Category.SmallStraight,arr));
//Debug.Log (getSuggestions(arr));
}
// Update is called once per frame
void Update () {
}
public int getScore(Category category, int[] array){
int intCategory = (int)category + 1; //Casting enum to int to seperate category types. By default enums start with index 0.
int score = 0; int count = 0;
int[] diffArray;
if(intCategory < 9){ //This handles categories from 'Ones' to 'Eights'.
for(int i = 0; i < array.Length; i++){
if(array[i] == intCategory){
score += intCategory;
}
}
}
else{ //Handles the other categories (SmallStraight, AllDifferent, AllSame);
switch(category){
case Category.SmallStraight:
//If there are four die in sequence, the score is 30. Otherwise, the score is 0.
diffArray = getDifferences(array); //getDifferences returns an array of elements that each represent the difference between element pairs.
count = 0;
for(int i = 0; i < diffArray.Length; i++){
if((diffArray[i] == 1)){ //Elements pairs that differ by one are considered part of a sequence
count++;
}else{
count = 0; //Reset the count if a sequence is interrupted
}
if(count == 3){ //Assign score to 30 if all 4 die are in sequence (loop
score = 30;
}else if(count > 3){ // Prevents largeStraights from scoring as a smallStraight
score = 0;
}
}
break;
case Category.AllDifferent:
//If all elements in the passed in array are unique, the score is 40. Otherwise, the score is 0.
System.Array.Sort(array); //Sort the elements so that duplicate elements evaluate to 0 when the differences are calculated.
diffArray = getDifferences(array); // Reusing the 'getDifference' function used in the previous category.
count = 0;
for(int i = 0; i < diffArray.Length; i++){
if(diffArray[i] != 0){ // If there is a difference element of 0, there are duplicate elements in the array. Don't count these.
count++;
}
}
if(count == diffArray.Length){ //If count equals the length of the array, then all elements are unique.
score = 40;
}
break;
case Category.AllSame:
//If all elements are the same, return 50. Otherwise, return 0.
diffArray = getDifferences(array); //Get the differences
count = 0;
for(int i = 0; i < diffArray.Length; i++){
if(diffArray[i] == 0){ //If the difference between an element pair is 0, increase count by one.
count++;
}
}
if(count == diffArray.Length){ //if the count equals to the length of the array, then all elements are the same.
score = 50;
}
break;
}
}
return score;
}
public Category getSuggestions(int[] array){
//Testing out available enumarators that would work with my example code.
// This was a fast way of testing out the getSuggestions function. It can be improved to evaluate the actual best 'number category' for a given array.
//Categories are added into an array from best scoring to least to quickly evaluage array without regards to array specifics. ({2,2,2,2,7} would be 8 for Twos, instead of 7 when evaluated with Sevens.
//In that array example, the algorithm would return 'Sevens' since it is the category of highest priority that does not return 0.
Category[] categoryTypes = {Category.AllSame, Category.AllDifferent, Category.SmallStraight, Category.Eights, Category.Sevens, Category.Sixes, Category.Fives,
Category.Fours, Category.Threes, Category.Twos, Category.Ones};
foreach(Category category in categoryTypes){
int score = getScore(category, array);
if(score > 0){
return category;
}
}
return Category.Ones;
}
private int[] getDifferences(int[] arr){ //Function that is reused by the categories above. Can be used for fullHouse and a few other categories.
int[] differenceArray = new int[4];
for (int i = 1; i < arr.Length; i++){
int difference = arr[i] - arr[i-1];
differenceArray[i-1] = difference;
}
return differenceArray;
}
}