Last active
August 29, 2015 14:02
-
-
Save jfall76/8d6481cffc6a3928a13e to your computer and use it in GitHub Desktop.
DeckofCards
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
<script> | |
// | |
// Simple Game where each player throws down the top card on their deck after it's been delt. Who ever has the | |
// higher ranked card value gets a point. If it's a tie, no one gets a point. Whoever has the most points at the | |
// end of the game, wins. | |
// | |
// | |
function Queue() { | |
this.queue = []; | |
this.enqueue = function(aCard) { | |
return this.queue.push(aCard); | |
} | |
this.dequeue = function() { | |
return this.queue.shift(); | |
} | |
this.front = function() { | |
return this.queue[0]; | |
} | |
this.back = function() { | |
return this.queue[this.queue.length - 1]; | |
} | |
this.isEmpty = function() { | |
if (this.queue.length == 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
this.count = function() { | |
return this.queue.length; | |
} | |
this.toString = function() { | |
var retStr = ""; | |
for (var i = 0; i < this.queue.length; ++i) { | |
retStr += this.queue[i].rank + "\n"; | |
} | |
return retStr; | |
} | |
} | |
function Deck() { | |
this.cards = []; | |
this.count = function() { | |
return this.cards.length; | |
} | |
this.init = function() { | |
for (s = 1; s <= 4; s++) { | |
for (r = 1; r <= 13; r++) { | |
this.cards.push(new Card(r, s)) | |
} | |
} | |
} | |
this.deal = function(player1, player2) { | |
var random_index; | |
var removed_card; | |
while (this.cards.length > 0) { | |
//give player 1 half the deck | |
random_index = Math.floor(Math.random() * this.cards.length); | |
removed_card = this.cards.splice(random_index, 1)[0]; | |
player1.enqueue(removed_card); | |
//give player 2 the other half | |
random_index = Math.floor(Math.random() * this.cards.length); | |
removed_card = this.cards.splice(random_index, 1)[0]; | |
player2.enqueue(removed_card); | |
} | |
} | |
} | |
function Card(rank, suit) { | |
this.rank = rank; | |
this.suit = suit; | |
this.namesuit; | |
this.namerank; | |
this.show = function() { | |
console.log(this.rank + " of " + this.suit); | |
} | |
this.showCard = function() { | |
if (this.suit == 1) { | |
this.namesuit = "Clubs"; | |
} else if (this.suit == 2) { | |
this.namesuit = "Spades"; | |
} else if (this.suit == 3) { | |
this.namesuit = "Diamonds"; | |
} else if (this.suit == 4) { | |
this.namesuit = "Hearts"; | |
} | |
if (this.rank == 11) { | |
this.namerank = "Jack"; | |
} else if (this.rank == 12) { | |
this.namerank = "Queen"; | |
} else if (this.rank == 13) { | |
this.namerank = "King"; | |
} else if (this.rank == 1) { | |
this.namerank = "Ace"; | |
} else if (this.rank == 2) { | |
this.namerank = "Two"; | |
} else if (this.rank == 3) { | |
this.namerank = "Three"; | |
} else if (this.rank == 4) { | |
this.namerank = "Four"; | |
} else if (this.rank == 5) { | |
this.namerank = "Five"; | |
} else if (this.rank == 6) { | |
this.namerank = "Six"; | |
} else if (this.rank == 7) { | |
this.namerank = "Seven"; | |
} else if (this.rank == 8) { | |
this.namerank = "Eight"; | |
} else if (this.rank == 9) { | |
this.namerank = "Nine"; | |
} else if (this.rank == 10) { | |
this.namerank = "Ten" | |
} | |
console.log(this.namerank + " of " + this.namesuit); | |
} | |
} | |
d = new Deck; | |
d.init(); | |
p1 = new Queue; | |
p2 = new Queue; | |
//create 2 players and deal them each 26 cards | |
d.deal(p1,p2); | |
var score_1 = 0; | |
var score_2 = 0; | |
var tie = 0; | |
var card_dealt; | |
while (p1.count() > 0 || p2.count() > 0) { | |
console.log("Player 1 Hand: "); | |
p1.front().showCard(); | |
console.log("Player 2 Hand: "); | |
p2.front().showCard(); | |
if (p1.front().rank > p2.front().rank) { | |
card_dealt = p1.dequeue(); | |
p2.dequeue(); | |
score_1++; | |
p1.enqueue(card_dealt); | |
console.log("Player 1 Wins"); | |
} else if (p1.front().rank < p2.front().rank) { | |
p1.dequeue(); | |
card_dealt = p2.dequeue(); | |
score_2++; | |
p2.enqueue(card_dealt); | |
console.log("Player 2 Wins"); | |
} else if (p1.front().rank == p2.front().rank) { | |
p1.dequeue(); | |
p2.dequeue(); | |
tie++; | |
console.log("It's a Tie!"); | |
} else { | |
console.log("I'm not sure what's going on"); | |
} | |
} | |
console.log("Player 1 Score: " + score_1 + ", Player 2 Score: " + score_2 + ", Tie: " + tie); | |
console.log("Player 1 Card Count: " + p1.count() + " Player 2 Card Count: " + p2.count()); | |
if (score_1 > score_2) { | |
console.log("Player 1 is the Winner"); | |
} else if (score_1 < score_2) { | |
console.log("Player 2 is the Winner"); | |
} else if (score_1 == score_2) { | |
console.log("It's a tie"); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment