Check the rules of the game, fill in the following functions, select the opponent, and click the Start Game button to start the battle.
Scissors are 1, Rock is 2, Paper is 3
Scissors win Paper, Rock wins Scissors, and Paper wins Rock.
1 point if you win with Scissors, 2 points if you win with Rock, 3 points if you win with Paper
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 5 rounds, 1 round consists of 100 turns.
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 myRecentChoice;
/**
* 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 5 rounds.
*/
function onRoundStart() {
printLog("onRoundStart!");
}
/**
* Called when the turn starts.
* 1 round runs 100 turns.
* @return 1(Scissors) or 2(Rock) or 3(Paper)
*/
function onTurnStart() {
printLog("onTurnStart!");
// random choice of 1 or 2 or 3
myRecentChoice = Math.floor((Math.random() * 3) + 1);
return myRecentChoice;
}
/**
* It is called when the turn is over and receives the result of the turn.
* @param result
* result.win: -1(lose) or 0(tie) or 1(win)
* result.opponentchoice: opponent's choice on this turn
* result.winscore: How many points have you won by winning the turn in this round?
* result.losescore: How many points have you lost in this round?
*/
function onTurnEnd(result) {
printLog("onTurnEnd! win:"+result.win+", opponent's choice:"+result.opponentchoice+", my choice:"+myRecentChoice);
}
/**
* 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?
*/
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("----------------------------");
}