私はOOPのjavascriptでデモゲームチックタックトウを作成します。しかし、私はその後、console.log();
console.log()の表に値を表示する方法は?
でテーブルを表示する値を添付したテーブルでGET値に問題がある。これは私のコードです:
/**
* @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();
私はあなたが正しくtable
を初期化していない
2次元配列のようにテーブルにアクセスしますが、そのように初期化しません。新しい配列(高さ*幅)。これはwidth * heightが評価され、結果の長さを持つ1次元配列が作成されることを意味します。たぶんそれはすでに問題です...そうでなければ:あなたが得るエラーを投稿できますか? – Fuzzzzel