用井字游戏宣布获胜者
Declaring a winner in tic tac toe
我正在编写一个井字游戏程序。 到目前为止,我有 3x3 网格,我可以将 X 和 O 图像拖放到网格中。
要宣布获胜者,我会使用 if else 语句吗?
我只是想知道我如何宣布获胜者。 我猜这需要javascript。
这是我在jsfiddle中的代码,以及我在下面编译的内容:
http://jsfiddle.net/VXQ7P/
<!DOCTYPE HTML> <html> <head> <style type="text/css"> #div1, #div2, #div3, #div4, #div5, #div6, #div7, #div8, #div9 { width: 80px; height: 80px; padding: 10px; border: 1px solid #aaaaaa; float: left; } </style> <script type="text/javascript"> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); helper:clone; } function drop(ev) { var data=ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data).cloneNode(true)); ev.preventDefault(); } </script> <title>JavaScript Drag & Drop Tic-Tac-Toe</title> </head> <body> <p>Drag the X and O images into the tic-tac-toe board:</p> <table> <tr> <td><div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> <td><div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> <td><div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> </tr> <tr> <td><div id="div4" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> <td><div id="div5" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> <td><div id="div6" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> </tr> <tr> <td><div id="div7" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> <td><div id="div8" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> <td><div id="div9" ondrop="drop(event)" ondragover="allowDrop(event)"></div></td> </tr> </table> <img id="drag1" src="X.png" ondragstart="drag(event)" width="75" height="75"/> <img id="drag2" src="O.png" draggable="true" ondragstart="drag(event)" width="75" height="75"/> </body> </html>
在我的观点中,你正在走一条路。您应该对每个字段使用 "onclick()",然后将div 的内部 html 更改为 X 或 O。您可能还想在谷歌上搜索"tic tac toe javascript"。以前很多人都这样做过。例如,看看这个:http://jsfiddle.net/rtoal/ThPEH/获胜者的定义方式非常容易理解,并且在那里得到了很好的解释。;)从网站上抓取:
/*
* To determine a win condition, each square is "tagged" from left
* to right, top to bottom, with successive powers of 2. Each cell
* thus represents an individual bit in a 9-bit string, and a
* player's squares at any given time can be represented as a
* unique 9-bit value. A winner can thus be easily determined by
* checking whether the player's current 9 bits have covered any
* of the eight "three-in-a-row" combinations.
*
* 273 84
* ' /
* 1 | 2 | 4 = 7
* -----+-----+-----
* 8 | 16 | 32 = 56
* -----+-----+-----
* 64 | 128 | 256 = 448
* =================
* 73 146 292
*
*/
wins = [7, 56, 448, 73, 146, 292, 273, 84],
编辑:您可以将每个 TicTacToe 单元格的状态和值存储在数组中。通过这种方式,您可以轻松访问它们以确定是否有赢家。http://pastebin.com/xDhBUfeS
玩家可以通过 8 种方式获胜。一个简单的方法是使用 if-then-else 测试每种方式。但首先,最好使用两个数组记录移动,并对每个方块进行编号。
循环怎么样?
遍历每一行和每一列(以及中间的 2 条对角线)
相关文章:
- 如何使用phaser使html5游戏在移动设备浏览器上运行
- 如何有效地将游戏数据存储在URL查询字符串中
- Javascript游戏输入失去焦点
- 如何在p5.js中管理多智力竞赛游戏的多个屏幕
- setInterval游戏循环的范围问题
- 为网络游戏制作动画
- 重置游戏和获胜者问题
- 帆布游戏的滞后运动
- Javascript单独的游戏窗口
- Javascript猜测游戏-计数器不工作
- JavaScript纸牌游戏
- 简单游戏的画布与SVG
- 我的HTML5游戏不时暂停,我如何才能知道它是否's是由垃圾收集引起的
- 多人游戏完全在浏览器中运行,服务器仅用于数据库
- 通过PHP将单词添加到游戏中
- 用井字游戏宣布获胜者不确定出了什么问题
- 用井字游戏宣布获胜者
- 如何使用 for 循环来检查井字游戏中的获胜者
- HTML 井字游戏获胜者公告
- 如何检查简单Jquery井字游戏的获胜者