-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.js
148 lines (132 loc) · 3.15 KB
/
TicTacToe.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
"use strict";
function Player (symbol) {
this.symbol = symbol;
this.turns = 0;
};
function Board (slots) {
var board = Object.create(Array.prototype);
board = Array.apply(board, [slots]);
for (var method in Board.prototype) {
if (Board.prototype.hasOwnProperty(method)) {
board[method] = Board.prototype[method];
}
}
return board;
};
Board.prototype.setSlot = function (slot, symbol) {
if (typeof this[slot] === "undefined") {
this[slot] = symbol;
return true;
} else {
return false; // taken
}
};
Board.prototype.print = function () {
var array = [];
for (var i = 0; i < this.length; i++) {
if (typeof this[i] === "undefined") {
array.push("-");
} else {
array.push(this[i]);
}
}
console.log("-----");
console.log(array[0] + " " + array[1] + " " + array[2]);
console.log(array[3] + " " + array[4] + " " + array[5]);
console.log(array[6] + " " + array[7] + " " + array[8]);
console.log("-----");
};
Board.prototype.isWinner = function (symbol) {
// check columns
var step = 3;
var i = 0;
while (i < this.length / step) {
if (this[i] === symbol && this[i + step] === symbol && this[i+ step * 2] === symbol) {
return true;
} else {
i++;
}
}
// check rows
step = 1;
i = 0;
while (i < this.length / step) {
if (this[i] === symbol && this[i + step] === symbol && this[i+ step * 2] === symbol) {
return true;
} else {
i = i + 3;
}
}
// check diagonal \
step = 4;
if (this[0] === symbol && this[step] === symbol && this[step * 2] === symbol) {
return true;
}
// check diagonal /
step = 2;
if (this[step] === symbol && this[step * 2] === symbol && this[step * 3] === symbol) {
return true;
}
return false;
};
function TicTacToe (symbol1, symbol2) {
var winner = null;
var board = new Board(9);
var player1 = new Player(symbol1);
var player2 = new Player(symbol2);
var isWinner = function (player) {
if (player.turns < 3) return false;
if (board.isWinner(player.symbol)) {
winner = player;
return true;
} else {
return false;
}
};
var getWinner = function () {
return winner;
};
var play = function (slot, player) {
if (winner) return false;
var set = board.setSlot(slot, player.symbol);
if (set) player.turns++;
return set;
};
var getPlayers = function () {
return [player1, player2]
};
var randomPlay = function () {
var plays = 0;
var foundSlot = false;
var slot = null;
var player = null;
var won = false;
while (!won && plays < 9) {
if (plays % 2 === 0) {
player = player1;
} else {
player = player2;
}
foundSlot = false;
while (!foundSlot) {
slot = parseInt(Math.random() * 10);
if (slot < 9) {
foundSlot = play(slot, player);
}
}
plays++;
board.print();
won = isWinner(player);
}
return winner;
};
return {
getPlayers: getPlayers,
play: play,
randomPlay: randomPlay,
getWinner: getWinner
};
};
var tic = new TicTacToe(0, 1);
tic.randomPlay();
console.log(tic.getWinner() || "Nobody won");