PDA

View Full Version : [AS2] checking values in an array


drumn4life0789
09-01-2008, 10:49 PM
Ok basically I am creating a sudoku game. If you don't know what it is, it will be ok. Basically I need to be able to test what the values of an array are.

for this instance lets say that I have on array with 9 values which are text boxes called a1-a9

the user can put numbers in the boxes 1-9. I want to click a button and have flash check and see if all the number from 1-9 are there and only once instance of each number is in a1-a9. does anyone know how this can be accomplished.

dialectric
09-02-2008, 03:30 PM
Hi,

Try pushing the values from the boxes into an array, user array, then checking this against an internal array containing the numbers 1-9, something like the code below.

- dialectric



user_array = [1,2,3,4,5]
internal_checking_array = [1,2,3,4,5]

for(j= 0; j< internal_checking_array.length; i++){
current_number = internal_checking_array[j];
is_in_user_array = false;
for (i = user_array.length; i>0; i--){
if ((user_array[i] == current_number) && (is_in_user_array == true)){
trace ("number has appeared twice")
break;
};
if ((user_array[i] == current_number) && (is_in_user_array != true)){
is_in_user_array = true;
};
if ((i == 1) && (user_array[i] != current_number) && (is_in_user_array != true)){
trace ("number has not appeared")
break;
};
};
//to reach this point, no breaks have occured
//so all numbers are present with no duplicates
};

drumn4life0789
09-02-2008, 05:21 PM
ok thanks I will try something like this. But just one question. the word break means that do the code and if the code prior to this is true then stop and start the loop again right?

dialectric
09-03-2008, 12:08 AM
Hi,

Yes, mostly. Break will drop out of the current for loop completely. I'm not sure I can explain it more clearly, though someone with a better understanding of it might be able to help. The code could be rewritten without them, using else if{ statements instead...

-dialectric