-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.js
79 lines (68 loc) · 2.02 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
var corners = [1, 3, 7, 9];
var edges = [2, 4, 6, 8];
function TicTacToe() {
this.moves = [];
this.currentPlayer;
this.message = "";
}
TicTacToe.prototype.move = function (field) {
if (this.moves.length === 0) {
if (!!!field) {
this.moves.push({ key: 5, value: "M" })
this.currentPlayer = "H";
}
else {
this.moves.push({ key: field, value: "H" });
this.currentPlayer = "M";
}
}
else {
if (this.currentPlayer === "M") {
this.machineEvaluation(field);
}
else {
if (!this.validatePosition(field)) this.message = "Illegal move";
this.moves.push({ key: field, value: "H" });
this.currentPlayer = "M";
}
}
if (this.moves.length > 5) {
evaluateWinner();
}
return this.message;
}
TicTacToe.prototype.machineEvaluation = function(field) {
var positions = [];
positions = this.moves.filter(function (m) {
return m.key === 5;
});
if (algo.length === 0) {
this.moves.push({ key: 5, value: "M" });
this.currentPlayer = "H";
return;
};
for (i = 0; i < corners.length; i++) {
positions = this.moves.filter(function (m) {
return m.key === corners[i];
});
}
if (positions.length > 0) this.moves.push({ key: positions[0], value: "M" });
for (i = 0; i < edges.length; i++) {
positions = this.moves.filter(function (m) {
return m.key === edges[i];
});
}
if (positions.length > 0) this.moves.push({ key: positions[0], value: "M" });
this.currentPlayer = "H";
}
TicTacToe.prototype.evaluateWinner = function () {
var posEvaluate = TicTacToe.moves.filter(function (m) {
return m.value = TicTacToe.currentPlayer;
});
}
TicTacToe.prototype.validatePosition = function(field) {
var valid = this.moves.filter(function (m) {
return m.key === edges[i];
});
return valid.length > 0 ? false : true;
}