Last active
December 30, 2015 07:39
-
-
Save harry1989/7797766 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a script to solve Yahoo bingo problem through | |
* websockets. http://yahoobingo.herokuapp.com/ | |
* | |
* @author harry_sistalam | |
*/ | |
var YAHOO_BINGO_URL = 'ws://yahoobingo.herokuapp.com'; | |
var user_obj = {name : 'harry_sistalam', | |
email : '[email protected]', | |
url : 'https://gist.github.com/harry1989/7797766' | |
}; | |
var socket = require('socket.io-client'); | |
var client = socket.connect(YAHOO_BINGO_URL); | |
/** | |
* Called each time a number is drawn | |
*/ | |
var number_drawn = function(number){ | |
var parts = number.match(/(\w)(\d+)/), | |
row = parts[1], | |
num = parts[2]; | |
check_if_bingo(row, num, number); | |
}; | |
/** | |
* Called when the card is issued | |
*/ | |
var card_issued = function(payload){ | |
var slots = payload.slots; | |
Object.keys(slots).forEach(function(item, index){ | |
key_index_map[item] = index; | |
bingo_card.push(slots[item]); | |
number_card.push(new Array(slots[item].length)); | |
}); | |
console.log(bingo_card); | |
}; | |
var number_card = []; | |
var bingo_card = []; | |
var key_index_map = {}; | |
var check_if_bingo = function(row, num, number){ | |
/** | |
* Logic here.. | |
*/ | |
var row_no = key_index_map[row]; | |
var index = bingo_card[row_no].indexOf(parseInt(num)); | |
if(index == -1){ | |
return; | |
}else{ | |
number_card[row_no][index] = 1; | |
} | |
var row,sum = 0,i,j; | |
console.log(num); | |
// check if any of the rows match | |
for (i = 0; i< 5; i++){ | |
row = bingo_card[i]; | |
sum = row.reduce(function(i,o){return i + o}); | |
if(sum == 5){ | |
console.log('Found the match in: ' + (i+1) + ' row'); | |
emit_bingo(); | |
return; | |
} | |
} | |
// check if any of the columns match | |
for (i = 0; i < 5; i++){ | |
sum = 0; | |
for (j =0; j < 5; j++){ | |
val = number_card[j][i] ? number_card[j][i] : 0; | |
sum += val; | |
if(sum == 5){ | |
console.log('Found the match in: ' + (i+1) + ' column'); | |
emit_bingo(); | |
return; | |
} | |
} | |
} | |
// check if the diagnols match | |
sum = 0; | |
for (i= 0; i < 5; i++){ | |
val = number_card[i][i] ? number_card[i][i] : 0; | |
sum+= val; | |
} | |
if(sum == 5){ | |
console.log('Found the match in primary diagnol'); | |
emit_bingo(); | |
return; | |
} | |
// On the secondary column | |
sum = 0; | |
for (i= 4; i >= 0; i--){ | |
val = number_card[4-i][i] ? number_card[i][i] : 0; | |
sum+= val; | |
} | |
if(sum == 5){ | |
console.log('Found the match in secondary diagnol'); | |
emit_bingo(); | |
return; | |
} | |
}; | |
function emit_bingo(){ | |
console.log('Bingo!!!!!'); | |
client.emit('bingo'); | |
} | |
/** | |
* When the socket gets connected, subscribe for the | |
* interested events and attach the proper event handlers | |
*/ | |
client.on('connect', function(){ | |
console.log('Connected to Yahoo bingo server'); | |
client.emit('register',user_obj); | |
client.on('card', card_issued); | |
client.on('number', number_drawn); | |
client.on('disconnect', function(){ | |
process.exit(0); | |
}); | |
client.on('win', function(){ | |
console.log('Won the game!'); | |
}); | |
client.on('lose', function(){ | |
console.log('Lost the game :('); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment