Bots Home
|
Create an App
rollanothersillytestbot02
Author:
rollimproveddiechancefix02
Description
Source Code
Launch Bot
Current Users
Created by:
Rollimproveddiechancefix02
if (cb == null) { var cb = { changeRoomSubject: function(new_subject) {}, drawPanel: function() {}, log: function(message) {}, onDrawPanel: function(func) {}, onEnter: function(func) {}, onLeave: function(func) {}, onMessage: function(func) {}, onShowStatus: function(func) {}, onTip: function(func) {}, room_slug: '', sendNotice: function(message, to_user, background, foreground, weight, to_group) {}, setTimeout: function(func, msec) {}, settings_choices: [], settings: {}, tipOptions: function(func) {}, limitCam_start: function(message, allowed_users) {}, limitCam_stop: function() {}, limitCam_addUsers: function(allowed_users) {}, limitCam_removeUsers: function(removed_users) {}, limitCam_removeAllUsers: function() {}, limitCam_userHasAccess: function(user) {}, limitCam_allUsersWithAccess: function() {}, limitCam_isRunning: function() {}, }; } cb.settings_choices = [ { name: 'misslecost', type: 'int', minValue: 1, label: 'How many tokens to take a shot?', defaultValue: 22 }, { name: 'tipdisplaychoice', type: 'choice', label: 'When a shot is made display the board to?', choice1: 'Everyone', choice2: 'Shot Tipper', choice3: 'No One', defaultValue: 'Shot Tipper' }, { name: 'intervaldisplaytime', type: 'choice', label: 'In minutes, how often should the bot advertise itself', choice1: 5, choice2: 10, choice3: 15, choice4: 20, choice5: 25, choice6: 30, choice7: 45, choice8: 60, defaultValue: 10 }, { name: 'intervaldisplaychoice', type: 'choice', label: 'Display the board as a part of the bot advertisement?', choice1: 'Yes', choice2: 'No', defaultValue: 'No' }, {name: 'cmd_prefix', type: 'str', minLength: 1, maxLength: 1, label: 'Prefix for bot commands. (Might want to change if you have other bots using the same commands.)', defaultValue: '/'} ]; var the_bot = { bot_name: 'A Bettter Battleship by aety', totalTokens: 0, misslecost: 0, cmdPrefix: "/", boardontip: false, userontip: true, boardonadvertise: false, loadCBSettings: function() { the_bot.misslecost = parseInt(cb.settings.misslecost); the_bot.cmdPrefix = cb.settings.cmd_prefix; switch(cb.settings.tipdisplaychoice) { case 'Everyone': boardontip = true; break; case 'Shot Tipper': userontip = true; break; case 'No One': boardontip = false; userontip = false; break; default: boardontip = false; userontip = true; break; } if(cb.settings.intervaldisplaychoice == 'Yes')boardonadvertise = true; }, advertise: function(username) { var msg = the_bot.bot_name; //CODE HERE cb.sendNotice(msg, username,'','','',''); } }; function getRandomIntInclusive(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } var Game = { validletters: "ABCDEFGHIJ", board: "_".repeat(100), shots: new Array, ship_array: [ {id: 0, name:'Carrier', shipsize:5, hit: 0, horizontal:true}, {id: 1, name:'Battleship', shipsize:4, hit: 0, horizontal:true}, {id: 2, name:'Cruiser', shipsize:3, hit: 0, horizontal:true}, {id: 3, name:'Submarine', shipsize:3, hit: 0, horizontal:true}, {id: 4, name:'Destroyer', shipsize:2, hit: 0, horizontal:true}, ], reset: function() { Game.board = "_".repeat(100); Game.shots = new Array; ship_array = [ {id: 0, name:'Carrier', shipsize:5, hit: 0, horizontal:true}, {id: 1, name:'Battleship', shipsize:4, hit: 0, horizontal:true}, {id: 2, name:'Cruiser', shipsize:3, hit: 0, horizontal:true}, {id: 3, name:'Submarine', shipsize:3, hit: 0, horizontal:true}, {id: 4, name:'Destroyer', shipsize:2, hit: 0, horizontal:true}, ]; }, posFromSqID: function(SquareID) { var pos = -1; if(!SquareID || SquareID.length > 3)return pos; SquareID = SquareID.toUpperCase(); var x = Game.validletters.indexOf(SquareID.charAt(0)); var y = 0; if(x == -1) { y = parseInt(SquareID.substring(0,(SquareID.length-1)))-1; x = Game.validletters.indexOf(SquareID.charAt((SquareID.length-1))); } else { y = parseInt(SquareID.substring(1,SquareID.length))-1; } // // debug('y: ' + y); // // debug('x: ' + x); return y*10+x; }, SqIDFromPos: function(pos) { y = Math.floor(pos/10); x = pos%10; sqid = Game.validletters.charAt(x) + y.toString(); return sqid; }, up: function(pos, count) { y = Math.floor(pos/10); x = pos%10; y -= 1; return y*10+x; }, down: function(pos, count) { y = Math.floor(pos/10); x = pos%10; y += 1; return y*10+x; }, left: function(pos, count) { y = Math.floor(pos/10); x = pos%10; x -= 1; return y*10+x; }, right: function(pos, count) { y = Math.floor(pos/10); x = pos%10; x += 1; return y*10+x; }, alreadyShot: function(pos) {switch (Game.board.charAt(pos)) { case '_': return false; case 'O': return true; case 'X': return true; default: return false; }}, shoot: function(SquareID) { var pos = Game.posFromSqID(SquareID); var msg = "Aww, position " + Game.SqIDFromPos(pos) + " was a miss."; var shit = -1; Game.shots.push(Game.SqIDFromPos(pos)); if(!Game.alreadyShot(pos)) { switch (Game.board.charAt(pos)) { case '_': shit = -1; Game.board = Game.board.substr(0, pos) + 'O' + Game.board.substr(pos+1); break; default: shit = parseInt(Game.board.charAt(pos)); Game.board = Game.board.substr(0, pos) + 'X' + Game.board.substr(pos+1); // debug(SquareID); // debug(shit); msg = "Position " + Game.SqIDFromPos(pos) + " was a hit!"; Game.ship_array[shit].hit++; if(Game.ship_array[shit].hit >= Game.ship_array[shit].shipsize) { msg += " You sunk the " + Game.ship_array[shit].name + "!"; } break; } } else msg = Game.SqIDFromPos(pos) + " has already been shot!"; if(Game.gameWon())msg = "YOU SUNK ALL THE SHIPS!!"; return msg; }, spaceBlank: function(pos) { if(pos < 0 || pos >= Game.board.length)return false; if(Game.board.charAt(pos) == '_')return true; return false; }, countBlank: function() { var count = 0; for(var i = 0; i < 100; i++)if(Game.spaceBlank(i))count++; return count; }, randomBlankSquare: function() { var opos = getRandomIntInclusive(0,Game.countBlank()); var rpos = 0; for(var i = 0, j = 0; j <= opos; i++) { if(Game.spaceBlank(i))j++; if(j == opos)rpos = i; } return rpos; }, shipFits: function(pos, ship, horizontal) { curpos = pos; if(horizontal && (pos + ship.shipsize-1) >= ((Math.floor(pos/10)+1)*10))return false; for(var i = 0; i < ship.shipsize; i++) { if(!Game.spaceBlank(curpos))return false; if(horizontal !== false)curpos = Game.right(curpos); else curpos = Game.down(curpos); } return true; }, placeShip: function(pos, ship, horizontal) { if(Game.shipFits(pos, ship, horizontal)) { curpos = pos; for(var i = 0; i < ship.shipsize; i++) { Game.board = Game.board.substr(0, curpos) + ship.id + Game.board.substr(curpos+1); if(horizontal !== false)curpos = Game.right(curpos); else curpos = Game.down(curpos); } return true; } return false; }, populateBoard: function() { var rpos = Game.randomBlankSquare(); var rdir = (getRandomIntInclusive(0,1) == 0)?true:false; for(var i = 0; i < Game.ship_array.length; i++) { // // debug("Placing ship " + i + " " + Game.ship_array[i].name); var placed = false; while(!placed) { // // debug(rpos + " " + rdir + " " + placed); if(Game.placeShip(rpos, Game.ship_array[i], rdir)) { placed = true; break; } rdir = !rdir; // // debug(rpos + " " + rdir + " " + placed); if(Game.placeShip(rpos, Game.ship_array[i], rdir)) { placed = true; break; } rpos = Game.randomBlankSquare(); rdir = (getRandomIntInclusive(0,1) == 0)?true:false; } } }, gameWon: function() { for(var i = 0; i < Game.ship_array.length; i++) { if(Game.ship_array[i].hit < Game.ship_array[i].shipsize)return false; } return true; }, boardDisplayArray: function() { var output = new Array; for(var i = 0; i < 10; i++) { output.push(Game.board.substring((i*10), (i*10)+10)); } return output; } }; function showBoard(username) { var msg = the_bot.bot_name; var spacer = " | "; boardarray = Game.boardDisplayArray(); for(var i = 0; i < boardarray.length; i++) { msg += "\n"; for(var j = 0; j < boardarray[i].length; j++) { msg += displayConversion(boardarray[i].charAt(j)) + spacer; } msg += Game.validletters.charAt(i); } msg += "\n1"; for(var i = 2; i <= 10; i++)msg += spacer + i; cb.sendNotice(msg, username, '', '', 'bold', ''); } function displayConversion(boardcharacter) { output_string = ""; switch(boardcharacter) { case '_': output_string = "_"; break; case 'O': output_string = "O"; break; case 'X': output_string = "X"; break; default: output_string = "_"; break; } return output_string; } cb.onTip(function(tip) { // regex for valid square id A1-J10 or 1J-10A etc if(tip.amount == the_bot.misslecost && tip.message.match(/^([A-J]{1}(?:10|[1-9]{1})|(?:10|[1-9]{1})[A-J]{1})$/gi)) { cb.chatNotice('*** '+Game.shoot(tip.message),'','','','bold'); if(the_bot.boardontip)showBoard(); if(the_bot.userontip)showBoard(tip.user); } the_bot.totalTokens += tip.amount; cb.drawPanel(); }); cb.onDrawPanel(function(user) { return { 'template': '3_rows_of_labels', 'row1_label': 'Total Tips:', 'row1_value': the_bot.totalTokens, 'row2_label': 'Ship Sizes:', 'row2_value': '5, 4, 3, 3, 2', 'row3_label': 'Missile Cost:', 'row3_value': (the_bot.misslecost==1)?the_bot.misslecost+' token':the_bot.misslecost+' tokens' }; }); cb.onEnter(function(user) { the_bot.advertise(user); }); cb.onMessage(function(msg) { var message = msg.m.split(" "); switch (message[0]) { case the_bot.cmdPrefix+'b': case the_bot.cmdPrefix+'board': msg["X-Spam"] = true; // Don't print command messages to chat. msg.background = "#EEEEEE"; if(message[1] == 'all' && ((msg.is_mod === true) || (msg.user == cb.room_slug)))showBoard(); else showBoard(msg.user); break; default: break; } }); function init() { // debug("starting..."); the_bot.loadCBSettings(); Game.populateBoard(); } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.