Bots Home
|
Create an App
Four in a Row
Author:
gargoyle1
Description
Source Code
Launch Bot
Current Users
Created by:
Gargoyle1
/** * Bot: Four in a Row * Version: 1.0 * Author: gargoyle1 * Date: 26.01.16 */ var board = []; var player1 = cb.room_slug; var player2 = ''; var playernow = cb.room_slug; var nocoin = '_'; var coin1 = 'o'; var coin2 = 'x'; var coin = ''; var count = 0; var column = 0; var row = 5; var win= 'no'; cb.onMessage(function (msg) { var message = msg['m'].split(' '); if (msg['user'] == cb.room_slug) { if (msg['m'] == '!c') { msg['X-Spam'] = true; cb.sendNotice('!c = show commands', '', '', '', ''); cb.sendNotice('!board = print game board', '', '', '', ''); cb.sendNotice('!start = start a new game', '', '', '', ''); cb.sendNotice('!player1 name = set player 1 (chatroom owner as default)', '', '', '', ''); cb.sendNotice('!player2 name = set player 2', '', '', '', ''); cb.sendNotice('!col # = actual player throw coin in column #', '', '', '', ''); } if (msg['m'] == '!board') { msg['X-Spam'] = true; printboard(); } if (msg['m'] == '!start') { msg['X-Spam'] = true; newgame(); } if (message[0] == '!player1') { msg['X-Spam'] = true; player1 = message[1]; cb.sendNotice(player1 + ' is set for player1!', '', '', '', ''); } if (message[0] == '!player2') { msg['X-Spam'] = true; player2 = message[1]; cb.sendNotice(player2 + ' is set for player2!', '', '', '', ''); } } if (msg['user'] == playernow && message[0] == '!col' && message[1] >= 1 && message[1] <= 7) { msg['X-Spam'] = true; column = message[1]; column = column-1; row = 5; checkcolumn(); printboard(); checkwinner(); if (win == 'yes') { newgame(); } else { changeplayer(); } } return msg; }); function setboard() { for (var i=0; i<=5; i++) { board[i] = new Object (); } } function resetboard() { count = 0; coin = ''; for (var y=0; y<=5; y++) { for (var x=0; x<=6; x++) { board[y][x] = nocoin; } } } function printboard() { for (var y=0; y<=5; y++) { var msg = ' | '; for (var x=0; x<=6; x++) { msg += board[y][x]; msg += ' | '; } cb.sendNotice(msg, '', '', '', 'bold'); } } function newgame() { if (player2 == '') { cb.sendNotice('First, set an opponent, ' + cb.room_slug + '!', '', '', '', ''); } else { resetboard(); playernow = player1; win= 'no'; cb.sendNotice('### NEW GAME! ###', '', '#000099', '#FFFFFF', ''); cb.sendNotice('# ' + player1 + ' vs. ' + player2 + '! #', '', '#000099', '#FFFFFF', ''); cb.sendNotice('', '', '', '', ''); printboard(); cb.sendNotice('', '', '', '', ''); cb.sendNotice('Choose a column, ' + playernow + '!', '', '', '', 'bold'); cb.sendNotice('', '', '', '', ''); } } function checkcolumn() { if (board[row][column] == nocoin) { if (playernow == player1) { board[row][column] = coin1; } else { board[row][column] = coin2; } } else { row = row-1; if (row >= 0) { checkcolumn(); } } } function checkwinner() { // check rows for (var y=0; y<=5; y++) { if (board[y][0] != nocoin) { count = 1; coin = board[y][0]; } for (var x=1; x<=6; x++) { if (board[y][x] == coin && board[y][x] != nocoin) { count++; } else { coin = board[y][x]; count = 1; } if (count == 4) { winner(); } } } // check columns for (var x=0; x<=6; x++) { if (board[0][x] != nocoin) { count = 1; coin = board[0][x]; } for (var y=1; y<=5; y++) { if (board[y][x] == coin && board[y][x] != nocoin) { count++; } else { coin = board[y][x]; count = 1; } if (count == 4) { winner(); } } } // check diagonals if (board[0][3] != nocoin) { coin = board[0][3]; y = 0; count = 0; for (var x=3; x>=0; x--) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } y = 0; count = 0; for (var x=3; x<=6; x++) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } } if (board[1][3] != nocoin) { coin = board[1][3]; y = 0; count = 0; for (var x=4; x>=0; x--) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } y = 0; count = 0; for (var x=2; x<=6; x++) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } } if (board[2][3] != nocoin) { coin = board[2][3]; y = 0; count = 0; for (var x=5; x>=0; x--) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } y = 0; count = 0; for (var x=1; x<=6; x++) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } } if (board[3][3] != nocoin) { coin = board[3][3]; y = 0; count = 0; for (var x=6; x>=1; x--) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } y = 0; count = 0; for (var x=0; x<=5; x++) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } } if (board[4][3] != nocoin) { coin = board[4][3]; y = 1; count = 0; for (var x=6; x>=2; x--) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } y = 1; count = 0; for (var x=0; x<=4; x++) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } } if (board[5][3] != nocoin) { coin = board[5][3]; y = 2; count = 0; for (var x=6; x>=3; x--) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } y = 2; count = 0; for (var x=0; x<=3; x++) { if (board[y][x] == coin) { count++; } y++; if (count == 4) { winner(); } } } } function winner() { if (coin == coin1 && count == 4) { cb.sendNotice('', '', '', '', ''); cb.sendNotice('Four in a Row! ' + player1 + ' wins the game!', '', '', '', 'bold'); cb.sendNotice('', '', '', '', ''); } else if (coin == coin2 && count == 4){ cb.sendNotice('', '', '', '', ''); cb.sendNotice('Four in a Row! ' + player2 + ' wins the game!', '', '', '', 'bold'); cb.sendNotice('', '', '', '', ''); } win = 'yes'; } function changeplayer() { if (playernow == player1) { playernow = player2; } else { playernow = player1; } cb.sendNotice('', '', '', '', ''); cb.sendNotice('Choose a column, ' + playernow + '!', '', '', '', 'bold'); cb.sendNotice('', '', '', '', ''); } function init() { setboard(); resetboard(); } init();
© Copyright Chaturbate 2011- 2026. All Rights Reserved.