jQuery实现的五子棋游戏实例

时间:2021-05-26

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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://puters.push(new Four_End(self)); } initialize();}var Machine = function(board, rival){ Role.call(this, board); this.setChess = function() { if(board.isGameOver()) return; var myPeak = this.getPeak(); var rivalPeak = rival.getPeak(); var peak ; if(myPeak.score >= rivalPeak.score) peak = myPeak; else peak = rivalPeak; var chess = new Chess(); chess.setOwner(this.getCode()); board.setChess(chess, peak.point); this._checkGameOver(peak.point); } this.getCode = function(){return 'machine';}}var Person = function(board , rival){ Role.call(this, board); this.setChess = function(x,y) { if(board.isGameOver()) return; var point = new Object; point.x = x; point.y = y; var chess = new Chess() chess.setOwner(this.getCode()); board.setChess(chess, point); this._checkGameOver(point); } this.getCode = function(){ return 'person'; }}var UIBase = function(){ var self = this; this._id = '$UI' + (++ UIBase.index); this._globalKey = ""; this.getHTML = function() { return ""; } var setGlobalKey = function() { var magic = '$UI_Items'; self._globalKey = 'window.'+magic+'.'+self._id; window[magic] = window[magic] || {}; window[magic][self._id] = self; } var formatHTML = function(html) { html = html.replace(/\$\$/g, self._globalKey); html = html.replace(/&&/g,self._id); return html; } var initUIBase = function() { setGlobalKey(); } this.renderHTML = function() { return formatHTML(this.getHTML()); } this.getDOM = function() { var dom = document.getElementById(this._id) return dom; } initUIBase();}UIBase.index = 0;var ChessUI = function(board, placeholder){ UIBase.call(this); this.setChess = function(){} this.getHTML = function() { var html = ''; var map = board.getMap(); for(var key in map) { var onclick = ''; var className = map[key]; if(className == '') onclick='$$._setChess('+ key +')'; html += '<div onclick="'+ onclick +'" class="'+ className +'"></div>'; } return html; } this.draw = function() { var html = this.renderHTML(); document.getElementById(placeholder).innerHTML = html; } this._setChess = function(x,y) { this.setChess(x,y); } this.draw();}function getMSIEVersion(){ var regex = /MSIE([^;]+)/; var userAgent = navigator.userAgent; var result = regex.exec(userAgent); if(result) return parseInt(result[1]);}function initGame(){ var version = getMSIEVersion(); if(version && version <= 8) { alert('请使用非IE浏览器(ie9、10除外)进行游戏(google chrome 、firefox等 )'); return; } var board = new Board(); var person = new Person(board); var machine = new Machine(board, person); var chessUI = new ChessUI(board, 'board'); chessUI.setChess = function(x,y) { person.setChess(x,y); machine.setChess(); chessUI.draw(); if(board.isGameOver()) { if(person.isVictory()) alert('您获得了胜利'); else if(machine.isVictory()) alert('机器获得了胜利'); else alert('游戏结束,胜负未分'); } } if(Math.floor(Math.random() * 10) % 2) { alert('机器执棋'); machine.setChess(); chessUI.draw(); } else { alert('您执棋'); }}</script></body></html>

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章