Bots Home
|
Create an App
earae
Author:
ho_222
Description
Source Code
Launch Bot
Current Users
Created by:
Ho_222
//Battleship by slavabo [cbslavabo[at]gmail.com] // var rls = 'v20130808'; //ReLeaSe version of game var tct = 0; //TipCounT: total tips received var act; //ACTivity (timestamp) var spc = String.fromCharCode(160); //SPaCe: non collapsible character // var bsz = 9; //BoardSiZe: cannot exceed 9 due to regex, brd[], ltr[] and fromLetter() var shp; //SHiPs: array of unhit ship coordinates; shp[0][0] is remaining (unhit) length of ship; shp[0][1+] is coordinates var brd; //BoaRD: status array of all board coordinates var ltr = ["A","B","C","D","E","F","G","H","I"]; //LeTteRs: used for numeric to alphabetic conversions // cb.settings_choices = [ {name:'cst', type:'int', minValue:1, maxValue:999, default:15, label: 'Missile Cost (1-999)'}, ]; cb.onEnter(function(user) { if (user['has_tokens']&&shp.length) cb.chatNotice('*** Battleship game app is in progress. Type /board to see the current board.',user['user'],'','','bold'); }); cb.tipOptions(function(user) { var opt = []; for (var to1=1;to1<=bsz;to1++) for (var to2=1;to2<=bsz;to2++) if (brd[to1-1][to2-1]=='_') opt.push({label:ltr[to1-1]+to2}); return {options:opt, label:'*** SELECT YOUR COORDINATES FROM BELOW ***'}; }); cb.onTip(function (tip) { if (tip['amount']>=cb.settings.cst&&tip['message'].match(/^[A-I]{1}[1-9]{1}$/gi)) { //regex a1-i9 cb.chatNotice('*** '+takeInput(tip['message']),'','','','bold'); showBoard(); } tct += tip['amount']; cb.drawPanel(); }); cb.onDrawPanel(function(user) { return { 'template': '3_rows_of_labels', 'row1_label': 'Total Tips:', 'row1_value': tct, 'row2_label': 'Ship Sizes:', 'row2_value': '5, 4, 3, 3, 2', 'row3_label': 'Missile Cost:', 'row3_value': (cb.settings.cst==1)?cb.settings.cst+' token':cb.settings.cst+' tokens' }; }); cb.onMessage(function (msg) { if (/(?:camshacks|danacamsessions|girls4party|nude4strip).*c[o0]m/ig.test(msg['m'])) msg['X-Spam'] = true; //block spam else if (msg['m']=='/board') { msg['X-Spam'] = true; showBoard(msg['user']); } else if (msg['m']=='/newgame'&&msg['user']==cb.room_slug&&!shp.length) { msg['X-Spam'] = true; cb.chatNotice('*** Broadcaster has begun a new round of Battleship!','','','','bold'); initGame(); } else if (msg['m']=='/help'||msg['m']=='/rules') { msg['X-Spam'] = true; cb.chatNotice('*** Legend: _ indicates an unhit location, X is a hit, 0 is a miss | Type /board to see the current board.',msg['user']); } else if (msg['m']=='/about') { msg['X-Spam'] = true; cb.chatNotice('*** BATTLESHIP '+rls+' by slavabo.','','','bold'); } return msg; }); // function createShip(ssz) { if (ssz>bsz-1) ssz = bsz-1; reroll: do { var tmp = [ssz]; do { var p1b=p1e=Math.floor((Math.random()*bsz)); var p2b=p2e=Math.floor((Math.random()*bsz)); } while (brd[p1b][p2b]=='X'); tmp.push(ltr[p1b]+(p2b+1)); switch (Math.floor((Math.random()*2))) { case 0: //vertical for (var aaa=0;aaa<ssz-1;aaa++) { if ((p1b==0)||((Math.floor((Math.random()*2))&&p1e+1<bsz)&&(brd[p1e+1][p2e]!='X'))) { if (brd[p1e+1][p2b]=='X') continue reroll; p1e++; tmp.push(ltr[p1e]+(p2e+1)); } else { if (brd[p1b-1][p2b]=='X') continue reroll; p1b--; tmp.push(ltr[p1b]+(p2b+1)); } brd[p1b][p2b]='X'; brd[p1e][p2e]='X'; } break reroll; case 1: //horizontal for (var aaa=0;aaa<ssz-1;aaa++) { if ((p2b==0)||((Math.floor((Math.random()*2))&&p2e+1<bsz)&&(brd[p2e+1][p2e]!='X'))) { if (brd[p1b][p2e+1]=='X') continue reroll; p2e++; tmp.push(ltr[p1e]+(p2e+1)); } else { if (brd[p1b][p2b-1]=='X') continue reroll p2b--; tmp.push(ltr[p1b]+(p2b+1)); } brd[p1b][p2b]='X'; brd[p1e][p2e]='X'; } break reroll; } } while (!0); shp.push(tmp); } function showBoard(user) { var sbm = spc+spc; for (var aaa=0;aaa<bsz;aaa++) { for (var bbb=0;bbb<bsz;bbb++) { sbm += brd[aaa][bbb]+spc+spc+spc; } sbm += '| '+ltr[aaa]+'\n'+spc+spc; } for (aaa=1;aaa<=bsz;aaa++) sbm += aaa+spc+spc+spc; sbm += '('+shp.length+' ship'; if (shp.length>1) sbm+= "s"; sbm += ' remaining)\n*** Legend: X = hit, o = miss | Type /help for more info'; if (user) cb.chatNotice(sbm,user); else cb.chatNotice(sbm); } function takeInput(loc) { loc=loc.toUpperCase(); act = new Date().getTime(); if (brd[fromLetter(loc.charAt(0))][loc.charAt(1)-1]!="_") return loc+" has already been guessed. Tip forfeited."; else if (shp.join().indexOf(loc)==-1) { //no ship at location tim = 'Sorry, '+loc+' is a miss. Better luck next time!'; brd[fromLetter(loc.charAt(0))][loc.charAt(1)-1]='o'; } else { baseloop: for (var aaa=0;aaa<shp.length;aaa++) { for (var bbb=1;bbb<shp[aaa].length;bbb++) { if (shp[aaa][bbb]===loc) { shp[aaa][0]--; if (!shp[aaa][0]) { //ship sunk var ssz = shp[aaa].length-1; shp.splice(aaa,1); if (!shp.length) { //no more ships cb.changeRoomSubject("Battleship- Game Over. Thanks For Tipping!"); tim = loc+" is a hit and sink! There are no remaining ships on the board."; cb.chatNotice('*** Type /newgame to begin a new game of Battleship.',cb.room_slug,'','','bold'); } else tim = loc+" is a hit and has sunk a 1x"+ssz+" sized "+shipName(ssz)+"!"; } else tim = loc+" is a hit! Keep firing, the ship is still afloat."; break baseloop; } } } brd[fromLetter(loc.charAt(0))][loc.charAt(1)-1]='X'; } return tim; } function fromLetter(ltr) { switch (ltr) { case "A": return 0; case "B": return 1; case "C": return 2; case "D": return 3; case "E": return 4; case "F": return 5; case "G": return 6; case "H": return 7; case "I": return 8; } } function shipName(ssz) { switch (ssz) { case 2: return 'Destroyer'; case 3: return 'Cruiser'; case 4: return 'Battleship'; case 5: return 'Carrier'; default: return 'Unknown Ship'; } } function initGame() { cb.changeRoomSubject("Battleship- "+cb.settings.cst+"tkn to fire a missile. Type /board to see the current board."); shp = []; brd = [[],[],[],[],[],[],[],[],[]]; //Populate ship coordinates; largest to smallest createShip(5); createShip(4); createShip(3); createShip(3); createShip(2); //Zero out status board; must occur after createShip() for (var aaa=0;aaa<bsz;aaa++) for (var bbb=0;bbb<bsz;bbb++) { brd[aaa][bbb] = "_"; //"fill" character } showBoard(); act = new Date().getTime(); counter(); } function counter() { if (!shp.length) return false; if (new Date().getTime()-act>=60000) { cb.chatNotice('Try your chance at sinking a ship- tip *'+cb.settings.cst+'tkn* to fire a missile! Type /board to see the board.','','','','bold'); if (!(Math.floor((new Date().getTime()-act)/1000)%300)) showBoard(); cb.setTimeout(counter,60000); } else cb.setTimeout(counter,60000-(new Date().getTime()-act)); } // initGame();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.