bootstrap+jquery实现井字棋游戏代码
代码语言:html
所属分类:游戏
代码描述:bootstrap+jquery实现井字棋游戏代码,可设置xo及电脑与玩家。
代码标签: bootstrap jquery 井字棋 游戏 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/bootstrap.3.3.4.css"> <style> * { margin: 0px; padding: 0px; } .gameboard { width: 480px; height: 450px; margin-left: auto; margin-right: auto; margin-top: 200px; background-color: #000; } .gameboard ul { list-style: none; height: 480px; padding: 15px; } .grid { width: 32.4%; height: 30.5%; float: left; border: 1px solid #E1F5A9; } .gameboard li { cursor: pointer; color: #F9F9F9; text-align: center; line-height: 138px; font-size: 2em; } .grid-0, .grid-3, .grid-6 { border-left: none; } .grid-0, .grid-1, .grid-2 { border-top: none; } .grid-2, .grid-5, .grid-8 { border-right: none; } .grid-6, .grid-7, .grid-8 { border-bottom: none; } .bg { /*display: none;*/ position: absolute; top: 0%; left: 0%; width: 100%; height: 120%; background-color: black; z-index:1001; -moz-opacity: 0.7; opacity:.70; filter: alpha(opacity=70); } .choose-panel { position: absolute; top: 0; left: 0; bottom: 0; right: 0; width: 560px; height: 200px; margin: auto; background-color: #FFF; margin-top: 200px; z-index: 1002; border-radius: 25px; } .choose-panel p { font-size: 2em; text-align: center; padding-top: 30px; } .choose-btn { text-align: center; } .btn { margin: 5px; padding:5px; } .choose-first { display: none; } </style> </head> <body> <div class="container"> <div class="gameboard"> <ul class="grid-list"> <li class="grid grid-0"></li> <li class="grid grid-1"></li> <li class="grid grid-2"></li> <li class="grid grid-3"></li> <li class="grid grid-4"></li> <li class="grid grid-5"></li> <li class="grid grid-6"></li> <li class="grid grid-7"></li> <li class="grid grid-8"></li> </ul> </div> </div> <div class="bg"> </div> <div class="choose-panel"> <div class="choose-player"> <p>选择角色:X OR O?</p> <div class="choose-btn"> <button class="btn btn-default xobtn" id="x-btn">X</button><button class="btn btn-default xobtn" id="o-btn">O</button> </div> </div> <div class="choose-first"> <p>设置先手:</p> <div class="choose-btn"> <button class="btn btn-default firstbtn" id="computer">电脑</button><button class="btn btn-default firstbtn" id="user">玩家</button> </div> </div> </div> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script> <script > /*game逻辑分析 * 新建一个game的构造函数,并给该构造函数的原型对象prototype添加两个属性:init/play * * 1.点击选择角色按钮,选择角色,关闭选择角色面板,打开选择先手面板; * 2.选择先手,若是电脑先手,调用对象的play()函数; * 3.设置点击棋盘位置的点击事件,每次点击玩更新棋盘相应处的棋子,并调用play()函数,让电脑落子; * 4.play()函数逻辑:判断是否下一手即可获胜,若能找到落子位置落子 * 否则,判断下一手player是否将要获胜,若是,找到落子位置落子; * 否则,判断是否是第一手,若是,落子棋盘正中间; * 否则,收集棋盘其余未落子的位置,并随机选择落子位置落子 */ $(function(){ var running = false, computerVal = -1, playerVal = 1, computer,player, copEnd = false, winResult = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]], panel = [0,0,0,0,0,0,0,0,0]; //设置角色 function setPlayer(val){ computer = val === 'O' ? 'X' : 'O'; $(".choose-player").css("display","none"); $(".choose-first").css("display","block"); }; //通过类名获得位置 function getPos(val){ return val.split(" ")[1].split("-")[1]; }; //判断玩家是否胜利 function playerWin(){ for(var i = 0;i <winResult.length;i++){ var winCount = 0; for(var j = 0;j < winResult[i].length;j++){ if(panel[winResult[i][j]] == 1){ winCount++; }; }; if(winCount == 3){ return true; }; }; return false; }; //判断电脑是否胜利 function computerWin(){ for(var i = 0;i <winResult.length;i++){ var winCount = 0; for(var j = 0;j < winResult[i].length;j++){ if(panel[winResult[i][j]] == (-1)){ winCount++; }; .........完整代码请登录后点击上方下载按钮下载查看
网友评论0