Skip to content

Instantly share code, notes, and snippets.

@stephanielingwood
Last active August 29, 2015 14:02
Show Gist options
  • Save stephanielingwood/2d98ce9d276dcd4d3263 to your computer and use it in GitHub Desktop.
Save stephanielingwood/2d98ce9d276dcd4d3263 to your computer and use it in GitHub Desktop.
CardGame
<script>
var debug = true
//NEED WELCOME/INTRO
//create class of cards
function Card(rank, suit) {
this.rank = rank;
this.suit = suit;
this.show = function() {
console.log(this.rank + " of " + this.suit);
}
}
//create class of decks
//s passes a value in for suit
//r passes a value in for rank
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));
}
}
}
}
//create class of players
function Player() {
this.name = name;
this.hand = [];
this.count = function() {
return this.hand.length;
}
}
//make a deck and add cards to it
stdDeck = new Deck();
stdDeck.init();
if (debug = true) {
for (var showMe = 0; showMe < stdDeck.cards.length; showMe++) {
console.log(stdDeck[showMe]);
}
}
//create two players
player1 = new Player("Stephanie");
player2 = new Player("Julia");
//start of do/while loop for deciding whether to play again
keepPlaying = true
do {
//pass cards from the deck in to the hands of the players
while (player1.hand.length <= 25) {
player1.hand.push(stdDeck.cards[Math.floor(Math.random() * stdDeck.count)]);
if (debug = true) {
console.log(player1.hand);
}
}
while (player2.hand.length <= 25) {
player2.hand.push(stdDeck.cards[Math.floor(Math.random() * stdDeck.count)]);
if (debug = true) {
console.log(player2.hand);
}
}
alert("Get ready to play!");
//game logic for War; winner of each round adds both cards to their hand. Shift takes the card from the beginning of the array; push adds it to the end.
while (player1.hand.length != 0 && player2.hand.length != 0) {
var p1Card = player1.cards.shift();
var p2Card = player2.cards.shift();
if (p1Card.rank > p2Card.rank) {
console.log("Player 1 played " + p1Card.rank + " of " + p1card.suit + "; Player 2 played" + p2Card.rank + " of " + p2Card.suit + ". Player 1 takes the cards!");
player1.cards.push(p1Card);
player1.cards.push(p2Card);
}
else if (p2Card.rank > p1Card.rank) {
console.log("Player 1 played " + p1Card.rank + " of " + p1card.suit + "; Player 2 played" + p2Card.rank + " of " + p2Card.suit + ". Player 2 takes the cards!");
player2.cards.push(p1Card);
player2.cards.push(p2Card);
}
else if (p1Card == p2Card) {
console.log("Player 1 played " + p1Card.rank + " of " + p1card.suit + "; Player 2 played" + p2Card.rank + " of " + p2Card.suit + ". You tied!");
player1.cards.push(p1Card);
player2.cards.push(p2Card);
}
}
if (player1.hand.length == 0) {
console.log("PLAYER 2 WINS THE GAME!!!");
}
else if (player2.hand.length == 0) {
console.log("PLAYER 1 WINS THE GAME!!!");
}
//do/while loop to evaluate whether to play again
okResponse = true
do {
var playMore = prompt("Thanks for playing! Play again? Type yes or no.");
if (playMore === "yes") {
alert("Okay, let's play again!");
okResponse = false
}
else if (playMore === "no") {
alert("Have a great day, card-players!");
okResponse = false
keepPlaying = false
}
else {
alert("Looks like you didn't type yes or no.")
}
} while(okResponse);
} while(keepPlaying);
</script>
@PlenipotentSS
Copy link

Going through your code I found a few issues:
Take a look at all the times where you access .count, it should be .count(). See lines 63 and 69.

Also make sure when you try to get the cards from the object, you reference the array it is stored. i.e. stdDeck[showMe] should be stdDeck.cards[showMe].

When you test for debug, don't forget to use double equals. Otherwise, you end up setting debug to true each time. Furthermore, if you know that debug is a boolean, you can check it like this:

if (debug) //to see if debug is true
OR!!
if (!debug) //to see if debug is false.

I have a gist coming that could help too =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment