<!DOCTYPE html>
<html>
<head>
	<title>N queen Problem solver</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

	<script src="jquery.js"></script>
    <script src=jPaginate.js></script>
    <!-- bootstarap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <link type="text/css" rel="stylesheet" href="style.css"  media="screen,projection"/>

</head>

<body>
<div class="container main">
<div class="row">
    <p style="margin: 0" class="pull-right"><a href="source.php" title="Source code" target="blank">view code ></a></p>
</div>
<div class="row">
	<div class="col s12">
		<h2>N queen solver</h2>
	</div>
</div>

<div class="row">
	<form class="col-lg-12" method="post">
		<div class="input-group">
			<input type="text" name="number" class="form-control" placeholder="How many queens?" autofocus <?php  if (isset($_POST["number"])) echo "value='".$_POST["number"]."'";?> />
<!-- 			<label for="number">How many queens?</label> -->
			<span class="input-group-btn">
				<button class="btn btn-default" type="submit">Submit</button>
			</span>
		</div>
	</form>
</div>
<div class="row">
	<div id="result">
<?php


if (!isset($_POST["number"])){
	die("Please enter a value");
}
	$board = [];
	$count = 0;


// check if it is safe to place the queen
function isSafe($row, $col){

	global $board;

    for($i=1; $i<$row; $i++){
    //checking column and digonal conflicts
	    if($board[$i]==$col)
	        return 0;
	    else if(abs($board[$i]-$col)==abs($i-$row))
	    	return 0;
    }

    return 1; //no conflicts
}


function printboard($n){
	global $board, $count;
	$count++;
    echo "<h3>Solution $count:</h3>";
	echo "<table class='table'>";
    echo "<tr><th></th>";

    for($i=1; $i<=$n; ++$i)
        echo "<th>$i</th>";
    echo "</tr>";


    for($i=1; $i<=$n; ++$i){
        echo "<tr><td>$i</td>";

        for($j=1; $j<=$n;++$j){ //for nxn board
            if($board[$i]==$j){
                echo "<td>Q</td>"; //queen at i,j position
            }
            else
                echo "<td>-</td>"; //empty slot
        }
        echo "</tr>";
    }

    echo "</table>";
}

function solveQ ($row, $n){
global $board;

    for($col=1; $col<=$n; ++$col){
        if(isSafe($row, $col)){
            $board[$row]=$col; //no conflicts so place queen
        if($row==$n){ //dead end
            printboard($n); //printing the board configuration
        }
        else //try queen with next position
            solveQ($row+1,$n);
        }
    }
}


// execute
$n=$_POST["number"];
solveQ(1,$n);
?>

</div><!-- result -->
<h4><?php echo $count; ?> solutions found.</h4>

</div> <!-- row -->

</div><!-- container -->
<script type="text/javascript">

$(document).ready(function(){
	$("#result").jPaginate({items: 2, active: "active", minimize: false, nav_items: 4, cookies: "false"});  
});

</script>
</body>
</html>