-
Notifications
You must be signed in to change notification settings - Fork 0
/
tittactoe.html
310 lines (245 loc) · 13.5 KB
/
tittactoe.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<html>
<head>
<style>
.bgs {
background-image: url("images/bg.jpg");
background-repeat: repeat;
}
.parent {
display: flex;
flex-direction: column;
justify-content:center;
align-items:center;
}
.titleDiv {
-webkit-text-stroke: 1px black;
color: white;
text-shadow:
3px 3px 0 #000,
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
font-size: 50px;
margin-right: 65px;
}
</style>
<script src='http://d3js.org/d3.v2.js'></script>
</head>
<body class="bgs">
<script type="text/javascript">
(function ticTacToe() {
var GameData = {
// Indexes representing cells in the Tic Tac Toe
cells : [0,1,2,3,4,5,6,7,8],
// List of cells frozen for any selection. Index of a cell gets added to frozenCells array once the user places his marker.
frozenCells : [],
// Marker 1
marker1: "X",
// Marker 2
marker2: "0",
// currentMarker
currentMarker : "0",
//Variable indicating if the Game is complete
gameComplete : false,
// Indexes of the boxes which indicate completion of the game. If a player had 3 of same markers in the below indices he would win the game.
gameCompletionPaths : [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]],
// Adjustment factors for x and y
adjustmentFactor : [[0,65,130,65],[0,65,130,65],[0,65,130,65],[65,0,65,130],[65,0,65,130],[65,0,65,130],[0,0,130,130],[130,0,0,130]],
// Status of the game
status: {
initializing : "0",
readyToPlay : "1",
inprogress: "2",
complete: "3"
},
// Current status of the game
gameStatus : "",
// Board settings for the Game
Board: {
width : "500px",
height : "500px",
// Container for the all of the Game graffics
svgContainer : ""
},
// Properties of the block that the user places his marker
Block: {
width : "130px",
height: "130px",
fill: "#AF975B",
stroke: "#AFA876",
strokeWidth: "9",
strokeOpacity: "0.8",
opacity: "0.2",
id: function(i) {return "rect" + i}
},
// Properties of the Text on each of the block
Text: {
fontFamily:'"Lato", "Helvetica", Helvetica, sans-serif',
fontSize:"75px",
stroke:"#4863A0",
fill: "#F5F5F5",
id: function(i) {return "text" + i}
}
};
GameData.gameStatus = GameData.status.initializing;
drawBoard();
GameData.gameStatus = GameData.status.readyToPlay;
var getCurrentMarker = function() {
GameData.currentMarker = (GameData.currentMarker == GameData.marker2 || GameData.currentMarker == "") ? GameData.marker1 : GameData.marker2;
return GameData.currentMarker;
}
function drawBoard() {
//d3.select("body").append("div").attr("class","titleDiv").text(function(d) {return "Tic-Tac-Toe"});
var parentDiv = d3.select("body").append("div").attr("class","parent");
parentDiv.append("div").attr("class","titleDiv").text(function(d) {return "Tic-Tac-Toe"});
//parentDiv.append("div").attr("class","left").attr("id","leftDiv");
var gameDiv = parentDiv.append("div").attr("class","gameDiv").attr("id","gameDiv");
// Create Parent SVG container
GameData.Board.svgContainer = gameDiv.append("svg");
// Create g elements to group rectangles and the Text on top
var parentG = GameData.Board.svgContainer
.attr("width", GameData.Board.width)
.attr("height", GameData.Board.height)
.selectAll("g")
.data(GameData.cells)
.enter()
.append("g");
// Create te Square cells to represent each block of the Game
parentG.append("rect")
.attr("x", function(d) { return ((d%3) * 138);})
.attr("y", function(d) { return (Math.floor(d/3) * 138);})
.attr("width", GameData.Block.width)
.attr("height", GameData.Block.height)
.style("fill", GameData.Block.fill)
.style("stroke", GameData.Block.stroke)
.style("opacity", "0.2")
.attr("id", function(d) { return GameData.Block.id(d)})
.style("stroke-width", GameData.Block.strokeWidth)
.style("stroke-opacity", GameData.Block.strokeOpacity)
.on('click', function(d,i){ handleClick(d,i);});
// Create Text elements to draw the text on each of the square
parentG.append("text")
.style("font-family", GameData.Text.fontFamily)
.style("font-size", GameData.Text.fontSize)
.style("stroke", GameData.Text.stroke)
.style("fill", GameData.Text.fill)
.attr("x", function(d) {return (40 + 140 * (d%3));})
.attr("y", function(d) {
var factor = (Math.floor(d/3) + 1);
return (factor * 80 + ((factor - 1) * 60));
})
.attr("id", function(d) {return GameData.Text.id(d)})
.on('click', function(d,i){handleClick(d,i);});
}
function handleClick(d, i) {
GameData.gameStatus = GameData.status.inprogress;
// If user is clicking on a cell that has been already frozen do not process the click
if((GameData.frozenCells.indexOf(GameData.Block.id(d)) > 0) || (GameData.frozenCells.indexOf(GameData.Text.id(d)) > 0) || GameData.gameComplete) {
// Do nothing value has been already set
return;
}
GameData.frozenCells.push(GameData.Block.id(d));
GameData.frozenCells.push(GameData.Text.id(d));
// Get the currentMarker to place
var currentMarker = getCurrentMarker();
document.getElementById(GameData.Text.id(d)).textContent = currentMarker;
// Check of the Game is complete
var index = isGameComplete(currentMarker);
// If the length of path is greater than one that means we have a path that completes the game.
if(index >= 0) {
GameData.gameComplete = true;
GameData.gameStatus = GameData.status.complete;
notifyGameOver(index, currentMarker);
}
if(index == -2) {
GameData.gameComplete = true;
notifyGameOver(index, "");
}
}
function isGameComplete(player) {
var matchCount = 0;
for(var i=0 ; i<GameData.gameCompletionPaths.length; i++) {
var completePath = GameData.gameCompletionPaths[i];
for(var j=0; j< completePath.length; j++) {
if(document.getElementById(GameData.Text.id(completePath[j])).textContent == player) {
matchCount++;
} else {
break;
}
}
if(matchCount == 3) {
return i;
} else {
matchCount = 0;
}
}
if(GameData.frozenCells.length >= 18) {
return -2;
}
return -1;
}
function notifyGameOver(index, winner) {
var msg;
if(index >= 0) {
var pathArray = GameData.gameCompletionPaths[index];
var textElement1 = document.getElementById('rect' + pathArray[0]);
var textElement2 = document.getElementById('rect' + pathArray[2]);
var x1 = textElement1.getAttribute("x");
var y1 = textElement1.getAttribute("y");
var x2 = textElement2.getAttribute("x");
var y2 = textElement2.getAttribute("y");
GameData.Board.svgContainer.append("g").append("line")
.attr("x1", Number(x1) + GameData.adjustmentFactor[index][0])
.attr("y1", Number(y1) + GameData.adjustmentFactor[index][1])
.attr("x2", Number(x2) + GameData.adjustmentFactor[index][2])
.attr("y2", Number(y2) + GameData.adjustmentFactor[index][3])
.attr("id", "gameCompletionLine")
.attr("stroke", "#000000")
.attr("stroke-width", "4");
msg = winner + " 's takes the game...";
} else if(index == -2) {
msg = "It is a draw.";
}
var newGameG = GameData.Board.svgContainer.append("g");
newGameG.append("rect")
.attr("x", function(d) { return 0;})
.attr("y", function(d) { return 140;})
.attr("width", 400)
.attr("height", 130)
.attr("id", "newGameRect")
.style("fill", "black")
.style("stroke", "black")
.on('click', function(){resetGame();});
newGameG.append("text")
.style("font-family", GameData.Text.fontFamily)
.style("font-size", "40px")
.style("stroke", GameData.Text.stroke)
.style("fill", GameData.Text.fill)
.attr("x", function(d) {return 20;})
.attr("y", function(d) {return 200;})
.text(function(d) {return msg;})
.attr("id", "newGameText")
.on('click', function(){resetGame();});
}
function resetGame() {
console.log("reset game");
GameData.gameComplete = false;
GameData.frozenCells = [];
GameData.currentMarker = "0";
for(var i=0;i<GameData.cells.length;i++) {
document.getElementById('text' + i).textContent = "";
}
var line = document.getElementById("gameCompletionLine");
if(line != undefined) {
line.parentNode.removeChild(line);
}
var rect = document.getElementById("newGameRect");
rect.parentNode.removeChild(rect);
var text = document.getElementById("newGameText");
text.parentNode.removeChild(text);
}
})();
</script>
</body>
</html>