我正在用 javascript 制作井字游戏。我列出了一些我认为是基本构建 block 的内容:
var board = [
0, 0, 0,
0, 0, 0,
0, 0, 0
];
gameOn = true;
player1Move = true;
counter = 0;
player1 = [];
player2 = [];
var ask = console.log(prompt('where would you like to go?'));
var drawBoard = function(){
console.log(" A " + (board[0]) + "| B " + (board[1]) + "| C " + (board[2]));
console.log(" ------------- ");
console.log(" D " + (board[3]) + "| E " + (board[4]) + "| F " + (board[5]));
console.log(" ------------- ");
console.log(" G " + (board[6]) + "| H " + (board[7]) + "| I " + (board[8]));
console.log(" ------------- ");
};
var solutions = [
(board[0] && board[1] && board[2]) || (board[3] && board[4] && board[5]) ||
(board[6] && board[7] && board[8]) || (board[0] && board[3] && board[6]) ||
(board[1] && board[4] && board[7]) || (board[2] && board[5] && board[8]) ||
(board[0] && board[4] && board[8]) || (board[2] && board[4] && board[6])
];
while (gameOn === true){
// for loop for game logic
for (var i = 0 ; i < 9; i++){
if (player1Move === true) {
player1.push(ask);
drawBoard();
player1Move = false;
} else if (player1Move === false){
player2.push(ask);
drawBoard();
player1Move = true;
} else if ((player1 || player2) === solutions){
gameOn = false;
console.log((player1 || player2) + "wins!");
} else {
gameOn = false;
console.log("Tie Game!");
}
}
}
我知道我的主循环的语法不正确。为什么我不能做当前写的?如果您有此现有代码,您将使用哪种类型的循环设置来完成此操作?抱歉,如果这看起来不够具体,我只是迷失在这一切的逻辑中!
对于这个现有的循环,我现在要做的就是遍历并提示 2 个用户 9 次(因为最大移动数 = 9)。一旦成功,我将继续向其中添加更多游戏逻辑。为什么我上来9次都提示不上来?现在的结果是提示了一次,打印了9次板子,全部都是false。
顺便说一句,这现在也只需要在终端窗口中工作。
最佳答案
我尽可能少地更改了您的代码,但有些地方有所不同,我正在逐一解释。
var board = {
A: null,
B: null,
C: null,
D: null,
E: null,
F: null,
G: null,
H: null,
I: null
};
gameOn = true;
player1Move = true;
var drawBoard = function(){
console.log(" A " + (board.A || '') + "| B " + (board.B || '') + "| C " + (board.C || ''));
console.log(" ------------- ");
console.log(" D " + (board.D || '') + "| E " + (board.E || '') + "| F " + (board.F || ''));
console.log(" ------------- ");
console.log(" G " + (board.G || '') + "| H " + (board.H || '') + "| I " + (board.I || ''));
console.log(" ------------- ");
};
var solutions = function() {
return (board.A && (board.A == board.B && board.A == board.C))
|| (board.D && (board.D == board.E && board.D == board.F))
|| (board.G && (board.G == board.H && board.G == board.I));
};
drawBoard();
var currentPlayer;
while (gameOn === true){
// for loop for game logic
for (var i = 0 ; i < 9; i++){
if (solutions()){
console.log(currentPlayer + " wins!");
gameOn = false;
break;
}
//else {
// gameOn = false;
// console.log("Tie Game!");
//}
currentPlayer = 'Player 1';
if(!player1Move)
currentPlayer = 'Player 2';
var ask = prompt(currentPlayer + ': where would you like to go (A or B or C or ..)?');
if(ask == 'exit') {
gameOn = false;
break;
}
if (player1Move === true) {
//player1.push(ask);
board[ask] = 'X';
drawBoard();
player1Move = false;
} else if (player1Move === false){
board[ask] = 'O';
drawBoard();
player1Move = true;
}
}
}
这还没有完全完成,但那是你之后可以做的事情,可能你不希望我为你做这一切。
这一切都在 Chrome 控制台中运行。
有一些基本的东西阻碍了你:
var ask = console.log(prompt('where would you like to go?'));
应该是var ask = prompt(currentPlayer + ': where would you like to go? go?');
- 你的版本正在用 null 填充ask
。- 这个提示也放错了地方:它需要在循环中,在 drawBoard() 之后,让玩家有东西可看。
- 我已将棋盘从一个数组更改为一个对象,这样像“A”这样的玩家答案直接指向对象字段。这样就可以删除播放器数组。
- 如您所见,
solutions()
函数需要完成,可能有更简洁的方法来完成此操作。 - 我增加了在提示中写'exit'的可能性,否则无法提前退出游戏。
- 我没有完成“领带游戏!”代码。
关于javascript - 井字游戏循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33069751/