-
-
Save PlenipotentSS/e6b0f607ae41cd26c147 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
<script> | |
var debug = false | |
var counter, openPile; | |
//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.cards[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) { | |
var randomIndex = Math.floor(Math.random() * stdDeck.count()) | |
var card = stdDeck.cards.splice(randomIndex,1) | |
player1.hand.push(card[0]); | |
if (debug) { | |
console.log(player1.hand); | |
} | |
} | |
while (player2.hand.length <= 25) { | |
var randomIndex = Math.floor(Math.random() * stdDeck.count()) | |
var card = stdDeck.cards.splice(randomIndex,1) | |
player2.hand.push(card[0]); | |
if (debug) { | |
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. | |
counter = 0; | |
openPile = [] | |
while ((player1.hand.length != 0 && player2.hand.length != 0) && counter < 2500) { | |
var p1Card = player1.hand.shift(); | |
var p2Card = player2.hand.shift(); | |
if (p1Card.rank > p2Card.rank) { | |
console.log("Player 1 played " + p1Card.rank + "; Player 2 played " + p2Card.rank + ". Player 1 takes the cards!"); | |
player1.hand.push(p1Card); | |
player1.hand.push(p2Card); | |
while (openPile.length > 0) { | |
player2.hand.push(openPile.shift()) | |
} | |
} | |
else if (p2Card.rank > p1Card.rank) { | |
console.log("Player 1 played " + p1Card.rank + "; Player 2 played " + p2Card.rank + ". Player 2 takes the cards!"); | |
player2.hand.push(p1Card); | |
player2.hand.push(p2Card); | |
while (openPile.length > 0) { | |
player2.hand.push(openPile.shift()) | |
} | |
} | |
else { | |
console.log("Player 1 played " + p1Card.rank + "; Player 2 played " + p2Card.rank + ". To Open Pile"); | |
openPile.push(p1Card); | |
openPile.push(p2Card); | |
console.log('openPile size:' + openPile.length) | |
} | |
counter++; | |
} | |
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!!!"); | |
} else { | |
console.log("They gave up!, Player 1 has " + player1.hand.length + " cards and Player 2 has " + player2.hand.length + " cards"); | |
} | |
//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); | |
console.log('end of file') | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment