Check the rules of the game, fill in the following functions, select the opponent, and click the Start Game button to start the battle.

The opponent and I each have their own number (3 digits with 3 different numbers), and it is a game to win by guessing the opponent's number first. If both sides guess at same time, the larger number wins.

When round is started, you need to return your number and predict opponent's number at the beginning of every turn. When turn is ended, you receive the results of comparing the opponent's number and the predicted number.

If the matching digits are in their right positions, they are 'bulls', if in different positions, they are 'cows'

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 31 rounds, 1 round will continue until one side wins or 30 turns(the larger number wins).

Code battle's lifecycle callback functions are onGameStart, onRoundStart, onTurnStart, onTurnEnd, 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 turnNumsSet = new Set();
/**
* 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!");
}
/**
* Return 3 digits number composed of different numbers.
*/
function getRandom3Digit() {
var num_pool = [0,1,2,3,4,5,6,7,8,9];
var nums = "";
for (var i=0; i<3; i++) {
var idx = Math.floor(Math.random() * num_pool.length);
nums += num_pool.splice(idx, 1);
}
return nums;
}
/**
* Called when the round starts.
* 1 game runs 11 rounds. 1 round means one game of bulls and cows.
* When the round start, you need to return your 3 digits number.
* @return Your 3 digits number composed of different numbers.
*/
function onRoundStart() {
turnNumsSet.clear();
var ret = getRandom3Digit();
printLog("onRoundStart! my number is "+ret);
return ret;
}
/**
* Called when the turn starts.
* 1 round means one game of bulls and cows,
* and if you do not win or lose by the 100th turn, you will be drawn.
* @return The number(as string) I predicted this turn to guess the opponent's number.
*/
function onTurnStart() {
var nums = getRandom3Digit();
for (var i=0; i<10; i++) {
if (turnNumsSet.has(nums)) {
nums = getRandom3Digit();
} else {
turnNumsSet.add(nums);
break;
}
}
printLog("onTurnStart! my choice is "+nums);
return nums;
}
/**
* It is called when the turn is over and receives the result of the turn.
* @param result
* result.result: [bulls, cows]
* result.opponentchoice: The number opponent predicted this turn to guess my number.
*/
function onTurnEnd(result) {
printLog("onTurnEnd! [bulls, cows]:"+result.result+", opponent's choice:"+result.opponentchoice);
}
/**
* 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.opponent_number: The opponent's number returned from onRoundStart.(The number I tried to guess)
*/
function onRoundEnd(result) {
printLog("onRoundEnd! win:"+result.win+", wincnt:"+result.wincnt+", losecnt:"+result.losecnt+", opponent's number:"+result.opponent_number);
}
/**
* 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("----------------------------");
}

Opponents:
<= If you join here, other users can see you.
<= If you leave, other users cannot see you.

TURN |

COUNT |

0 |

ROUND | |

WIN | LOSE |

0 | 0 |

GAME | |

WIN | LOSE |

0 | 0 |

The entire game log is displayed in the area below.

If you or the opponent's screen is not visible, the communication speed will be slower.