-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameBot.js
45 lines (42 loc) · 1.32 KB
/
gameBot.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
const axios = require('axios');
const COIN_NUM = 13;
// Using the algorithm in https://www.i-am.ai/build-your-own-ai.html
exports.weightUpdate = (gameStatus) => {
let res = {};
res["algoType"] = 1;
res["gamerType"] = 1;
res["gameBoard"] = gameStatus["gameBoard"];
res["win"] = gameStatus["win"];
res["first"] = gameStatus["first"];
res["weight"] = gameStatus["gameWeight"];
res["playerID"] = gameStatus["playerID"];
let flag = true;
let pos = res["gameBoard"].length - 1;
let coins = [];
coins[0] = COIN_NUM;
for(var i = 1;i<pos+1;i++)
coins[i] = coins[i-1] - res["gameBoard"][i-1];
while(res["win"]===false && flag && pos>0){
if(res["weight"][coins[pos]-1][0] + res["weight"][coins[pos]-1][1] + res["weight"][coins[pos]-1][2] > 1){
res["weight"][coins[pos]-1][gameStatus["gameBoard"][pos] - 1] -= 1;
flag = false;
}
pos -= 2;
}
return res;
}
exports.storeGame = async (gameStatus) =>{
let res;
await axios ({
method: 'get',
baseURL: 'http://localhost:3032',
url: '/storeGame',
'Content-Type': 'application/json',
params: gameStatus
})
.then((result) => {
res = result.data;
})
.catch((err) => { console.error(err) });
return res;
}