Check the rules of the game, fill in the following functions, select the opponent, and click the Start Game button to start the battle.
It selects one of the coordinates from (0,0) to (14,14) on the 15x15 coordinate plate and returns it. If you take five consecutive coordinates in one direction of horizontal / vertical / diagonal, you win.
The player who starts the game first starts 1 round and the player who lost in the previous round starts the next round.
If you win 4 rounds out of a total of 6 rounds, you win the game.
Your color is white, and your opponent's color is black.
Code battle is a battle between your code and opponent's code, and the language is JAVASCRIPT.
Fill in the code, select the opponent (RANDOM, LEVEL1, LEVEL2, LEVEL3, Online opponent) and then press the game start button will start the match.
1 game consists of 6 rounds, 1 round will continue until one side wins or all coordinates are occupied.
Code battle's lifecycle callback functions are onGameStart, onRoundStart, onTurnStart, onRoundEnd, onGameEnd, and you have to implement your own logic on each function.
Building a basic strategy of the game, analyzing your opponent's pattern and returning the best choice from onTurnStart is the key to winning.
/**
* Please write code with JAVASCRIPT.
*/
var turnMap = null; // 2D array representing the state of the 15x15 board
var candidates = null; // Candidates of position values that may be selected on the next turn
var val_notyet = 0; // State of do not selected
var val_me = 1; // State of my choice
var val_opponent = 2; // State of opponent choice
/**
* Called when the game starts.
* Here is the initialization of the variables to use during the game.
*/
function onGameStart() {
// printLog(string) is a function that outputs text to the game log area at the bottom of the play area.
printLog("onGameStart!");
}
/**
* Called when the round starts.
* 1 game runs 6 rounds.
* 1 round means one game of Omok.
*/
function onRoundStart() {
printLog("onRoundStart!");
// 15x15 board initialization
turnMap = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];
candidates = [];
}
/**
* Convert [row,col] to 4 digits number
*/
function _locToNum(loc) {
return loc[0]*100 + loc[1];
}
/**
* Convert 4 digits number to [row,col]
*/
function _numToLoc(num) {
return [Math.floor(num/100), num%100];
}
/**
* Update candidate locations that you may choose next turn
* Unlike Go, it is assumed that Omok is likely to place the next stone near the placed stones.
*/
function _updateCandidates(loc) {
var diffs = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]];
candidates.splice(candidates.indexOf(_locToNum(loc)), 1);
diffs.forEach(function(diff) {
var r = loc[0] + diff[0];
var c = loc[1] + diff[1];
var num = _locToNum([r,c]);
if (r>=0 && r<15 && c>=0 && c<15 && turnMap[r][c]==0 && candidates.indexOf(num)<0) {
candidates.push(num);
}
});
}
/**
* Called when the turn starts.
* @param data
* data.turncnt: Number of times turns were executed
* data.opponentchoice: Last selected by opponent. Without it, it means the first turn.
* @return [row,col]
*/
function onTurnStart(data) {
var loc;
if (data.opponentchoice) {
loc = data.opponentchoice;
turnMap[loc[0]][loc[1]] = val_opponent;
_updateCandidates(loc);
loc = _numToLoc(candidates[Math.floor(Math.random()*candidates.length)]);
turnMap[loc[0]][loc[1]] = val_me;
_updateCandidates(loc);
} else {
// If there is no data, it means that it is the first turn.
loc = [7,7];
turnMap[loc[0]][loc[1]] = val_me;
_updateCandidates(loc);
}
printLog("onTurnStart: op-"+data.opponentchoice+", me-"+loc);
return loc;
}
/**
* It is called when the round is over and receives the result of the round.
* @param result
* result.win: -1(lose) or 0(tie) or 1(win)
* result.wincnt: How many rounds did you win in this game?
* result.losecnt: How many rounds did you lose in this game?
* result.turncnt: Number of times turns were executed
* result.opponentchoice:
* Last selected by opponent.
* This value is required because onRoundEnd is called without onTurnStart if the opponent wins the round.
*/
function onRoundEnd(result) {
printLog("onRoundEnd! win:"+result.win+", wincnt:"+result.wincnt+", losecnt:"+result.losecnt);
}
/**
* It is called when the game is over and receives the result of the game.
* @param result
* result.win: -1(lose) or 0(tie) or 1(win)
*/
function onGameEnd(result) {
printLog("onGameEnd! win:"+result.win);
printLog("----------------------------");
}