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 up to 99 Sets, and in 1 Set, the Round is repeated until all coins are used up. 1 Round means one hand, and Turns are repeated until both players' betting coins are equal or until one side gives up.
1 Game has a time limit of 20 seconds. If 20 seconds have passed at the end of the Set, the Game ends without proceeding further with the Set, and the player who wins more Rounds at that time wins the Game.
Code battle's lifecycle callback functions are onGameStart, onSetStart, onRoundStart, onTurnStart, onRoundEnd, onSetEnd, onGameEnd, and you have to implement your own logic on each function.(There is no onTurnEnd.)
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 or delaying time, will result in defeat.)
The game rules
- At the start of each Set, you start with 1000 coins.
- At the start of each Round, both players bet 10 coins by default.
- In each turn, you are given 2 of your cards, 1 of your opponent's cards, and the number of your opponent's betting coins, and you must return the number of your betting coins.
- You must return a value greater than or equal to the number of coins your opponent bet, but less than or equal to the total number of coins you have.
- If you return a number that does not meet the above conditions or -1, you will give up the round and the coins you bet until then will be transferred to your opponent.
- If you return a value greater than the number of coins your opponent bet, it is the same as "raising" the game.
- If it returns a value equal to the number of coins the opponent bet, it is the same as making a "call".
- If your opponent's and your number of betting coins are equal, the win or loss is determined according to the card sequence listed below, and the winning side takes all the betting coins.
- Number cards consist of numbers from 1 to 20, and the order of card pairs is as follows (sorted in descending order).
- When two cards are 10 and 20
- When two cards are 9 and 19
- When two cards are 8 and 18
- When two cards are 7 and 17
- When two cards are 6 and 16
- When two cards are 5 and 15
- When two cards are 4 and 14
- When two cards are 3 and 13
- When two cards are 2 and 12
- When two cards are 1 and 11
- When the "units" number of the sum of two card is 9. (Ex: 5 and 14, or 3 and 6). If it's the same, compare the big cards. (Ex: ['5' and 4] < [3 and '6'])
- When the "units" number of the sum of two card is 8
- When the "units" number of the sum of two card is 7
- When the "units" number of the sum of two card is 6
- When the "units" number of the sum of two card is 5
- When the "units" number of the sum of two card is 4
- When the "units" number of the sum of two card is 3
- When the "units" number of the sum of two card is 2
- When the "units" number of the sum of two card is 1
- When the "units" number of the sum of two card is 0
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 TURN / ROUND / SET(GAME) boards.
*/
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 Set starts.
* 1 game can run up to 99 sets.
*
* @param data
* data.my_coins: Quantity of my coins
*/
onSetStart(data) {
printLog(
this.player_name+": onSetStart! my_coins:"+data.my_coins
);
}
/**
* Called when the Round starts.
* 1 round means one hand, and you receive the necessary information for that round.
*
* @param data
* data.my_coins: Quantity of my coins.
* data.op_coins: Quantity of opponent's coins.
* data.my_cards: The numbers of my two cards. [First number, Second number]
* data.op_card: The number of one of the opponent's cards.
* data.first_turn: Whether I am first turn or not. true/false
* data.remain_round_count:
* Number of rounds remaining in this set.
* A value of 0 means that this is the last round of the set,
* and the player with the most coins remaining after the round ends is the winner of the set.
*/
onRoundStart(data) {
printLog(
this.player_name+": onRoundStart!"
+" my_coins:"+data.my_coins+", op_coins:"+data.op_coins
+", my_cards:["+data.my_cards[0]+","+data.my_cards[1]+"]"
+", op_card:"+data.op_card+", first_turn:"+data.first_turn
+", remain_round_count:"+data.remain_round_count
);
}
/**
* Called when the Turn starts.
* I check the number of coins the opponent has bet and decide how much I will bet and return it.
*
* @param data
* data.my_coins: Quantity of my coins.
* data.op_coins: Quantity of opponent's coins.
* data.my_bet_coins: Number of coins I bet.(10 if I haven't bet yet)
* data.op_bet_coins: Number of coins bet by opponent.(10 if your opponent hasn't bet yet)
*
* @result -1 or the number of coins to bet this round
* You can return the number that is greater than or equal to data.op_bet_coins and less than or equal to data.my_coins.
* If you return -1 or a value that does not meet the above conditions,
* you will give up this round and data.my_bet_coins will be transferred to your opponent.
*/
onTurnStart(data) {
// The sample code below determines the number of coins to bet randomly within the possible range,
// and is simply written to give up the round with about a 20% probability
// and call with about a 30% probability.
// Please write a better algorithm.
let min = data.op_bet_coins;
let max = data.my_coins;
let rand = Math.random();
let result = Math.floor(rand * (max - min + 1)) + min;
if (rand < 0.2) result = -1;
else if (rand < 0.5) result = min;
printLog(
this.player_name+": onTurnStart! "
+"my_coins: "+data.my_coins+", op_coins: "+data.op_coins
+", my_bet_coins: "+data.my_bet_coins+", op_bet_coins: "+data.op_bet_coins
+", result: "+result
);
return result;
}
/**
* It is called when the Round is over and receives the result of the Round.
* When both players have the same number of coins they bet, or one player gives up their bet,
* the Round ends and someone wins a coin.
*
* @param result
* result.win: -1(lose) or 0(tie) or 1(win)
* result.my_coins: Quantity of my coins.
* result.op_coins: Quantity of opponent's coins.
* result.op_cards: The numbers of the opponent's two cards. [First number, Second number(0 if the opponent or I gave up the round)]
*/
onRoundEnd(result) {
printLog(
this.player_name+": onRoundEnd! win:"+result.win
+", my_coins:"+result.my_coins+", op_coins:"+result.op_coins
+", op_cards:["+result.op_cards[0]+","+result.op_cards[1]+"]"
);
}
/**
* It is called when the Set is over and receives the result of the Set.
* The Set ends when you or your opponent runs out of coins, or when 200 rounds have been played.
*
* @param result
* result.win: -1(lose) or 0(tie) or 1(win)
* result.my_coins: Quantity of my coins.
* result.op_coins: Quantity of opponent's coins.
*/
onSetEnd(result) {
printLog(
this.player_name+": onSetEnd! win:"+result.win
+", my_coins:"+result.my_coins+", op_coins:"+result.op_coins
);
}
/**
* 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);
}
}