-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
175 lines (148 loc) · 4.72 KB
/
main.js
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
var board = new Array(9);
function init() {
/* use touch events if they're supported, otherwise use mouse events */
var down = "mousedown"; var up = "mouseup";
if ('createTouch' in document) { down = "touchstart"; up ="touchend"; }
/* add event listeners */
document.querySelector("input.button").addEventListener(up, newGame, false);
var squares = document.getElementsByTagName("td");
for (var s = 0; s < squares.length; s++) {
squares[s].addEventListener(down, function(evt){squareSelected(evt, getCurrentPlayer());}, false);
}
/* create the board and set the initial player */
createBoard();
setInitialPlayer();
}
function createBoard() {
/* create a board from the stored version, if a stored version exists */
if (window.localStorage && localStorage.getItem('tic-tac-toe-board')) {
/* parse the string that represents our playing board to an array */
board = (JSON.parse(localStorage.getItem('tic-tac-toe-board')));
for (var i = 0; i < board.length; i++) {
if (board[i] != "") {
fillSquareWithMarker(document.getElementById(i), board[i]);
}
}
}
/* otherwise, create a clean board */
else {
for (var i = 0; i < board.length; i++) {
board[i] = "";
document.getElementById(i).innerHTML = "";
}
}
}
function squareSelected(evt, currentPlayer) {
var square = evt.target;
/* check to see if the square already contains an X or O marker */
if (square.className.match(/marker/)) {
alert("Sorry, that space is taken! Please choose another square.");
return;
}
/* if not already marked, mark the square, update the array that tracks our board, check for a winner, and switch players */
else {
fillSquareWithMarker(square, currentPlayer);
updateBoard(square.id, currentPlayer);
checkForWinner();
switchPlayers();
}
}
function fillSquareWithMarker(square, player) {
var marker = document.createElement('div');
/* set the class name on the new div to X-marker or O-marker, depending on the current player */
marker.className = player + "-marker";
square.appendChild(marker);
}
function updateBoard(index, marker) {
board[index] = marker;
/* HTML5 localStorage only allows storage of strings - convert our array to a string */
var boardstring = JSON.stringify(board);
/* store this string to localStorage, along with the last player who marked a square */
localStorage.setItem('tic-tac-toe-board', boardstring);
localStorage.setItem('last-player', getCurrentPlayer());
}
function declareWinner() {
if (confirm("We have a winner! New game?")) {
newGame();
}
}
function weHaveAWinner(a, b, c) {
if ((board[a] === board[b]) && (board[b] === board[c]) && (board[a] != "" || board[b] != "" || board[c] != "")) {
setTimeout(declareWinner(), 100);
return true;
}
else
return false;
}
function checkForWinner() {
/* check rows */
var a = 0; var b = 1; var c = 2;
while (c < board.length) {
if (weHaveAWinner(a, b, c)) {
return;
}
a+=3; b+=3; c+=3;
}
/* check columns */
a = 0; b = 3; c = 6;
while (c < board.length) {
if (weHaveAWinner(a, b, c)) {
return;
}
a+=1; b+=1; c+=1;
}
/* check diagonal right */
if (weHaveAWinner(0, 4, 8)) {
return;
}
/* check diagonal left */
if (weHaveAWinner(2, 4, 6)) {
return;
}
/* if there's no winner but the board is full, ask the user if they want to start a new game */
if (!JSON.stringify(board).match(/,"",/)) {
if (confirm("It's a draw. New game?")) {
newGame();
}
}
}
function getCurrentPlayer() {
return document.querySelector(".current-player").id;
}
function setInitialPlayer() {
var playerX = document.getElementById("X");
var playerO = document.getElementById("O");
playerX.className = "";
playerO.className = "";
/* if there's no localStorage, or no last-player stored in localStorage, always set the first player to X by default */
if (!window.localStorage || !localStorage.getItem('last-player')) {
playerX.className = "current-player";
return;
}
var lastPlayer = localStorage.getItem('last-player');
if (lastPlayer == 'X') {
playerO.className = "current-player";
}
else {
playerX.className = "current-player";
}
}
function switchPlayers() {
var playerX = document.getElementById("X");
var playerO = document.getElementById("O");
if (playerX.className.match(/current-player/)) {
playerO.className = "current-player";
playerX.className = "";
}
else {
playerX.className = "current-player";
playerO.className = "";
}
}
function newGame() {
/* clear the currently stored game out of local storage */
localStorage.removeItem('tic-tac-toe-board');
localStorage.removeItem('last-player');
/* create a new game */
createBoard();
}