Created
June 17, 2014 07:20
-
-
Save jakehawken/13ff51ac0a45a53707ba to your computer and use it in GitHub Desktop.
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> | |
var suits = ["Clubs", "Diamonds", "Hearts", "Spades"]; | |
var ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]; | |
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)) | |
} | |
} | |
} | |
} | |
function Card(rank, suit) | |
{ | |
this.rank = ranks[(rank-1)]; | |
this.suit = suits[(suit-1)]; | |
this.show = function () | |
{ | |
return this.rank + " of " + this.suit; | |
} | |
} | |
function highLowGame (deck) | |
{ | |
this.randomCard = function (deck) | |
{ | |
return deck.cards[Math.floor(Math.random() * 51)]; | |
} | |
this.highCard = function (next, last) | |
{ | |
if (ranks.indexOf(next.rank) > ranks.indexOf(last.rank)) | |
{ | |
return "h"; | |
} | |
else if (ranks.indexOf(next.rank) < ranks.indexOf(last.rank)) | |
{ | |
return "l"; | |
} | |
else | |
{ | |
if (suits.indexOf(next.suit) > suits.indexOf(last.suit)) | |
{ | |
return "h"; | |
} | |
else | |
{ | |
return "l"; | |
} | |
} | |
} | |
this.thisOrThat = function (input, first, second) | |
{ | |
var output = input.toLowerCase(); | |
first = first.toLowerCase(); | |
second = second.toLowerCase(); | |
while (output != first && output != second) //keeps re-asking you if you don't say Y or N | |
{ | |
input = prompt("No, silly. Type '" + first + "'' or '" + second + ".'"); | |
output = input.toLowerCase(); | |
} | |
return output; | |
} | |
this.gameLoop = function (deck) | |
{ | |
var firstCard, guess, playAgain, nextCard, rightTimes, wrongTimes; | |
playAgain = "no"; | |
rightTimes = 0; | |
wrongTimes = 0; | |
firstCard = this.randomCard(deck); | |
do { | |
guess = prompt("The first card is " + firstCard.show() + ". Do you think the next card will be higher or lower? (Type 'h' or 'l')."); | |
guess = this.thisOrThat(guess, "h", "l"); | |
do { | |
nextCard = this.randomCard(deck); | |
} while (firstCard == nextCard); //generates a card that is not the same card as the last one | |
if (this.highCard(nextCard, firstCard) == guess) | |
{ | |
alert("The next card is " + nextCard.show() + ". You were right!"); | |
rightTimes++; | |
} | |
else | |
{ | |
alert("The next card is " + nextCard.show() + ". You were wrong!"); | |
wrongTimes++; | |
} | |
playAgain = prompt("Continue? (Type 'yes' or 'no'.)"); | |
playAgain = this.thisOrThat(playAgain, "yes", "no"); | |
firstCard = nextCard; | |
} while (playAgain == "yes"); | |
alert("Thanks for playing! You guessed right " + rightTimes + " time(s), and you guessed wrong " + wrongTimes + " time(s)."); | |
} | |
} | |
var d = new Deck(); | |
d.init(); | |
var newGame = new highLowGame(); | |
newGame.gameLoop(d); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment