Created
March 17, 2016 14:38
-
-
Save izelnakri/21d1f4584985ca50eb04 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var App = function(params) { | |
| var cardNumbers = [ | |
| 2, 3, 4, 5, 6, 7, 8, 9, | |
| 10, 'J', 'Q', 'K', 'A' | |
| ], | |
| cardTypes = ['Clubs', 'Diamonds', 'Hearts', 'Spades']; | |
| var app = { | |
| VERSION: '0.0.1', | |
| isActive: false, | |
| players: [], // array of Objects | |
| currentPlayer: undefined, | |
| deck: _.reduce(cardNumbers, function(array, element, index) { | |
| _.times(4, function(number) { | |
| array.push({ | |
| type: cardTypes[number], | |
| value: element, | |
| implicitValue: getImplicitValue(element) | |
| }); | |
| }); | |
| if (index === cardNumbers.length - 1) { | |
| _.times(2, function(n) { | |
| array.push({ value: 'Joker' }); | |
| }); | |
| } | |
| return array; | |
| }, []), | |
| pickUpPile: [], | |
| exilePile: [], | |
| init: startTheGame, | |
| addUser: function(username) { | |
| // make this Promise based!!; | |
| if(this.players.length < 4) { | |
| console.log(username + ' has joined the room'); | |
| distributeCardsFor(username); | |
| return this; | |
| } | |
| }, | |
| sendToPickUpPile: sendToPickUpPile, | |
| }; | |
| return app; | |
| function getImplicitValue(value) { | |
| // maybe turn this into switch statement | |
| if (value === 'J') { return 11; } | |
| if (value === 'Q') { return 12; } | |
| if (value === 'K') { return 13; } | |
| if (value === 'A') { return 14; } | |
| if (value === 3 || value === 'Joker') { | |
| return; | |
| } | |
| return value; | |
| } | |
| function setImplicitValue(card, optional) { | |
| if (card.value === 3) { | |
| if (app.deck.length) { | |
| card.implicitValue = _.last(app.deck).value; | |
| } else { | |
| card.implicitValue = 3; | |
| } | |
| } else if (card.value === 'Joker') { | |
| card.implicitValue = optional; | |
| } | |
| return card; | |
| } | |
| function takeCardFromDeck(user, destinationArray) { | |
| if (destinationArray && app.deck.length > 0 && destinationArray.length < 3) { | |
| var randomIndex = _.random(0, app.deck.length), | |
| chosenCard = app.deck.splice(randomIndex - 1, 1)[0]; | |
| destinationArray.push(chosenCard); | |
| if (destinationArray.length === 3) { | |
| return chosenCard; | |
| } | |
| return takeCardFromDeck(user, destinationArray); | |
| } | |
| } | |
| function distributeCardsFor(username) { | |
| var user = { | |
| username: username, | |
| faceUpCards: [], | |
| faceDownCards: [], | |
| hand: [], | |
| status: '' // disconnected, finished | |
| }; | |
| takeCardFromDeck(user, user.faceDownCards); | |
| takeCardFromDeck(user, user.faceUpCards); | |
| takeCardFromDeck(user, user.hand); | |
| console.log(user); | |
| app.players.push(user); | |
| return user; | |
| } | |
| function startTheGame() { | |
| app.currentPlayer = app.players[0]; | |
| app.isActive = true; | |
| return findTheLowestCardInPlay(); | |
| } | |
| function findTheLowestCardInPlay(number) { | |
| if (!number) { number = 4; } | |
| var foundLowCard = _.find(app.currentPlayer.hand, function(card) { | |
| return card.implicitValue === number; | |
| }), | |
| playerIndex = app.players.indexOf(app.currentPlayer); | |
| if (foundLowCard) { | |
| var cardIndex = _.findIndex(app.currentPlayer.hand, foundLowCard); | |
| app.pickUpPile.push(foundLowCard); | |
| app.currentPlayer.hand.splice(cardIndex + 1, 1); | |
| takeCardFromDeck(app.currentPlayer, app.currentPlayer.hand); | |
| return number; | |
| } | |
| if (playerIndex === app.players.length - 1) { | |
| number += 1; | |
| } | |
| changeCurrentPlayer(); | |
| return findTheLowestCardInPlay(number); | |
| } | |
| // test this function hardcore | |
| function sendToPickUpPile(username, card) { | |
| // add also next person number 8 functionality | |
| // exilePickupPile functionality with 10 and 4 cards | |
| // give this function care | |
| var user = _.find(app.players, function(player) { | |
| return player.username === username; | |
| }); | |
| if (!user) { console.warn('USER NOT FOUND'); } | |
| // THIS WILL CHANGE | |
| if (_.includes(user.hand, card) && cardIsAcceptableToPickUpPile(card)) { | |
| if (app.currentPlayer !== user) { | |
| // take it out from the deck | |
| return app.pickUpPile.push(card); | |
| } | |
| // find the index from the hand | |
| // take it off from the hand | |
| if (app.currentPlayer === user) { | |
| // takeCardFromDeck(user, user.hand); | |
| app.currentPlayer = app.players[_.findIndex(app.players, user) + 1]; | |
| console.log('currentPlayer changed to ' + app.currentPlayer.username); | |
| // user should take a card | |
| // increment the user | |
| // find the currentPlayer index | |
| // increment it one from app.players index | |
| } | |
| return true; | |
| } | |
| console.log('CARD CANNOT GO TO PILE : ' + card.value); | |
| console.log('Last card in the pile is ' + _.last(app.pickUpPile).value); | |
| return false; | |
| } | |
| function checkIfUserCanPlay() { // maybe accept username param | |
| var userCanPlay = _.every(app.currentPlayer.hand, function (card) { | |
| return cardIsAcceptableToPickUpPile(card); | |
| }); | |
| if (!userCanPlay) { return changeCurrentPlayer(); } | |
| } | |
| function completeUserHandToThree() { | |
| if (app.deck.length > 0 && app.currentPlayer.hand.length < 3) { | |
| takeCardFromDeck(app.currentPlayer, app.currentPlayer.hand); | |
| return completeUserHandToThree(); | |
| } | |
| return changeCurrentPlayer(); | |
| } | |
| function changeCurrentPlayer() { | |
| var playerIndex = _.findIndex(app.players, app.currentPlayer); | |
| if (playerIndex === app.players.length - 1) { | |
| app.currentPlayer = app.players[0]; | |
| } else { | |
| app.currentPlayer = app.players[playerIndex + 1]; | |
| } | |
| // checkIfUserCanPlay here | |
| return app.currentPlayer; | |
| } | |
| function takePickupPile() { | |
| if (!app.currentPlayer) { console.warn('CURRENT USER NOT FOUND'); } | |
| _.each(app.pickUpPile, function (card) { | |
| app.currentPlayer.hand.push(card); | |
| }); | |
| app.pickUpPile.length = 0; | |
| return app.currentPlayer.hand; | |
| } | |
| function cardIsAcceptableToPickUpPile(card) { | |
| if (_.last(app.pickUpPile).value <= card.value || _.includes([2, 3, 'Joker'], card.value)) { | |
| console.log('card is accepted to the pickUpPile'); | |
| console.log('pickUpPile had ' + _.last(app.pickUpPile).value); | |
| console.log('current card was ' + card.value); | |
| return true; | |
| } | |
| return false; | |
| } | |
| function clearPickupPile() { | |
| _.each(app.pickUpPile, function (card) { | |
| app.exilePile.push(card); | |
| }); | |
| app.pickUpPile.length = 0; | |
| return app.exilePile; | |
| } | |
| }(); | |
| App.addUser('izel'); | |
| App.addUser('garry'); | |
| App.addUser('alex'); | |
| App.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment