I am making a program that gennerates a code, as a string of number, then the user inputs a number of the same lenght and the program compares the two sets of numbers.
The programme then generates a guitext for each number and colors it according to wether the number is the correct number and place in the code(green), the number is in the code but not at the given place (yellow), or the number is not pressent in the code (red).
I want the code to only return as manny yellows of a number as times that the number is prsent in the code.
I coded it in c# and I’m using three forloops, but for a reason i can not figur out the innermost forloop never returns true even though it should.
any ideas, solutions are welcome.
I’m posten the funtion where the comparring is happening.
the var line is used for controlling the spacing of the guiTexts
teh var n is the lenght of the code being checked.
void hack () {
//sets input=the player indput
input = codeIn.text;
line++;
//transforms code and input to char arrays
code.ToCharArray(0, n);
input.ToCharArray(0, n);
//creating array for storing the number of used instances of number
numberUsed=new int[10];
//setting each int to 0
for (int i = 0; i < n; i++){
numberUsed*=0;*
-
}*
-
//tjekking input against code*
-
if (input==code){*
-
//if the code is correct the output is "code accepted"*
-
//resets input guiText*
-
codeIn.text="";*
-
//prints the correct code in a guiText*
-
GameObject test01 = new GameObject("GUIText");*
-
test03 = (GUIText)test01.AddComponent(typeof(GUIText));*
-
test03.text = input;*
-
test03.material.color = Color.green;*
_ test03.transform.position = new Vector3(0.1f,0.8f-line*0.025f,0f);_
-
line++;*
-
//prints the "code accepted" in a guiText*
-
GameObject test04 = new GameObject("GUIText");*
-
test03 = (GUIText)test04.AddComponent(typeof(GUIText));*
-
test03.text = "code accepted";*
_ test03.transform.position = new Vector3(0.1f,0.8f-line*0.025f,0f);_
-
Log(timer.ToString());*
-
}else{*
-
for (int i = 0; i < n; i++){*
-
string[] inputCorrectness = new string[n];*
-
for (int j = 0; j < n; j++){*
-
//if the number is correct the code returns green*
if (code_==input*){
inputCorrectness = “green”;
//raises the count of uses of the number by one*
numberUsed[input*-48]++;
break;*_
_ //if the number(input*) is present at another location in the code
}else if(code[j]==input){*_
* //cheks the number of used instances of the digit*
if (numberUsed[input_-48]>=numberCount[input*-48]){
inputCorrectness = “red”;
break;
} else {
for (int k=0; k<n; k++){
print(“derp”);
/if the input is also input at another place in the code (input[k])
and that input is equal to the code, set current inputcolor to red/
if (code[k]==input[k] && input[k]==input){
inputCorrectness = “red”;
print(“herp”);
break;
//else set current inputcolor to yellow
}else{
inputCorrectness = “yellow”;
numberUsed[input-48]++;
break;
}
}
//breaks outer forloop else the texts turns back to red.
break;
}
//if the number is not in the code it returns red
} else {*_
_ inputCorrectness = “red”;
* } *_
* } *
_ //print(numberCount[input*-48]);
//print(numberUsed[input-48]);*_
//print(input_+inputCorrectness*);
codeIn.text=“”;
//prints the number with the earlyer given color*
* GameObject test01 = new GameObject("GUIText " + i);
test02 = (GUIText)test01.AddComponent(typeof(GUIText));
test02.text = “”+input;
test02.transform.position = new Vector3(0.1f+i0.01f,0.8f-line0.025f,0f);
switch(inputCorrectness){
case “green”:
test02.material.color = Color.green;
break;
case “yellow”:
test02.material.color = Color.yellow;
break;
case “red”:
test02.material.color = Color.red;
break;
}
}
}
} *_