我正在构建一个可以玩AI的tic tac toe游戏。在$scope.move function()
中有一个while循环,它抓取一个随机单元格并使其成为AI的值。不知怎的,这不起作用。这是代码链http://codepen.io/theMugician/pen/ojJrRp
var app = angular.module("ticTacToe", []);
app.controller("MainCtrl", function($scope){
var cell = $(".square");
$scope.player = "";
$scope.AI = "";
var cross = "×";
var circle = "◯";
/*** Choose a shape ***/
$scope.choosePlayer = function(e) {
$scope.player = $(e.currentTarget).text();
$('.choose').css('top', '-2000px');
$('#wrapper').css('top', '-600px');
$('#wrapper').css('opacity', '1');
if($scope.player === "×"){
$scope.AI = "◯";
}else if($scope.player === "◯"){
$scope.AI = "×";
}
}
/*** Shape Cells ***/
$scope.cells = [ { value: '' }, { value: '' }, { value: '' },
{ value: '' }, { value: '' }, { value: '' } ,
{ value: '' }, { value: '' }, { value: '' }
];
/*** Make a move ***/
$scope.move = function(cell){
cell.value = $scope.player;
var round = 0;
/*** AI makes a move ***/
while(round < 1){
var randomCell = $scope.cells[Math.floor((Math.random()*8)+1)];
if(randomCell.value === "" ){
randomCell.value = $scope.AI;
round = 1;
}else{
round = 0;
}
}
};
});
答案 0 :(得分:2)
我已经更改了一些代码并在我看到问题的地方添加了评论
var app = angular.module("ticTacToe", []);
app.controller("MainCtrl", function($scope){
var cell = $(".square");
$scope.player = "";
$scope.AI = "";
// changed special chars to X and O as the if statement failed.
var cross = "X";
var circle = "O";
/*** Choose a shape ***/
$scope.choosePlayer = function(e) {
$scope.player = $(e.currentTarget).text();
$('.choose').css('top', '-2000px');
$('#wrapper').css('top', '-600px');
$('#wrapper').css('opacity', '1');
//these if statements failed before (AI was always empty)
if($scope.player === cross){
$scope.AI = circle;
}else if($scope.player === circle){
$scope.AI = cross;
}
}
/*** Shape Cells ***/
$scope.cells = [ { value: '' }, { value: '' }, { value: '' },
{ value: '' }, { value: '' }, { value: '' } ,
{ value: '' }, { value: '' }, { value: '' }
];
// made a ref to scope cells
$scope.emptyCells = $scope.cells;
/*** Make a move ***/
$scope.move = function(cell){
cell.value = $scope.player;
var round = 0;
/*** AI makes a move ***/
while(round < 1){
// filtered to get only available cells (for performance)
$scope.emptyCells = $scope.cells.filter(function(cell){
return cell.value === '';
});
// got random cell according to empty cells
var randomCell = $scope.emptyCells[Math.floor((Math.random()*($scope.emptyCells.length-1))+1)];
if(randomCell.value === "" ){
randomCell.value = $scope.AI;
round = 1;
}else{
round = 0;
}
}
};
});
还需要更改HTML:
<button ng-click="choosePlayer($event)" class="btn btn-red" id="choose-cross">X</button>
<button ng-click="choosePlayer($event)" class="btn btn-green" id="choose-circle">O</button>
答案 1 :(得分:1)
您使用错误的unicode字符进行基于HTML的十字架。将其更改为✖,它将起作用。
避免使用“魔法值”。只需将circle
和var app = angular.module("ticTacToe", []);
app.controller("MainCtrl", function($scope){
var cell = $(".square");
$scope.player = "";
$scope.AI = "";
// *** fixed unicode char
var cross = "✖";
var circle = "◯";
/*** Choose a shape ***/
$scope.choosePlayer = function(e) {
$scope.player = $(e.currentTarget).text();
$('.choose').css('top', '-2000px');
$('#wrapper').css('top', '-600px');
$('#wrapper').css('opacity', '1');
// *** use correct unicode chars above
if($scope.player === cross){
$scope.AI = circle;
}else if($scope.player === circle){
$scope.AI = cross;
}
}
/*** Shape Cells ***/
$scope.cells = [ { value: '' }, { value: '' }, { value: '' },
{ value: '' }, { value: '' }, { value: '' } ,
{ value: '' }, { value: '' }, { value: '' }
];
/*** Make a move ***/
$scope.move = function(cell){
cell.value = $scope.player;
var round = 0;
/*** AI makes a move ***/
while(round < 1){
// *** random select fix
var randomCell = $scope.cells[Math.floor((Math.random()*9))];
if(randomCell.value === "" ){
randomCell.value = $scope.AI;
round = 1;
}else{
round = 0;
}
}
};
});
分配给正确的值一次,然后在代码中的其他位置参考交叉和循环。这将有助于防止此类错误,因为这些值将具有一个参考点,并且可以轻松更改,而不是挖掘代码并更改所有不正确的字符串文字。
理想情况下,最佳做法是将所有内容都基于一个中心参考点。因此,您的HTML应该引用JS中的变量,或者您的JS应该引用HTML中的文本节点。这是一个称为DRY或Dont Repeat Yourself的软件开发原则,基本上它意味着代码中唯一的重复应该是对其他代码的引用。不应重复字符串文字。而是将引用重复到该字符串文字。
演示:http://codepen.io/anon/pen/GpbaLz
更新了JS:
{{1}}