【发布时间】:2017-10-16 11:26:03
【问题描述】:
我使用 OOP javascript 创建演示游戏井字游戏。但是我在获取具有附加值的表的值时遇到问题,然后在console.log();中显示表
这是我的代码:
/**
* @constructor
* @param {Number} width - dimension width for table
* @param {Number} height - dimension height for table
*/
function Table(width, height) {
this.table = new Array(height * width);
this.width = width;
this.height = height;
}
Table.prototype = {
/**
* Representation for get width of table
*/
getWidth: function () {
return this.width;
},
/**
* Representation for get height of table
*/
getHeight: function () {
return this.height;
},
/**
* Representation for get table array 2d
*/
getTable: function () {
var x = new Array(this.getHeight());
for (var i = 0; i < this.getHeight(); i++) {
x[i] = new Array(this.getWidth());
};
},
/**
* Representation for set position of table
*/
setPosition: function (x, y, ch) {
return this.table[x][y] = ch;
},
我这里有问题。我无法在餐桌上获得价值并检查 isEmpty。
/**
* Representation for get value detail at cell of table
*/
getValueAt: function (x, y) {
return this.table[x][y];
},
/**
* Representation for check empty conditional
*/
isEmptyAt: function (x, y) {
if (!this.table[x][y])
return true;
},
};
/**
* @constructor
* @param {String} character - X or O
*/
function Player(name, ch) {
this.name = name;
this.ch = ch;
}
var Printer = function () {
};
这是 console.log() 中的函数打印显示。
Printer.prototype = {
/**
* Representation print table
*/
printTable: function (table) {
var str = '';
for (var i = 0; i < table.width; i++) {
var x = i;
for (var j = 0; j < table.height; j++) {
var y = j;
str += '' + table.getValueAt(x, y) + '|';
}
str += '\n------------\n';
}
console.log(str);
},
/**
* Representation check winner conditional
*/
printWinner: function (player) {
},
};
Math.floor(Math.random() * 2);
/**
* @param newTable [array] : The array two-direction table
* @param player [object] : the object contain player X and O
*/
var GamePlay = function (table, playerOne, playerTwo) {
this.table = table;
this.playerOne = playerOne;
this.playerTwo = playerTwo;
this.printer = new Printer();
};
GamePlay.prototype = {
run: function (x, y) {
console.log('Game start ...!');
x = Math.floor(Math.random() * 2);
y = Math.floor(Math.random() * 2);
this.putChessman(x, y, this.playerOne.ch);
console.log('put', this.putChessman());
this.printer.printTable(this.table);
},
/**
* @param player [keywork] : the keywork X and O
*/
checkWin: function (player) {
},
putChessman: function (x, y, ch) {
console.log('isEmptyAt', table.isEmptyAt(x, y));
if (this.table.isEmptyAt(x, y) === true) {
console.log('@ player ' + ch + ' put');
this.table.setPosition(x, y, ch);
} else {
console.log('@ Other player already put on it');
}
},
};
var table = new Table(3, 3);
var playerOne = new Player('playerOne', 'O');
var playerTwo = new Player('playerTwo', 'X');
var game = new GamePlay(table, playerOne, playerTwo);
game.run();
请帮我解决问题-.-
【问题讨论】:
-
您可以像访问二维数组一样访问您的表,但您不会像那样初始化它。新数组(高度 * 宽度)。这意味着评估宽度 * 高度,然后创建一个具有结果长度的一维数组。也许这已经是问题所在了……否则:你能发布你得到的错误吗?
标签: javascript arrays oop