Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The code listed below is a slightly optimized segment from the function called attemptToInchTowardSolution: The change is the addition of the break statement which exits the doubly-nested loop as...
#1: Initial revision
The code listed below is a slightly optimized segment from the function called
**attemptToInchTowardSolution:**
The change is the addition of the ```break``` statement which exits the doubly-nested loop as soon as a duplicate is found. With this ```break``` statement, the program no longer wastes time looking for additional duplicates.
This change doesn’t make the program super fast but at least it is somewhat faster.
```javascript
// Randomly pick a set of tiles to be permuted and/or rotated. ...
// ... Hopefully this modified tile set is closer to a collection of monochromatic closed loops.
pickRow = new Array(PERMUTE_ROTATE_SIZE);
pickCol = new Array(PERMUTE_ROTATE_SIZE);
do {
for( pickSub=0; pickSub<PERMUTE_ROTATE_SIZE; pickSub++ ) {
pickRow[pickSub] = floor(random(gridSize));
pickCol[pickSub] = floor(random(gridSize));
}
// Check for duplicate tile positions
duplicateTiles = false; // So far, no duplicate tiles
CHECK_FOR_DUPLICATES_LOOP:
for( checkSub_1=0; checkSub_1<PERMUTE_ROTATE_SIZE-1; checkSub_1++ ) {
for( checkSub_2=checkSub_1+1; checkSub_2<PERMUTE_ROTATE_SIZE; checkSub_2++ ) {
if( pickRow[checkSub_1] === pickRow[checkSub_2] &&
pickCol[checkSub_1] === pickCol[checkSub_2] ) {
duplicateTiles = true; // Drats!
break CHECK_FOR_DUPLICATES_LOOP;
}
}
}
} while (duplicateTiles);
```
