Create a custom size puzzle game using jQuery

Source: Internet
Author: User

Create a custom size puzzle game using jQuery

This article mainly introduces the code used by jQuery to create a puzzle game with a custom size. It is very simple and practical. Although it is only limited to digital order puzzles, it will be gradually improved in the future, but I want to share it with you. If you need it, you can refer to it.

 

 

I have limited the size to 3-10. I am really idle, or I can try to change it if I have a tendency to self-abuse ..
I was going to get the image. I haven't done it yet ..

Pintu.html

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> puzzle </title>

<Style type = "text/css">

# Pt_main {

Margin: 0 auto;

Border: 2px solid #000 ;}

# Menu {

Text-align: center ;}

# Pt_main div {

Padding: 0px;

Margin: 0px;

Float: left;

Line-height: 200px;

Font-size: 100px;

Text-align: center ;}

# Pt_main div: hover {

Cursor: pointer ;}

. Hui {

Background: # CCC ;}

</Style>

 

<Script src = "http://code.jquery.com/jquery-latest.js"> </script>

<Script type = "text/javascript">

// Div Border Width

Var border_w = 2;

// Div size

Var size = 50;

// Pass

Var st = false;

// The address of the background image (not provided yet)

Var imgurl = '';

// Position of the gray block

Var hui = 0;

// Game width and height

Var width = 3;

Var height = 3;

$ (Function (){

Init ();

});

// Determine whether two numbers can be exchanged

Function ut_jh (a, B ){

If (Math. abs (a-B) = width ){

Return true;

}

If (Math. abs (a-B) = 1 & ut_line (a, B, width )){

Return true;

}

Return false;

}

// Determine whether two numbers are in the same row

Function ut_line (a, B, width ){

If (parseInt (a/width) = parseInt (B/width )){

Return true;

} Else {

Return false;

}

}

// Generate an Interchangeable set (a set that can be exchanged with gray blocks)

Function ut_ SC (){

// Up to 4

Var li = new Array ();

Var I = 0;

If (a-width> = 0 ){

Li [I ++] = a-width;

}

If (a + width <= width * height-1 ){

Li [I ++] = a + width;

}

If (ut_line (a, A-1, width) & A-1> = 0 ){

Li [I ++] = A-1;

}

If (ut_line (a, a + 1, width) & a + 1 <= width * height-1 ){

Li [I ++] = a + 1;

}

// Alert (a + '-length:' + li. length)

Return li;

}

// Block Click Event

Function pt_click (){

If (! St ){

Return false;

}

// Index of the clicked Block

Var index = get (). index ($ (this ));

 

If (ut_ut( hui, index )){

Aggregate (hui, index );

 

// Determine whether to pass

Pd ();

}

 

 

}

// Two exchanges

Function compute (hui _, index _){

// Alert (hui + 1 + "," + (index _ + 1 ));

Var list = get (); // all blocks

Var h = list. eq (hui _); // gray block

Var d = list. eq (index _); // click the block.

 

// Exchange styles

H. removeClass ();

D. addClass ('hui ');

H.html(d.html ());

D.html ('');

Hui = index _;

 

// Exchange attributes

Tg = h. attr ('tg ');

H. attr ('tg ', d. attr ('tg '));

D. attr ('tg ', tg );

}

 

Function init (){

// Cannot be too small...

If (width <3 | height <3 ){

Return false;

}

Hui = width * height-1;

Var pp = $ ("# pt_main ");

Pp.html ('');

// Create and initialize

Var k = width * size + border_w * width * 2;

Var g = height * size + border_w * height * 2;

Pp.css ({'width': k, 'height': g });

For (I = 0; I <width * height; I ++ ){

Var n = $ ("<div>" + (I + 1) + "</div> ");

N.css ({'width': size,

'Height': size,

Border: border_w + 'px solid #000 ',

LineHeight: size + 'px ',

FontSize: parseInt (size/3) + 'px'

});

If (imgurl! = ''){

 

}

Pp. append (n );

// Alert (I)

 

}

Get().last().html ('');

Get (). last (). addClass ('hui ');

Get (). on ('click', pt_click );

 

// Append attributes for each block and record the current value

$ ("# Pt_main div"). each (function (index, element ){

$ (Element). attr ('tg ', index );

});

 

 

Dl ();

St = true;

}

// Determine whether to pass (when each block value corresponds to its own subscript)

Function pd (){

Var B = true;

Get (). each (function (index, element ){

If ($ (element). attr ('tg ')! = Index ){

B = false;

Return false;

}

 

});

If (B ){

St = false;

Alert ("Congratulations! ");

Return true;

} Else {

Return false;

}

}

// Obtain the set

Function get (){

Return $ ("# pt_main div ");

}

// Disrupt

Function dl (){

// Number of interruptions

Var count = width * width;

 

For (I = 0; I <count; I ++ ){

// Interchangeable set

Var li = ut_ SC (hui );

Var num = parseInt (li. length) * Math. random ());

Aggregate (hui, li [num]);

}

 

}

// Click events of the initialization button

Function csh (){

Var dxv = $ ('# dx'). val ();

If (! ParseInt (dxv )){

Alert ('enter a number between 3 and 10 ');

$ ('# Dx'). val ('');

Return false;

}

Var v = parseInt (dxv );

If (v <3 | v> 10 ){

Alert ('enter a number between 3 and 10 ');

$ ('# Dx'). val ('');

Return false;

}

Width = v;

Height = v;

Init ();

}

</Script>

</Head>

 

<Body>

<Div id = 'menu '>

Size: <input id = 'dx' style = "text-align: center; width: 40px;"/>

<Button onclick = "csh ()"> initialization </button>

<Button onclick = "init ()"> refresh </button>

</Div>

<Div id = "pt_main">

 

</Div>

 

</Body>

</Html>

The above is all the content shared in this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.