- Joined
- Aug 10, 2007
- Messages
- 4,267 (0.70/day)
- Location
- Sanford, FL, USA
Processor | Intel i5-6600 |
---|---|
Motherboard | ASRock H170M-ITX |
Cooling | Cooler Master Geminii S524 |
Memory | G.Skill DDR4-2133 16GB (8GB x 2) |
Video Card(s) | Gigabyte R9-380X 4GB |
Storage | Samsung 950 EVO 250GB (mSATA) |
Display(s) | LG 29UM69G-B 2560x1080 IPS |
Case | Lian Li PC-Q25 |
Audio Device(s) | Realtek ALC892 |
Power Supply | Seasonic SS-460FL2 |
Mouse | Logitech G700s |
Keyboard | Logitech G110 |
Software | Windows 10 Pro |
With all the talk about n-puzzle, I wanted to start playing around with a PHP implementation. I've gotten a solver in place, but it only solves it some of the time. Want to get my validator looked at to see if I understood the parity calculation correctly. If it looks sound, then I know the problem is later in the code.
- I'm using explanation given here about how to calculate parity.
- "total" is considered the space square.
- While permutations parity is odd, reshuffle the order until even parity is achieved.
- Loop through the range and for each square, test if value is greater than value of squares further in the sequence. If so, increment the counter.
- I'm using explanation given here about how to calculate parity.
- "total" is considered the space square.
- While permutations parity is odd, reshuffle the order until even parity is achieved.
- Loop through the range and for each square, test if value is greater than value of squares further in the sequence. If so, increment the counter.
PHP:
$tall = 3;
$wide = 3;
$total = $tall * $wide;
$range = range(1, $total);
do {
shuffle($range);
$permutations = 0;
for ( $i = 0; $i < ($total - 1); ++$i ) {
for ( $j = ($i + 1); $j < $total; ++$j ) {
if ( $range[$i] > $range[$j] ) {
++$permutations;
}
}
}
} while ( $permutations & 1 );
//use validated range for board