-
Notifications
You must be signed in to change notification settings - Fork 3
Create CPU opponent
ECMAScript version: Qt >= 5.12 supported, which brings compatibility with ECMAScript 6 and 7!
Computer opponents are scripted in a separate JavaScript file. If one wants to create his own CPU opponent, one has to put the new created .js file into the user folder ~/.local/share/stackandconquer/cpu/ (Linux) or into the sub folder cpu of the installation (Windows). After restarting the application, the script shows up inside the settings dialogue for choosing an opponent. One can use any available script as a template.
While the script is loaded the first time at game start, the (optional) function initCPU()
is called, which could be used for initialization purposes. If initCPU()
doesn't exist, the script engine of the game won't call it. The only mandatory function is callCPU(jsonBoard)
, which is the interface between game and script and is called as soon as it is CPUs turn. The function has 1 parameter (names could be changed):
-
jsonBoard
: Current board encoded as JSON string. It is an array including padding around the board and the board itself with the placed stones. Each field is represented as string (padding: "-", outside board only used for non rectangular boards: "#", e.g. empty field: "", tower example: "112" = stone of second player is on top).
The return value of callCPU()
is an array with size 3 and has to contain a valid move (see chapter below). The game checks, if CPU is making a valid move and throwing an exception in case of an invalid move.
Optionally additional information, which might be helpful for creating a strong CPU opponent, can be received through the game
object. Following methods can be called:
-
game.getID()
: Player ID (player #1 = 1, player #2 = 2, ...) -
game.getNumOfPlayers()
: Number of players -
game.getHeightToWin()
: Number of (minimum) stones, which are building a winning tower -
game.getTowersNeededToWin()
: Array containing how many towers must be conquered for each player to win the game -
game.getNumberOfStones()
: Array containing number of stones left for each player -
game.getLastMove()
: Move of the previous player, if the player moved a tower. In any other case an empty array is returned -
game.getLegalMoves()
: List of all legal moves for the current player encoded as JSON string -
game.getDirection()
: Current playing direction (only interesting for more than 2 players): +1 clockwise / -1 counter clockwise -
game.getBoardDimensionX()
: Width / number of columns of the board -
game.getBoardDimensionY()
: Height / number of rows of the board -
game.getOutside()
: Character used to define a field "outside" of the board, but within the board x * y dimension (only used for non-rectangular boards) -
game.getPadding()
: Character used to define a padding field "around" the board x * y dimension (see picture below, grey is padding)
Additionally one can use game.log(...)
to write messages into StackAndConquer debug.log file, e.g.: game.log("Loading CPU script 'DummyCPU' with player ID " + game.getID());
- A move is an array with size 3 containing only integers
- General move array: [index from, number of stones, index to]
- Example place a stone on empty field with index 80: [-1, 1, 80]
- Example move 2 stones from field 80 to field 83: [80, 2, 83]