-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.js
131 lines (121 loc) · 4.5 KB
/
ai.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
import {WinnerCheck} from './WinnerCheck.js'
const vTable = document.createElement('div')
const winnerCheck = new WinnerCheck(vTable)
let listOfWinningMoves = []
let theWinningMove = {
move: '',
rating: 0,
winner: '',
winnerRating: 0
}
const setUpGameBoard = () => {
const gameBoard = document.querySelector('.game-board')
vTable.innerHTML = gameBoard.innerHTML;
}
const getRandomCoordinates = (rowsRange, columnsRange) => {
const row = Math.floor(Math.random() * rowsRange + 1)
const column = Math.floor(Math.random() * columnsRange + 1)
return [row, column]
}
const moveRandom = (rows, columns) => {
while (true) {
const [row , column] = getRandomCoordinates(rows, columns)
if (vTable.querySelector(`#Row\\:${row}-Column\\:${column}`).innerText === '') {
return `Row:${row}-Column:${column}`
}
}
}
const oneMoveWin = (whosTurn, winCondition, rows, columns) => {
for (let row = 1; row <= rows; row++) {
for (let column = 1; column <= columns; column++) {
const aMove = vTable.querySelector(`#Row\\:${row}-Column\\:${column}`)
if ( aMove.innerHTML === '' &&
winnerCheck.check(aMove.id, whosTurn, winCondition) &&
!listOfWinningMoves.includes(aMove.id)
) {
return aMove.id
}
}
}
return null
}
const TwoMovesWin = (whosTurn, winCondition, rows, columns) => {
for (let row = 1; row <= rows; row++) {
for (let column = 1; column <= columns; column++) {
const aMove = vTable.querySelector(`#Row\\:${row}-Column\\:${column}`)
if (aMove.innerHTML === '') {
aMove.innerHTML = whosTurn
while (true) {
let returnedMove = oneMoveWin(whosTurn, winCondition, rows, columns)
if (returnedMove) {
if (!listOfWinningMoves.includes(returnedMove))
{
listOfWinningMoves.push(returnedMove)
if (theWinningMove.rating < listOfWinningMoves.length){
theWinningMove.move = aMove.id
theWinningMove.rating = listOfWinningMoves.length
}
}
} else {
listOfWinningMoves = []
break
}
}
aMove.innerHTML = ''
}
if (theWinningMove.rating > 1) {
if (theWinningMove.rating > theWinningMove.winnerRating) {
theWinningMove.winner = theWinningMove.move
theWinningMove.winnerRating = theWinningMove.rating
theWinningMove.move = ''
theWinningMove.rating = 0
}
} else {
theWinningMove = {
move: '',
rating: 0,
winner: theWinningMove.winner,
winnerRating: theWinningMove.winnerRating
}
}
}
}
return theWinningMove.winner
}
const cleanUp = () => {
listOfWinningMoves = []
theWinningMove =
{
move: '',
rating: 0,
winner: '',
winnerRating: 0
}
}
export const playByAi = (aiTurn, oppTurn, winCondition, rows, columns) => {
cleanUp()
setUpGameBoard()
let move = null
move = oneMoveWin(aiTurn, winCondition, rows, columns)
if (move) {
return move
}
move = oneMoveWin(oppTurn, winCondition, rows, columns)
if (move) {
console.log(`I don't want you to play there.`)
return move
}
move = TwoMovesWin(aiTurn, winCondition, rows, columns)
if (move) {
console.log('I win in two moves!')
return move
}
cleanUp()
move = TwoMovesWin(oppTurn, winCondition, rows, columns)
if (move) {
console.log('I stopped you from playing a move that would let you win in two moves.')
return move
}
console.log('I just played a random move.')
return moveRandom(rows, columns)
}