This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
206 lines (179 loc) · 5.43 KB
/
Copy pathscript.js
File metadata and controls
206 lines (179 loc) · 5.43 KB
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
//Could use a two-dimensional array, but a one-dimensional array is easy enough for the 8 victory conditions. Two-dimensional array would be better for general solution on Tic-Tac-Toe board of N-size
function Board() {
"use strict";
var squares, i;
squares = [];
for (i = 0; i < 9; i += 1) {
squares[i] = null;
}
return squares;
}
function Player(symbol) {
"use strict";
this.Symbol = symbol;
}
function Game(board, calculated) {
"use strict";
var i;
if (calculated) {
this.Calculated = true;
} else {
this.Calculated = false;
}
this.Player1 = new Player('X');
this.Player2 = new Player('O');
if (!board) {
this.Board = new Board();
} else {
this.Board = board;
}
this.MoveHistory = [];
for (i = 0; i < this.Board.length; i += 1) {
if (this.Board[i] !== null) {
this.MoveHistory.push({Player: this.Board[i], Move: this.MoveHistory.length});
}
}
this.Winner = null;
}
function Simulator(board) {
"use strict";
this.SimulationCount = 10000;
this.Board = board.slice();
this.Results = {
X: {
WinCount: 0,
Histories: []
},
O: {
WinCount: 0,
Histories: []
},
C: {
WinCount: 0,
Histories: []
}
};
}
Game.prototype.currentPlayer = function () {
"use strict";
if (this.MoveHistory.length % 2 === 0) {
return this.Player1;
} else {
return this.Player2;
}
};
Game.prototype.randomMove = function () {
"use strict";
var selectedSquare;
while ((selectedSquare === undefined) || (this.Board[selectedSquare] !== null)) {
selectedSquare = Math.floor(Math.random() * this.Board.length);
}
return selectedSquare;
};
Game.prototype.calculatedMove = function () {
"use strict";
var simulator;
simulator = new Simulator(this.Board);
simulator.simulate();
return simulator.choose();
};
Game.prototype.won = function () {
"use strict";
var i;
//Check all possible ways to win
if ((this.Board[0] !== null) && (this.Board[0] === this.Board[1]) && (this.Board[0] === this.Board[2])) {
return this.Board[0];
}
if ((this.Board[3] !== null) && (this.Board[3] === this.Board[4]) && (this.Board[3] === this.Board[5])) {
return this.Board[3];
}
if ((this.Board[6] !== null) && (this.Board[6] === this.Board[7]) && (this.Board[6] === this.Board[8])) {
return this.Board[6];
}
if ((this.Board[0] !== null) && (this.Board[0] === this.Board[3]) && (this.Board[0] === this.Board[6])) {
return this.Board[0];
}
if ((this.Board[1] !== null) && (this.Board[1] === this.Board[4]) && (this.Board[1] === this.Board[7])) {
return this.Board[1];
}
if ((this.Board[2] !== null) && (this.Board[2] === this.Board[5]) && (this.Board[2] === this.Board[8])) {
return this.Board[2];
}
if ((this.Board[0] !== null) && (this.Board[0] === this.Board[4]) && (this.Board[0] === this.Board[8])) {
return this.Board[0];
}
if ((this.Board[2] !== null) && (this.Board[2] === this.Board[4]) && (this.Board[2] === this.Board[6])) {
return this.Board[2];
}
for (i = 0; i < this.Board.length; i += 1) {
if (this.Board[i] === null) {
return false;
}
}
return "C";
};
Game.prototype.play = function () {
"use strict";
var move, moveHistory, symbol;
while (this.won() === false) {
if(this.Calculated){
move = this.calculatedMove();
} else{
move = this.randomMove();
}
symbol = this.currentPlayer().Symbol;
moveHistory = { Player : symbol, Move: move };
this.MoveHistory.push(moveHistory);
this.Board[move] = symbol;
}
this.Winner = this.won();
};
Simulator.prototype.simulate = function () {
"use strict";
var i, game;
for (i = 0; i < this.SimulationCount; i += 1) {
game = new Game(this.Board.slice());
game.play();
this.Results[game.Winner].WinCount += 1;
this.Results[game.Winner].Histories.push(game.MoveHistory);
}
};
Simulator.prototype.choose = function () {
"use strict";
var game, group1, group2, group3, max, i;
game = new Game(this.Board.slice());
group1 = this.groupResults(this.Results[game.currentPlayer().Symbol], game.MoveHistory.length);
group2 = this.groupResults(this.Results.C, game.MoveHistory.length);
group3 = [];
for (i = 0; i < group1.length; i += 1) {
group3[i] = group1[i] + group2[i];
}
max = 0;
for (i = 1; i < group3.length; i += 1) {
if (group3[max] < group3[i]) {
max = i;
}
}
return max;
};
Simulator.prototype.groupResults = function (result, move) {
"use strict";
var i, grouping;
grouping = [];
for (i = 0; i < 9; i += 1) {
grouping[i] = 0;
}
for (i = 0; i < result.Histories.length; i += 1) {
grouping[result.Histories[i][move].Move] += 1;
}
return grouping;
};
function runSimulation(that){
console.log(that);
var game = new Game(null, true);
game.play();
game.MoveHistory.forEach(function(move){
//document.write("<p>" + move.Player + " chooses " + move.Move +"</p>")
document.getElementById(move.Move).innerHTML = move.Player;
})
}