Permalink
Browse files

transfer of WIP files from private repo.

  • Loading branch information...
1 parent dff025b commit 93663d68941f18e5fbc56934aa328f0a3d071798 James Gater committed Mar 1, 2012
View
@@ -0,0 +1,24 @@
+function Card(s,n){
+ var suit = s;
+ var number = n;
+ this.getSuit = function() { return suit; };
+ this.getNumber = function() {return number;};
+ this.getValue = function() {
+ return (number > 10) ? 10 : (number === 1) ? 11 : number;
+ };
+}
+
+function Hand() {
+ var card1 = deal();
+ var card2 = deal();
+ this.score = function () {
+ return card1.getValue() + card2.getValue();
+ };
+}
+
+// Our deal function will return a random card
+function deal() {
+ var suit = Math.floor((Math.random()*4) + 1);
+ var number = Math.floor((Math.random()*13) + 1);
+ return new Card(suit,number);
+};
@@ -0,0 +1,49 @@
+//Create an object called FizzBuzzPlus
+
+var FizzBuzzPlus = {
+// isFizzBuzzie
+// return true if the provided value is
+// a multiple of 3 or 5 but not both 3 and 5.
+// otherwise it returns false
+// arguments: number - integer
+// returns: true or false - boolean
+ isFizzBuzzie : function (n) {
+ return ((n%3 === 0 || n%5 === 0) && !(n%3 === 0 && n%5 ===0));
+ },
+ //returns the sum of all the numbers below the maximum provided
+ //which are multiples of 3 or 5 but not both
+ //arguments: number - maximum value for search
+ //returns: number - sum of the numbers below the maximum which are multiples of 3 or 5 but not both
+ getFizzBuzzSum : function (n) {
+ var sum = 0;
+ for (i=1;i<n;i++) {
+ if (this.isFizzBuzzie(i)) { sum += i;}
+ }
+ return sum;
+ },
+ //getFizzBuzzCount
+ //returns the count of all the numbers below the maximum provided
+ //which are multiples of 3 or 5 but not both
+ //arguments: number - maximum value for search
+ //returns: number - count of the numbers below the maximum whihch are multiples of 3 or 5 but not both
+ getFizzBuzzCount : function (n) {
+ var count = 0;
+ for (i=1;i<n;i++) {
+ if (this.isFizzBuzzie(i)) { count++; }
+ }
+ return count;
+ },
+ //getFizzBuzzAverage
+ //returns the average(sum/count) of all the numbers below the maximum provided
+ //which are multiples of 3 or 5 but not both
+ //arguments: number - maximum value for search
+ //returns: number - average(sum/count) of the numbers below the maximum whihch are multiples of 3 or 5 but not both
+ getFizzBuzzAverage : function (n) {
+ return this.getFizzBuzzSum(n) / this.getFizzBuzzCount(n);
+ }
+
+};
+
+console.log (FizzBuzzPlus.getFizzBuzzSum(15));
+console.log (FizzBuzzPlus.getFizzBuzzCount(15));
+console.log (FizzBuzzPlus.getFizzBuzzAverage(15));
@@ -0,0 +1,18 @@
+var comedy = {
+ schadenfreude : function (n) {
+ for (i=0;i<n;i++){
+ console.log("HaHa!");
+ }
+ },
+ irony : function () {
+ alert("You have successfully run this function.");
+ },
+ slapstick: function (name) {
+ return (name === "Murdoch" || name === "Gates" ) ? "Pie!" : ((name === "Hollande") ? "Flour" : "Make Up!" );
+ }
+
+};
+
+comedy.schadenfreude(4);
+comedy.irony();
+
@@ -0,0 +1,22 @@
+// if number = 5 or 6 return 20
+// if number = 1 return 0
+// if number = 15 return 40
+// else return number *2
+
+// (test) ? true do... : false do... ;
+
+// (number === 5 || number === 6 ) ? 20 : number*2 ;
+
+// (number === 5 || number === 6 ) ? 20 : (number === 1) ? 0 : number * 2 ;
+
+// (number === 5 || number === 6 ) ? 20 : (number === 1) ? 0 : (number === 15) ? 40 : number * 2 ;
+
+function mytest (number) {
+ return (number === 5 || number === 6 ) ? 20 : (number === 1) ? 0 : (number === 15) ? 40 : number * 2 ;
+}
+
+console.log( mytest(5) );
+console.log( mytest(6) );
+console.log( mytest(1) );
+console.log( mytest(15) );
+console.log( mytest(7) );
View
@@ -0,0 +1,28 @@
+var Wibble = {
+
+ Wobble: function(a, b) {
+ if(a===b)
+ //if the variables are equal return 0
+ return 0;
+ else {
+ //decrement the larger of the 2 values
+ if (a>b) {
+ a--;
+ } else {
+ b--;
+ }
+ //call this function again with the decremented values
+ //once they are equal the functions will return up the stack
+ //adding 1 to the return value for each recursion
+ return Wibble.Wobble(a,b)+1;
+ }
+ },
+ Wob: function(a,b) {
+ return (a===b) ? 0 : (a>b) ? a-b : b-a ;
+ }
+
+};
+
+console.log(Wibble.Wob(7,4));
+
+
View
@@ -0,0 +1,161 @@
+
+// create necessary classes
+
+ // the ArrayOfCards class holds an array of cards, and the methods to access them
+
+ // the DeckOfCards class contains our playing deck of standard playing cards, it is a subclass of ArrayOfCards
+
+ // the Hand class contains the cards for a single player, it is a subclass of ArrayOfCards
+
+ // the PlayerHands class contains Hands
+
+
+ function ArrayOfCards() {
+ this.cards = [];
+ var acard;
+ this.addCard = function (acard) { this.cards.push(acard); };
+ this.removeCard = function () {
+ acard = this.cards.pop();
+ console.log(acard.getNumber());
+ return acard;
+ };
+ this.showAllCards = function () { return this.cards; };
+ this.empty = function () { this.cards = []; };
+ }
+
+
+ function DeckOfCards() {
+ var tmp, i, j, k, n;
+ this.shuffle = function () {
+ for ( i = this.cards.length - 1 ; i ; i-- ) {
+ j = Math.floor(Math.random() * (i + 1));
+ tmp = this.cards[j];
+ this.cards[j] = this.cards[i];
+ this.cards[i] = tmp;
+ }
+ };
+ this.addDeck = function (n) {
+ for ( k=0; k<n ; k++ ) {
+ for ( j=1; j<=4; j++ ) {
+ for ( i=1; i<=13; i++ ) { this.addCard( new Card(j,i) ); }
+ }
+ }
+ };
+ }
+ DeckOfCards.prototype = new ArrayOfCards();
+
+ function Hand(name) {
+ this.playerName = name;
+ this.getScore = function () {
+ var score = 0;
+ for (acard in this.cards) { score+=this.cards[acard].getValue(); }
+ return score;
+ };
+
+ this.showHand = function() {
+ console.log( this.playerName + "'s cards are:" );
+ for (acard in this.cards) {
+ console.log(' - The ' + this.cards[acard].getNumber() + ' of ' + this.cards[acard].getSuit() + ', worth ' + this.cards[acard].getValue() + ' points');
+ }
+ console.log("total score of " + this.playerName + "'s hand is " + this.getScore() + ' points.\n');
+ };
+ }
+ Hand.prototype = new ArrayOfCards();
+
+
+ function PlayerHands() {
+ this.hands = [];
+ this.addPlayer = function (name) { this.hands.push( new Hand(name) ) };
+ }
+
+
+
+ function Card(s,n){
+ this.acesHigh = true;
+ var suit = s;
+ var number = n;
+ var allSuits = ["Hearts", "Clubs", "Diamonds", "Spades"];
+ this.getSuit = function() { return allSuits[suit-1]; };
+ this.getValue = function() {
+ return (number > 10) ? 10 : (number === 1) ? ( (this.acesHigh) ? 11 : 1 ) : number;
+ };
+ this.getNumber = function() {
+ switch(number) {
+ case 11 :
+ return "Jack";
+ case 12 :
+ return "Queen";
+ case 13 :
+ return "King";
+ case 1 :
+ return "Ace";
+ default :
+ return number;
+ }
+ };
+ }
+
+
+ //
+ // Let's prepare the game
+ //
+
+ // Create a deck object to play with
+ var blackjackDeck = new DeckOfCards();
+
+ // Add a single x52 deck of cards to it
+ blackjackDeck.addDeck(1);
+
+ // test code to confirm deck has been created properly
+ //var mytestarray = blackjackDeck.showAllCards();
+ //for (card in mytestarray) {
+ // console.log(mytestarray[card].getNumber() + " of " + mytestarray[card].getSuit() );
+ //}
+
+ // and shuffle them
+ blackjackDeck.shuffle();
+
+ // test code to confirm cards have been shuffled
+ //var mytestarray = blackjackDeck.showAllCards();
+ //for (card in mytestarray) {
+ // console.log(mytestarray[card].getNumber() + " of " + mytestarray[card].getSuit() );
+ //}
+
+ //Let's make it two player
+ //var blackjackHands = new PlayerHands ();
+ //blackjackHands.addPlayer("Dealer");
+
+ // add sanity check for askName
+ //var askName = prompt("What's your name?");
+
+ //blackjackHands.addPlayer("James");
+ var allhands = [];
+ allhands[0] = new Hand("Dealer");
+ allhands[1] = new Hand("James");
+ //
+ // Finished prep, now start playing!
+ //
+
+ // show the deck
+
+
+ // deal 2 cards into dealer's hand from the deck
+ //(and if you can follow the levels of object recursion in this step without pen and paper, you're a better person than I)
+ //console.log ( blackjackDeck.removeCard() );
+ allhands[0].addCard( blackjackDeck.removeCard() );
+ allhands[0].addCard( blackjackDeck.removeCard() );
+
+ // deal 2 cards into player 1's hand
+ allhands[1].addCard( blackjackDeck.removeCard() );
+ allhands[1].addCard( blackjackDeck.removeCard() );
+
+ //console.log(blackjackHands.hands[0].cards[1].getNumber());
+
+ // Let's see who got what
+ //allhands[0].showHand();
+ //allhands[1].showHand();
+
+
+
+
+
View
@@ -0,0 +1,30 @@
+// shuffle an array function
+ exports.shuffle = function(array) {
+ var tmp, i, j;
+ for ( i = array.length - 1 ; i ; i-- ) {
+ j = Math.floor(Math.random() * (i + 1));
+ tmp = array[j];
+ array[j] = array[i];
+ array[i] = tmp;
+ }
+ return array;
+ };
+
+// Our deal function will return the last card(s) from the deck
+ exports.deal = function(cardsToDeal,hand,deck) {
+ if (!hand) {hand = [];}
+ for (i=0;i<cardsToDeal;i++) {
+ hand.push(deck.pop());
+ }
+ return hand;
+ };
+
+// Score the hand
+ exports.score = function(hand) {
+ var totalScore=0;
+ for (var mycard in hand) {
+ totalScore += hand[mycard].value;
+ }
+ return totalScore;
+ };
+

0 comments on commit 93663d6

Please sign in to comment.