Skip to content

Instantly share code, notes, and snippets.

@dmdeller
Created January 15, 2019 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmdeller/d21de1ad0493afb544a7c23cddc647ce to your computer and use it in GitHub Desktop.
Save dmdeller/d21de1ad0493afb544a7c23cddc647ce to your computer and use it in GitHub Desktop.
Sphere Break Solver
<!--
Sphere Break Solver
version 1.0.2 DESYNC
Copyright 2004-2007 D.M.Deller (except for the 'script
execution time segment' by David Lancini)
This file is part of Sphere Break Solver.
Sphere Break Solver is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Sphere Break Solver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Sphere Break Solver; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
You can view the GNU General Public license at:
http://www.gnu.org/copyleft/gpl.html
The author is not associated with Square Enix in any way.
-->
<html>
<head>
<title>Sphere Break Solver</title>
<style><!--
.base {
background-color: gold;
}
.border {
background-color: lightblue;
}
.table {
font-family: Arial,Helvetica,Geneva;
font-size: 8pt;
}
--></style>
</head>
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
// EXECUTION TIME SCRIPT SEGMENT 1
// This part of the script was written by David Lancini and is not
// subject to either DMD's copyright or the GNU General
// Public License. It is understood that the following script
// segment is 'free for use on a personal site but not for commercial
// gain'. For more information, please visit the author's website:
// http://www.sdjl.co.uk/free-php/
// Pop these two functions at the top
// of your page/s
function getmicrotime() {
$arr = explode(" ",microtime());
return $arr[0] + $arr[1];
}
// Stop Timer
function StopTimer($start) {
$stop = getmicrotime();
$sr = 'Generated in '.substr($stop - $start, 0, 5).' seconds';
Return $sr;
}
// Put this underneath the above two functions.
// This is where timing will start from
$start = getmicrotime();
// END OF EXECUTION TIME SEGMENT 1
// Putting HTTP POST values into an array, if they exist... if not, fill
// with zeroes
if ($_POST["a"] == 'b') { // if data exists, let's put it into an array
$base = array(1 => $_POST["base1"], $_POST["base2"], $_POST["base3"], $_POST["base4"]);
// base coins
$border = array(1 => $_POST["border1"], $_POST["border2"], $_POST["border3"], $_POST["border4"],
$_POST["border5"], $_POST["border6"], $_POST["border7"], $_POST["border8"], $_POST["border9"],
$_POST["border10"], $_POST["border11"], $_POST["border12"]);
// border coins
$allcoins = array (1 => $_POST["border1"], $_POST["border2"], $_POST["border3"], $_POST["border4"],
$_POST["border5"], $_POST["border6"], $_POST["border7"], $_POST["border8"], $_POST["border9"],
$_POST["border10"], $_POST["border11"], $_POST["border12"],
$_POST["base1"], $_POST["base2"], $_POST["base3"], $_POST["base4"]);
// all coins; borders are listed first because they are preferrable for scoring points
}
else { // else, fill it with zeroes
$base = array(1 => 0,0,0,0);
$border = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
}
?>
<form action='sphere2.php' method='post'>
Number of coins to be used:
<select name='numcoins'>
<option value='2' <?php if (($_POST["numcoins"] == 2) || ($_POST["numcoins"] == '')) { echo 'selected'; } ?> >2</option>
<option value='3' <?php if ($_POST["numcoins"] == 3) { echo 'selected'; } ?> >3</option>
<option value='4' <?php if ($_POST["numcoins"] == 4) { echo 'selected'; } ?> >4</option>
<!--
<option value='5' <?php if ($_POST["numcoins"] == 5) { echo 'selected'; } ?> >5</option>
<option value='6' <?php if ($_POST["numcoins"] == 6) { echo 'selected'; } ?> >6</option>
-->
</select>
<br><br>
Fill in the grid...
<br><br>
<center>
<table border=0 cellpadding=2 cellspacing=0>
<tr>
<td><input type=text name='border1' value='<?php echo $border[1]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='border2' value='<?php echo $border[2]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='border3' value='<?php echo $border[3]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='border4' value='<?php echo $border[4]; ?>' size=1 limit=1 class='border'></td>
</tr>
<tr>
<td><input type=text name='border5' value='<?php echo $border[5]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='base1' value='<?php echo $base[1]; ?>' size=1 limit=1 class='base'></td>
<td><input type=text name='base2' value='<?php echo $base[2]; ?>' size=1 limit=1 class='base'></td>
<td><input type=text name='border6' value='<?php echo $border[6]; ?>' size=1 limit=1 class='border'></td>
</tr>
<tr>
<td><input type=text name='border7' value='<?php echo $border[7]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='base3' value='<?php echo $base[3]; ?>' size=1 limit=1 class='base'></td>
<td><input type=text name='base4' value='<?php echo $base[4]; ?>' size=1 limit=1 class='base'></td>
<td><input type=text name='border8' value='<?php echo $border[8]; ?>' size=1 limit=1 class='border'></td>
</tr>
<tr>
<td><input type=text name='border9' value='<?php echo $border[9]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='border10' value='<?php echo $border[10]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='border11' value='<?php echo $border[11]; ?>' size=1 limit=1 class='border'></td>
<td><input type=text name='border12' value='<?php echo $border[12]; ?>' size=1 limit=1 class='border'></td>
</tr>
</table>
<font size='1'><br><i>leave 0 to indicate no coin. do not leave base coins as 0!<br>tip: use the tab key to quickly move between fields</i><br><br></font>
<input type='hidden' name='a' value='b'>
<input type='submit' value='Generate'>
<input type='button' value='Erase grid' onClick='location.replace("sphere2.php");'>
</center>
</form>
<?php
// Okay, are you ready for some MAD PHP ACTION?!?!?!
// First, I'm gonna make sure they input data. If not, kill the script, cos
// they haven't done anything yet.
if ($_POST["a"] != 'b') { exit('</body></html>'); }
// Argh, this is going to be hard. Okay, don't get stressed, one step
// at a time.
// We want higher numbers first... so let's sort the border coins, in a
// separate array
$rborder = $border;
rsort($rborder); // REMEMBER! Now that it's sorted, the array starts at 0!
// Now we'll add the base coins onto the end of this array
// There's probably a more efficient way to do this, I just don't know it
// We don't need to sort the base coins since their values don't fill quota
// SC stands for Sorted Coins btw.. a little bit of shorthand there
$sc = array(1 => $rborder[0], $rborder[1], $rborder[2], $rborder[3], $rborder[4],
$rborder[5], $rborder[6], $rborder[7], $rborder[8], $rborder[9], $rborder[10],
$rborder[11], $base[1], $base[2], $base[3], $base[4]);
// ismultiple() is a simple math function that determines if $x is a
// multiple of $y
function ismultiple($x,$y) {
if (is_int($x / $y)) { return TRUE; } // if you get an integer (no decimals)
// when dividing, it must be a multiple
else { return FALSE; }
}
// coincolour() returns the correct colour of a coin for the $allborder array
// value $coin; since border coins are listed first in that array, 1-12 will
// return 'blue' for the border colour, and 13-16 will return 'gold' for the
// base colour
function coincolour($coin) {
if (($coin >= 1) && ($coin <= 12)) { return 'lightblue'; }
else if (($coin >= 13) && ($coin <= 16)) { return 'gold'; }
else { return 'red'; } // just in case i made a mistake
}
// findseq() is the magical function that fixes everything. Yeah, right.
// argument: $coresphere is the theoretical core sphere value for which
// we're finding a sequence.
function findseq($coresphere) {
// Let's fetch our arrays, I'm not dealing with this 'passing' business
global $base;
global $border;
global $allcoins;
global $sc;
// Okay, I'm sure there's a better way to do this than an 'if' statement
// and a bunch of copy/paste, but my head is already spinning so give
// me a break okay?
if ($_POST["numcoins"] == '2') { // if 2 shalt be the number of thy counting
for($a = 13; $a <= 16; $a++) { // $a is the base coin, let's try them in order
for($b = 1; $b <= 16; $b++) { // $b is all coins (border+base), border first
if ( ($a != $b) && ($sc[$b] != 0) && (!ismultiple($sc[$a],$coresphere))
&& (ismultiple(($sc[$a] + $sc[$b]),$coresphere)) ) {
return "<span style='background-color: ".coincolour($a)."'>&nbsp;".$sc[$a]."&nbsp;</span> + <span style='background-color: ".coincolour($b)."'>&nbsp;".$sc[$b]."&nbsp;</span></td><td align='center'>".($allcoins[$a] + $sc[$b]);
}
}
}
return "<font color='red'>No possible sequence found!! :(</font>";
}
else if ($_POST["numcoins"] == '3') { // '5!' '3, sir!' '3!!'
for($a = 13; $a <= 16; $a++) { // $a is the base coin, let's try them in order
for($b = 1; $b <= 16; $b++) { // $b is all coins (border+base), border first
for ($c = 1; $c <= 16; $c++) { // same as $b
if ( ($a != $b) && ($b != $c) && ($a != $c) && ($sc[$b] != 0) && ($sc[$c] != 0)
&& (!ismultiple($sc[$a],$coresphere)) && (!ismultiple(($sc[$a] + $sc[$b]),$coresphere))
&& (ismultiple(($sc[$a] + $sc[$b] + $sc[$c]),$coresphere)) ) {
return "<span style='background-color: ".coincolour($a)."'>&nbsp;".$sc[$a]."&nbsp;</span> + <span style='background-color: ".coincolour($b)."'>&nbsp;".$sc[$b]."&nbsp;</span> + <span style='background-color: ".coincolour($c)."'>&nbsp;".$sc[$c]."&nbsp;</span></td><td align='center'>".($sc[$a] + $sc[$b] + $sc[$c]);
}
}
}
}
return "<font color='red'>No possible sequence found!! :(</font>";
}
else if ($_POST["numcoins"] == '4') { // 4 shalt not be the number of thy counting (well, it might)
for($a = 13; $a <= 16; $a++) { // $a is the base coin, let's try them in order
for($b = 1; $b <= 16; $b++) { // $b is all coins (border+base), border first
for ($c = 1; $c <= 16; $c++) { // same as $b
for ($d = 1; $d <= 16; $d++) { // same as $c
if ( ($a != $b) && ($a != $c) && ($a != $d) && ($b != $c) && ($b != $d) && ($c != $d)
&& ($sc[$b] != 0) && ($sc[$c] != 0) && ($sc[$d] != 0)
&& (!ismultiple($sc[$a],$coresphere)) && (!ismultiple(($sc[$a] + $sc[$b]),$coresphere))
&& (!ismultiple(($sc[$a] + $sc[$b] + $sc[$c]),$coresphere))
&& (ismultiple(($sc[$a] + $sc[$b] + $sc[$c] + $sc[$d]),$coresphere)) ) {
return "<span style='background-color: ".coincolour($a)."'>&nbsp;".$sc[$a]."&nbsp;</span> + <span style='background-color: ".coincolour($b)."'>&nbsp;".$sc[$b]."&nbsp;</span> + <span style='background-color: ".coincolour($c)."'>&nbsp;".$sc[$c]."&nbsp;</span> + <span style='background-color: ".coincolour($d)."'>&nbsp;".$sc[$d]."&nbsp;</span></td><td align='center'>".($sc[$a] + $sc[$b] + $sc[$c] + $sc[$d]);
}
}
}
}
}
return "<font color='red'>No possible sequence found!! :(</font>";
}
// and as we all know, 5 is right out.
// what? never seen Monty Python? DORK.
return "wtf error";
}
// Let's make sure the user didn't make silly errors when inputting
if (($base[1] == 0) || ($base[2] == 0) || ($base[3] == 0) || ($base[4] == 0)) {
exit("<font color='red'>Error: one or more base coins is set to zero. Zero means they don't exist. There are always four base coins. Please double-check your data.</font></body></html>");
}
?>
<center>
<table border=1 cellpadding=2 cellspacing=0 class='table' width='250'>
<tr>
<td align='center' width='15%'><b>Core</b></td>
<td align='center'><b>Sequence</b></td>
<td align='center' width='15%'><b>Sum</b></td>
</td>
<tr>
<td align='center'>1</td>
<td align='center'>*</td>
<td align='center'>*</td>
</tr>
<tr>
<td align='center'>2</td>
<td align='center'><?php echo findseq(2); ?></td>
</tr>
<tr>
<td align='center'>3</td>
<td align='center'><?php echo findseq(3); ?></td>
</tr>
<tr>
<td align='center'>4</td>
<td align='center'><?php echo findseq(4); ?></td>
</tr>
<tr>
<td align='center'>5</td>
<td align='center'><?php echo findseq(5); ?></td>
</tr>
<tr>
<td align='center'>6</td>
<td align='center'><?php echo findseq(6); ?></td>
</tr>
<tr>
<td align='center'>7</td>
<td align='center'><?php echo findseq(7); ?></td>
</tr>
<tr>
<td align='center'>8</td>
<td align='center'><?php echo findseq(8); ?></td>
</tr>
<tr>
<td align='center'>9</td>
<td align='center'><?php echo findseq(9); ?></td>
</tr>
</table>
</center>
<br>
<font size='1'><i>Note: the 'sum' column is only provided for reference.<br>
<!--
DEBUG INFO
sorted coins list: (first 12 are border in order from high to low, last 4 are base)
<?php
echo $sc[1].','.$sc[2].','.$sc[3].','.$sc[4].','.$sc[5].','.$sc[6].','.$sc[7].','.$sc[8].','.$sc[9].','.$sc[10].','.$sc[11].','.$sc[12].',';
?>
<?php
echo $sc[13].','.$sc[14].','.$sc[15].','.$sc[16];
?>
-->
<?php
// EXECUTION TIME SEGMENT 2
// This segment is not subject to DMD's copyright
// or the GNU General Public License. See the other 'execution
// time' segment above for more information.
//Put this at the very bottom of the page
echo StopTimer($start);
// END OF EXECUTION TIME SEGMENT 2
?>
</i></font><br>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment