1 /** 2 * This class represents a card. 3 * A card has an immutable rank and suit 4 */ 5 function Card(rank, suit) { 6 7 //PRIVATE FIELDS 8 9 var isFaceUp = true; 10 11 //PUBLIC FUNCTIONS 12 13 /** 14 * suit getter method 15 * returns String suit 16 */ 17 this.getSuit = function () { 18 return suit; 19 }; 20 21 /** 22 * Returns "r" if the card is red (hearts or diamonds) and "s" 23 * otherwise. 24 */ 25 this.getColor = function () { 26 return (suit == "h" || suit == "d") ? "r" : "s"; 27 }; 28 29 /** 30 * Returns whether the card is face up or not. 31 */ 32 this.isFaceUp = function () { 33 return isFaceUp; 34 }; 35 36 /** 37 * Sets whether the card is face up--true means it is. 38 */ 39 this.setFaceUp = function (flag) { 40 isFaceUp = flag; 41 }; 42 43 /** 44 * rank getter method 45 * returns int rank 46 */ 47 this.getRank = function () { 48 return rank; 49 }; 50 51 /** 52 * rank getter method 53 * returns String rank 54 */ 55 this.getRankAsString = function () { 56 switch(rank) { 57 case 11: 58 return "J"; 59 case 12: 60 return "Q"; 61 case 13: 62 return "K"; 63 case 1: 64 return "A"; 65 default: 66 return rank; 67 } 68 }; 69 }