Last active
August 29, 2015 14:17
-
-
Save mixtmeta/e4f60531fea20ba4b7c8 to your computer and use it in GitHub Desktop.
KnockoutJS Cards
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
| *, | |
| *:before, | |
| *:after { | |
| box-sizing: border-box; | |
| } | |
| .card { | |
| margin: 10px; | |
| text-align: center; | |
| padding: 10px; | |
| width: 100px; | |
| height: 150px; | |
| float: left; | |
| position: relative; | |
| } | |
| .diamonds, | |
| .hearts { | |
| background: #E74C3C; | |
| } | |
| .spades, | |
| .clubs { | |
| background: #2C3E50; | |
| color: white; | |
| } | |
| .hearts:before, | |
| .diamonds:before, | |
| .spades:before, | |
| .clubs:before { | |
| display: block; | |
| position: absolute; | |
| bottom: 10px; | |
| right: 25px; | |
| font-size: 64px; | |
| width: 50px; | |
| } | |
| .hearts:before { | |
| content: '\2665'; | |
| } | |
| .diamonds:before { | |
| content: '\2666'; | |
| } | |
| .clubs:before { | |
| content: '\2663'; | |
| color: white; | |
| } | |
| .spades:before { | |
| content: '\2660'; | |
| color: white; | |
| } | |
| .clearfix { | |
| float:none; | |
| clear: both; | |
| } |
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Cards</title> | |
| <link href="./cards.css" rel="stylesheet"> | |
| </head> | |
| <body> | |
| <button data-bind="click: newGame">New Game</button> | |
| <button data-bind="click: deal.bind($data, player1Cards, 1)">Deal Player 1</button> | |
| <button data-bind="click: deal.bind($data, player2Cards, 1)">Deal Player 2</button> | |
| <h4>Cards left <span data-bind="text: remainingInDeck"></span></h3> | |
| <h4>Points: <span data-bind="text: player1Points"></span></h4> | |
| <div data-bind="foreach: player1Cards"> | |
| <div class="card" data-bind="css: $data.suit.toLowerCase()"> | |
| <span data-bind="text: rank"></span> of | |
| <span data-bind="text: suit"></span> | |
| </div> | |
| </div> | |
| <div class="clearfix"></div> | |
| <h3>Player 2 Cards</h3> | |
| <h4>Points: <span data-bind="text: player2Points"></span></h4> | |
| <div data-bind="foreach: player2Cards"> | |
| <div class="card" data-bind="css: $data.suit.toLowerCase()"> | |
| <span data-bind="text: rank"></span> of | |
| <span data-bind="text: suit"></span> | |
| </div> | |
| </div> | |
| </body> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script> | |
| <script type="text/javascript" src="./cards.js"></script> | |
| <script type="text/javascript" src="./viewmodel.js"></script> | |
| <script> | |
| var gameData = { | |
| cards : app.shuffle(app.makeDeck()), | |
| player1Cards: [], | |
| player2Cards: [] | |
| } | |
| var model = new CardGameViewModel(gameData); | |
| ko.applyBindings(model); | |
| </script> | |
| </html> |
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 ranks = { | |
| ACE: 1, | |
| TWO: 2, | |
| THREE: 3, | |
| FOUR: 4, | |
| FIVE: 5, | |
| SIX: 6, | |
| SEVEN: 7, | |
| EIGHT: 8, | |
| NINE: 9, | |
| TEN: 10, | |
| JACK: 10, | |
| QUEEN: 10, | |
| KING: 10 | |
| }; | |
| var suits = { | |
| HEARTS: 'hearts', | |
| CLUBS: 'clubs', | |
| SPADES: 'spades', | |
| DIAMONDS: 'diamonds' | |
| }; | |
| var app = { | |
| _ranks : ranks, | |
| _suits : suits, | |
| cards : (function() { | |
| var cards = {}; // suit:ranks | |
| Object.keys(suits).forEach(function(suit) { | |
| cards[suit] = ranks; | |
| }); | |
| return cards; | |
| }()), | |
| makeDeck : function() { | |
| // { suit: "SUIT", rank: "RANK" } | |
| var deck = []; | |
| Object.keys(suits).forEach(function(suit){ | |
| Object.keys(ranks).forEach(function(rank) { | |
| //console.log({ | |
| deck.push({ | |
| suit: suit | |
| ,rank: rank | |
| }); | |
| }); | |
| }); | |
| return deck; | |
| }, | |
| shuffle : function(array) { | |
| var currentIndex = array.length, temporaryValue, randomIndex ; | |
| // While there remain elements to shuffle... | |
| while (0 !== currentIndex) { | |
| // Pick a remaining element... | |
| randomIndex = Math.floor(Math.random() * currentIndex); | |
| currentIndex -= 1; | |
| // And swap it with the current element. | |
| temporaryValue = array[currentIndex]; | |
| array[currentIndex] = array[randomIndex]; | |
| array[randomIndex] = temporaryValue; | |
| } | |
| return array; | |
| } | |
| }; |
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
| function CardGameViewModel(data) { | |
| var self = this; | |
| self.cards = ko.observableArray(data.cards); | |
| self.player1Cards= ko.observableArray(data.player1Cards); | |
| self.player2Cards = ko.observableArray(data.player2Cards); | |
| self.player1Points = ko.computed(function() { | |
| var points = 0; | |
| self.player1Cards().forEach(function(card) { | |
| points = points + app.cards[card.suit][card.rank]; | |
| }); | |
| return points; | |
| }); | |
| self.player2Points = ko.computed(function() { | |
| var points = 0; | |
| self.player2Cards().forEach(function(card) { | |
| points = points + app.cards[card.suit][card.rank]; | |
| }); | |
| return points; | |
| }); | |
| self.remainingInDeck = ko.computed(function() { | |
| return self.cards().length; | |
| }); | |
| self.deal = function(hand, num) { | |
| if(num > self.cards().length) { | |
| num = self.cards().length; | |
| } | |
| for(var i = 0; i < num; i++) { | |
| var card = self.cards.pop(); | |
| hand.push(card); | |
| } | |
| } | |
| self.newGame = function() { | |
| while(self.player1Cards().length > 0) { | |
| self.cards.push(self.player1Cards.pop()); | |
| } | |
| while(self.player2Cards().length > 0) { | |
| self.cards.push(self.player2Cards.pop()); | |
| } | |
| app.shuffle(self.cards()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment