/// <summary> /// Compares the given input code to the colors set in here. /// </summary> /// <param name="other">ColorCode that is compared</param> /// <returns>A color representation of the number of correctly colored and placed inputs. /// Cyan means correct input color at the right place, white that there is a correct color. /// </returns> public ColorCode compareCodes(ColorCode other) { ColorCode indicator = new ColorCode(); int index; int correctlyPlaced = 0; int rightColors = 0; GT.Color thisColor; GT.Color[] testcase = { colors[0], colors[1], colors[2], colors[3] }; //First find all correctly placed colors... for (index = 0; index < 4; index++) { thisColor = other.getColor(index); if (testcase[index].Equals(thisColor)) { correctlyPlaced++; //... and remove them, to not test against them again testcase[index] = correct; } } //Now find colors that are present in wrong places... for (index = 0; index < 4; index++) { if (testcase[index] != correct) //already found { thisColor = other.getColor(index); for (int i = 0; i < 4; i++) { if ( testcase[i].Equals(thisColor) ) { //found an incorrect placed color rightColors++; testcase[i] = semiRight; //remove and don't test for this index further... break; } } } } //fill the to be returned indicator... index = 0; for (int i = 0; i < correctlyPlaced; i++) { indicator.setColor(index, correct); index++; } for (int i = 0; i < rightColors; i++) { indicator.setColor(index, semiRight); index++; } return indicator; }
/// <summary> /// checks if ColorCodes match exactly, for game finish checks /// </summary> /// <param name="other">Colorsequence to check against</param> /// <returns>true if identical</returns> public bool isEqual(ColorCode other) { for (int i = 0; i < 4; i++) { if ( !colors[i].Equals( other.getColor(i) ) ) { return false; } } return true; }
/// <summary> /// changes the color of the given color code at a certain position /// </summary> /// <param name="d"> the direction (UP or DOWN)</param> /// <param name="code"> the color code</param> /// <param name="pos"> position of the color to be changed in the code </param> private void changeColor(direction d, ColorCode code, int pos) { if (d == direction.UP) { code.setColor(pos, ColorCode.getNextColor(code.getColor(pos))); } else { code.setColor(pos, ColorCode.getPrevColor(code.getColor(pos))); } }