TicTacToe-javascript
HTML:TicTacToe-javascript,javascript,Javascript,HTML: 只是一个函数问题,我如何创建一个“O”,它在X和O之间轮换,并且不会超越表中现有的X或O 我把几周前我在制作游戏时在网上看到的代码放在这里,我希望它能帮助你 我从这里得到这个 这对你真的很有帮助,请阅读 变量状态=函数(旧){ /* *公众:轮到玩家的玩家 */ 此参数为“”; /* *公众:AI玩家的移动次数 */ 此.oMovesCount=0; /* *公众:这个州的比赛结果 */ this.result=“仍在运行”; /* *public:此状态下的板配置 */ this.
只是一个函数问题,我如何创建一个“O”,它在X和O之间轮换,并且不会超越表中现有的X或O 我把几周前我在制作游戏时在网上看到的代码放在这里,我希望它能帮助你 我从这里得到这个 这对你真的很有帮助,请阅读 变量状态=函数(旧){
/*
*公众:轮到玩家的玩家
*/
此参数为“”;
/*
*公众:AI玩家的移动次数
*/
此.oMovesCount=0;
/*
*公众:这个州的比赛结果
*/
this.result=“仍在运行”;
/*
*public:此状态下的板配置
*/
this.board=[];
/*开始对象构造*/
如果(旧的类型!=“未定义”){
//如果状态是使用另一个状态的副本构造的
var len=旧板长度;
this.board=新阵列(len);
对于(var-itr=0;itr 对于(var i=0;i我把几周前我在制作游戏时在互联网上看到的代码放在这里,我希望它能帮助你
我从这里得到这个
这对你真的很有帮助,请阅读
变量状态=函数(旧){
/*
*公众:轮到玩家的玩家
*/
此参数为“”;
/*
*公众:AI玩家的移动次数
*/
此.oMovesCount=0;
/*
*公众:这个州的比赛结果
*/
this.result=“仍在运行”;
/*
*public:此状态下的板配置
*/
this.board=[];
/*开始对象构造*/
如果(旧的类型!=“未定义”){
//如果状态是使用另一个状态的副本构造的
var len=旧板长度;
this.board=新阵列(len);
对于(var-itr=0;itr 对于(var i=0;i而言,只需使用一个布尔运算符,并在一个回合后将其更改为启用
/*
* public : the player who has the turn to player
*/
this.turn = "";
/*
* public : the number of moves of the AI player
*/
this.oMovesCount = 0;
/*
* public : the result of the game in this State
*/
this.result = "still running";
/*
* public : the board configuration in this state
*/
this.board = [];
/* Begin Object Construction */
if(typeof old !== "undefined") {
// if the state is constructed using a copy of another state
var len = old.board.length;
this.board = new Array(len);
for(var itr = 0 ; itr < len ; itr++) {
this.board[itr] = old.board[itr];
}
this.oMovesCount = old.oMovesCount;
this.result = old.result;
this.turn = old.turn;
}
/* End Object Construction */
/*
* public : advances the turn in a the state
*/
this.advanceTurn = function() {
this.turn = this.turn === "X" ? "O" : "X";
}
/*
* public function that enumerates the empty cells in state
* @return [Array]: indices of all empty cells
*/
this.emptyCells = function() {
var indxs = [];
for(var itr = 0; itr < 9 ; itr++) {
if(this.board[itr] === "E") {
indxs.push(itr);
}
}
return indxs;
}
/*
* public function that checks if the state is a terminal state or not
* the state result is updated to reflect the result of the game
* @returns [Boolean]: true if it's terminal, false otherwise
*/
this.isTerminal = function() {
var B = this.board;
//check rows
for(var i = 0; i <= 6; i = i + 3) {
if(B[i] !== "E" && B[i] === B[i + 1] && B[i + 1] == B[i + 2]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
//check columns
for(var i = 0; i <= 2 ; i++) {
if(B[i] !== "E" && B[i] === B[i + 3] && B[i + 3] === B[i + 6]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
//check diagonals
for(var i = 0, j = 4; i <= 2 ; i = i + 2, j = j - 2) {
if(B[i] !== "E" && B[i] == B[i + j] && B[i + j] === B[i + 2*j]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
var available = this.emptyCells();
if(available.length == 0) {
//the game is draw
this.result = "draw"; //update the state result
return true;
}
else {
return false;
}
};
只要有一个布尔运算符,并在一个回合后将其更改为启用
/*
* public : the player who has the turn to player
*/
this.turn = "";
/*
* public : the number of moves of the AI player
*/
this.oMovesCount = 0;
/*
* public : the result of the game in this State
*/
this.result = "still running";
/*
* public : the board configuration in this state
*/
this.board = [];
/* Begin Object Construction */
if(typeof old !== "undefined") {
// if the state is constructed using a copy of another state
var len = old.board.length;
this.board = new Array(len);
for(var itr = 0 ; itr < len ; itr++) {
this.board[itr] = old.board[itr];
}
this.oMovesCount = old.oMovesCount;
this.result = old.result;
this.turn = old.turn;
}
/* End Object Construction */
/*
* public : advances the turn in a the state
*/
this.advanceTurn = function() {
this.turn = this.turn === "X" ? "O" : "X";
}
/*
* public function that enumerates the empty cells in state
* @return [Array]: indices of all empty cells
*/
this.emptyCells = function() {
var indxs = [];
for(var itr = 0; itr < 9 ; itr++) {
if(this.board[itr] === "E") {
indxs.push(itr);
}
}
return indxs;
}
/*
* public function that checks if the state is a terminal state or not
* the state result is updated to reflect the result of the game
* @returns [Boolean]: true if it's terminal, false otherwise
*/
this.isTerminal = function() {
var B = this.board;
//check rows
for(var i = 0; i <= 6; i = i + 3) {
if(B[i] !== "E" && B[i] === B[i + 1] && B[i + 1] == B[i + 2]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
//check columns
for(var i = 0; i <= 2 ; i++) {
if(B[i] !== "E" && B[i] === B[i + 3] && B[i + 3] === B[i + 6]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
//check diagonals
for(var i = 0, j = 4; i <= 2 ; i = i + 2, j = j - 2) {
if(B[i] !== "E" && B[i] == B[i + j] && B[i + j] === B[i + 2*j]) {
this.result = B[i] + "-won"; //update the state result
return true;
}
}
var available = this.emptyCells();
if(available.length == 0) {
//the game is draw
this.result = "draw"; //update the state result
return true;
}
else {
return false;
}
};
一个简单的方法是:
请注意:在这种情况下,“o”玩家在移动中始终具有作为第一玩家的优先权(但您可以简单地更改它或要求用户选择第一个玩家)
js:
var isX = true;
function ticTac(){
if (this.innerHTML == "-"){
if(isX){
this.innerHTML = "X";
} else {
this.innerHTML = "O";
}
isX = !isX;
}
}
document.getElementById("c1").onclick = ticTac;
document.getElementById("c2").onclick = ticTac;
document.getElementById("c3").onclick = ticTac;
document.getElementById("c4").onclick = ticTac;
document.getElementById("c5").onclick = ticTac;
document.getElementById("c6").onclick = ticTac;
document.getElementById("c7").onclick = ticTac;
document.getElementById("c8").onclick = ticTac;
document.getElementById("c9").onclick = ticTac;
一个简单的方法是:
请注意:在这种情况下,“o”玩家在移动中始终具有作为第一玩家的优先权(但您可以简单地更改它或要求用户选择第一个玩家)
js:
var isX = true;
function ticTac(){
if (this.innerHTML == "-"){
if(isX){
this.innerHTML = "X";
} else {
this.innerHTML = "O";
}
isX = !isX;
}
}
document.getElementById("c1").onclick = ticTac;
document.getElementById("c2").onclick = ticTac;
document.getElementById("c3").onclick = ticTac;
document.getElementById("c4").onclick = ticTac;
document.getElementById("c5").onclick = ticTac;
document.getElementById("c6").onclick = ticTac;
document.getElementById("c7").onclick = ticTac;
document.getElementById("c8").onclick = ticTac;
document.getElementById("c9").onclick = ticTac;
只需设置一个存储当前玩家的全局变量,并在每次回合后更改玩家:
var whosTurn = 'x';
function whoIsTurn(){
if(whosTurn == 'x'){
whosTurn = 'o';
} else {
whosTurn = 'x';
}
return whosTurn;
}
function ticTac(){
if (this.innerHTML == "-"){
this.innerHTML = whoIsTurn();
}
}
document.getElementById("c1").onclick = ticTac;
document.getElementById("c2").onclick = ticTac;
document.getElementById("c3").onclick = ticTac;
document.getElementById("c4").onclick = ticTac;
document.getElementById("c5").onclick = ticTac;
document.getElementById("c6").onclick = ticTac;
document.getElementById("c7").onclick = ticTac;
document.getElementById("c8").onclick = ticTac;
document.getElementById("c9").onclick = ticTac;
只需设置一个存储当前玩家的全局变量,并在每次回合后更改玩家:
var whosTurn = 'x';
function whoIsTurn(){
if(whosTurn == 'x'){
whosTurn = 'o';
} else {
whosTurn = 'x';
}
return whosTurn;
}
function ticTac(){
if (this.innerHTML == "-"){
this.innerHTML = whoIsTurn();
}
}
document.getElementById("c1").onclick = ticTac;
document.getElementById("c2").onclick = ticTac;
document.getElementById("c3").onclick = ticTac;
document.getElementById("c4").onclick = ticTac;
document.getElementById("c5").onclick = ticTac;
document.getElementById("c6").onclick = ticTac;
document.getElementById("c7").onclick = ticTac;
document.getElementById("c8").onclick = ticTac;
document.getElementById("c9").onclick = ticTac;
在每个td
标签中添加一个按钮,单击一次后将其禁用。这将完成工作
document.getElementById(id).disabled=true;
有关更多信息,请按照MyGitHub中的代码进行操作
在每个td
标签中添加一个按钮,单击一次后将其禁用。这将完成工作
document.getElementById(id).disabled=true;
有关更多信息,请按照MyGitHub中的代码进行操作
检查此项,假设您将id“board”放在表中,而不是逐个单元格分配相同的函数,您可以使用document.getElementById(“board”).onclick=ticTac;
,然后在函数ticTac中:函数ticTac(e){if(this.innerHTML…}
replacethis
bye.target
检查此项假设您将id“board”放在表中,而不是逐个单元格分配相同的函数,您可以在一行中使用document.getElementById(“board”).onclick=ticTac;
,然后在函数ticTac:函数ticTac(e){if(this.innerHTML…}
用e.target