first commit
This commit is contained in:
parent
0b23cd8942
commit
a275990f62
4 changed files with 140 additions and 0 deletions
27
games/tic-tac-toe-js/css/screen.css
Normal file
27
games/tic-tac-toe-js/css/screen.css
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
body {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
td {
|
||||||
|
width: 50px;
|
||||||
|
height: 50px;
|
||||||
|
font-size: 20px;
|
||||||
|
vertical-align: middle;
|
||||||
|
text-align: center;
|
||||||
|
cursor: pointer;
|
||||||
|
border-left: 1px solid gray;
|
||||||
|
border-top: 1px solid gray;
|
||||||
|
}
|
||||||
|
|
||||||
|
tr:first-child td {
|
||||||
|
border-top: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
td:first-child {
|
||||||
|
border-left: none;
|
||||||
|
}
|
12
games/tic-tac-toe-js/index.html
Normal file
12
games/tic-tac-toe-js/index.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Tic Tac Toe</title>
|
||||||
|
<script src="js/game.js"></script>
|
||||||
|
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen">
|
||||||
|
</head>
|
||||||
|
<body onload="new TicTacToe('game')">
|
||||||
|
<div id="game"></div>
|
||||||
|
<p><a href="#" onclick="new TicTacToe('game'); return false;">New game</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
38
games/tic-tac-toe-js/js/game.js
Normal file
38
games/tic-tac-toe-js/js/game.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
// This first version doesn't take networking into account yet.
|
||||||
|
|
||||||
|
function TicTacToe(container_id) {
|
||||||
|
this.player_turn = 1;
|
||||||
|
this.rows = 3;
|
||||||
|
|
||||||
|
this.container = document.getElementById(container_id);
|
||||||
|
var table = document.createElement("table");
|
||||||
|
var tr = document.createElement("tr");
|
||||||
|
var td = document.createElement("td");
|
||||||
|
|
||||||
|
for(var i=0; i < this.rows; i++) {
|
||||||
|
var atr = tr.cloneNode();
|
||||||
|
|
||||||
|
for(var j=0; j < this.rows; j++) {
|
||||||
|
|
||||||
|
|
||||||
|
var atd = td.cloneNode();
|
||||||
|
var self = this;
|
||||||
|
atd.onclick = function(e) {
|
||||||
|
if (e.target.innerHTML == "") {
|
||||||
|
if (self.player_turn == 1) {
|
||||||
|
e.target.innerHTML = "X";
|
||||||
|
self.player_turn = 2;
|
||||||
|
} else {
|
||||||
|
e.target.innerHTML = "O";
|
||||||
|
self.player_turn = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
atr.appendChild(atd);
|
||||||
|
}
|
||||||
|
table.appendChild(atr);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.container.innerHTML = "";
|
||||||
|
this.container.appendChild(table)
|
||||||
|
}
|
63
games/tic-tac-toe-js/js/server.js
Normal file
63
games/tic-tac-toe-js/js/server.js
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
function TicTacToeServer(rows) {}
|
||||||
|
|
||||||
|
TicTacToeServer.prototype.init = function() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TicTacToeServer.newGame = function() {
|
||||||
|
this.rows = 3;
|
||||||
|
|
||||||
|
// Initiate game with empty rows and columns
|
||||||
|
this.gameBoard = [];
|
||||||
|
for (var i=0; i < this.rows; i++) {
|
||||||
|
this.gameBoard[i] = [];
|
||||||
|
for (var j=0; i < this.rows; i++) {
|
||||||
|
this.gameBoard[i][j] = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TicTacToeServer.prototype.checkIfWon = function(player) {
|
||||||
|
|
||||||
|
for (i = 0; i < this.rows; ++i) {
|
||||||
|
for (j = 0; j < this.rows; ++j) {
|
||||||
|
if (this.gameBoard[i][j] != 'X') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j == this.rows) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j = 0; j < this.rows; ++j) {
|
||||||
|
if (this.gameBoard[j][i] != 'X') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j == this.rows) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now check diagnols
|
||||||
|
for (i = 0; i < this.rows; ++i) {
|
||||||
|
if (this.gameBoard[i][i] != 'X') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == this.rows) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < this.rows; ++i) {
|
||||||
|
if (this.gameBoard[i][this.rows - i - 1] != 'X') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == this.rows) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
Reference in a new issue