<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table {border-collapse: collapse;}
table tr td {padding: 0; border: 1px solid black; text-align: center; vertical-align: middle; cursor: default;}
table#scores tr td {width: 65px; height: 30px;}
table#play_field tr td {width: 50px; height: 50px;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
function newArray () {
return [
[
[0, 0], [0, 1], [0, 2]
],
[
[1, 0], [1, 1], [1, 2]
],
[
[2, 0], [2, 1], [2, 2]
]
];
}
var empty_cells = newArray ();
// Random number from range function
function randomNumber (low_limit, high_limit) {
return Math.round(Math.random()*(high_limit - low_limit)) + low_limit;
}
$(document).ready(function(){
var current_player = "PlayerX";
var current_game_turn = 1;
var max_game_turns_number = 9;
var current_game_round = 1;
var max_game_rounds_number = 5;
var scores_table = '<table id="scores">' +
'<tr><td>Round</td><td>Player X</td><td>Player O</td></tr>';
for (var i = 0; i < max_game_rounds_number; i++) {
scores_table += '<tr><td>' + ( i + 1) + '</td><td></td><td></td></tr>';
}
scores_table += '</table>';
var play_field_table = '<table id="play_field">' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'</table>';
var round_field = '<p id="round">Round: <span>1</span></p>';
$("body").append(round_field).append(scores_table).append('<br />').append(play_field_table);
function refreshScoresTable () {
for (var i = 0; i < 5; i++) {
$("table#scores tr:eq(" + (i + 1) + ") td:eq(1)").text("");
$("table#scores tr:eq(" + (i + 1) + ") td:eq(2)").text("");
}
}
function refreshPlayFieldTable () {
$("table#play_field tr td").text("").removeClass("checked");
}
var classPlayer = function () {};
classPlayer.prototype = {
scores: 0
};
var PlayerX = new classPlayer();
var PlayerO = new classPlayer();
function winCheck() {
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "O")
) {
return "Player O Win";
}
for (var i=0; i < 3; i++) {
// Horizontal Win X X X
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Horizontal Win O O O
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Vertical Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "O")
) {
return "Player O Win";
}
}
return "Nobody Win";
};
function AIplayer () {
var random_row, random_cell, row, cell;
function checkCell () {
random_row = randomNumber (0, 2);
random_cell = randomNumber (0, 2); console.log("[" + random_row + "," + random_cell + "]");
if (empty_cells[random_row][random_cell] == undefined) {
for (var i=0; i < 3; i++) {
for (var j=0; j < 3; j++) {console.log("i: " + i + ", j: " + j);
if (empty_cells[i][j] != undefined) {
row = i;
cell = j;
break;
}
}
}
} else {
row = random_row;
cell = random_cell;
}
}
checkCell ();
$("table#play_field tr:eq(" + empty_cells[row][cell][0] + ") td:eq(" + empty_cells[row][cell][1] + ")").text("O").addClass("checked");
delete empty_cells[row][cell];
current_player = "PlayerX";
}
$("table#play_field tr td").click(function(){
if (!$(this).hasClass("checked")) {
if (current_player == "PlayerX") {
$(this).text("X").addClass("checked");
delete empty_cells[$(this).parent().index()][$(this).index()];
current_player = "PlayerO";
checkWinConditions ();
setTimeout (function() {
AIplayer ();
checkWinConditions ();
}, 1000);
}
}
function checkWinConditions () {
console.log(empty_cells);
current_game_turn++;
if(winCheck() == "Nobody Win") {
if (current_game_turn != (max_game_turns_number + 1)) {
// Game round continue.
} else {
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Draw");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Draw");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
} else if (winCheck() == "Player X Win") {
PlayerX.scores = PlayerX.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Win");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Lose");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
} else if (winCheck() == "Player O Win") {
PlayerO.scores = PlayerO.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Lose");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Win");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
if (current_game_round == (max_game_rounds_number + 1)) {
refreshPlayFieldTable ();
empty_cells = newArray ();
if (PlayerX.scores > PlayerO.scores) {
alert("Game End. Player X Win.");
} else if (PlayerX.scores < PlayerO.scores) {
alert("Game End. Player O Win.");
} else if (PlayerX.scores == PlayerO.scores) {
alert("Game End. Draw.");
}
refreshScoresTable ();
current_game_turn = 1;
current_game_round = 1;
$("p#round span").text(current_game_round);
}
}
});
});
</script>
<title>XO Game</title>
</head>
<body>
</body>
</html>
пятница, 30 ноября 2012 г.
JavaScript Крестики-Нолики с AI
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table {border-collapse: collapse;}
table tr td {padding: 0; border: 1px solid black; text-align: center; vertical-align: middle; cursor: default;}
table#scores tr td {width: 65px; height: 30px;}
table#play_field tr td {width: 50px; height: 50px;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
function newArray () {
return [
[
[0, 0], [0, 1], [0, 2]
],
[
[1, 0], [1, 1], [1, 2]
],
[
[2, 0], [2, 1], [2, 2]
]
];
}
var empty_cells = newArray ();
// Random number from range function
function randomNumber (low_limit, high_limit) {
return Math.round(Math.random()*(high_limit - low_limit)) + low_limit;
}
$(document).ready(function(){
var current_player = "PlayerX";
var current_game_turn = 1;
var max_game_turns_number = 9;
var current_game_round = 1;
var max_game_rounds_number = 5;
var scores_table = '<table id="scores">' +
'<tr><td>Round</td><td>Player X</td><td>Player O</td></tr>';
for (var i = 0; i < max_game_rounds_number; i++) {
scores_table += '<tr><td>' + ( i + 1) + '</td><td></td><td></td></tr>';
}
scores_table += '</table>';
var play_field_table = '<table id="play_field">' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'</table>';
var round_field = '<p id="round">Round: <span>1</span></p>';
$("body").append(round_field).append(scores_table).append('<br />').append(play_field_table);
function refreshScoresTable () {
for (var i = 0; i < 5; i++) {
$("table#scores tr:eq(" + (i + 1) + ") td:eq(1)").text("");
$("table#scores tr:eq(" + (i + 1) + ") td:eq(2)").text("");
}
}
function refreshPlayFieldTable () {
$("table#play_field tr td").text("").removeClass("checked");
}
var classPlayer = function () {};
classPlayer.prototype = {
scores: 0
};
var PlayerX = new classPlayer();
var PlayerO = new classPlayer();
function winCheck() {
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "O")
) {
return "Player O Win";
}
for (var i=0; i < 3; i++) {
// Horizontal Win X X X
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Horizontal Win O O O
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Vertical Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "O")
) {
return "Player O Win";
}
}
return "Nobody Win";
};
function AIplayer () {
var random_row, random_cell, row, cell;
function checkCell () {
random_row = randomNumber (0, 2);
random_cell = randomNumber (0, 2); console.log("[" + random_row + "," + random_cell + "]");
if (empty_cells[random_row][random_cell] == undefined) {
for (var i=0; i < 3; i++) {
for (var j=0; j < 3; j++) {console.log("i: " + i + ", j: " + j);
if (empty_cells[i][j] != undefined) {
row = i;
cell = j;
break;
}
}
}
} else {
row = random_row;
cell = random_cell;
}
}
checkCell ();
$("table#play_field tr:eq(" + empty_cells[row][cell][0] + ") td:eq(" + empty_cells[row][cell][1] + ")").text("O").addClass("checked");
delete empty_cells[row][cell];
current_player = "PlayerX";
}
$("table#play_field tr td").click(function(){
if (!$(this).hasClass("checked")) {
if (current_player == "PlayerX") {
$(this).text("X").addClass("checked");
delete empty_cells[$(this).parent().index()][$(this).index()];
current_player = "PlayerO";
} else if (current_player == "PlayerO") {
AIplayer ();
}
console.log(empty_cells);
current_game_turn++;
if(winCheck() == "Nobody Win") {
if (current_game_turn != (max_game_turns_number + 1)) {
// Game round continue.
} else {
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Draw");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Draw");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
} else if (winCheck() == "Player X Win") {
PlayerX.scores = PlayerX.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Win");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Lose");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
} else if (winCheck() == "Player O Win") {
PlayerO.scores = PlayerO.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Lose");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Win");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
if (current_game_round == (max_game_rounds_number + 1)) {
refreshPlayFieldTable ();
empty_cells = newArray ();
if (PlayerX.scores > PlayerO.scores) {
alert("Game End. Player X Win.");
} else if (PlayerX.scores < PlayerO.scores) {
alert("Game End. Player O Win.");
} else if (PlayerX.scores == PlayerO.scores) {
alert("Game End. Draw.");
}
refreshScoresTable ();
current_game_turn = 1;
current_game_round = 1;
$("p#round span").text(current_game_round);
}
}
});
});
</script>
<title>XO Game</title>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table {border-collapse: collapse;}
table tr td {padding: 0; border: 1px solid black; text-align: center; vertical-align: middle; cursor: default;}
table#scores tr td {width: 65px; height: 30px;}
table#play_field tr td {width: 50px; height: 50px;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
function newArray () {
return [
[
[0, 0], [0, 1], [0, 2]
],
[
[1, 0], [1, 1], [1, 2]
],
[
[2, 0], [2, 1], [2, 2]
]
];
}
var empty_cells = newArray ();
// Random number from range function
function randomNumber (low_limit, high_limit) {
return Math.round(Math.random()*(high_limit - low_limit)) + low_limit;
}
$(document).ready(function(){
var current_player = "PlayerX";
var current_game_turn = 1;
var max_game_turns_number = 9;
var current_game_round = 1;
var max_game_rounds_number = 5;
var scores_table = '<table id="scores">' +
'<tr><td>Round</td><td>Player X</td><td>Player O</td></tr>';
for (var i = 0; i < max_game_rounds_number; i++) {
scores_table += '<tr><td>' + ( i + 1) + '</td><td></td><td></td></tr>';
}
scores_table += '</table>';
var play_field_table = '<table id="play_field">' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'</table>';
var round_field = '<p id="round">Round: <span>1</span></p>';
$("body").append(round_field).append(scores_table).append('<br />').append(play_field_table);
function refreshScoresTable () {
for (var i = 0; i < 5; i++) {
$("table#scores tr:eq(" + (i + 1) + ") td:eq(1)").text("");
$("table#scores tr:eq(" + (i + 1) + ") td:eq(2)").text("");
}
}
function refreshPlayFieldTable () {
$("table#play_field tr td").text("").removeClass("checked");
}
var classPlayer = function () {};
classPlayer.prototype = {
scores: 0
};
var PlayerX = new classPlayer();
var PlayerO = new classPlayer();
function winCheck() {
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "O")
) {
return "Player O Win";
}
for (var i=0; i < 3; i++) {
// Horizontal Win X X X
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Horizontal Win O O O
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Vertical Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "O")
) {
return "Player O Win";
}
}
return "Nobody Win";
};
function AIplayer () {
var random_row, random_cell, row, cell;
function checkCell () {
random_row = randomNumber (0, 2);
random_cell = randomNumber (0, 2); console.log("[" + random_row + "," + random_cell + "]");
if (empty_cells[random_row][random_cell] == undefined) {
for (var i=0; i < 3; i++) {
for (var j=0; j < 3; j++) {console.log("i: " + i + ", j: " + j);
if (empty_cells[i][j] != undefined) {
row = i;
cell = j;
break;
}
}
}
} else {
row = random_row;
cell = random_cell;
}
}
checkCell ();
$("table#play_field tr:eq(" + empty_cells[row][cell][0] + ") td:eq(" + empty_cells[row][cell][1] + ")").text("O").addClass("checked");
delete empty_cells[row][cell];
current_player = "PlayerX";
}
$("table#play_field tr td").click(function(){
if (!$(this).hasClass("checked")) {
if (current_player == "PlayerX") {
$(this).text("X").addClass("checked");
delete empty_cells[$(this).parent().index()][$(this).index()];
current_player = "PlayerO";
} else if (current_player == "PlayerO") {
AIplayer ();
}
console.log(empty_cells);
current_game_turn++;
if(winCheck() == "Nobody Win") {
if (current_game_turn != (max_game_turns_number + 1)) {
// Game round continue.
} else {
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Draw");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Draw");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
} else if (winCheck() == "Player X Win") {
PlayerX.scores = PlayerX.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Win");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Lose");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
} else if (winCheck() == "Player O Win") {
PlayerO.scores = PlayerO.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Lose");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Win");
refreshPlayFieldTable ();
empty_cells = newArray ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
if (current_game_round == (max_game_rounds_number + 1)) {
refreshPlayFieldTable ();
empty_cells = newArray ();
if (PlayerX.scores > PlayerO.scores) {
alert("Game End. Player X Win.");
} else if (PlayerX.scores < PlayerO.scores) {
alert("Game End. Player O Win.");
} else if (PlayerX.scores == PlayerO.scores) {
alert("Game End. Draw.");
}
refreshScoresTable ();
current_game_turn = 1;
current_game_round = 1;
$("p#round span").text(current_game_round);
}
}
});
});
</script>
<title>XO Game</title>
</head>
<body>
</body>
</html>
четверг, 29 ноября 2012 г.
JavaScript игра Крестики-Нолики
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table {border-collapse: collapse;}
table tr td {padding: 0; border: 1px solid black; text-align: center; vertical-align: middle; cursor: default;}
table#scores tr td {width: 65px; height: 30px;}
table#play_field tr td {width: 50px; height: 50px;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var current_player = "PlayerX";
var current_game_turn = 1;
var max_game_turns_number = 9;
var current_game_round = 1;
var max_game_rounds_number = 5;
var scores_table = '<table id="scores">' +
'<tr><td>Round</td><td>Player X</td><td>Player O</td></tr>';
for (var i = 0; i < max_game_rounds_number; i++) {
scores_table += '<tr><td>' + ( i + 1) + '</td><td></td><td></td></tr>';
}
scores_table += '</table>';
var play_field_table = '<table id="play_field">' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'</table>';
var round_field = '<p id="round">Round: <span>1</span></p>';
$("body").append(round_field).append(scores_table).append('<br />').append(play_field_table);
function refreshScoresTable () {
for (var i = 0; i < 5; i++) {
$("table#scores tr:eq(" + (i + 1) + ") td:eq(1)").text("");
$("table#scores tr:eq(" + (i + 1) + ") td:eq(2)").text("");
}
}
function refreshPlayFieldTable () {
$("table#play_field tr td").text("").removeClass("checked");
}
var classPlayer = function () {};
classPlayer.prototype = {
scores: 0
};
var PlayerX = new classPlayer();
var PlayerO = new classPlayer();
function winCheck() {
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "O")
) {
return "Player O Win";
}
for (var i=0; i < 3; i++) {
// Horizontal Win X X X
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Horizontal Win O O O
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Vertical Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "O")
) {
return "Player O Win";
}
}
return "Nobody Win";
};
$("table#play_field tr td").click(function(){ console.log("Round: " + current_game_round + " Turn: " + current_game_turn + " CurrentPlayer: " + current_player);
if (!$(this).hasClass("checked")) {
if (current_player == "PlayerX") {
$(this).text("X").addClass("checked");
current_player = "PlayerO";
} else if (current_player == "PlayerO") {
$(this).text("O").addClass("checked");
current_player = "PlayerX";
}
current_game_turn++;
if(winCheck() == "Nobody Win") {
if (current_game_turn != (max_game_turns_number + 1)) {
// Game round continue.
} else {
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Draw");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Draw");
refreshPlayFieldTable ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
} else if (winCheck() == "Player X Win") {
PlayerX.scores = PlayerX.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Win");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Lose");
refreshPlayFieldTable ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
} else if (winCheck() == "Player O Win") {
PlayerO.scores = PlayerO.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Lose");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Win");
refreshPlayFieldTable ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
if (current_game_round == (max_game_rounds_number + 1)) {
refreshPlayFieldTable ();
if (PlayerX.scores > PlayerO.scores) {
alert("Game End. Player X Win.");
} else if (PlayerX.scores < PlayerO.scores) {
alert("Game End. Player O Win.");
} else if (PlayerX.scores == PlayerO.scores) {
alert("Game End. Draw.");
}
refreshScoresTable ();
current_game_turn = 1;
current_game_round = 1;
$("p#round span").text(current_game_round);
}
}
console.log("Round: " + current_game_round + " Turn: " + current_game_turn + " CurrentPlayer: " + current_player + " WinCheck: " + winCheck());
});
});
</script>
<title>XO Game</title>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table {border-collapse: collapse;}
table tr td {padding: 0; border: 1px solid black; text-align: center; vertical-align: middle; cursor: default;}
table#scores tr td {width: 65px; height: 30px;}
table#play_field tr td {width: 50px; height: 50px;}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var current_player = "PlayerX";
var current_game_turn = 1;
var max_game_turns_number = 9;
var current_game_round = 1;
var max_game_rounds_number = 5;
var scores_table = '<table id="scores">' +
'<tr><td>Round</td><td>Player X</td><td>Player O</td></tr>';
for (var i = 0; i < max_game_rounds_number; i++) {
scores_table += '<tr><td>' + ( i + 1) + '</td><td></td><td></td></tr>';
}
scores_table += '</table>';
var play_field_table = '<table id="play_field">' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'<tr><td></td><td></td><td></td></tr>' +
'</table>';
var round_field = '<p id="round">Round: <span>1</span></p>';
$("body").append(round_field).append(scores_table).append('<br />').append(play_field_table);
function refreshScoresTable () {
for (var i = 0; i < 5; i++) {
$("table#scores tr:eq(" + (i + 1) + ") td:eq(1)").text("");
$("table#scores tr:eq(" + (i + 1) + ") td:eq(2)").text("");
}
}
function refreshPlayFieldTable () {
$("table#play_field tr td").text("").removeClass("checked");
}
var classPlayer = function () {};
classPlayer.prototype = {
scores: 0
};
var PlayerX = new classPlayer();
var PlayerO = new classPlayer();
function winCheck() {
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Diagonal Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(2)").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(0)").text() == "O")
) {
return "Player O Win";
}
for (var i=0; i < 3; i++) {
// Horizontal Win X X X
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "X") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "X")
) {
return "Player X Win";
}
// Horizontal Win O O O
if (
($("table#play_field tr:eq(" + i + ") td:eq(0)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(1)").text() == "O") &&
($("table#play_field tr:eq(" + i + ") td:eq(2)").text() == "O")
) {
return "Player O Win";
}
// Vertical Win
// X
// X
// X
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "X") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "X")
) {
return "Player X Win";
}
// Vertical Win
// O
// O
// O
if (
($("table#play_field tr:eq(0) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(1) td:eq(" + i + ")").text() == "O") &&
($("table#play_field tr:eq(2) td:eq(" + i + ")").text() == "O")
) {
return "Player O Win";
}
}
return "Nobody Win";
};
$("table#play_field tr td").click(function(){ console.log("Round: " + current_game_round + " Turn: " + current_game_turn + " CurrentPlayer: " + current_player);
if (!$(this).hasClass("checked")) {
if (current_player == "PlayerX") {
$(this).text("X").addClass("checked");
current_player = "PlayerO";
} else if (current_player == "PlayerO") {
$(this).text("O").addClass("checked");
current_player = "PlayerX";
}
current_game_turn++;
if(winCheck() == "Nobody Win") {
if (current_game_turn != (max_game_turns_number + 1)) {
// Game round continue.
} else {
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Draw");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Draw");
refreshPlayFieldTable ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
} else if (winCheck() == "Player X Win") {
PlayerX.scores = PlayerX.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Win");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Lose");
refreshPlayFieldTable ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
} else if (winCheck() == "Player O Win") {
PlayerO.scores = PlayerO.scores + 1;
$("table#scores tr:eq(" + current_game_round + ") td:eq(1)").text("Lose");
$("table#scores tr:eq(" + current_game_round + ") td:eq(2)").text("Win");
refreshPlayFieldTable ();
current_game_turn = 1;
current_game_round++;
if (current_game_round > 5) {
$("p#round span").text(max_game_rounds_number);
} else {
$("p#round span").text(current_game_round);
}
}
if (current_game_round == (max_game_rounds_number + 1)) {
refreshPlayFieldTable ();
if (PlayerX.scores > PlayerO.scores) {
alert("Game End. Player X Win.");
} else if (PlayerX.scores < PlayerO.scores) {
alert("Game End. Player O Win.");
} else if (PlayerX.scores == PlayerO.scores) {
alert("Game End. Draw.");
}
refreshScoresTable ();
current_game_turn = 1;
current_game_round = 1;
$("p#round span").text(current_game_round);
}
}
console.log("Round: " + current_game_round + " Turn: " + current_game_turn + " CurrentPlayer: " + current_player + " WinCheck: " + winCheck());
});
});
</script>
<title>XO Game</title>
</head>
<body>
</body>
</html>
среда, 14 ноября 2012 г.
DNA Mutate
function in_array (item, array){
for (var i = 0; i < array.length; i++) {
if (array[i] === item) {
return true;
}
}
return false;
}
function getRandomInt(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var Cell = function(){};
Cell.prototype = {
DNA: [],
init: function(){
var dna_letters = ["A", "T", "G", "C"];
for (var i = 0; i < 100; i++) {
this.DNA[i] = dna_letters[getRandomInt(0, 3)];
}
},
hammingDistance: function(cell_object) {
var distance = 0;
for (var i = 0; i < 100; i++) {
if (this.DNA[i] != cell_object.DNA[i]) {
distance++;
}
}
return distance;
},
mutate: function(x){
var mutated_cells = [];
var dna_position;
var number_of_mutations = x;
var dna_letters = ["A", "T", "G", "C"];
while (number_of_mutations > 0) {
dna_position = getRandomInt(0, 99);
if (in_array(dna_position, mutated_cells)) {
continue;
} else {
this.DNA[dna_position] = dna_letters[getRandomInt(0, 3)];
mutated_cells.push(dna_position);
number_of_mutations--;
}
}
}
}
var FirstCell = new Cell();
FirstCell.init();
var SecondCell = new Cell();
SecondCell.init();
console.log(FirstCell.hammingDistance(SecondCell));
FirstCell.mutate(10);
SecondCell.mutate(20);
console.log(FirstCell.hammingDistance(SecondCell));
for (var i = 0; i < array.length; i++) {
if (array[i] === item) {
return true;
}
}
return false;
}
function getRandomInt(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var Cell = function(){};
Cell.prototype = {
DNA: [],
init: function(){
var dna_letters = ["A", "T", "G", "C"];
for (var i = 0; i < 100; i++) {
this.DNA[i] = dna_letters[getRandomInt(0, 3)];
}
},
hammingDistance: function(cell_object) {
var distance = 0;
for (var i = 0; i < 100; i++) {
if (this.DNA[i] != cell_object.DNA[i]) {
distance++;
}
}
return distance;
},
mutate: function(x){
var mutated_cells = [];
var dna_position;
var number_of_mutations = x;
var dna_letters = ["A", "T", "G", "C"];
while (number_of_mutations > 0) {
dna_position = getRandomInt(0, 99);
if (in_array(dna_position, mutated_cells)) {
continue;
} else {
this.DNA[dna_position] = dna_letters[getRandomInt(0, 3)];
mutated_cells.push(dna_position);
number_of_mutations--;
}
}
}
}
var FirstCell = new Cell();
FirstCell.init();
var SecondCell = new Cell();
SecondCell.init();
console.log(FirstCell.hammingDistance(SecondCell));
FirstCell.mutate(10);
SecondCell.mutate(20);
console.log(FirstCell.hammingDistance(SecondCell));
Vanilla JS Наследование классов
function ExtendClass (ChildClass, ParentClass)
{
var BaseParentClass = function(){};
BaseParentClass.prototype = ParentClass.prototype;
var TemporaryParentClass = new BaseParentClass();
for (var property in ChildClass.prototype) {
TemporaryParentClass[property] = ChildClass.prototype[property];
}
ChildClass.prototype = TemporaryParentClass;
ChildClass.prototype.super = ParentClass.prototype;
}
{
var BaseParentClass = function(){};
BaseParentClass.prototype = ParentClass.prototype;
var TemporaryParentClass = new BaseParentClass();
for (var property in ChildClass.prototype) {
TemporaryParentClass[property] = ChildClass.prototype[property];
}
ChildClass.prototype = TemporaryParentClass;
ChildClass.prototype.super = ParentClass.prototype;
}
Подписаться на:
Сообщения (Atom)