Advertisement
Guest User

Card

a guest
Sep 22nd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. /*
  2. * File: Cards.cpp
  3. * Author: John Hooper
  4. *
  5. * Created on September 19, 2014, 7:04 PM
  6. */
  7.  
  8. #include "card.h"
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <string>
  12. #include <sstream>
  13.  
  14. Card::Card(){
  15. mySuit = spades; // default, ace of spades
  16. myRank = 1;
  17. }
  18.  
  19. Card::Card(int rank, Suit s){
  20. string card;
  21. myRank = rank;
  22. mySuit = s;
  23. }
  24. string Card::toString() const{ //Post-Condition: return string version e.g. Ac 4h Js
  25. string temp;
  26. temp = rankString(myRank) + suitString(mySuit);
  27. return temp;
  28. }
  29.  
  30. bool Card::sameSuitAs(const Card& c) const{ //Post-Condition: true if suit same as c
  31. return mySuit == c.mySuit;
  32. }
  33.  
  34. int Card::getRank() const{
  35. return myRank;
  36. } // return rank, 1..13
  37.  
  38. string Card::suitString(Suit s) const{ // return "s", "h",...
  39. switch(s){
  40. case 0: return "Spades\n";
  41. case 1: return "Hearts\n";
  42. case 2: return "Diamonds\n";
  43. case 3: return "Clubs\n";
  44. }
  45. }
  46.  
  47. string Card::rankString(int r) const{ // return "A", "2", ..."Q"
  48. switch(r){
  49. case 1: return "Ace of ";
  50. case 11: return "Jack of ";
  51. case 12: return "Queen of ";
  52. case 13: return "King of ";
  53. default: {ostringstream temp;
  54. temp << r;
  55. return temp.str();
  56. }
  57. }
  58. }
  59.  
  60. bool Card::operator == (const Card& rhs) const{
  61. if (myRank != rhs.myRank && mySuit != rhs.mySuit){
  62. return false;
  63. }
  64. else return true;
  65. }
  66.  
  67. bool Card::operator != (const Card& rhs) const{
  68. if(myRank != rhs.myRank && mySuit != rhs.mySuit){
  69. return true;
  70. }
  71. else return false;
  72. }
  73.  
  74. ostream& operator << (ostream& out, const Card& c){
  75. out << c.toString();
  76. return out;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement