This match is a 1:1 match between Player 1 and Player 2's code, and the programming language is JAVASCRIPT.
After checking the game rules, writing the codes for Player 1 and Player 2, click the Start Game button to start the match.
1 game consists of 7 rounds, and 1 round repeats the turn until all 9 innings are finished.
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 at onTurnStart is the key to winning.
(Any attempt to cheat, such as affecting the opponent's code, will result in defeat.)
The game rules
- On each turn, you choose where to throw the ball if you are defending and where to swing the bat if you are offending.
0,0 | 0,1 | 0,2 |
1,0 | 1,1 | 1,2 |
2,0 | 2,1 | 2,2 |
- Defense (Pitcher): You can choose one of the 9 values above, or an out-of-range value (e.g. [3,3]). Values out of range are treated as balls. And anything other than the center ([1,1]) can be a ball. 4 corners ([0,0], [0,2], [2,0], [2,2]) result in a ball with 5/9 probability, and 4 sides ([0,1], [ 2,1], [1,0], [1,2]) become a ball with a probability of 1/3.
- Offence (Hitter): You can choose one of the 9 values above, or an out-of-range value (e.g. [3,3]). Values out of range will be treated as not swinging the bat.
- A turn's result is one of four.
- Home Run: When the pitcher's choice matches the hitter's choice
- Hits: When the pitcher's row of choices matches the hitter's row of choices (e.g. [1,0] and [1,2]). A hit is always a single.
- Strike: When the pitcher throws a strike and the hitter misses, or when the pitcher throws the ball and the hitter swings it
- Ball: If not a home run, hit, or strike
- The basic rules of progression are similar to "real baseball".
- 3 strikes, 1 out.
- If it is a 4 ball, the runner advances by walking.
- If 3 out, the inning is over and the offense and defense alternate.
- If it hits or walks, it advances to single base.
- It consists of first base, second base, third base, and home, and when a runner comes to first base -> second base -> third base -> home, a score is scored.
- If it is a home run, all runners on base, including the hitter, enter the home and score.
- Up to 9 innings, no overtime.
class {
/**
* Please write code with JAVASCRIPT, and do not change the first line and the name of the on...() methods.
*/
/**
* Please set the player's name or nickname.
* If left as is, it will be replaced by Player1 or Player2.
* The player_name can be used to identify whose logs when call printLog,
* and it is displayed on the Score Board.
*/
player_name = "_will be replaced_";
/**
* Called when the game starts.
* Here is the initialization of the variables to use during the game.
*/
onGameStart() {
// printLog(string) is a function that outputs text to the game log area at the bottom of the play area.
printLog(this.player_name+": onGameStart!");
}
/**
* Called when the round starts.
* 1 game runs 7 rounds.
* A round is one baseball game (9 innings).
*/
onRoundStart() {
printLog(this.player_name+": onRoundStart!");
}
/**
* Called with data when the round starts.
* You must return what your choices are this turn.
* For one round, the turn is repeated until 9 innings have been completed.
*
* @param data
* data.inning: current inning. 1~9
* data.is_offence: true(offence), false(defence)
* data.ball_count: ball counts. {s:0, b:0, o:0}
* data.score: current scores. {me:0, op:0}
* data.runner: runner's count. 0~3
*
* @return [row, col]
* row: 0 or 1 or 2
* col: 0 or 1 or 2
* If you enter a coordinate that is out of range or null,
* In the case of defense (pitcher), it is not a strike,
* If it is an offense (hitter), it is considered not to swing.
*/
onTurnStart(data) {
// random choice of 1 or 2 or 3. 3 is an out-of-range choice.
let row = Math.floor(Math.random() * 4);
let col = Math.floor(Math.random() * 4);
printLog(this.player_name+": onTurnStart! is_offence:"+data.is_offence+", choice:["+row+", "+col+"]");
return [row, col];
}
/**
* It is called when the turn is over and receives the result of the turn.
*
* @param result
* result.is_offence: true(offence), false(defence)
* result.choice: {me:[0,0], op:[0,0]}
* If the value is out of range, it will be null.
* result.result: 0 or 1 or 2 or 3
* 0: strike
* 1: ball
* 2: hit
* 3: home run
* result.ball_count: ball counts. {s:0, b:0, o:0}
* result.score: current scores. {me:0, op:0}
* result.runner: runner's count. 0~3
*/
onTurnEnd(result) {
printLog(this.player_name+": onTurnEnd! is_offence:"+result.is_offence+", choice:"+JSON.stringify(result.choice)+", result:"+result.result);
}
/**
* 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.score: current scores. {me:0, op:0}
* result.wincnt: How many rounds did you win in this game?
* result.losecnt: How many rounds did you lose in this game?
*/
onRoundEnd(result) {
printLog(this.player_name+": onRoundEnd! win:"+result.win+", score:"+JSON.stringify(result.score));
}
/**
* 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)
*/
onGameEnd(result) {
printLog(this.player_name+": onGameEnd! win:"+result.win);
}
}