Skip to content

Commit

Permalink
增加最后一个作业内容
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyan123 committed Nov 29, 2015
1 parent 542325a commit 65e4383
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 4 deletions.
23 changes: 23 additions & 0 deletions zhangyan/Puzzle8/css/puzzle.css
@@ -0,0 +1,23 @@
body {
background: #ebe4ea;
text-align: center;
color: #d9a3d5;
}

input {
height: 30px;
margin: 10px 2px;
}
table {
border:1px #d9a3d5 solid;
}
td {
padding: 50px;
background-color: #e8d1e7;
font-size: 25px;
margin: 2px;
}

.zero {
background-color: #ebe4ea;
}
24 changes: 24 additions & 0 deletions zhangyan/Puzzle8/index.html
@@ -0,0 +1,24 @@
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/puzzle.css" />
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="js/puzzle.js"></script>
</head>

<body>
<center>
<h2>数字拼图游戏</h2>
<table></table>
</cebter>
<div>
<input class="num" placeholder="n宫格拼图" />
<button>新开一局</button>
</div>
</body>

</html>
4 changes: 4 additions & 0 deletions zhangyan/Puzzle8/js/jquery-2.1.4.min.js

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions zhangyan/Puzzle8/js/puzzle.js
@@ -0,0 +1,128 @@
var num = 3; //默认九宫格
$(function() {
initPic();
initNum();
$("button").click(suffleCards);
});

function initPic() {
$("table").empty();
for (var row = 0; row < num; row++) {
$("<tr></tr>").appendTo("table");
}
for (var col = 0; col < num; col++) {
$("<td></td>").appendTo("tr");
}
$("td").click(moveCard);
}

function initNum() {
$.post("php/initNum.php", {
num: num
}, function(data, status) {
if (status == "success") {
if (data == "dataIllegal") {
alert("1<n<10");
} else {
var randomNum = eval(data);
for (var i = 0; i < num * num; i++) {
if (randomNum[i] == 0) {
$("td").eq(i).text("").addClass("zero");
} else {
$("td").eq(i).text(randomNum[i]).removeClass("zero");
}
}
}
}
});
}
var suffleCards = function() {
var numSet = $(".num").val();
if (!numSet) {
numSet = num;
} else if (!isValid(numSet)) {
alert("1<n<10");
return;
}
if (numSet != num) {
num = parseInt(numSet);
initPic();
}
initNum();
}

function isValid(input) {
var reg = new RegExp("^[2-9]*$");
return reg.test(input);
}

var moveCard = function() {
var index = $("td").index($(this));
for (var i = 1; i < 5; i++) {
var surIndex = detectContext(index, i);
if (typeof surIndex !== "undefined") {
if ($("td").eq(surIndex).hasClass("zero")) {
exContent(index, surIndex);
return;
}
}
}
}

function detectContext(index, direction) {
switch (direction) {
case 1: //上
if (index > num - 1) {
return index - num;
}
break;
case 2: //右
if (index % num !== num - 1) {
return index + 1;
}
break;
case 3: //下
if (index < num * num - num) {
return index + num;
}
break;
case 4: //左
if (index % num !== 0) {
return index - 1;
}
break;
}
}

function exContent(index, indexZero) {
var indexText = $("td").eq(index).text();
$("td").eq(index).text("").addClass("zero");
$("td").eq(indexZero).text(indexText).removeClass("zero");
isWin();
}

function isWin() {
var zeroIndex = $("td").index($(".zero"));
if (zeroIndex === num * num - 1) {
var rank = $("td").text();
var rankWin = "1";
for (var i = 2; i < num * num; i++) {
var rankWin = rankWin + i;
}
if (rank === rankWin) {
alert("恭喜你成功啦!");
initNum();
}
}
}
$(document).keydown(function(event) {
var k = event.keyCode;
if (k > 36 && k < 41) {
var zeroIndex = $("td").index($(".zero"));
var direction = (k == 40) ? 1 : k - 35; //左:k=37 direction=2;上:k=38 direction=3;右:k=39 direction=4;下:k=40 direction=1;
var surIndex = detectContext(zeroIndex, direction);
if (typeof surIndex !== "undefined") {
exContent(surIndex, zeroIndex);
}
}
});
11 changes: 11 additions & 0 deletions zhangyan/Puzzle8/php/initNum.php
@@ -0,0 +1,11 @@
<?php
$num= $_POST["num"];
$pattern = "/^[2-9]*$/";
if (!preg_match($pattern, $num)) {
echo "dataIllegal";
return;
}
$arr = range(0, pow($num, 2) - 1);
shuffle($arr);
echo json_encode($arr);
?>
14 changes: 11 additions & 3 deletions zhangyan/blog/add.php
Expand Up @@ -8,14 +8,17 @@
$psw = $_POST["password"];
$psw_confirm = $_POST["confirm"];
if(!preg_match('/^[\w\x80-\xff]{3,15}$/', $user)){
exit('错误:用户名不符合规定。<a href="javascript:history.back(-1);">返回</a>');
echo "<script>alert('用户名不符合规范'); history.go(-1);</script>";
exit;
}
if(strlen($psw) < 6){
exit('错误:密码长度不符合规定。<a href="javascript:history.back(-1);">返回</a>');
echo "<script>alert('密码长度不符合规范'); history.go(-1);</script>";
exit;
}
if($user == "" || $psw == "" || $psw_confirm == "")
{
echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>";
exit;
}
else
{
Expand All @@ -37,6 +40,7 @@
if($num) //如果已经存在该用户
{
echo "<script>alert('用户名已存在'); history.go(-1);</script>";
exit;
}
else //不存在当前注册用户名称
{
Expand All @@ -46,22 +50,26 @@
if($res_insert)
{

echo "<script>alert('注册成功!');window.location.replace('login.php')</script>";
echo "<script>alert('注册成功!');window.location.replace('login.php')</script>";
exit;
}
else
{
echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>";
exit;
}
}
}
else
{
echo "<script>alert('密码不一致!'); history.go(-1);</script>";
exit;
}
}
}
else
{
echo "<script>alert('信息不完整!'); history.go(-1);</script>";
exit;
}
?>
2 changes: 1 addition & 1 deletion zhangyan/blog/login.php
Expand Up @@ -23,7 +23,7 @@
<input type="text" name="username" placeholder="username" />
</p>
<p>
<input type="text" name="password" placeholder="password" id="password" />
<input type="password" name="password" placeholder="password" id="password" />
</p>
<p>
<input name="Signin" type="submit" class="btn" value="登陆"></input>
Expand Down

0 comments on commit 65e4383

Please sign in to comment.