Apps Home
|
Create an App
random battle ship
Author:
crystalsopen2
Description
Source Code
Launch App
Current Users
Created by:
Crystalsopen2
//Battleship by slavabo [cbslavabo[at]gmail.com] //Modified with prizes by rowan596 //Modified with random shot by crystalsopen var rls = 'v20180213'; //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 var installmentFund = 0; // allows missiles to be purchased with a series of small tips. // cb.settings_choices = [ {name:'cst', type:'int', minValue:1, maxValue:999, default:10, label: 'Missile Cost (1-999)'}, {name: 'hashtags', type: 'str', label: 'Hashtags', defaultValue:'#sexy #strip #pussy #cumshow #dildo #boobs #wet #sex'}, {name: 'prize_1', type: 'str', label: '1st sink prize'}, {name: 'prize_2', type: 'str', label: '2nd sink prize'}, {name: 'prize_3', type: 'str', label: '3rd sink prize'}, {name: 'prize_4', type: 'str', label: '4th sink prize'}, {name: 'bulk', type: 'choice', choice1: 'yes', choice2: 'no', defaultValue: 'yes', label: "Allow bulk missile purchases"}, {name: 'installment', type: 'choice', choice1: 'yes', choice2: 'no', defaultValue: 'yes', label: "Allow installment missile purchases"}, {name: 'grand_prize', type: 'str', label: 'Grand prize (all boats sunken)', defaultValue:'cum show'}, ]; cb.onEnter(function(user) { if (user['has_tokens']&&shp.length) cb.chatNotice('*** A Raiders of the Battleship game is in progress. tip '+cb.settings.cst+' tokens to fire a missile. Type /prizes to see the prizes and /board to see the current board.',user['user'],'','','bold'); }); cb.onTip(function (tip) { var tipLeft = tip['amount'] + installmentFund; var myMsg = tip['message']; while (tipLeft>=cb.settings.cst) { if (cb.settings['bulk'] == 'yes') { tipLeft = tipLeft - cb.settings.cst; } else { tipLeft = 0; } if (myMsg.match(/^[A-I]{1}[1-9]{1}$/gi)) { //regex a1-i9 ret = takeInput(myMsg); myMsg = ""; } else { ret = takeInput(randomInput()); } cb.chatNotice('*** '+ret.message,'','','','bold'); showBoard(); if (ret.prize!=null) { cb.sendNotice(tip['from_user']+' '+ret.prize, '', '#99FF99', '', 'bold'); } } if (cb.settings['installment'] == 'yes'){ installmentFund = tipLeft; if (installmentFund > 0) { cb.chatNotice("Installment fund for next random missile:".concat(installmentFund, " of ", cb.settings.cst, " tokens."),'','','','bold'); } } 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']=='/prizes') { msg['X-Spam'] = true; showPrizes(msg['user']); } 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 /prizes to see what you can win.',msg['user']); cb.chatNotice('*** Enter row [a-i] then column [1-9] in tipnote to fire. ie. "a4" | Type /board to see the board',msg['user']); } else if (msg['m']=='/about') { msg['X-Spam'] = true; cb.chatNotice('*** BATTLESHIP '+rls+' by slavabo (prizes by rowan596).','','','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 showPrizes(username) { var notices = '**** Possible Prizes ****\n'; notices += '1st sink - ' + cb.settings.prize_1 + '\n'; notices += '2nd sink - ' + cb.settings.prize_2 + '\n'; notices += '3rd sink - ' + cb.settings.prize_3 + '\n'; notices += '4th sink - ' + cb.settings.prize_4 + '\n'; notices += 'Win game (all 5 sunken) - ' + cb.settings.grand_prize; cb.sendNotice(notices, username, '#D1F0FF', '', 'bold'); } function randomInput () { do{ var row = Math.floor((Math.random() * ltr.length)); cb.log("generated row:".concat(row)); var col = Math.floor((Math.random() * bsz + 1)) - 1; cb.log("generated col:".concat(col)); } while (brd[row][col] != '_'); cb.log("randomInput sent:".concat(ltr[row].concat(col + 1))) return ltr[row].concat(col + 1); } function takeInput(loc) { cb.log("took input:".concat(loc)); loc=loc.toUpperCase(); var prizemsg = null; act = new Date().getTime(); if (brd[fromLetter(loc.charAt(0))][loc.charAt(1)-1]!="_") { cb.log("guessed->".concat(fromLetter(loc.charAt(0)), " ,",loc.charAt(1)-1)); cb.log("board has:".concat(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("Raiders of the Battleship - Game Over. Thanks For Tipping!"+ cb.settings.hashtags); tim = loc+" is a hit and sink! There are no remaining ships on the board."; prizemsg = 'won the grand prize! '+cb.settings.grand_prize+'!'; cb.chatNotice('*** Type /newgame to begin a new game of Battleship.',cb.room_slug,'','','bold'); } else { if (shp.length == 4) { prize = cb.settings.prize_1; } else if (shp.length == 3) { prize = cb.settings.prize_2; } else if (shp.length == 2) { prize = cb.settings.prize_3; } else if (shp.length == 1) { prize = cb.settings.prize_4; } prizemsg = 'won a prize! ' + prize; 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 { message: tim, prize: prizemsg } } 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("AIM/random missile Battleship|tip "+cb.settings.cst+" tokens to fire a missile. " + cb.settings.hashtags); if (cb.settings['installment'] == 'yes' && cb.settings['bulk'] != 'yes') { cb.settings['bulk'] = 'yes'; cb.chatNotice("Installment fund requires bluk tipping",'','','','bold'); } 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 /prizes to see the prizes and /board to see the board','','','','bold'); if (!(Math.floor((new Date().getTime()-act)/1000)%300)) showBoard(); if (installmentFund > 0 ) { cb.chatNotice("Installment fund for next random missile:".concat(installmentFund, " of ", cb.settings.cst, " tokens."),'','','','bold'); } cb.setTimeout(counter,60000); } else cb.setTimeout(counter,60000-(new Date().getTime()-act)); } // initGame();
© Copyright Chaturbate 2011- 2025. All Rights Reserved.