This is function control Table.
/**
* @constructor
* @param {Number} width - dimension width for table
* @param {Number} height - dimension height for table
*/
function Table(width, height) {
this.table = [];
for (var x = 0; x < height; x++) {
this.table[x] = [];
for (var y = 0; y < width; y++) {
this.table[x][y] = ' ';
}
}
console.log('test', this.table);
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
*/
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) {
this.table[x][y] = ch;
},
/**
* 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) {
return !this.table[x][y];
},
};
/**
* @constructor
* @param {String} character - X or O
*/
function Player(name, ch) {
this.name = name;
this.ch = ch;
}
var Printer = function () {
};
This is the function print to console .
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) {
},
};
/**
* @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();
};
The GamePlay Control Table and Player.
GamePlay.prototype = {
run: function () {
console.log('Game start ...!');
var x = Math.floor(Math.random() * 3);
var y = Math.floor(Math.random() * 3);
this.putChessman(x, y, this.playerOne.ch);
this.printer.printTable(this.table);
if (ch === 'O') {
this.putChessman(x, y, this.playerTwo.ch);
} else {
this.putChessman(x, y, this.playerOne.ch);
}
},
This is CheckWin .
/**
* @param player [keywork] : the keywork X and O
*/
checkWin: function (table) {
var winner = 0;
//check each row for a winner
if(table[0][0] == table[0][1] && table[0][0] == table[0][2] && table[0][0] != 0) {
winner = table[0][0];
}
// check row 2 for winner
if (table[1][0] == table[1][1] && table[1][0] == table[1][2] && table[1][0] != 0 ){
winner = table[1][0];
}
// check row 3 for winner
if (table[2][0] == table[2][1] && table[2][0] == table[2][2] && table[2][0] != 0 ){
winner = table[2][0];
};
// check column 1 for winner
if (table[0][0] == table[1][0] && table[0][0] == table[2][0] && table[0][0] != 0 ){
winner = table[0][0];
}
// check column 2 for winner
if (table[0][1] == table[1][1] && table[0][1] == table[2][1] && table[0][1] != 0 ){
winner = table[0][1];
}
// check column 3 for winner
if (table[0][2] == table[1][2] && table[0][2] == table[2][2] && table[0][2] != 0 ){
winner = table[0][2];
}
//check each diagnoal for a winner
if (table[0][0] == table[1][1] && table[0][0] == table[2][2] && table[0][0] != 0 ){
winner = table[0][0];
}
// check second diagonal for winner
if (table[0][2] == table[1][1] && table[0][2] == table[2][0] && table[0][2] != 0 ){
winner = table[0][2];
}
return winner;
},
This is function put chess man . But it always turn else
.
putChessman: function (x, y, ch) {
if (this.table.isEmptyAt(x, y) === true) {
console.log('@ player ' + ch + ' put');
this.table.setPosition(x, y, ch);
if (ch === 'O') {
result = this.putChessman(x, y, this.playerTwo.ch);
if (result) {
ch = 'X';
}
} else {
result = this.putChessman(x, y, this.playerOne.ch);
if (result) {
ch = 'O';
}
}
} 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();