首页 > 语言 > JavaScript > 正文

jQuery实现的五子棋游戏实例

2024-05-06 16:22:26
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了jQuery实现的五子棋游戏,以完整实例形式分析了jQuery实现五子棋游戏的方法,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了jQuery实现的五子棋游戏。分享给大家供大家参考。具体如下:

这是一款非常不错的代码,就是人工智能方面差了一点

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <title>五子棋</title> 
  6. <style type="text/css"> 
  7. div{margin:0;padding:0;} 
  8. div.board{width:561px; height:561px; border:1px solid #ccc; margin:0 auto;} 
  9. div.board div{ width:31px; height:31px; border:1px solid #ccc; float:left; cursor:pointer; background-repeat:no-repeat; } 
  10. div.board .person { background-image:url('images/1/files/demo/white.jpg')} 
  11. div.board .machine{ background-image:url('images/1/files/demo/black.jpg')} 
  12. div.board .person_star{background-image:url('images/1/files/demo/white_star.jpg')} 
  13. div.board .machine_star{background-image:url('images/1/files/demo/black_star.jpg')} 
  14. input.ipt{ display:block; margin:0 auto; margin-top:8px;width:70px} 
  15. </style> 
  16. </head> 
  17. <body> 
  18. <div class='board' id='board'> 
  19. </div> 
  20. <input type='button' value='开始游戏' onclick="initGame(); 
  21. this.value='重新开始'" class='ipt'/> 
  22. <script type='text/javascript'> 
  23. var TRANSVERSE = 16; 
  24. var VERTICAL = 16; 
  25. var LEFT = 1; 
  26. var RIGHT = 2; 
  27. var TOP = 3; 
  28. var BOTTOM = 4; 
  29. var LEFT_TOP = 5; 
  30. var LEFT_BOTTOM = 6; 
  31. var RIGHT_TOP = 7; 
  32. var RIGHT_BOTTOM = 8; 
  33. var Chess = function() 
  34. { 
  35. var owner = ''; 
  36. var victory = false; 
  37. this.getOwner = function(){return owner;}; 
  38. this.setOwner = function(value){owner = value;}; 
  39. this.getVictory = function(){ return victory;} 
  40. this.setVictory = function(value){ victory = value; }  
  41. } 
  42. var Board = function() 
  43. { 
  44. var chessBoard = []; 
  45. var isGameOver = false; 
  46. this.getChess = function(point) 
  47. { 
  48. var x = point.x , y = point.y; 
  49. return chessBoard[y][x]; 
  50. } 
  51. this.setChess = function(chess , point) 
  52. { 
  53. var x = point.x , y = point.y; 
  54. chessBoard[y][x] = chess; 
  55. } 
  56. this.setVictory = function(points) 
  57. { 
  58. for(var i = 0 ; i < points.length ; i ++) 
  59. { 
  60. for(var j = 0 ; j < points[i].length; j ++) 
  61. { 
  62. var chess = this.getChess(points[i][j]); 
  63. chess.setVictory(true); 
  64. } 
  65. } 
  66. } 
  67. this.getAvaiablePoints = function() 
  68. { 
  69. var avaiable = new Array; 
  70. for(var y = 0 ; y <= VERTICAL ; y ++) 
  71. { 
  72. for(var x = 0 ; x <= TRANSVERSE ; x ++) 
  73. { 
  74. if(chessBoard[y][x]) continue; 
  75. var point = {x : x , y : y}; 
  76. avaiable.push(point); 
  77. } 
  78. } 
  79. return avaiable; 
  80. } 
  81. this.getMap = function() 
  82. { 
  83. var map = {}; 
  84. for(var y = 0 ; y <= VERTICAL ; y ++) 
  85. { 
  86. for(var x = 0 ; x <= TRANSVERSE ; x++) 
  87. { 
  88. var chess = chessBoard[y][x]; 
  89. var value = ''; 
  90. if(chess) 
  91. { 
  92. value = chess.getOwner(); 
  93. if(chess.getVictory()) value += '_star'; 
  94. } 
  95. else 
  96. { 
  97. value = ''; 
  98. } 
  99. map[ x + ',' + y ] = value; 
  100. } 
  101. } 
  102. return map; 
  103. } 
  104. this.gameOver = function() 
  105. { 
  106. return isGameOver = true; 
  107. } 
  108. this.isGameOver = function() 
  109. { 
  110. return isGameOver; 
  111. } 
  112. this.getNextPoint = function(point , direction) 
  113. { 
  114. var next = {x : point.x , y : point.y}; 
  115. switch(direction) 
  116. { 
  117. case LEFT : 
  118. next.x -= 1; 
  119. break; 
  120. case RIGHT: 
  121. next.x += 1; 
  122. break; 
  123. case TOP: 
  124. next.y -= 1; 
  125. break; 
  126. case BOTTOM: 
  127. next.y += 1; 
  128. break; 
  129. case LEFT_TOP: 
  130. next.x-= 1 , next.y-= 1; 
  131. break; 
  132. case RIGHT_TOP: 
  133. next.x += 1 , next.y -= 1; 
  134. break; 
  135. case LEFT_BOTTOM: 
  136. next.x -= 1 , next.y += 1; 
  137. break; 
  138. case RIGHT_BOTTOM: 
  139. next.x += 1 , next.y += 1; 
  140. break; 
  141. default : 
  142. alert('方向错误'); 
  143. } 
  144. return next; 
  145. } 
  146. var initialize = function() 
  147. { 
  148. for(var i = 0 ; i <= VERTICAL ; i++ ) chessBoard.push([]); 
  149. }  
  150. initialize(); 
  151. } 
  152. var Compute = function(role) 
  153. { 
  154. var directions = [LEFT , TOP , RIGHT , BOTTOM , LEFT_TOP , LEFT_BOTTOM , RIGHT_TOP , RIGHT_BOTTOM]; 
  155. var score = 0; 
  156. var self = this; 
  157. this._computeScore = function(direction) 
  158. { 
  159. throw new Error('未实现'); 
  160. } 
  161. this._convertToPattern = function(chesslist) 
  162. { 
  163. return role.convertToPattern(chesslist) 
  164. } 
  165. this.compute = function(point) 
  166. { 
  167. score = 0; 
  168. for(var i = 0, direction ; direction = directions[i++];) 
  169. { 
  170. score += this._computeScore(point , direction); 
  171. }  
  172. } 
  173. this.getScore = function(refPoint) 
  174. { 
  175. return score ; 
  176. } 
  177. } 
  178. var Five = function(role) 
  179. { 
  180. Compute.call(this, role); 
  181. var computeScore1 = function(refPoint , direction) 
  182. { 
  183. var predefined = 'IIII'; 
  184. var chesslist = role.find(refPoint , direction , 4); 
  185. var pattern = role.convertToPattern(chesslist); 
  186. if(predefined == pattern) return true; 
  187. return false ;  
  188. } 
  189. var computeScore2 = function(refPoint , direction) 
  190. { 
  191. var prev = role.find(refPoint , direction , 2); 
  192. var next = role.find(refPoint , role.reverseDirection(direction) , 2); 
  193. var prevPattern = role.convertToPattern(prev); 
  194. var nextPattern = role.convertToPattern(next); 
  195. if(prevPattern == 'II' && nextPattern == 'II') return true; 
  196. return false; 
  197. } 
  198. var computeScore3 = function(refPoint , direction) 
  199. { 
  200. var prev = role.find(refPoint , direction , 3); 
  201. var next = role.find(refPoint , role.reverseDirection(direction) , 1); 
  202. var prevPattern = role.convertToPattern(prev); 
  203. var nextPattern = role.convertToPattern(next); 
  204. if(prevPattern == 'III' && nextPattern == 'I') return true; 
  205. return false;  
  206. } 
  207. this._computeScore = function(refPoint , direction) 
  208. { 
  209. if(computeScore1(refPoint , direction) || computeScore2(refPoint , direction) || computeScore3(refPoint , direction)) 
  210. return 100000; 
  211. else return 0; 
  212. } 
  213. } 
  214. var Four_Live = function(role) 
  215. { 
  216. Compute.call(this, role); 
  217. this._computeScore = function(refPoint , direction) 
  218. { 
  219. var score = 0; 
  220. var prev = role.find(refPoint , direction , 4); 
  221. var next = role.find(refPoint , role.reverseDirection(direction), 1); 
  222. var prevPattern = this._convertToPattern(prev); 
  223. var nextPattern = this._convertToPattern(next); 
  224. if(prevPattern == 'III0' && nextPattern == '0') score = 10000;  
  225. return score;  
  226. } 
  227. } 
  228. var Four_Live1 = function(role) 
  229. { 
  230. Compute.call(this, role); 
  231. this._computeScore = function(refPoint , direction) 
  232. { 
  233. var prev = role.find(refPoint , direction , 3); 
  234. var next = role.find(refPoint , role.reverseDirection(direction) , 2); 
  235. var prevPattern = this._convertToPattern(prev); 
  236. var nextPattern = this._convertToPattern(next);  
  237. if(prevPattern == 'II0' && nextPattern == 'I0') return 10000; 
  238. else return 0; 
  239. } 
  240. } 
  241. var Tree_Live = function(role) 
  242. { 
  243. Compute.call(this, role); 
  244. this._computeScore = function(refPoint , direction) 
  245. { 
  246. var score = 0; 
  247. var prev = role.find(refPoint , direction , 3); 
  248. var next = role.find(refPoint , role.reverseDirection(direction), 2); 
  249. var prevPattern = this._convertToPattern(prev); 
  250. var nextPattern = this._convertToPattern(next); 
  251. if(prevPattern == 'II0' && nextPattern == '00') 
  252. score += 1000; 
  253. return score; 
  254. } 
  255. } 
  256. var Tree_Live1 = function(role) 
  257. { 
  258. Compute.call(this, role); 
  259. this._computeScore = function(refPoint , direction) 
  260. { 
  261. var prev = role.find(refPoint , direction , 2); 
  262. var next = role.find(refPoint , role.reverseDirection(direction), 3); 
  263. var prevPattern = this._convertToPattern(prev); 
  264. var nextPattern = this._convertToPattern(next); 
  265. if(prevPattern == 'I0' && nextPattern == 'I00') 
  266. return 1000 
  267. else return 0;  
  268. } 
  269. } 
  270. var Two_Live = function(role) 
  271. { 
  272. Compute.call(this, role); 
  273. this._computeScore = function(refPoint , direction) 
  274. { 
  275. var prev = role.find(refPoint , direction , 3); 
  276. var next = role.find(refPoint , role.reverseDirection(direction), 2);  
  277. var prevPattern = this._convertToPattern(prev); 
  278. var nextPattern = this._convertToPattern(next); 
  279. if(prevPattern == 'I00' && nextPattern == '00') return 100; 
  280. else return 0;  
  281. } 
  282. } 
  283. var One_Live = function(role) 
  284. { 
  285. Compute.call(this, role); 
  286. this._computeScore = function(refPoint , direction) 
  287. { 
  288. var prev = role.find(refPoint , direction , 3); 
  289. var next = role.find(refPoint , role.reverseDirection(direction), 3);  
  290. var prevPattern = this._convertToPattern(prev); 
  291. var nextPattern = this._convertToPattern(next); 
  292. if(prevPattern == '000' && nextPattern == '000') return 10; 
  293. else return 0;  
  294. } 
  295. } 
  296. var Four_End = function(role) 
  297. { 
  298. Compute.call(this, role); 
  299. this._computeScore = function(refPoint , direction) 
  300. { 
  301. var prev = role.find(refPoint , direction , 3); 
  302. var next = role.find(refPoint , role.reverseDirection(direction), 1);  
  303. var prevPattern = this._convertToPattern(prev); 
  304. var nextPattern = this._convertToPattern(next); 
  305. if(prevPattern == 'III' && nextPattern == '0') return 150; 
  306. else return 0;  
  307. } 
  308. } 
  309. var Role = function(board) 
  310. { 
  311. var computers = []; 
  312. var self = this; 
  313. var isVictory = false; 
  314. this.isVictory = function() 
  315. { 
  316. return isVictory; 
  317. } 
  318. var getScore = function(point) 
  319. { 
  320. var score = 0; 
  321. for(var i = 0 , computer; computer = computers[i++];) 
  322. { 
  323. computer.compute(point); 
  324. score += computer.getScore(); 
  325. } 
  326. var result = {score: score , point : point}; 
  327. return result; 
  328. } 
  329. var getScoreList = function() 
  330. { 
  331. var result = []; 
  332. var avaiablePoints = board.getAvaiablePoints(); 
  333. for(var i = 0 , point; point = avaiablePoints[i++];)  
  334. { 
  335. result.push(getScore(point)); 
  336. } 
  337. return result; 
  338. } 
  339. this.getCode = function() 
  340. { 
  341. throw new Error('未实现'); 
  342. } 
  343. this.getPeak = function() 
  344. { 
  345. var scoreInfo = getScoreList(); 
  346. scoreInfo.sort(function(a,b){ 
  347. return b.score - a.score ; 
  348. }); 
  349. return scoreInfo[0]; 
  350. }  
  351. this.convertToPattern = function(chesslist) 
  352. { 
  353. var pattern = ''; 
  354. if(!chesslist) return ''; 
  355. for(var i = 0 ; i < chesslist.length ; i ++) 
  356. { 
  357. var chess = chesslist[i]; 
  358. if(chess == undefined) pattern += '0'; 
  359. else if(chess.getOwner() == this.getCode()) pattern += 'I'; 
  360. else pattern += 'Y'; 
  361. } 
  362. return pattern ; 
  363. } 
  364. this.reverseDirection = function(direction) 
  365. { 
  366. switch(direction) 
  367. { 
  368. case LEFT : return RIGHT; 
  369. case RIGHT : return LEFT; 
  370. case TOP : return BOTTOM; 
  371. case BOTTOM : return TOP; 
  372. case LEFT_TOP : return RIGHT_BOTTOM; 
  373. case RIGHT_BOTTOM : return LEFT_TOP; 
  374. case RIGHT_TOP : return LEFT_BOTTOM; 
  375. case LEFT_BOTTOM : return RIGHT_TOP; 
  376. default : alert('方向错误'); 
  377. } 
  378. }  
  379. this._checkGameOver = function(point) 
  380. { 
  381. var leftRight = findVictory(point , LEFT); 
  382. var topBottom = findVictory(point , TOP); 
  383. var leftTopRightBottom = findVictory(point , LEFT_TOP); 
  384. var rightTopLeftBottom = findVictory(point , RIGHT_TOP); 
  385. var array = [leftRight , topBottom , leftTopRightBottom , rightTopLeftBottom]; 
  386. var victory = []; 
  387. for(var i = 0 ; i < array.length ; i ++) 
  388. { 
  389. if(array[i].length >= 5) victory.push(array[i]); 
  390. } 
  391. if(victory.length > 0) 
  392. { 
  393. board.gameOver(); 
  394. board.setVictory(victory); 
  395. isVictory = true; 
  396. } 
  397. if(board.getAvaiablePoints().length ==0) board.gameOver(); 
  398. } 
  399. var isLicitPoint = function(point) 
  400. { 
  401. return point.x >= 0 && point.y >= 0 && point.x <= TRANSVERSE && point.y <= VERTICAL  
  402. && board.getChess(point) && board.getChess(point).getOwner() == self.getCode() 
  403. } 
  404. var findVictory = function(refPoint , direction) 
  405. { 
  406. var reverse = self.reverseDirection(direction); 
  407. var result = []; 
  408. var nextPoint ; 
  409. var currPoint = {x: refPoint.x , y: refPoint.y}; 
  410. while(true) 
  411. { 
  412. nextPoint = board.getNextPoint(currPoint, direction); 
  413. if(!isLicitPoint(nextPoint)) break; 
  414. currPoint = {x :nextPoint.x , y:nextPoint.y}; 
  415. } 
  416. while(true) 
  417. { 
  418. result.push(currPoint);  
  419. nextPoint = board.getNextPoint(currPoint , reverse); 
  420. if(!isLicitPoint(nextPoint)) break;  
  421. currPoint = { x: nextPoint.x , y: nextPoint.y }; 
  422. } 
  423. return result; 
  424. } 
  425. this.find = function(point , direction , deep) 
  426. { 
  427. var refPoint = {x: point.x , y : point.y}; 
  428. var result = new Array; 
  429. var index = 1; 
  430. var nextPoint; 
  431. while(index <= deep) 
  432. { 
  433. nextPoint = board.getNextPoint(refPoint, direction); 
  434. if(nextPoint.x < 0 || nextPoint.y < 0 ||  
  435. nextPoint.x > TRANSVERSE || nextPoint.y > VERTICAL) return null; 
  436. var chess = board.getChess(nextPoint); 
  437. if(chess) chess.point = {x:nextPoint.x , y:nextPoint.y}; 
  438. result.push(chess); 
  439. refPoint = nextPoint; 
  440. index ++; 
  441. } 
  442. return result; 
  443. }  
  444. var initialize = function() 
  445. { 
  446. computers.push(new Five(self)); 
  447. computers.push(new Four_Live(self)); 
  448. computers.push(new Tree_Live(self)); 
  449. computers.push(new Four_Live1(self)); 
  450. computers.push(new Tree_Live1(self)); 
  451. computers.push(new Two_Live(self)); 
  452. computers.push(new One_Live(self)); 
  453. computers.push(new Four_End(self)); 
  454. } 
  455. initialize(); 
  456. } 
  457. var Machine = function(board, rival) 
  458. { 
  459. Role.call(this, board); 
  460. this.setChess = function() 
  461. { 
  462. if(board.isGameOver()) return; 
  463. var myPeak = this.getPeak(); 
  464. var rivalPeak = rival.getPeak(); 
  465. var peak ; 
  466. if(myPeak.score >= rivalPeak.score) peak = myPeak; 
  467. else peak = rivalPeak; 
  468. var chess = new Chess(); 
  469. chess.setOwner(this.getCode()); 
  470. board.setChess(chess, peak.point); 
  471. this._checkGameOver(peak.point); 
  472. } 
  473. this.getCode = function(){return 'machine';} 
  474. } 
  475. var Person = function(board , rival) 
  476. { 
  477. Role.call(this, board); 
  478. this.setChess = function(x,y) 
  479. { 
  480. if(board.isGameOver()) return; 
  481. var point = new Object; 
  482. point.x = x; 
  483. point.y = y; 
  484. var chess = new Chess() 
  485. chess.setOwner(this.getCode()); 
  486. board.setChess(chess, point); 
  487. this._checkGameOver(point); 
  488. } 
  489. this.getCode = function(){ return 'person'; } 
  490. } 
  491. var UIBase = function() 
  492. { 
  493. var self = this; 
  494. this._id = '$UI' + (++ UIBase.index); 
  495. this._globalKey = ""; 
  496. this.getHTML = function() 
  497. { 
  498. return ""; 
  499. } 
  500. var setGlobalKey = function() 
  501. { 
  502. var magic = '$UI_Items'; 
  503. self._globalKey = 'window.'+magic+'.'+self._id;  
  504. window[magic] = window[magic] || {}; 
  505. window[magic][self._id] = self; 
  506. } 
  507. var formatHTML = function(html) 
  508. { 
  509. html = html.replace(//$/$/g, self._globalKey); 
  510. html = html.replace(/&&/g,self._id); 
  511. return html; 
  512. }  
  513. var initUIBase = function() 
  514. { 
  515. setGlobalKey(); 
  516. } 
  517. this.renderHTML = function() 
  518. { 
  519. return formatHTML(this.getHTML()); 
  520. } 
  521. this.getDOM = function() 
  522. { 
  523. var dom = document.getElementById(this._id) 
  524. return dom; 
  525. } 
  526. initUIBase(); 
  527. } 
  528. UIBase.index = 0; 
  529. var ChessUI = function(board, placeholder) 
  530. { 
  531. UIBase.call(this); 
  532. this.setChess = function(){} 
  533. this.getHTML = function() 
  534. { 
  535. var html = ''; 
  536. var map = board.getMap(); 
  537. for(var key in map) 
  538. { 
  539. var onclick = ''; 
  540. var className = map[key]; 
  541. if(className == '') onclick='$._setChess('+ key +')'; 
  542. html += '<div onclick="'+ onclick +'" class="'+ className +'"></div>'; 
  543. } 
  544. return html; 
  545. } 
  546. this.draw = function() 
  547. { 
  548. var html = this.renderHTML(); 
  549. document.getElementById(placeholder).innerHTML = html; 
  550. } 
  551. this._setChess = function(x,y) 
  552. { 
  553. this.setChess(x,y); 
  554. } 
  555. this.draw(); 
  556. } 
  557. function getMSIEVersion() 
  558. { 
  559. var regex = /MSIE([^;]+)/; 
  560. var userAgent = navigator.userAgent; 
  561. var result = regex.exec(userAgent); 
  562. if(result) return parseInt(result[1]); 
  563. } 
  564. function initGame() 
  565. { 
  566. var version = getMSIEVersion(); 
  567. if(version && version <= 8) 
  568. { 
  569. alert('请使用非IE浏览器(ie9、10除外)进行游戏(google chrome 、firefox等 )'); 
  570. return; 
  571. } 
  572. var board = new Board(); 
  573. var person = new Person(board); 
  574. var machine = new Machine(board, person); 
  575. var chessUI = new ChessUI(board, 'board'); 
  576. chessUI.setChess = function(x,y) 
  577. { 
  578. person.setChess(x,y); 
  579. machine.setChess(); 
  580. chessUI.draw(); 
  581. if(board.isGameOver()) 
  582. { 
  583. if(person.isVictory()) alert('您获得了胜利'); 
  584. else if(machine.isVictory()) alert('机器获得了胜利'); 
  585. else alert('游戏结束,胜负未分'); 
  586. } 
  587. } 
  588. if(Math.floor(Math.random() * 10) % 2) 
  589. { 
  590. alert('机器执棋'); 
  591. machine.setChess(); 
  592. chessUI.draw(); 
  593. } 
  594. else 
  595. { 
  596. alert('您执棋'); 
  597. } 
  598. } 
  599. </script> 
  600. </body> 
  601. </html> 

希望本文所述对大家的jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选