-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai-bot.js
199 lines (185 loc) · 5.58 KB
/
ai-bot.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// simulation board for alorithm testing and development
const sim = [
[ 2, 0, 0, 0, 0, 0, 0 ],
[ 2, 0, 0, 0, 0, 0, 0 ],
[ 1, 0, 0, 0, 0, 0, 0 ],
[ 1, 0, 1, 1, 1, 0, 0 ],
[ 1, 0, 2, 2, 1, 2, 2 ],
[ 1, 2, 2, 1, 2, 2, 2 ]
];
// define playing board to board from script.js
// const dummyBoard = game.board;
const bot = 2;
const human = 1;
let count = 0;
//This function is called when it's bot's turn to play
function aiTurn(dummyBoard) {
// console.log(dummyBoard);
//firstit checks for any immediate winning spots for bot or human
const winningSpot = checkWin(bot, dummyBoard);
const losingSpot = checkWin(human, dummyBoard);
//second it checks for any moves that can give human a win in human's next turn. And removes those moves from possibilities
const firstMoves = makeBoards(bot, dummyBoard); //bot
removeFlaggedSpotsFromFirstMoves(firstMoves);
//if theres a winning spot, move there
if (winningSpot.length > 0) {
count++;
console.log('winning Spot');
return moveHere(winningSpot[0]);
} else if (losingSpot.length > 0) {
//if there;s a losing spot, move there
count++;
console.log('losing Spot');
return moveHere(losingSpot[0]);
} else if (count < 1) {
// on the first turn place next to human's piece (this strategy is to avoid human gaininng leverage in the first horizontal moves)
count++;
let possibleMoves = checkPosMoves(dummyBoard);
for (let thisMove of possibleMoves) {
let y = thisMove[0];
let x = thisMove[1];
if (dummyBoard[y][x + 1] === human) {
console.log('first Spot');
return moveHere([ y, x ]);
}
}
} else {
//bot will now calculate up to 3 moves ahead and decide to move based on which immidiate position offers least amount of losing possibilities or more winning possibilites wihtin the next 3 moves.
const secondMoves = makeBoardsForFirstMoves(firstMoves); //human
const thirdMoves = makeBoardsForSecondMoves(secondMoves); //bot
const fourthMoves = makeBoardsForThirdMoves(thirdMoves); //human
const possibleWins2 = findPotentialWinningSpots2(fourthMoves, firstMoves, dummyBoard);
if (possibleWins2.length > 0) {
console.log('possiblewins2 Spot');
return moveHere(possibleWins2);
}
let possibleMoves = checkPosMoves(dummyBoard);
for (let thisMove of possibleMoves) {
let y = thisMove[0];
let x = thisMove[1];
if (dummyBoard[y][x + 1] === human) {
console.log('possiblewins Spot');
return moveHere([ y, x ]);
}
}
// possibleMoves=checkPosMoves(dummyBoard);
let rand = Math.floor(Math.random() * possibleMoves.length);
return moveHere(possibleMoves[rand]);
// }
}
}
function findMoveFromBoard(board, currBoard) {
for (let y = 0; y < board.length; y++) {
for (let x = 0; x < board[0].length; x++) {
if (board[y][x] !== currBoard[y][x]) {
return [ y, x ];
}
}
}
}
function removeFlaggedSpotsFromFirstMoves(firstMoves) {
// const flaggedSpots=[];
firstMoves.forEach((board, i) => {
if (checkWin(human, board).length > 0) {
// flaggedSpots.push(flaggedSpot[0])
firstMoves.splice(i, 1);
}
});
}
function findPotentialWinningSpots2(fourthMove, firstMoves, dummyBoard) {
const thisArr2 = [];
let c2 = 0;
for (let thirdMove of fourthMove) {
for (let secondMove of thirdMove) {
for (let firstMove of secondMove) {
for (let board of firstMove) {
const winningSpot = checkWin(bot, board);
if (winningSpot.length > 0) {
c2++;
break;
}
}
}
}
thisArr2.push(c2);
c2 = 0;
}
if (thisArr2.length < 1) {
return [];
}
const max = Math.max(...thisArr2);
// console.log(thisArr2,"<--thisArr, min-->",max);
// console.log(thisArr2.indexOf(max),"<--index. move--> ",findMoveFromBoard(firstMoves[thisArr2.indexOf(max)],dummyBoard));
return findMoveFromBoard(firstMoves[thisArr2.indexOf(max)], dummyBoard);
}
function makeBoards(currPlayer, board) {
const possibleMoves = checkPosMoves(board);
const boards = [];
for (let thisMove of possibleMoves) {
const newBoard = JSON.parse(JSON.stringify(board));
newBoard[thisMove[0]][thisMove[1]] = currPlayer;
boards.push(newBoard);
}
return boards;
}
function makeBoardsForFirstMoves(firstMoves) {
const secondMoves = [];
for (let board of firstMoves) {
secondMoves.push(makeBoards(human, board));
}
return secondMoves;
}
function makeBoardsForSecondMoves(secondMoves) {
const thirdMoves = [];
for (let firstMoves of secondMoves) {
const thirdMove = [];
for (let board of firstMoves) {
thirdMove.push(makeBoards(bot, board));
}
thirdMoves.push(thirdMove);
}
return thirdMoves;
}
function makeBoardsForThirdMoves(thirdMoves) {
const fourthMoves = [];
for (let secondMoves of thirdMoves) {
const third3Moves = [];
for (let firstMoves of secondMoves) {
const thirdMove = [];
for (let board of firstMoves) {
thirdMove.push(makeBoards(human, board));
}
third3Moves.push(thirdMove);
}
fourthMoves.push(third3Moves);
}
return fourthMoves;
}
function checkWin(currPlayer, board) {
const possibleMoves = checkPosMoves(board);
const wins = [];
//console.log(possibleMoves,currPlayer);
for (let thisMove of possibleMoves) {
const predBoard = JSON.parse(JSON.stringify(board));
let yMove = thisMove[0];
let xMove = thisMove[1];
predBoard[yMove][xMove] = currPlayer;
if (game.checkForWin(currPlayer, predBoard) === true) {
wins.push([ yMove, xMove ]);
}
}
return wins;
}
function checkPosMoves(board) {
const possibleMoves = [];
for (let x = 0; x < board[0].length; x++) {
y = game.findSpotForCol(x, board);
if (y != null) possibleMoves.push([ y, x ]);
}
return possibleMoves;
}
function moveHere(pos) {
console.log('bot move here', pos);
return pos;
}
// aiTurn(sim);