# A new javascript KOTH challenge, Fastest gun of the west! [duplicate]

Sometime ago i wanted to make a KOTH challenge, but i wanted it to be more interactive and easier to test, i didnt really come up with anything good. But then i saw this awesome thing Red vs. Blue - Pixel Team Battlebots from calvins-hobbies. Essentialy it pulled the code directly from the answers and the users just had to paste their stuff into their submissions. After i got his permission to use that part of his code i made my own challenge and now i just need to put it up for a test to see what i missed and if the instruction are clear.

Everything after this line is going to be posted in the real post!

This is a king of the hill challenge everybody vs everybody. Its a battle the find the best gunman of the west!

You be able to compete in this contest you need to make two functions, the first one determines the stats of your gunman and the second is the main logic function.

# Stat function

function () {

var bot = {
name: "testBot",
numbOfBullets: 7,
shotsPerTurn: 1,
moveSpeed: 2
}
return bot
}


The stat function includes 5 variables that you will need to set according to some rules (except the name which can be anything). You can spend a total of 15 points on your gunman, the points are spend differently on every stat and you need to spend all 15 for your gunman to be eligible. Here is how the stats work:

• numbOfBullets, defines how many bullets your gun holds the base and minimum value is 1, each addititional bullet costs 1 point with the maximum being 16 bullets with 15 points spend.
• reloadSpeed, defines how many turns your gunman needs to reload his gun after he ran out of bullets. The base and maximum value is 4 with the minimum being 1, each decrease of this stat costs 2 points
• shotsPerTurn, defines how many times your gunman can shoot in one turn the base and minimum value is 1, each increases costs 3 points so you can have a maximum of 6 shots per round with 15 points spend. Raising this stat above the number of bullets your gun can hold is counter productive since you cant shot more bullets then your gun can hold.
• moveSpeed, defines how many spaces your gunman can run in one turn the base and minimum value is 1, each increase costs 3 points with a maximum of 6 speed with 15 points spend. The gunman can run either left or right every turn up to a maximum of his move speed, he can also stand still which gives him a bonus, more on this later.

So the example above has 6 points spend on bullets, 6 points spend on reload speed and 3 points spend on movement.

# Main function

function main(bulletsLeft, yourShots, enemyShots, yourMovement, enemyMovement) {

var shots = [];
shots.push(Math.floor((Math.random() * 24) + 1));
var move = yourMovement[yourMovement.length - 1] + 2
var play = [];
play.shots = shots;
play.move = move;
return play;
}


Paremeters:

• bulletsLeft, number of bullets left in your gun
• yourShots, this is a array of arrays of all past position your gunman has fired at.

Example for a gunman who can shoot 1 bullet per round:
[[12],[4],[22],...]
Example for a gunman who can shoot 3 bullets per round:
[[12,13,14],[11,15,16],[9,14],...]

• enemyShots, same as above but for your enemy
• yourMovement, is a array of all your past positions
• enemyMovement, same as above but for your enemy

What you need to return:

You are required to return a variable which has 3 attributes

• shots, is array of numbers which determine at what space your gunman will shoot
• move, is a single number that determines to what space your gunman will try to move

# The duel

The contest follows a round robin 1vs1 system. Each gunman has 50 rounds against every other gunman a round lasts until someone is hit by a bullet or a maximum of 66 turns with one turn being one shot of each player.

The gunman can earn either 2 points by killing their opponent, 1 point if they both die in the same turn or 0 points if they reach the 66 turns limit. The shooting field is 25 spaces width (1-24 inclusive). Here is a step by step guide on how a duel works, this also covers all invalid comands and special rules:

• At the start of each duel both players are put on space 12 and their revolvers are fully loaded
• The main function is called and the gunman make their first move command and choose where they want to shoot
• First the gunman move to the new location, if any invalid input is made at the move command (positions lower then 1 or higher then 24 or they moved more spaces then they are allowed too) they stay at the same position
• Now comes checking your shooting values, every turn you can enter as many shooting location as you like they will always be cut off to the actual valid amount which is determined by: The number of shots per turn and the number of bullets in your revolver whichever is lower. Your shotsPerTurn value is increased by 1 if you decide to stand still this turn, so you can make a extra shot if you decide to stand still. If you are in the reload cycle you have 0 shots.
• Now comes the actual shooting, there are 2 ways this can go down. If both bots have the same movespeed stat then they both shoot at the same time and can both kill eachother at the same time. In the case that they have different movespeed stats the bot with the higher movespeed stat starts shooting first and if he kills his opponent he wins this round. If the bots have more then one bullet to shoot in one round then it follows the same rules as above except in more cycles as a example: Lets say that bot1 has 3 bullets and is faster and bot 2 has 2 bullets it would go like this:

Bot1 shoots, Bot2 shoots cycle 1
Bot1 shoots, Bot2 shoots cycle 2
Bot1 shoots              cycle 3


This would look the same if they had the same movespeed only that if now Bot1 hit Bot2 in the same cycle Bot2 could also hit Bot1 and it would be a tie.

# RULES

First i am copying some rules from calvins-hobbies entry that also applys here.

When declaring a new JavaScript variable, you must use the var keyword. This is because a variable declared without var becomes global rather than local, so it would be easy to accidentally (or intentionally) mess with the controller or communicate freely with other players. It has to be clear that you aren't trying to cheat.

When declaring functions it is best to use the var keyword as well. i.e. use var f = function(...) {...} instead of function f(...) {...}. I'm not entirely sure why, but sometimes it appears to make a difference.

In your code you may not...

• attempt to access or modify the controller or other player's code.
• attempt to modify anything built into JavaScript.
• attempt to communicate with other players except by using getMsg and setMsg.
• make web queries.
• do otherwise malicious things.

• The users can create as many gunman as they want and change their functions for any period of time
• I will remove any entry from the game that either takes a excessive amount of time or trys to cheat in anyways that i see fit

Your answer should be in this format, the first function being the stats and the second being the actual logic, notice that i used a exclamation mark because if you just make new lines between code blocks the parser wont see two different code blocks so you have to use any symbol(just use a exclamation mark) to seperate them:

var bot = {
name: "testBot",
numbOfBullets: 7,
shotsPerTurn: 1,
moveSpeed: 2
}
return bot


!

var shots = []
var testBot_moveUp = true
if(Math.random() * 2 > 1)
testBot_moveUp = false
shots.push(Math.floor((Math.random() * 24) + 1))
var move = 0
if (testBot_moveUp)
move = yourMovement[yourMovement.length - 1] + 2
else
move = yourMovement[yourMovement.length - 1] - 2
move = 12
var play = []
play.shots = shots
play.move = move
return play


And here is the controller: http://jsfiddle.net/vajura/sh4wr82r/23/ .Just open the link and wait for the bots to load, then select which one you want in the fight(probably all) and press the start button.

• Why is this a separate post and not in the sandbox? Jun 13, 2015 at 4:40
• Becuase of the way the answers work Jun 13, 2015 at 10:55
• The shooting field is 25 spaces width (1-24 inclusive) That is only 24 spaces. 0-24 would be 25. Dec 1, 2017 at 14:27

var bot = {
name: "testBot",
numbOfBullets: 7,
shotsPerTurn: 1,
moveSpeed: 2
}
return bot


!

var shots = []
var testBot_moveUp = true
if(Math.random() * 2 > 1)
testBot_moveUp = false
shots.push(Math.floor((Math.random() * 24) + 1))
var move = 0
if (testBot_moveUp)
move = yourMovement[yourMovement.length - 1] + 2
else
move = yourMovement[yourMovement.length - 1] - 2
move = 12
var play = []
play.shots = shots
play.move = move