So for my final you have to make a class with various card methods, but I have already made the program last semester but in Python. So I'm just going to rewrite it all in C++, however I need some help on a few things. My biggest area of concerns are the methods in the class, as it involves array syntax I don't know how I should write in C++. Also unsure about all the (self), and the __.
My original Python code:
#This is a class that contains methods for various card game actions. Methods include
#resetting, shuffling, and cutting decks.
class deck:
#Constructor
def __init__(self):
self.__cards = []
self.resetdeck()
#Method that resets the deck
def resetdeck(self):
del self.__cards[0:]
for suit in range(1,5):
for value in range(1,14):
self.__cards.append(str(value)+"|"+str(suit))
#Method that shuffles the deck
def shuffle(self):
random.shuffle(self.__cards)
#Method that cuts the deck
def cutdeck(self):
cutdeck1 = self.__cards[:26]
cutdeck2 = self.__cards[26:]
self.__cards = cutdeck2 + cutdeck1
#Method that cuts to a card
def cuttocard(self):
self.cutdeck()
print self.__cards[-1]
#Method that both CPU and user cut to card and highest card wins
def cutCPU(self):
playerTop = self.__cards[-1]
del self.__cards[-1]
compTop = self.__cards[-1]
del self.__cards[-1]
print "Player", "Computer"
print ""
print playerTop," ", compTop
#Method that returns the cards
def get_cards(self):
return self.__cards
#This program gives the user a choice of various actions to be performed with a deck of cards
#Uses the class designed above.
import random
def main():
getcard = deck()
menuloop = 1
#Loop for actions until user decides to exit
while menuloop == 1:
print ""
print "1. Reset the deck"
print "2. Shuffle the deck"
print "3. Cut the deck"
print "4. Cut to a card"
print "5. Cut against the computer"
print "6. Exit the program"
print ""
uM = input("Select a menu option(#): ")
print ""
if uM == 1:
getcard.resetdeck()
print getcard.get_cards()
if uM == 2:
getcard.shuffle()
print getcard.get_cards()
if uM == 3:
getcard.cutdeck()
print getcard.get_cards()
if uM == 4:
getcard.cuttocard()
if uM == 5:
getcard.cutCPU()
#Exit option, ends loop
if uM == 6:
print "BYE"
menuloop = 0
#Secret option, used to show cards
if uM == 7:
print getcard.get_cards()
#Defensive if statement, only accepts #'s 1-7
if uM != 1 and uM != 2 and uM != 3 and uM != 4 and uM != 5 and uM != 6 and uM != 7:
print "Wrong input entered"
//This is a class that contains methods for various card game actions. Methods include
//resetting, shuffling, and cutting decks.
#include <iostream>
#include <random>;
usingnamespace std;
class deck {
public:
//Constructor
deck() {
self.__cards = [];
self.resetdeck(); }
//Method that resets the deck
int resetdeck() {
del self.__cards[0:];
for suit in range(1,5):
for value in range(1,14):
self.__cards.append(str(value)+"|"+str(suit)); }
//Method that shuffles the deck
int shuffle() {
random.shuffle(self.__cards); }
//Method that cuts the deck
int cutdeck() {
cutdeck1 = self.__cards[:26];
cutdeck2 = self.__cards[26:];
self.__cards = cutdeck2 + cutdeck1; }
//Method that cuts to a card
int cuttocard() {
self.cutdeck();
cout << self.__cards[-1]; }
//Method that both CPU and user cut to card and highest card wins
int cutCPU() {
playerTop = self.__cards[-1];
del self.__cards[-1];
compTop = self.__cards[-1];
del self.__cards[-1];
cout << "Player", "Computer";
cout << "";
cout << playerTop," ", compTop; }
//Method that returns the cards
int get_cards() {
return self.__cards; }
}
//This program gives the user a choice of various actions to be performed with a deck of cards
//Uses the class designed above.
int main()
{
deck getcard;
int menuloop = 1;
int uM;
//Loop for actions until user decides to exit
while (menuloop == 1)
{
cout << "";
cout << "1. Reset the deck";
cout << "2. Shuffle the deck";
cout << "3. Cut the deck";
cout << "4. Cut to a card";
cout << "5. Cut against the computer";
cout << "6. Exit the program";
cout << "";
cout << "Select a menu option(#): ";
cin >> uM;
cout << "";
if (uM == 1) {
getcard.resetdeck();
cout << getcard.get_cards(); }
if (uM == 2) {
getcard.shuffle();
cout << getcard.get_cards(); }
if (uM == 3) {
getcard.cutdeck();
cout << getcard.get_cards(); }
if (uM == 4)
getcard.cuttocard();
if (uM == 5)
getcard.cutCPU();
//Exit option, ends loop
if (uM == 6) {
cout << "BYE";
menuloop = 0; }
//Secret option, used to show cards
if (uM == 7)
cout << getcard.get_cards();
//Defensive if statement, only accepts #'s 1-7
if (uM != 1 && uM != 2 && uM != 3 && uM != 4 && uM != 5 && uM != 6 && uM != 7)
cout << "Wrong input entered";
}
}
I don't know much about python, but I'm guessing that self is similar to this in c++ and that you are trying to access a local member of the class.
Replace self. everywhere in this code with this->. That is the C++ equivalent. However, you could also just delete self. which is more common in C++ as member objects are automatically within scope everywhere in the class (as long as they are declared!).
As for __cards, you need to define that as a private object in the class. Why private? Because you already have get_cards() to access it by the outside world and so it doesn't need to be available to them.
1 2 3 4 5 6 7
class deck {
public:
deck()
//...
private:
int __cards; // perhaps card __cards; and define a class called "card"
}
Finally, your for loops in resetdeck() aren't c++. Use:
1 2 3
for (int suit = 1; suit < 5; ++suit)
for (int value = 1; value < 14; ++value)
this->__cards...
You may need to use another class for the cards if yuou want to do things like append as you do in your resetdeck.
Holy cow, up until now I didn't know anybody replied, I could have sworn I subscribed to it too. JLborges, I plugged that class in with my main(), had a few errors in my int main(), however I got it to compile and run.
I made a few changes to the class however they didn't work the way I wanted, originally in my python program, when it shuffled or the secret menu option, it would display each element in the array, basically all 52 cards on screen, I added a cout statement to those two methods however they print just two of the cards. Anyway to make them print all?
int input_int( int minv, int maxv )
{
while(true)
{
std::cout << "enter a number in the interval [" << minv << ',' << maxv << "]: " ;
int value ;
if( std::cin >> value && value >= minv && value <= maxv ) return value ;
std::cerr << "invalid input.\n" ;
// http://en.cppreference.com/w/cpp/io/basic_ios/clear
std::cin.clear() ; // clear the error state (if any)
// http://en.cppreference.com/w/cpp/io/basic_istream/ignore
std::cin.ignore( 1024, '\n' ) ; // throw away crud in the input buffer
}
}
And then:
1 2 3 4 5 6 7 8 9
int main()
{
// ...
// only accepts #'s 1-7
int uM = input_int( 1, 7 ) ;
// ...
}