forked from kamukrass/Bitburner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipvgo.js
77 lines (66 loc) · 2.1 KB
/
ipvgo.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
const OPPONENTS = [
"Netburners",
"Slum Snakes",
"The Black Hand",
"Tetrads",
"Daedalus",
"Illuminati",
];
/** @param {NS} ns */
export async function main(ns) {
ns.go.resetBoardState(ns.go.getOpponent(), 5);
let count = 0;
while (true) {
let result, x, y;
do {
const board = ns.go.getBoardState();
const move = getRandomMove(board, ns.go.analysis.getValidMoves());
[x, y] = move;
if (x === undefined) {
// Pass turn if no moves are found
result = await ns.go.passTurn();
} else {
// Play the selected move
result = await ns.go.makeMove(x, y);
}
// Log opponent's next move, once it happens
await ns.go.opponentNextTurn();
await ns.sleep(200);
// Keep looping as long as the opponent is playing moves
} while (result?.type !== "gameOver");
count++;
ns.print(`\n${count}\n`);
if (count >= 5) {
count = 0;
// pick a new random opponent
const opponent = OPPONENTS[Math.floor(Math.random() * OPPONENTS.length)];
ns.go.resetBoardState(opponent, 5);
} else {
ns.go.resetBoardState(ns.go.getOpponent(), 5);
}
}
// TODO: add a loop to keep playing
}
/**
* Choose one of the empty points on the board at random to play
*/
const getRandomMove = (board, validMoves) => {
const moveOptions = [];
const size = board[0].length;
// Look through all the points on the board
for (let x = 0; x < size; x++) {
for (let y = 0; y < size; y++) {
// Make sure the point is a valid move
const isValidMove = validMoves[x][y] === true;
// Leave some spaces to make it harder to capture our pieces.
// We don't want to run out of empty node connections!
const isNotReservedSpace = x % 2 === 1 || y % 2 === 1;
if (isValidMove && isNotReservedSpace) {
moveOptions.push([x, y]);
}
}
}
// Choose one of the found moves at random
const randomIndex = Math.floor(Math.random() * moveOptions.length);
return moveOptions[randomIndex] ?? [];
};