-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseAi.lua
80 lines (69 loc) · 2.21 KB
/
baseAi.lua
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
local baseAi = {}
local scoring = require("configs.scoring")
local difficultyConfig = require("configs.difficulty")
local selectingWord = nil
local selectedTiles = {}
local function getAdjacentTiles(x, y, board)
local result = {}
for newX=(x - 1),(x + 1) do
for newY=(y - 1),(y + 1) do
local sameTile = newX == x and newY == y
local inBounds = (newX >= 1 and newX <= #board) and (newY >= 1 and newY <= #(board[1]))
if not sameTile and inBounds then
result[#result + 1] = {x=newX, y=newY}
end
end
end
return result
end
function inBoard(word, board, x, y, acc)
if word == "" then
selectedTiles[selectingWord] = acc
return true
end
if string.lower(board[y][x]) == string.sub(word, 1, 1) then
local adjacentTiles = getAdjacentTiles(x, y, board)
for i=1, #adjacentTiles do
local tile = adjacentTiles[i]
if inBoard(string.sub(word, 2), board, tile['x'], tile['y'], table.add(acc, {x=x, y=y})) then
return true
end
end
end
return false
end
local function getValidWordsInBoard(board, words)
local result = {}
for x=1, #board do
for y=1, #(board[1]) do
for i=1,#words do
selectingWord = words[i]
if inBoard(words[i], board, x, y, {}) then
result[#result + 1] = words[i]
end
end
end
end
return result
end
baseAi.makeMove = function(boardModel, possibleWords, difficulty)
if difficulty == difficultyConfig.difficulties.easy.level then
possibleWords = table.take(possibleWords, 1000)
elseif difficulty == difficultyConfig.difficulties.medium.level then
possibleWords = table.take(possibleWords, 10000)
end
local validWords = getValidWordsInBoard(boardModel, possibleWords)
local bestWord = function(word1, word2)
if scoring.ofWord(word1) > scoring.ofWord(word2) then
return word1
else
return word2
end
end
local chosenWord = table.reduce(validWords, bestWord, "a")
local finalSelectedTiles = selectedTiles[chosenWord]
selectedTiles = {}
selectedWord = nil
return chosenWord, boardModel, finalSelectedTiles
end
return baseAi