I will try to explain my issue. I'm trying to create a chessengine in PHP (just for fun :-)) The integers in the code simply returns valid moves (for simplicity - in real code it's objects and movement pattern depending on which piece it's about)
I'm looking for a way to effectively search through an array. By effectively I mean as fast as possible. Look at my comment in code below "IS it possible to break out of loop without going through all 1000 values?" I hope the comments would try to explain what I want to achieve. I'm just looking for ideas to optimize below code, not full code :-)
//This is for demonstrating
//1000 values to go through
$moves_maybe_valid = [];
foreach(range(1,1000) as $nr) {
$moves_maybe_valid[] = $nr;
}
shuffle($moves_maybe_valid);
//Go through possible values
$move_checked = [];
$nr=0;
foreach($moves_maybe_valid as $mmv) {
$move_is_valid = check_move($mmv);
//Check if not in checked array
if ($move_is_valid === false && !in_array($mmv, $move_checked)) {
//Add to checked move array
$move_checked[] = $mmv;
}
//IS it possible to break out of loop without
//going through all 1000 values?
}
//for demonstration purpose only
//numbers (5,6) that returns true are unknown until an
//an actual check is done in this function
function check_move($nr) {
if ($nr == 5 || $nr == 6) {
return true;
}
return false;
}
Aucun commentaire:
Enregistrer un commentaire