@longnt80/

# Algorithm for Tic Tac Toe

## No description

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
```
```center = 4;
var testRandom = [
[0,'x',2],
['o',4,5],
[6,7,8],
[0,'o',6],
['x',4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];

var testCheckMate = [
['o','x',2],
[3,4,5],
['o',7,8],
['o',3,'o'],
['x','x',7],
[2,5,8],
[0,4,8],
[2,'o',4]
];
var testSetup = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
];
(function() {
var arr = testSetup;
for (var k = 0; k < arr.length; k++) {
for (var h = 0; h < arr[k].length; h++) {
if ( arr[k][h] === 0 || arr[k][h] === 5 ) {
arr[k][h] = 'o';
}
else if ( arr[k][h] === 2 || arr[k][h] === 7 ) {
arr[k][h] = 'x';
}
}
}
secondLongNameArr = arr;
// console.log(secondLongNameArr);
})();

var findSetupMove = function(arr) {
// var arr = secondLongNameArr; // Paste the name of the array here
var sortedTemp = [];
var priority = 'x';
var setupCheckmateAvai = []; //To store all available possibilities
var setupBlockAvai = [];

// Find duplicate
var findDuplicate = function(result,curr,index,array){
if ( array.indexOf(curr) !== index ) {
result.push(curr);
}
if ( result.indexOf(4) >= 0 ) {
return [4];
}
else {
return ['random']; // This will be replace as a random number in [result]
}
}

for (var i = 0; i < arr.length; i++) {
sortedTemp = arr[i].slice().sort(); // Make a copy of sorted child array
console.log(sortedTemp);
for (var j = 0; j < sortedTemp.length; j++) {
if ( sortedTemp[2] === priority && typeof sortedTemp[2] === 'string' && typeof sortedTemp[1] === 'number'  ) {
setupCheckmateAvai.push(sortedTemp[0],sortedTemp[1]);
break;
}
else if ( sortedTemp[2] !== priority && typeof sortedTemp[2] === 'string' && typeof sortedTemp[1] === 'number' ) {
setupBlockAvai.push(sortedTemp[0],sortedTemp[1]);
break;
}
}
}
if (setupCheckmateAvai) {
console.log( setupCheckmateAvai.reduce(findDuplicate,[])[0] );
}
else if ( !setupCheckmateAvai && setupBlockAvai ) {
console.log( setupBlockAvai.reduce(findDuplicate,[])[0] );
}
else {
console.log('No setup available!')
}

console.log(setupCheckmateAvai);
console.log(setupBlockAvai);
// console.log(setupCheckmate);

}

var checkmateFind = function() {
var arr = secondLongNameArr; // Paste the name of the array here
var priority = 'x';
var sortedTemp = [];
var checkmate;
var block;

for (var i = 0; i < arr.length; i++) {
sortedTemp = arr[i].slice().sort(); // Make a copy of sorted child array
// console.log(sortedTemp);
for (var j = 0; j < sortedTemp.length; j++) {
if ( (sortedTemp[j] === sortedTemp[j+1]) && sortedTemp[j] === priority  ) {
checkmate = sortedTemp[0];
}
else if ( (sortedTemp[j] === sortedTemp[j+1]) && sortedTemp[j] !== priority  ) {
block = sortedTemp[0];
}
}
}
if (checkmate) {
return checkmate;
}
else if (!checkmate && block) {
return block;
}
else {
findSetupMove(arr);
}

}
console.log(checkmateFind());

```
Native Browser JavaScript